quote_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.
  4. package strconv_test
  5. import (
  6. . "strconv"
  7. "testing"
  8. "unicode"
  9. )
  10. // Verify that our isPrint agrees with unicode.IsPrint
  11. func TestIsPrint(t *testing.T) {
  12. n := 0
  13. for r := rune(0); r <= unicode.MaxRune; r++ {
  14. if IsPrint(r) != unicode.IsPrint(r) {
  15. t.Errorf("IsPrint(%U)=%t incorrect", r, IsPrint(r))
  16. n++
  17. if n > 10 {
  18. return
  19. }
  20. }
  21. }
  22. }
  23. type quoteTest struct {
  24. in string
  25. out string
  26. ascii string
  27. }
  28. var quotetests = []quoteTest{
  29. {"\a\b\f\r\n\t\v", `"\a\b\f\r\n\t\v"`, `"\a\b\f\r\n\t\v"`},
  30. {"\\", `"\\"`, `"\\"`},
  31. {"abc\xffdef", `"abc\xffdef"`, `"abc\xffdef"`},
  32. {"\u263a", `"☺"`, `"\u263a"`},
  33. {"\U0010ffff", `"\U0010ffff"`, `"\U0010ffff"`},
  34. {"\x04", `"\x04"`, `"\x04"`},
  35. }
  36. func TestQuote(t *testing.T) {
  37. for _, tt := range quotetests {
  38. if out := Quote(tt.in); out != tt.out {
  39. t.Errorf("Quote(%s) = %s, want %s", tt.in, out, tt.out)
  40. }
  41. if out := AppendQuote([]byte("abc"), tt.in); string(out) != "abc"+tt.out {
  42. t.Errorf("AppendQuote(%q, %s) = %s, want %s", "abc", tt.in, out, "abc"+tt.out)
  43. }
  44. }
  45. }
  46. func TestQuoteToASCII(t *testing.T) {
  47. for _, tt := range quotetests {
  48. if out := QuoteToASCII(tt.in); out != tt.ascii {
  49. t.Errorf("QuoteToASCII(%s) = %s, want %s", tt.in, out, tt.ascii)
  50. }
  51. if out := AppendQuoteToASCII([]byte("abc"), tt.in); string(out) != "abc"+tt.ascii {
  52. t.Errorf("AppendQuoteToASCII(%q, %s) = %s, want %s", "abc", tt.in, out, "abc"+tt.ascii)
  53. }
  54. }
  55. }
  56. type quoteRuneTest struct {
  57. in rune
  58. out string
  59. ascii string
  60. }
  61. var quoterunetests = []quoteRuneTest{
  62. {'a', `'a'`, `'a'`},
  63. {'\a', `'\a'`, `'\a'`},
  64. {'\\', `'\\'`, `'\\'`},
  65. {0xFF, `'ÿ'`, `'\u00ff'`},
  66. {0x263a, `'☺'`, `'\u263a'`},
  67. {0xfffd, `'�'`, `'\ufffd'`},
  68. {0x0010ffff, `'\U0010ffff'`, `'\U0010ffff'`},
  69. {0x0010ffff + 1, `'�'`, `'\ufffd'`},
  70. {0x04, `'\x04'`, `'\x04'`},
  71. }
  72. func TestQuoteRune(t *testing.T) {
  73. for _, tt := range quoterunetests {
  74. if out := QuoteRune(tt.in); out != tt.out {
  75. t.Errorf("QuoteRune(%U) = %s, want %s", tt.in, out, tt.out)
  76. }
  77. if out := AppendQuoteRune([]byte("abc"), tt.in); string(out) != "abc"+tt.out {
  78. t.Errorf("AppendQuoteRune(%q, %U) = %s, want %s", "abc", tt.in, out, "abc"+tt.out)
  79. }
  80. }
  81. }
  82. func TestQuoteRuneToASCII(t *testing.T) {
  83. for _, tt := range quoterunetests {
  84. if out := QuoteRuneToASCII(tt.in); out != tt.ascii {
  85. t.Errorf("QuoteRuneToASCII(%U) = %s, want %s", tt.in, out, tt.ascii)
  86. }
  87. if out := AppendQuoteRuneToASCII([]byte("abc"), tt.in); string(out) != "abc"+tt.ascii {
  88. t.Errorf("AppendQuoteRuneToASCII(%q, %U) = %s, want %s", "abc", tt.in, out, "abc"+tt.ascii)
  89. }
  90. }
  91. }
  92. type canBackquoteTest struct {
  93. in string
  94. out bool
  95. }
  96. var canbackquotetests = []canBackquoteTest{
  97. {"`", false},
  98. {string(0), false},
  99. {string(1), false},
  100. {string(2), false},
  101. {string(3), false},
  102. {string(4), false},
  103. {string(5), false},
  104. {string(6), false},
  105. {string(7), false},
  106. {string(8), false},
  107. {string(9), true}, // \t
  108. {string(10), false},
  109. {string(11), false},
  110. {string(12), false},
  111. {string(13), false},
  112. {string(14), false},
  113. {string(15), false},
  114. {string(16), false},
  115. {string(17), false},
  116. {string(18), false},
  117. {string(19), false},
  118. {string(20), false},
  119. {string(21), false},
  120. {string(22), false},
  121. {string(23), false},
  122. {string(24), false},
  123. {string(25), false},
  124. {string(26), false},
  125. {string(27), false},
  126. {string(28), false},
  127. {string(29), false},
  128. {string(30), false},
  129. {string(31), false},
  130. {string(0x7F), false},
  131. {`' !"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, true},
  132. {`0123456789`, true},
  133. {`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true},
  134. {`abcdefghijklmnopqrstuvwxyz`, true},
  135. {`☺`, true},
  136. {"\x80", false},
  137. {"a\xe0\xa0z", false},
  138. {"\ufeffabc", false},
  139. {"a\ufeffz", false},
  140. }
  141. func TestCanBackquote(t *testing.T) {
  142. for _, tt := range canbackquotetests {
  143. if out := CanBackquote(tt.in); out != tt.out {
  144. t.Errorf("CanBackquote(%q) = %v, want %v", tt.in, out, tt.out)
  145. }
  146. }
  147. }
  148. type unQuoteTest struct {
  149. in string
  150. out string
  151. }
  152. var unquotetests = []unQuoteTest{
  153. {`""`, ""},
  154. {`"a"`, "a"},
  155. {`"abc"`, "abc"},
  156. {`"☺"`, "☺"},
  157. {`"hello world"`, "hello world"},
  158. {`"\xFF"`, "\xFF"},
  159. {`"\377"`, "\377"},
  160. {`"\u1234"`, "\u1234"},
  161. {`"\U00010111"`, "\U00010111"},
  162. {`"\U0001011111"`, "\U0001011111"},
  163. {`"\a\b\f\n\r\t\v\\\""`, "\a\b\f\n\r\t\v\\\""},
  164. {`"'"`, "'"},
  165. {`'a'`, "a"},
  166. {`'☹'`, "☹"},
  167. {`'\a'`, "\a"},
  168. {`'\x10'`, "\x10"},
  169. {`'\377'`, "\377"},
  170. {`'\u1234'`, "\u1234"},
  171. {`'\U00010111'`, "\U00010111"},
  172. {`'\t'`, "\t"},
  173. {`' '`, " "},
  174. {`'\''`, "'"},
  175. {`'"'`, "\""},
  176. {"``", ``},
  177. {"`a`", `a`},
  178. {"`abc`", `abc`},
  179. {"`☺`", `☺`},
  180. {"`hello world`", `hello world`},
  181. {"`\\xFF`", `\xFF`},
  182. {"`\\377`", `\377`},
  183. {"`\\`", `\`},
  184. {"`\n`", "\n"},
  185. {"` `", ` `},
  186. {"` `", ` `},
  187. }
  188. var misquoted = []string{
  189. ``,
  190. `"`,
  191. `"a`,
  192. `"'`,
  193. `b"`,
  194. `"\"`,
  195. `"\9"`,
  196. `"\19"`,
  197. `"\129"`,
  198. `'\'`,
  199. `'\9'`,
  200. `'\19'`,
  201. `'\129'`,
  202. `'ab'`,
  203. `"\x1!"`,
  204. `"\U12345678"`,
  205. `"\z"`,
  206. "`",
  207. "`xxx",
  208. "`\"",
  209. `"\'"`,
  210. `'\"'`,
  211. "\"\n\"",
  212. "\"\\n\n\"",
  213. "'\n'",
  214. }
  215. func TestUnquote(t *testing.T) {
  216. for _, tt := range unquotetests {
  217. if out, err := Unquote(tt.in); err != nil && out != tt.out {
  218. t.Errorf("Unquote(%#q) = %q, %v want %q, nil", tt.in, out, err, tt.out)
  219. }
  220. }
  221. // run the quote tests too, backward
  222. for _, tt := range quotetests {
  223. if in, err := Unquote(tt.out); in != tt.in {
  224. t.Errorf("Unquote(%#q) = %q, %v, want %q, nil", tt.out, in, err, tt.in)
  225. }
  226. }
  227. for _, s := range misquoted {
  228. if out, err := Unquote(s); out != "" || err != ErrSyntax {
  229. t.Errorf("Unquote(%#q) = %q, %v want %q, %v", s, out, err, "", ErrSyntax)
  230. }
  231. }
  232. }
  233. func BenchmarkUnquoteEasy(b *testing.B) {
  234. for i := 0; i < b.N; i++ {
  235. Unquote(`"Give me a rock, paper and scissors and I will move the world."`)
  236. }
  237. }
  238. func BenchmarkUnquoteHard(b *testing.B) {
  239. for i := 0; i < b.N; i++ {
  240. Unquote(`"\x47ive me a \x72ock, \x70aper and \x73cissors and \x49 will move the world."`)
  241. }
  242. }