package parser import ( "fmt" "testing" ) func TestString(t *testing.T) { for _, test := range []parseTest{ {``, []string{`Error: unexpected end of file (expected a string value) at start of file`}}, {`no start quote"`, []string{`Error: unexpected input (expected a string value) at start of file`}}, {`"simple string"`, []string{`string("simple string")`}}, } { p := &parser{} testParseHandler(t, p, p.startString, test) } } func TestBasicString(t *testing.T) { for _, test := range []parseTest{ {``, []string{`Error: unexpected end of file (expected a basic string) at start of file`}}, {`no start quote"`, []string{`Error: unexpected input (expected a basic string) at start of file`}}, {`"no end quote`, []string{`Error: unexpected end of file (expected end of string) at line 1, column 14`}}, {`""`, []string{`string("")`}}, {`"simple string"`, []string{`string("simple string")`}}, {`"with\tsome\r\nvalid escapes\b"`, []string{`string("with\tsome\r\nvalid escapes\b")`}}, {`"with an \invalid escape"`, []string{`Error: invalid escape sequence at line 1, column 10`}}, {`"A cool UTF8 ƃuıɹʇs"`, []string{`string("A cool UTF8 ƃuıɹʇs")`}}, {`"A string with UTF8 escape \u2318"`, []string{`string("A string with UTF8 escape ⌘")`}}, {"\"Invalid character for UTF \xcd\"", []string{`Error: invalid UTF8 rune at line 1, column 28`}}, {"\"Character that mus\t be escaped\"", []string{`Error: invalid character in basic string: '\t' (must be escaped) at line 1, column 20`}}, {"\"Character that must be escaped \u0000\"", []string{`Error: invalid character in basic string: '\x00' (must be escaped) at line 1, column 33`}}, {"\"Character that must be escaped \x7f\"", []string{`Error: invalid character in basic string: '\u007f' (must be escaped) at line 1, column 33`}}, } { p := &parser{} testParseHandler(t, p, p.startBasicString, test) } } func TestBasicStringWithUnescapedControlCharacters(t *testing.T) { // A quick check for almost all characters that must be escaped. // The missing one (\x7f) is covered in the previous test. for i := 0x00; i <= 0x1F; i++ { p := &parser{} input := fmt.Sprintf(`"%c"`, rune(i)) expected := fmt.Sprintf(`Error: invalid character in basic string: %q (must be escaped) at line 1, column 2`, rune(i)) testParseHandler(t, p, p.startString, parseTest{input, []string{expected}}) } }