1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package cue_test
16
17 import (
18 "fmt"
19 "path"
20 "testing"
21
22 "cuelang.org/go/cue"
23 "cuelang.org/go/cue/cuecontext"
24 )
25
26 func ExampleValue_Format() {
27 ctx := cuecontext.New()
28
29 v := ctx.CompileString(`
30 a: 2 + b
31 b: *3 | int
32 s: "foo\nbar"
33 `)
34
35 fmt.Println("### ALL")
36 fmt.Println(v)
37 fmt.Println("---")
38 fmt.Printf("%#v\n", v)
39 fmt.Println("---")
40 fmt.Printf("%+v\n", v)
41
42 a := v.LookupPath(cue.ParsePath("a"))
43 fmt.Println("\n### INT")
44 fmt.Printf("%%v: %v\n", a)
45 fmt.Printf("%%05d: %05d\n", a)
46
47 s := v.LookupPath(cue.ParsePath("s"))
48 fmt.Println("\n### STRING")
49 fmt.Printf("%%v: %v\n", s)
50 fmt.Printf("%%s: %s\n", s)
51 fmt.Printf("%%q: %q\n", s)
52
53 v = ctx.CompileString(`
54 #Def: a: [string]: int
55 b: #Def
56 b: a: {
57 a: 3
58 b: 3
59 }
60 `)
61 b := v.LookupPath(cue.ParsePath("b.a"))
62 fmt.Println("\n### DEF")
63 fmt.Println(b)
64 fmt.Println("---")
65
66
67 fmt.Printf("%#v\n", b)
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
109
110
111
112
113
114
115
116
117
118
119
120 }
121
122 func TestFormat(t *testing.T) {
123 tests := func(s ...string) (a [][2]string) {
124 for i := 0; i < len(s); i += 2 {
125 a = append(a, [2]string{s[i], s[i+1]})
126 }
127 return a
128 }
129 testCases := []struct {
130 desc string
131 in string
132 out [][2]string
133 }{{
134 desc: "int",
135 in: `12 + 14`,
136 out: tests(
137 "%#v", "26",
138 "%d", "26",
139 "%o", "32",
140 "%O", "0o32",
141 "%x", "1a",
142 "%X", "1A",
143 "%q", `"26"`,
144 "%0.3d", "026",
145 ),
146 }, {
147 desc: "float",
148 in: `12.2 + 14.4`,
149 out: tests(
150 "%#v", "26.6",
151 "%5f", " 26.6",
152 "%e", "2.66e+1",
153 "%08E", "02.66E+1",
154 "%g", "26.6",
155 "%3G", "26.6",
156 ),
157 }, {
158 desc: "strings",
159 in: `"string"`,
160 out: tests(
161 "%v", `"string"`,
162 "%s", "string",
163 "%x", "737472696e67",
164 "%X", "737472696E67",
165 ),
166 }, {
167 desc: "multiline string",
168 in: `"""
169 foo
170 bar
171 """`,
172 out: tests(
173 "%#v", `"""
174 foo
175 bar
176 """`,
177 "%s", "foo\nbar",
178 "%q", `"foo\nbar"`,
179 ),
180 }, {
181 desc: "multiline bytes",
182 in: `'''
183 foo
184 bar
185 '''`,
186 out: tests(
187 "%#v", `'''
188 foo
189 bar
190 '''`,
191 "%s", "foo\nbar",
192 "%q", `"foo\nbar"`,
193 ),
194 }, {
195 desc: "interpolation",
196 in: `
197 #D: {
198 a: string
199 b: "hello \(a)"
200 }
201 d: #D
202 d: a: "world"
203 x: *1 | int
204 `,
205 out: tests(
206 "%v", `{
207 d: {
208 a: "world"
209 b: "hello world"
210 }
211 x: *1 | int
212 }`,
213 "%#v", `#D: {
214 a: string
215 b: "hello \(a)"
216 }
217 d: #D & {
218 a: "world"
219 }
220 x: *1 | int`,
221 "%+v", `{
222 d: {
223 a: "world"
224 b: "hello world"
225 }
226 x: 1
227 }`,
228 ),
229 }, {
230 desc: "indent",
231 in: `
232 a: {
233 b: """
234 foo
235 bar
236 """
237 c: int
238 }`,
239 out: tests(
240 "%v", `{
241 a: {
242 b: """
243 foo
244 bar
245 """
246 c: int
247 }
248 }`,
249 "%3v", `{
250 a: {
251 b: """
252 foo
253 bar
254 """
255 c: int
256 }
257 }`,
258 "%.1v", `{
259 a: {
260 b: """
261 foo
262 bar
263 """
264 c: int
265 }
266 }`,
267 "%3.1v", `{
268 a: {
269 b: """
270 foo
271 bar
272 """
273 c: int
274 }
275 }`,
276 ),
277 }, {
278 desc: "imports",
279 in: `
280 import "strings"
281 a: strings.Contains("foo")
282 `,
283 out: tests(
284 "%v", `{
285 a: strings.Contains("foo")
286 }`,
287 "%+v", `{
288 a: strings.Contains("foo")
289 }`,
290 "%#v", `import "strings"
291
292 a: strings.Contains("foo")`,
293 ),
294 }}
295 ctx := cuecontext.New()
296 for _, tc := range testCases {
297 for _, test := range tc.out {
298 t.Run(path.Join(tc.desc, test[0]), func(t *testing.T) {
299 v := ctx.CompileString(tc.in)
300 got := fmt.Sprintf(test[0], v)
301 if got != test[1] {
302 t.Errorf(" got: %s\nwant: %s", got, test[1])
303 }
304 })
305 }
306 }
307 }
308
View as plain text