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

Function TestTokensForFunctionCall

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

Source from the content-addressed store, hash-verified

727}
728
729func TestTokensForFunctionCall(t *testing.T) {
730 tests := map[string]struct {
731 FuncName string
732 Val []Tokens
733 Want Tokens
734 }{
735 "no arguments": {
736 "uuid",
737 nil,
738 Tokens{
739 {Type: hclsyntax.TokenIdent, Bytes: []byte("uuid")},
740 {Type: hclsyntax.TokenOParen, Bytes: []byte{'('}},
741 {Type: hclsyntax.TokenCParen, Bytes: []byte(")")},
742 },
743 },
744 "one argument": {
745 "strlen",
746 []Tokens{
747 TokensForValue(cty.StringVal("hello")),
748 },
749 Tokens{
750 {Type: hclsyntax.TokenIdent, Bytes: []byte("strlen")},
751 {Type: hclsyntax.TokenOParen, Bytes: []byte{'('}},
752 {Type: hclsyntax.TokenOQuote, Bytes: []byte(`"`)},
753 {Type: hclsyntax.TokenQuotedLit, Bytes: []byte("hello")},
754 {Type: hclsyntax.TokenCQuote, Bytes: []byte(`"`)},
755 {Type: hclsyntax.TokenCParen, Bytes: []byte(")")},
756 },
757 },
758 "two arguments": {
759 "list",
760 []Tokens{
761 TokensForIdentifier("string"),
762 TokensForIdentifier("int"),
763 },
764 Tokens{
765 {Type: hclsyntax.TokenIdent, Bytes: []byte("list")},
766 {Type: hclsyntax.TokenOParen, Bytes: []byte{'('}},
767 {Type: hclsyntax.TokenIdent, Bytes: []byte("string")},
768 {Type: hclsyntax.TokenComma, Bytes: []byte(",")},
769 {Type: hclsyntax.TokenIdent, Bytes: []byte("int"), SpacesBefore: 1},
770 {Type: hclsyntax.TokenCParen, Bytes: []byte(")")},
771 },
772 },
773 }
774
775 for name, test := range tests {
776 t.Run(name, func(t *testing.T) {
777 got := TokensForFunctionCall(test.FuncName, test.Val...)
778
779 if !cmp.Equal(got, test.Want) {
780 diff := cmp.Diff(got, test.Want, cmp.Comparer(func(a, b []byte) bool {
781 return bytes.Equal(a, b)
782 }))
783 var gotBuf, wantBuf bytes.Buffer
784 _, err := got.WriteTo(&gotBuf)
785 if err != nil {
786 t.Fatalf("failed to write got tokens: %v", err)

Callers

nothing calls this directly

Calls 7

TokensForValueFunction · 0.85
TokensForIdentifierFunction · 0.85
TokensForFunctionCallFunction · 0.85
RunMethod · 0.80
EqualMethod · 0.80
WriteToMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected