<commit_before>package vm

import (
	"testing"
)

func TestStringClassSuperclass(t *testing.T) {
	tests := []struct {
		input    string
		expected string
	}{
		{`String.class.name`, "Class"},
		{`String.superclass.name`, "Object"},
	}

	for i, tt := range tests {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkExpected(t, i, evaluated, tt.expected)
		v.checkCFP(t, i, 0)
		v.checkSP(t, i, 1)
	}
}

func TestEvalStringExpression(t *testing.T) {
	tests := []struct {
		input    string
		expected string
	}{
		{`"st0012"`, "st0012"},
		{`'Monkey'`, "Monkey"},
	}

	for i, tt := range tests {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkExpected(t, i, evaluated, tt.expected)
		v.checkCFP(t, i, 0)
		v.checkSP(t, i, 1)
	}
}

func TestStringConversion(t *testing.T) {
	tests := []struct {
		input    string
		expected interface{}
	}{
		{`"string".to_s`, "string"},
		{`"123".to_i`, 123},
		{`"string".to_i`, 0},
		{`"123string123".to_i`, 123},
		{`"string123".to_i`, 0},
		{`
		  arr = "Goby".to_a
		  arr[0]
		`, "G"},
		{`
		  arr = "Goby".to_a
		  arr[1]
		`, "o"},
		{`
		  arr = "Goby".to_a
		  arr[2]
		`, "b"},
		{`
		  arr = "Goby".to_a
		  arr[3]
		`, "y"},
		{`
		  arr = "🍣Goby🍺".to_a
		  arr[0]
		`, "🍣"},
		{`
		  arr = "🍣Goby🍺".to_a
		  arr[5]
		`, "🍺"},
	}

	for i, tt := range tests {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkExpected(t, i, evaluated, tt.expected)
		v.checkCFP(t, i, 0)
		v.checkSP(t, i, 1)
	}
}

func TestStringComparison(t *testing.T) {
	tests := []struct {
		input    string
		expected interface{}
	}{
		{`"123" == "123"`, true},
		{`"123" == "124"`, false},
		{`"123" == 123`, false},
		{`"123" == (1..3)`, false},
		{`"123" == { a: 1, b: 2 }`, false},
		{`"123" == [1, "String", true, 2..5]`, false},
		{`"123" != "123"`, false},
		{`"123" != "124"`, true},
		{`"123" != 123`, true},
		{`"123" != (1..3)`, true},
		{`"123" != { a: 1, b: 2 }`, true},
		{`"123" != [1, "String", true, 2..5]`, true},
		{`"123" != String`, true},
		{`"1234" > "123"`, true},
		{`"123" > "1235"`, false},
		{`"1234" < "123"`, false},
		{`"1234" < "12jdkfj3"`, true},
		{`"1234" <=> "1234"`, 0},
		{`"1234" <=> "4"`, -1},
		{`"abcdef" <=> "abcde"`, 1},
		{`"一" <=> "一"`, 0},
		{`"二" <=> "一"`, 1},
		{`"一" <=> "二"`, -1},
		{`"🍣" <=> "🍣"`, 0},
		{`"🍣" <=> "一"`, 1},
		{`"一" <=> "🍣"`, -1},
		{`"🍺" <=> "🍣"`, 1},
		{`"🍣" <=> "🍺"`, -1},
	}

	for i, tt := range tests {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkExpected(t, i, evaluated, tt.expected)
		v.checkCFP(t, i, 0)
		v.checkSP(t, i, 1)
	}
}

func TestStringComparisonFail(t *testing.T) {
	testsFail := []errorTestCase{
		{`"a" < 1`, "TypeError: Expect argument to be String. got: Integer", 1},
		{`"a" > 1`, "TypeError: Expect argument to be String. got: Integer", 1},
		{`"a" <=> 1`, "TypeError: Expect argument to be String. got: Integer", 1},
	}
	for i, tt := range testsFail {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkError(t, i, evaluated, tt.expected, getFilename(), tt.errorLine)
		v.checkCFP(t, i, 1)
		v.checkSP(t, i, 1)
	}
}

