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 // Verify that our isPrint agrees with unicode.IsPrint
14 func TestIsPrint(t
*testing
.T
) {
16 for r
:= rune(0); r
<= unicode
.MaxRune
; r
++ {
17 if IsPrint(r
) != unicode
.IsPrint(r
) {
18 t
.Errorf("IsPrint(%U)=%t incorrect", r
, IsPrint(r
))
27 type quoteTest
struct {
33 var quotetests
= []quoteTest
{
34 {"\a\b\f\r\n\t\v", `"\a\b\f\r\n\t\v"`, `"\a\b\f\r\n\t\v"`},
35 {"\\", `"\\"`, `"\\"`},
36 {"abc\xffdef", `"abc\xffdef"`, `"abc\xffdef"`},
37 {"\u263a", `"☺"`, `"\u263a"`},
38 {"\U0010ffff", `"\U0010ffff"`, `"\U0010ffff"`},
39 {"\x04", `"\x04"`, `"\x04"`},
42 func TestQuote(t
*testing
.T
) {
43 for _
, tt
:= range quotetests
{
44 if out
:= Quote(tt
.in
); out
!= tt
.out
{
45 t
.Errorf("Quote(%s) = %s, want %s", tt
.in
, out
, tt
.out
)
47 if out
:= AppendQuote([]byte("abc"), tt
.in
); string(out
) != "abc"+tt
.out
{
48 t
.Errorf("AppendQuote(%q, %s) = %s, want %s", "abc", tt
.in
, out
, "abc"+tt
.out
)
53 func TestQuoteToASCII(t
*testing
.T
) {
54 for _
, tt
:= range quotetests
{
55 if out
:= QuoteToASCII(tt
.in
); out
!= tt
.ascii
{
56 t
.Errorf("QuoteToASCII(%s) = %s, want %s", tt
.in
, out
, tt
.ascii
)
58 if out
:= AppendQuoteToASCII([]byte("abc"), tt
.in
); string(out
) != "abc"+tt
.ascii
{
59 t
.Errorf("AppendQuoteToASCII(%q, %s) = %s, want %s", "abc", tt
.in
, out
, "abc"+tt
.ascii
)
64 type quoteRuneTest
struct {
70 var quoterunetests
= []quoteRuneTest
{
72 {'\a', `'\a'`, `'\a'`},
73 {'\\', `'\\'`, `'\\'`},
74 {0xFF, `'ÿ'`, `'\u00ff'`},
75 {0x263a, `'☺'`, `'\u263a'`},
76 {0xfffd, `'�'`, `'\ufffd'`},
77 {0x0010ffff, `'\U0010ffff'`, `'\U0010ffff'`},
78 {0x0010ffff + 1, `'�'`, `'\ufffd'`},
79 {0x04, `'\x04'`, `'\x04'`},
82 func TestQuoteRune(t
*testing
.T
) {
83 for _
, tt
:= range quoterunetests
{
84 if out
:= QuoteRune(tt
.in
); out
!= tt
.out
{
85 t
.Errorf("QuoteRune(%U) = %s, want %s", tt
.in
, out
, tt
.out
)
87 if out
:= AppendQuoteRune([]byte("abc"), tt
.in
); string(out
) != "abc"+tt
.out
{
88 t
.Errorf("AppendQuoteRune(%q, %U) = %s, want %s", "abc", tt
.in
, out
, "abc"+tt
.out
)
93 func TestQuoteRuneToASCII(t
*testing
.T
) {
94 for _
, tt
:= range quoterunetests
{
95 if out
:= QuoteRuneToASCII(tt
.in
); out
!= tt
.ascii
{
96 t
.Errorf("QuoteRuneToASCII(%U) = %s, want %s", tt
.in
, out
, tt
.ascii
)
98 if out
:= AppendQuoteRuneToASCII([]byte("abc"), tt
.in
); string(out
) != "abc"+tt
.ascii
{
99 t
.Errorf("AppendQuoteRuneToASCII(%q, %U) = %s, want %s", "abc", tt
.in
, out
, "abc"+tt
.ascii
)
104 type canBackquoteTest
struct {
109 var canbackquotetests
= []canBackquoteTest
{
120 {string(9), true}, // \t
143 {string(0x7F), false},
144 {`' !"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, true},
145 {`0123456789`, true},
146 {`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true},
147 {`abcdefghijklmnopqrstuvwxyz`, true},
150 {"a\xe0\xa0z", false},
151 {"\ufeffabc", false},
155 func TestCanBackquote(t
*testing
.T
) {
156 for _
, tt
:= range canbackquotetests
{
157 if out
:= CanBackquote(tt
.in
); out
!= tt
.out
{
158 t
.Errorf("CanBackquote(%q) = %v, want %v", tt
.in
, out
, tt
.out
)
163 type unQuoteTest
struct {
168 var unquotetests
= []unQuoteTest
{
173 {`"hello world"`, "hello world"},
176 {`"\u1234"`, "\u1234"},
177 {`"\U00010111"`, "\U00010111"},
178 {`"\U0001011111"`, "\U0001011111"},
179 {`"\a\b\f\n\r\t\v\\\""`, "\a\b\f\n\r\t\v\\\""},
187 {`'\u1234'`, "\u1234"},
188 {`'\U00010111'`, "\U00010111"},
198 {"`hello world`", `hello world`},
207 var misquoted
= []string{
235 func TestUnquote(t
*testing
.T
) {
236 for _
, tt
:= range unquotetests
{
237 if out
, err
:= Unquote(tt
.in
); err
!= nil && out
!= tt
.out
{
238 t
.Errorf("Unquote(%#q) = %q, %v want %q, nil", tt
.in
, out
, err
, tt
.out
)
242 // run the quote tests too, backward
243 for _
, tt
:= range quotetests
{
244 if in
, err
:= Unquote(tt
.out
); in
!= tt
.in
{
245 t
.Errorf("Unquote(%#q) = %q, %v, want %q, nil", tt
.out
, in
, err
, tt
.in
)
249 for _
, s
:= range misquoted
{
250 if out
, err
:= Unquote(s
); out
!= "" || err
!= ErrSyntax
{
251 t
.Errorf("Unquote(%#q) = %q, %v want %q, %v", s
, out
, err
, "", ErrSyntax
)
256 func BenchmarkUnquoteEasy(b
*testing
.B
) {
257 for i
:= 0; i
< b
.N
; i
++ {
258 Unquote(`"Give me a rock, paper and scissors and I will move the world."`)
262 func BenchmarkUnquoteHard(b
*testing
.B
) {
263 for i
:= 0; i
< b
.N
; i
++ {
264 Unquote(`"\x47ive me a \x72ock, \x70aper and \x73cissors and \x49 will move the world."`)