1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 type quoteTest struct {
19 var quotetests = []quoteTest{
20 {"\a\b\f\r\n\t\v", `"\a\b\f\r\n\t\v"`, `"\a\b\f\r\n\t\v"`},
21 {"\\", `"\\"`, `"\\"`},
22 {"abc\xffdef", `"abc\xffdef"`, `"abc\xffdef"`},
23 {"\u263a", `"☺"`, `"\u263a"`},
24 {"\U0010ffff", `"\U0010ffff"`, `"\U0010ffff"`},
25 {"\x04", `"\x04"`, `"\x04"`},
28 func TestQuote(t *testing.T) {
29 for _, tt := range quotetests {
30 if out := Quote(tt.in); out != tt.out {
31 t.Errorf("Quote(%s) = %s, want %s", tt.in, out, tt.out)
36 func TestQuoteToASCII(t *testing.T) {
37 for _, tt := range quotetests {
38 if out := QuoteToASCII(tt.in); out != tt.ascii {
39 t.Errorf("QuoteToASCII(%s) = %s, want %s", tt.in, out, tt.ascii)
44 type quoteRuneTest struct {
50 var quoterunetests = []quoteRuneTest{
52 {'\a', `'\a'`, `'\a'`},
53 {'\\', `'\\'`, `'\\'`},
54 {0xFF, `'ÿ'`, `'\u00ff'`},
55 {0x263a, `'☺'`, `'\u263a'`},
56 {0xfffd, `'�'`, `'\ufffd'`},
57 {0x0010ffff, `'\U0010ffff'`, `'\U0010ffff'`},
58 {0x0010ffff + 1, `'�'`, `'\ufffd'`},
59 {0x04, `'\x04'`, `'\x04'`},
62 func TestQuoteRune(t *testing.T) {
63 for _, tt := range quoterunetests {
64 if out := QuoteRune(tt.in); out != tt.out {
65 t.Errorf("QuoteRune(%U) = %s, want %s", tt.in, out, tt.out)
70 func TestQuoteRuneToASCII(t *testing.T) {
71 for _, tt := range quoterunetests {
72 if out := QuoteRuneToASCII(tt.in); out != tt.ascii {
73 t.Errorf("QuoteRuneToASCII(%U) = %s, want %s", tt.in, out, tt.ascii)
78 type canBackquoteTest struct {
83 var canbackquotetests = []canBackquoteTest{
94 {string(9), true}, // \t
117 {`' !"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, true},
118 {`0123456789`, true},
119 {`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true},
120 {`abcdefghijklmnopqrstuvwxyz`, true},
124 func TestCanBackquote(t *testing.T) {
125 for _, tt := range canbackquotetests {
126 if out := CanBackquote(tt.in); out != tt.out {
127 t.Errorf("CanBackquote(%q) = %v, want %v", tt.in, out, tt.out)
132 type unQuoteTest struct {
137 var unquotetests = []unQuoteTest{
142 {`"hello world"`, "hello world"},
145 {`"\u1234"`, "\u1234"},
146 {`"\U00010111"`, "\U00010111"},
147 {`"\U0001011111"`, "\U0001011111"},
148 {`"\a\b\f\n\r\t\v\\\""`, "\a\b\f\n\r\t\v\\\""},
156 {`'\u1234'`, "\u1234"},
157 {`'\U00010111'`, "\U00010111"},
167 {"`hello world`", `hello world`},
175 var misquoted = []string{
194 func TestUnquote(t *testing.T) {
195 for _, tt := range unquotetests {
196 if out, err := Unquote(tt.in); err != nil && out != tt.out {
197 t.Errorf("Unquote(%#q) = %q, %v want %q, nil", tt.in, out, err, tt.out)
201 // run the quote tests too, backward
202 for _, tt := range quotetests {
203 if in, err := Unquote(tt.out); in != tt.in {
204 t.Errorf("Unquote(%#q) = %q, %v, want %q, nil", tt.out, in, err, tt.in)
208 for _, s := range misquoted {
209 if out, err := Unquote(s); out != "" || err != os.EINVAL {
210 t.Errorf("Unquote(%#q) = %q, %v want %q, %v", s, out, err, "", os.EINVAL)