func TestStringOperation(t *testing.T) {
	tests := []struct {
		input    string
		expected interface{}
	}{
		{`"Stan " + "Lo"`, "Stan Lo"},
		{`"Dog" + "&" + "Cat"`, "Dog&Cat"},
		{`"Three " * 3`, "Three Three Three "},
		{`"Zero" * 0`, ""},
		{`"Minus" * 1`, "Minus"},
		{`"Hello"[1]`, "e"},
		{`"Hello"[5]`, nil},
		{`"Hello"[-1]`, "o"},
		{`"Hello"[-6]`, nil},
		{`"Hello🍣"[5]`, "🍣"},
		{`"Hello🍣"[-1]`, "🍣"},
		{`"Hello\nWorld"[5]`, "\n"},
		{`"Ruby"[1] = "oo"`, "Rooby"},
		{`"Go"[2] = "by"`, "Goby"},
		{`"Ruby"[-3] = "oo"`, "Rooby"},
		{`"Hello"[-5] = "Tr"`, "Trello"},
		{`"Hello\nWorld"[5] = " "`, "Hello World"},
		{`"Hello🍣"[5] = "🍺"`, "Hello🍺"},
		{`"Hello🍣"[1] = "🍺"`, "H🍺llo🍣"},
		{`"Hello🍣"[-1] = "🍺"`, "Hello🍺"},
	}

	for i, tt := range tests {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkExpected(t, i, evaluated, tt.expected)
		v.checkCFP(t, i, 0)
		v.checkSP(t, i, 1)
	}
}

