MCPcopy
hub / github.com/hashicorp/hcl / TestTokensForTraversal

Function TestTokensForTraversal

hclwrite/generate_test.go:508–556  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

506}
507
508func TestTokensForTraversal(t *testing.T) {
509 tests := []struct {
510 Val hcl.Traversal
511 Want Tokens
512 }{
513 {
514 hcl.Traversal{
515 hcl.TraverseRoot{Name: "root"},
516 hcl.TraverseAttr{Name: "attr"},
517 hcl.TraverseIndex{Key: cty.StringVal("index")},
518 },
519 Tokens{
520 {Type: hclsyntax.TokenIdent, Bytes: []byte("root")},
521 {Type: hclsyntax.TokenDot, Bytes: []byte(".")},
522 {Type: hclsyntax.TokenIdent, Bytes: []byte("attr")},
523 {Type: hclsyntax.TokenOBrack, Bytes: []byte{'['}},
524 {Type: hclsyntax.TokenOQuote, Bytes: []byte(`"`)},
525 {Type: hclsyntax.TokenQuotedLit, Bytes: []byte("index")},
526 {Type: hclsyntax.TokenCQuote, Bytes: []byte(`"`)},
527 {Type: hclsyntax.TokenCBrack, Bytes: []byte{']'}},
528 },
529 },
530 }
531
532 for _, test := range tests {
533 got := TokensForTraversal(test.Val)
534
535 if !cmp.Equal(got, test.Want) {
536 diff := cmp.Diff(got, test.Want, cmp.Comparer(func(a, b []byte) bool {
537 return bytes.Equal(a, b)
538 }))
539 var gotBuf, wantBuf bytes.Buffer
540 _, err := got.WriteTo(&gotBuf)
541 if err != nil {
542 t.Fatalf("failed to write got tokens: %v", err)
543 }
544
545 _, err = test.Want.WriteTo(&wantBuf)
546 if err != nil {
547 t.Fatalf("failed to write Want tokens: %v", err)
548 }
549
550 t.Errorf(
551 "wrong result\nvalue: %#v\ngot: %s\nwant: %s\ndiff: %s",
552 test.Val, gotBuf.String(), wantBuf.String(), diff,
553 )
554 }
555 }
556}
557
558func TestTokensForTuple(t *testing.T) {
559 tests := map[string]struct {

Callers

nothing calls this directly

Calls 4

TokensForTraversalFunction · 0.85
EqualMethod · 0.80
WriteToMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected