// Adapted from https://github.com/golang/go // Original License: // // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the https://go.dev/LICENSE file. package caql import ( "errors" "strconv" "testing" ) type quoteTest struct { in string out string } var quotetests = []quoteTest{ {in: "\a\b\f\r\n\t\v", out: `"\a\b\f\r\n\t\v"`}, {"\\", `"\\"`}, {"abc\xffdef", `"abc\xffdef"`}, {"\u263a", `"☺"`}, {"\U0010ffff", `"\U0010ffff"`}, {"\x04", `"\x04"`}, // Some non-printable but graphic runes. Final column is double-quoted. {"!\u00a0!\u2000!\u3000!", `"!\u00a0!\u2000!\u3000!"`}, } type unQuoteTest struct { in string out string } var unquotetests = []unQuoteTest{ {`""`, ""}, {`"a"`, "a"}, {`"abc"`, "abc"}, {`"☺"`, "☺"}, {`"hello world"`, "hello world"}, {`"\xFF"`, "\xFF"}, {`"\377"`, "\377"}, {`"\u1234"`, "\u1234"}, {`"\U00010111"`, "\U00010111"}, {`"\U0001011111"`, "\U0001011111"}, {`"\a\b\f\n\r\t\v\\\""`, "\a\b\f\n\r\t\v\\\""}, {`"'"`, "'"}, {`'a'`, "a"}, {`'☹'`, "☹"}, {`'\a'`, "\a"}, {`'\x10'`, "\x10"}, {`'\377'`, "\377"}, {`'\u1234'`, "\u1234"}, {`'\U00010111'`, "\U00010111"}, {`'\t'`, "\t"}, {`' '`, " "}, {`'\''`, "'"}, {`'"'`, "\""}, {"``", ``}, {"`a`", `a`}, {"`abc`", `abc`}, {"`☺`", `☺`}, {"`hello world`", `hello world`}, {"`\\xFF`", `\xFF`}, {"`\\377`", `\377`}, {"`\\`", `\`}, {"`\n`", "\n"}, {"` `", ` `}, {"` `", ` `}, {"`a\rb`", "ab"}, } var misquoted = []string{ ``, `"`, `"a`, `"'`, `b"`, `"\"`, `"\9"`, `"\19"`, `"\129"`, `'\'`, `'\9'`, `'\19'`, `'\129'`, // `'ab'`, `"\x1!"`, `"\U12345678"`, `"\z"`, "`", "`xxx", "`\"", `"\'"`, `'\"'`, "\"\n\"", "\"\\n\n\"", "'\n'", } func TestUnquote(t *testing.T) { t.Parallel() for _, tt := range unquotetests { if out, err := unquote(tt.in); err != nil || out != tt.out { t.Errorf("unquote(%#q) = %q, %v want %q, nil", tt.in, out, err, tt.out) } } // run the quote tests too, backward for _, tt := range quotetests { if in, err := unquote(tt.out); in != tt.in { t.Errorf("unquote(%#q) = %q, %v, want %q, nil", tt.out, in, err, tt.in) } } for _, s := range misquoted { if out, err := unquote(s); out != "" || !errors.Is(err, strconv.ErrSyntax) { t.Errorf("unquote(%#q) = %q, %v want %q, %v", s, out, err, "", strconv.ErrSyntax) } } }