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

Function TestTokensForTuple

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

Source from the content-addressed store, hash-verified

556}
557
558func TestTokensForTuple(t *testing.T) {
559 tests := map[string]struct {
560 Val []Tokens
561 Want Tokens
562 }{
563 "no elements": {
564 nil,
565 Tokens{
566 {Type: hclsyntax.TokenOBrack, Bytes: []byte{'['}},
567 {Type: hclsyntax.TokenCBrack, Bytes: []byte{']'}},
568 },
569 },
570 "one element": {
571 []Tokens{
572 TokensForValue(cty.StringVal("foo")),
573 },
574 Tokens{
575 {Type: hclsyntax.TokenOBrack, Bytes: []byte{'['}},
576 {Type: hclsyntax.TokenOQuote, Bytes: []byte(`"`)},
577 {Type: hclsyntax.TokenQuotedLit, Bytes: []byte("foo")},
578 {Type: hclsyntax.TokenCQuote, Bytes: []byte(`"`)},
579 {Type: hclsyntax.TokenCBrack, Bytes: []byte{']'}},
580 },
581 },
582 "two elements": {
583 []Tokens{
584 TokensForTraversal(hcl.Traversal{
585 hcl.TraverseRoot{Name: "root"},
586 hcl.TraverseAttr{Name: "attr"},
587 }),
588 TokensForValue(cty.StringVal("foo")),
589 },
590 Tokens{
591 {Type: hclsyntax.TokenOBrack, Bytes: []byte{'['}},
592 {Type: hclsyntax.TokenIdent, Bytes: []byte("root")},
593 {Type: hclsyntax.TokenDot, Bytes: []byte(".")},
594 {Type: hclsyntax.TokenIdent, Bytes: []byte("attr")},
595 {Type: hclsyntax.TokenComma, Bytes: []byte{','}},
596 {Type: hclsyntax.TokenOQuote, Bytes: []byte(`"`), SpacesBefore: 1},
597 {Type: hclsyntax.TokenQuotedLit, Bytes: []byte("foo")},
598 {Type: hclsyntax.TokenCQuote, Bytes: []byte(`"`)},
599 {Type: hclsyntax.TokenCBrack, Bytes: []byte{']'}},
600 },
601 },
602 }
603
604 for name, test := range tests {
605 t.Run(name, func(t *testing.T) {
606 got := TokensForTuple(test.Val)
607
608 if !cmp.Equal(got, test.Want) {
609 diff := cmp.Diff(got, test.Want, cmp.Comparer(func(a, b []byte) bool {
610 return bytes.Equal(a, b)
611 }))
612 var gotBuf, wantBuf bytes.Buffer
613 _, err := got.WriteTo(&gotBuf)
614 if err != nil {
615 t.Fatalf("failed to write got tokens: %v", err)

Callers

nothing calls this directly

Calls 7

TokensForValueFunction · 0.85
TokensForTraversalFunction · 0.85
TokensForTupleFunction · 0.85
RunMethod · 0.80
EqualMethod · 0.80
WriteToMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected