mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2025-12-07 07:42:45 +01:00
Release catalyst
This commit is contained in:
125
caql/unquote_test.go
Normal file
125
caql/unquote_test.go
Normal file
@@ -0,0 +1,125 @@
|
||||
// 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 (
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type quoteTest struct {
|
||||
in string
|
||||
out string
|
||||
ascii string
|
||||
graphic string
|
||||
}
|
||||
|
||||
var quotetests = []quoteTest{
|
||||
{in: "\a\b\f\r\n\t\v", out: `"\a\b\f\r\n\t\v"`, ascii: `"\a\b\f\r\n\t\v"`, graphic: `"\a\b\f\r\n\t\v"`},
|
||||
{"\\", `"\\"`, `"\\"`, `"\\"`},
|
||||
{"abc\xffdef", `"abc\xffdef"`, `"abc\xffdef"`, `"abc\xffdef"`},
|
||||
{"\u263a", `"☺"`, `"\u263a"`, `"☺"`},
|
||||
{"\U0010ffff", `"\U0010ffff"`, `"\U0010ffff"`, `"\U0010ffff"`},
|
||||
{"\x04", `"\x04"`, `"\x04"`, `"\x04"`},
|
||||
// Some non-printable but graphic runes. Final column is double-quoted.
|
||||
{"!\u00a0!\u2000!\u3000!", `"!\u00a0!\u2000!\u3000!"`, `"!\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) {
|
||||
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 != "" || err != strconv.ErrSyntax {
|
||||
t.Errorf("unquote(%#q) = %q, %v want %q, %v", s, out, err, "", strconv.ErrSyntax)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user