func TestStringOperationFail(t *testing.T) {
	testsFail := []errorTestCase{
		{`"Taipei" + 101`, "TypeError: Expect argument to be String. got: Integer", 1},
		{`"Taipei" * "101"`, "TypeError: Expect argument to be Integer. got: String", 1},
		{`"Taipei" * (-101)`, "ArgumentError: Second argument must be greater than or equal to 0. got=-101", 1},
		{`"Taipei"[1] = 1`, "TypeError: Expect argument to be String. got: Integer", 1},<commit_msg>Add String VM Testing for Escaped Character Cases<commit_after>package vm

import (
	"testing"
)

func TestStringClassSuperclass(t *testing.T) {
	tests := []struct {
		input    string
		expected string
	}{
		{`String.class.name`, "Class"},
		{`String.superclass.name`, "Object"},
	}

	for i, tt := range tests {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkExpected(t, i, evaluated, tt.expected)
		v.checkCFP(t, i, 0)
		v.checkSP(t, i, 1)
	}
}

func TestEvalStringExpression(t *testing.T) {
	tests := []struct {
		input    string
		expected string
	}{
		{`"st0012"`, "st0012"},
		{`'Monkey'`, "Monkey"},
		{`"\"Maxwell\""`, "\"Maxwell\""},
		{`"'Alexius'"`, "'Alexius'"},
		{`"\'Maxwell\'"`, "'Maxwell'"},
		{`'\'Alexius\''`, "'Alexius'"},
		{`"Maxwell\nAlexius"`, "Maxwell\nAlexius"},
		{`'Maxwell\nAlexius'`, "Maxwell\\nAlexius"},
	}

	for i, tt := range tests {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkExpected(t, i, evaluated, tt.expected)
		v.checkCFP(t, i, 0)
		v.checkSP(t, i, 1)
	}
}

func TestStringConversion(t *testing.T) {
	tests := []struct {
		input    string
		expected interface{}
	}{
		{`"string".to_s`, "string"},
		{`"Maxwell\nAlexius".to_s`, "Maxwell\nAlexius"},
		{`'Maxwell\nAlexius'.to_s`, "Maxwell\\nAlexius"},
		{`"\"Maxwell\"".to_s`, "\"Maxwell\""},
		{`'\"Maxwell\"'.to_s`, "\\\"Maxwell\\\""},
		{`"\'Maxwell\'".to_s`, "'Maxwell'"},
		{`'\'Maxwell\''.to_s`, "'Maxwell'"},
		{`"123".to_i`, 123},
		{`"string".to_i`, 0},
		{`"123string123".to_i`, 123},
		{`"string123".to_i`, 0},
		{`
		  arr = "Goby".to_a
		  arr[0]
		`, "G"},
		{`
		  arr = "Goby".to_a
		  arr[1]
		`, "o"},
		{`
		  arr = "Goby".to_a
		  arr[2]
		`, "b"},
		{`
		  arr = "Goby".to_a
		  arr[3]
		`, "y"},
		{`
		  arr = "🍣Goby🍺".to_a
		  arr[0]
		`, "🍣"},
		{`
		  arr = "🍣Goby🍺".to_a
		  arr[5]
		`, "🍺"},
		{`
		  arr = "Maxwell\nAlexius".to_a
		  arr[7]
		 `, "\n"},
		{`
		  arr = 'Maxwell\nAlexius'.to_a
		  arr[7]
		 `, "\\"},
		{`
		  arr = 'Maxwell\nAlexius'.to_a
		  arr[8]
		 `, "n"},
		{`
		  arr = "\"Maxwell\"".to_a
		  arr[0]
		 `, "\""},
		{`
		  arr = "\"Maxwell\"".to_a
		  arr[-1]
		 `, "\""},
		{`
		  arr = "\'Maxwell\'".to_a
		  arr[0]
		 `, "'"},
		{`
		  arr = "\'Maxwell\'".to_a
		  arr[-1]
		 `, "'"},
		{`
		  arr = '\"Maxwell\"'.to_a
		  arr[0]
		 `, "\\"},
		{`
		  arr = '\"Maxwell\"'.to_a
		  arr[1]
		 `, "\""},
		{`
		  arr = '\"Maxwell\"'.to_a
		  arr[-1]
		 `, "\""},
		{`
		  arr = '\"Maxwell\"'.to_a
		  arr[-2]
		 `, "\\"},
		{`
		  arr = '\'Maxwell\''.to_a
		  arr[0]
		 `, "'"},
		{`
		  arr = '\'Maxwell\''.to_a
		  arr[-1]
		 `, "'"},
	}

	for i, tt := range tests {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkExpected(t, i, evaluated, tt.expected)
		v.checkCFP(t, i, 0)
		v.checkSP(t, i, 1)
	}
}

func TestStringComparison(t *testing.T) {
	tests := []struct {
		input    string
		expected interface{}
	}{
		{`"123" == "123"`, true},
		{`"123" == "124"`, false},
		{`"123" == 123`, false},
		{`"123" == (1..3)`, false},
		{`"123" == { a: 1, b: 2 }`, false},
		{`"123" == [1, "String", true, 2..5]`, false},
		{`"\"Maxwell\"" == '"Maxwell"'`, true},
		{`"\'Maxwell\'" == '\'Maxwell\''`, true},
		{`"\"Maxwell\"" == '\"Maxwell\"'`, false},
		{`"123" != "123"`, false},
		{`"123" != "124"`, true},
		{`"123" != 123`, true},
		{`"123" != (1..3)`, true},
		{`"123" != { a: 1, b: 2 }`, true},
		{`"123" != [1, "String", true, 2..5]`, true},
		{`"123" != String`, true},
		{`"1234" > "123"`, true},
		{`"123" > "1235"`, false},
		{`"1234" < "123"`, false},
		{`"1234" < "12jdkfj3"`, true},
		{`"1234" <=> "1234"`, 0},
		{`"1234" <=> "4"`, -1},
		{`"abcdef" <=> "abcde"`, 1},
		{`"一" <=> "一"`, 0},
		{`"二" <=> "一"`, 1},
		{`"一" <=> "二"`, -1},
		{`"🍣" <=> "🍣"`, 0},
		{`"🍣" <=> "一"`, 1},
		{`"一" <=> "🍣"`, -1},
		{`"🍺" <=> "🍣"`, 1},
		{`"🍣" <=> "🍺"`, -1},
	}

	for i, tt := range tests {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkExpected(t, i, evaluated, tt.expected)
		v.checkCFP(t, i, 0)
		v.checkSP(t, i, 1)
	}
}

func TestStringComparisonFail(t *testing.T) {
	testsFail := []errorTestCase{
		{`"a" < 1`, "TypeError: Expect argument to be String. got: Integer", 1},
		{`"a" > 1`, "TypeError: Expect argument to be String. got: Integer", 1},
		{`"a" <=> 1`, "TypeError: Expect argument to be String. got: Integer", 1},
	}
	for i, tt := range testsFail {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkError(t, i, evaluated, tt.expected, getFilename(), tt.errorLine)
		v.checkCFP(t, i, 1)
		v.checkSP(t, i, 1)
	}
}

func TestStringOperation(t *testing.T) {
	tests := []struct {
		input    string
		expected interface{}
	}{
		{`"Stan " + "Lo"`, "Stan Lo"},
		{`"Dog" + "&" + "Cat"`, "Dog&Cat"},
		{`"Three " * 3`, "Three Three Three "},
		{`"Zero" * 0`, ""},
		{`"Minus" * 1`, "Minus"},
		{`"Hello"[1]`, "e"},
		{`"Hello"[5]`, nil},
		{`"Hello"[-1]`, "o"},
		{`"Hello"[-6]`, nil},
		{`"Hello🍣"[5]`, "🍣"},
		{`"Hello🍣"[-1]`, "🍣"},
		{`"Hello\nWorld"[5]`, "\n"},
		{`"\"Maxwell\""[0]`, "\""},
		{`"\"Maxwell\""[-1]`, "\""},
		{`"\'Maxwell\'"[0]`, "'"},
		{`"\'Maxwell\'"[-1]`, "'"},
		{`'\"Maxwell\"'[0]`, "\\"},
		{`'\"Maxwell\"'[1]`, "\""},
		{`'\"Maxwell\"'[-1]`, "\""},
		{`'\"Maxwell\"'[-2]`, "\\"},
		{`'\'Maxwell\''[0]`, "'"},
		{`'\'Maxwell\''[-1]`, "'"},
		{`"Ruby"[1] = "oo"`, "Rooby"},
		{`"Go"[2] = "by"`, "Goby"},
		{`"Ruby"[-3] = "oo"`, "Rooby"},
		{`"Hello"[-5] = "Tr"`, "Trello"},
		{`"Hello\nWorld"[5] = " "`, "Hello World"},
		{`"Hello🍣"[5] = "🍺"`, "Hello🍺"},
		{`"Hello🍣"[1] = "🍺"`, "H🍺llo🍣"},
		{`"Hello🍣"[-1] = "🍺"`, "Hello🍺"},
	}

	for i, tt := range tests {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkExpected(t, i, evaluated, tt.expected)
		v.checkCFP(t, i, 0)
		v.checkSP(t, i, 1)
	}
}

func TestStringOperationFail(t *testing.T) {
	testsFail := []errorTestCase{
		{`"Taipei" + 101`, "TypeError: Expect argument to be String. got: Integer", 1},
		{`"Taipei" * "101"`, "TypeError: Expect argument to be Integer. got: String", 1},
		{`"Taipei" * (-101)`, "ArgumentError: Second argument must be greater than or equal to 0. got=-101", 1},
		{`"Taipei"[1] = 1`, "TypeError: Expect argument to be String. got: Integer", 1},
{'fraction_non_alphanumeric': 0.2968965834791417, 'fraction_numerical': 0.04116401141909937, 'mean_word_length': 3.2339181286549707, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3171906', 'n_tokens_mistral': 5825, 'n_tokens_neox': 5023, 'n_words': 1012}