this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cue/format: add a test showing how each Option behaves

And especially showing how the current default tabWidth of 8
can affect cue/format users.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Iad9df762bbd51f136c0b31c2189461f1c15ef4d5
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1233636
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>

+73
+73
cue/format/format_test.go
··· 27 27 "cuelang.org/go/cue/parser" 28 28 "cuelang.org/go/cue/token" 29 29 "cuelang.org/go/internal" 30 + "cuelang.org/go/internal/cuetest" 30 31 "cuelang.org/go/internal/cuetxtar" 32 + "cuelang.org/go/internal/tdtest" 31 33 ) 32 34 33 35 const debug = false ··· 352 354 } 353 355 }) 354 356 } 357 + } 358 + 359 + func TestSourceOptions(t *testing.T) { 360 + // Input with a nested struct, aligned fields, 361 + // and a quoted label that could be simplified to a plain identifier. 362 + src := ` 363 + "foo": { 364 + a: 1 365 + longField: 2 366 + } 367 + `[1:] 368 + type testCase struct { 369 + name string 370 + options []format.Option 371 + want string 372 + } 373 + testCases := []testCase{ 374 + // No options: default behavior uses tabs for indentation and spaces for alignment. 375 + // The input source remains as-is. 376 + { 377 + name: "Defaults", 378 + want: src, 379 + }, 380 + // Setting options to their default values is also a no-op. 381 + { 382 + name: "DefaultValues", 383 + options: []format.Option{format.TabIndent(true), format.UseSpaces(8)}, 384 + want: src, 385 + }, 386 + // UseSpaces setting a different tabWidth makes no difference unless we indent with spaces. 387 + { 388 + name: "UseSpaces=2", 389 + options: []format.Option{format.UseSpaces(2)}, 390 + want: src, 391 + }, 392 + 393 + // Simplify removes unnecessary quotes from "foo". 394 + { 395 + name: "Simplify", 396 + options: []format.Option{format.Simplify()}, 397 + want: "foo: {\n\ta: 1\n\tlongField: 2\n}\n", 398 + }, 399 + // TabIndent(false) makes the indentation use a tabWidth number of spaces. 400 + // Note that this exposes the default tabWidth value of 8. 401 + { 402 + name: "TabIndent=false", 403 + options: []format.Option{format.TabIndent(false)}, 404 + want: "\"foo\": {\n a: 1\n longField: 2\n}\n", 405 + }, 406 + // TabIndent(false) with a custom number of spaces. 407 + { 408 + name: "TabIndent=false,UseSpaces=2", 409 + options: []format.Option{format.TabIndent(false), format.UseSpaces(2)}, 410 + want: "\"foo\": {\n a: 1\n longField: 2\n}\n", 411 + }, 412 + // IndentPrefix indents every line as a prefix. 413 + // 414 + // TODO(mvdan): this seems buggy? note the trailing tabs, and the lack of leading tabs. 415 + // Or at the very least, the docs are misleading. 416 + { 417 + name: "IndentPrefix(3)", 418 + options: []format.Option{format.IndentPrefix(3)}, 419 + want: "\"foo\": {\n\t\t\t\ta: 1\n\t\t\t\tlongField: 2\n\t\t\t}\n\t\t", 420 + }, 421 + } 422 + tdtest.Run(t, testCases, func(t *cuetest.T, tc *testCase) { 423 + t.Update(cuetest.UpdateGoldenFiles) 424 + got, err := format.Source([]byte(src), tc.options...) 425 + qt.Assert(t, qt.IsNil(err)) 426 + t.Equal(string(got), tc.want) 427 + }) 355 428 } 356 429 357 430 // TextX is a skeleton test that can be filled in for debugging one-off cases.