This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquote_test.go
108 lines (96 loc) · 2.13 KB
/
quote_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause
package yamled
import (
"fmt"
"testing"
)
func TestYamlString(t *testing.T) {
testCases := []struct {
src string
want string
}{
{"a", "a"},
{"@a", "'@a'"},
{"a#b", "a#b"},
{"a #b", "'a #b'"},
{"a\n", "|\n a"},
{"a\n\n", "|+\n a\n"},
{"a\nb\n", "|\n a\n b"},
{"a\nb", "|-\n a\n b"},
{"1", `"1"`},
{"1.0", `"1.0"`},
{"1.0.0", `1.0.0`},
{"1a", `1a`},
}
for i, tc := range testCases {
t.Run(fmt.Sprint(i), func(t *testing.T) {
got, err := yamlString(tc.src, 2)
if err != nil {
t.Fatal(err)
}
if want := tc.want; got != want {
t.Errorf("got: %q, want: %q", got, want)
}
})
}
}
func TestQuote(t *testing.T) {
testCases := []struct {
src string
old string
want string
indent int
}{
{"a", "b", "a", 0},
{"a", `"b"`, `"a"`, 0},
{"1", "b", `"1"`, 0},
{"1.0", "b", `"1.0"`, 0},
{"1.0.0", "b", "1.0.0", 0},
{"1.0.0", `"b"`, `"1.0.0"`, 0},
{"1.0.0", `"1"`, `1.0.0`, 0},
{"a", "'b'", "'a'", 0},
{"a", "'#a'", "a", 0},
{"a\nb", "'b'", "|-\n a\n b", 0},
{"x: y\nbar: y\n", "|\n x: y\nbar: x\n", "|\n x: y\n bar: y", 0},
{"x: y\nbar: y\n", "|\n x: y\n bar: x\n", "|\n x: y\n bar: y", 2},
{"bar: y\n", "|\nbar: x\n", "|\n bar: y", 0},
{"bar: y\n", "|\n bar: x\n", "|\n bar: y", 2},
{`a`, `""`, `"a"`, 0},
{`a`, `''`, `'a'`, 0},
{"1", "0", `1`, 0},
{"true", "false", `true`, 0},
}
for i, tc := range testCases {
t.Run(fmt.Sprint(i), func(t *testing.T) {
got, err := quote(tc.src, tc.old, tc.indent)
if err != nil {
t.Fatal(err)
}
if want := tc.want; got != want {
t.Errorf("got: %q, want: %q", got, want)
}
})
}
}
func TestSingleQuoted(t *testing.T) {
testCases := []struct {
src string
want string
}{
{"a", "'a'"},
{`a\nb`, `'a\nb'`},
{"a\nb", "|-\n a\n b"},
}
for i, tc := range testCases {
t.Run(fmt.Sprint(i), func(t *testing.T) {
got, err := yamlStringTrySingleQuoted(tc.src, 2)
if err != nil {
t.Fatal(err)
}
if want := tc.want; got != want {
t.Errorf("got: %q, want: %q", got, want)
}
})
}
}