OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / hashicorp / hcl / hcl / strconv / quote_test.go
1 package strconv
2
3 import "testing"
4
5 type quoteTest struct {
6         in    string
7         out   string
8         ascii string
9 }
10
11 var quotetests = []quoteTest{
12         {"\a\b\f\r\n\t\v", `"\a\b\f\r\n\t\v"`, `"\a\b\f\r\n\t\v"`},
13         {"\\", `"\\"`, `"\\"`},
14         {"abc\xffdef", `"abc\xffdef"`, `"abc\xffdef"`},
15         {"\u263a", `"☺"`, `"\u263a"`},
16         {"\U0010ffff", `"\U0010ffff"`, `"\U0010ffff"`},
17         {"\x04", `"\x04"`, `"\x04"`},
18 }
19
20 type unQuoteTest struct {
21         in  string
22         out string
23 }
24
25 var unquotetests = []unQuoteTest{
26         {`""`, ""},
27         {`"a"`, "a"},
28         {`"abc"`, "abc"},
29         {`"☺"`, "☺"},
30         {`"hello world"`, "hello world"},
31         {`"\xFF"`, "\xFF"},
32         {`"\377"`, "\377"},
33         {`"\u1234"`, "\u1234"},
34         {`"\U00010111"`, "\U00010111"},
35         {`"\U0001011111"`, "\U0001011111"},
36         {`"\a\b\f\n\r\t\v\\\""`, "\a\b\f\n\r\t\v\\\""},
37         {`"'"`, "'"},
38         {`"${file("foo")}"`, `${file("foo")}`},
39         {`"${file("\"foo\"")}"`, `${file("\"foo\"")}`},
40         {`"echo ${var.region}${element(split(",",var.zones),0)}"`,
41                 `echo ${var.region}${element(split(",",var.zones),0)}`},
42         {`"${HH\\:mm\\:ss}"`, `${HH\\:mm\\:ss}`},
43         {`"${\n}"`, `${\n}`},
44 }
45
46 var misquoted = []string{
47         ``,
48         `"`,
49         `"a`,
50         `"'`,
51         `b"`,
52         `"\"`,
53         `"\9"`,
54         `"\19"`,
55         `"\129"`,
56         `'\'`,
57         `'\9'`,
58         `'\19'`,
59         `'\129'`,
60         `'ab'`,
61         `"\x1!"`,
62         `"\U12345678"`,
63         `"\z"`,
64         "`",
65         "`xxx",
66         "`\"",
67         `"\'"`,
68         `'\"'`,
69         "\"\n\"",
70         "\"\\n\n\"",
71         "'\n'",
72         `"${"`,
73         `"${foo{}"`,
74         "\"${foo}\n\"",
75 }
76
77 func TestUnquote(t *testing.T) {
78         for _, tt := range unquotetests {
79                 if out, err := Unquote(tt.in); err != nil || out != tt.out {
80                         t.Errorf("Unquote(%#q) = %q, %v want %q, nil", tt.in, out, err, tt.out)
81                 }
82         }
83
84         // run the quote tests too, backward
85         for _, tt := range quotetests {
86                 if in, err := Unquote(tt.out); in != tt.in {
87                         t.Errorf("Unquote(%#q) = %q, %v, want %q, nil", tt.out, in, err, tt.in)
88                 }
89         }
90
91         for _, s := range misquoted {
92                 if out, err := Unquote(s); out != "" || err != ErrSyntax {
93                         t.Errorf("Unquote(%#q) = %q, %v want %q, %v", s, out, err, "", ErrSyntax)
94                 }
95         }
96 }