github.com/aretext/aretext@v1.3.0/syntax/languages/gotemplate_test.go (about) 1 package languages 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/aretext/aretext/syntax/parser" 9 ) 10 11 func TestGoTemplateParseFunc(t *testing.T) { 12 testCases := []struct { 13 name string 14 text string 15 expected []TokenWithText 16 }{ 17 { 18 name: "text with no actions", 19 text: "abcd", 20 expected: []TokenWithText{}, 21 }, 22 { 23 name: "action interpolate variable", 24 text: "abc {{ .Test }} xyz", 25 expected: []TokenWithText{ 26 {Text: `{{`, Role: parser.TokenRoleOperator}, 27 {Text: `}}`, Role: parser.TokenRoleOperator}, 28 }, 29 }, 30 { 31 name: "action trim whitespace", 32 text: "{{- .Test -}}", 33 expected: []TokenWithText{ 34 {Text: `{{-`, Role: parser.TokenRoleOperator}, 35 {Text: `-}}`, Role: parser.TokenRoleOperator}, 36 }, 37 }, 38 { 39 name: "comment", 40 text: "{{ /* abc */ }}", 41 expected: []TokenWithText{ 42 {Text: `{{`, Role: parser.TokenRoleOperator}, 43 {Text: `/* abc */`, Role: parser.TokenRoleComment}, 44 {Text: `}}`, Role: parser.TokenRoleOperator}, 45 }, 46 }, 47 { 48 name: "string", 49 text: `{{ "abc" }}`, 50 expected: []TokenWithText{ 51 {Text: `{{`, Role: parser.TokenRoleOperator}, 52 {Text: `"abc"`, Role: parser.TokenRoleString}, 53 {Text: `}}`, Role: parser.TokenRoleOperator}, 54 }, 55 }, 56 { 57 name: "if else", 58 text: "{{ if .Value }} abc {{ else }} xyz {{ end }}", 59 expected: []TokenWithText{ 60 {Text: `{{`, Role: parser.TokenRoleOperator}, 61 {Text: `if`, Role: parser.TokenRoleKeyword}, 62 {Text: `}}`, Role: parser.TokenRoleOperator}, 63 {Text: `{{`, Role: parser.TokenRoleOperator}, 64 {Text: `else`, Role: parser.TokenRoleKeyword}, 65 {Text: `}}`, Role: parser.TokenRoleOperator}, 66 {Text: `{{`, Role: parser.TokenRoleOperator}, 67 {Text: `end`, Role: parser.TokenRoleKeyword}, 68 {Text: `}}`, Role: parser.TokenRoleOperator}, 69 }, 70 }, 71 { 72 name: "keyword in variable name", 73 text: `{{ .Values.range.end }}`, 74 expected: []TokenWithText{ 75 {Text: `{{`, Role: parser.TokenRoleOperator}, 76 {Text: `}}`, Role: parser.TokenRoleOperator}, 77 }, 78 }, 79 { 80 name: "pipe", 81 text: `{{"abcd" | printf "%q"}}`, 82 expected: []TokenWithText{ 83 {Text: `{{`, Role: parser.TokenRoleOperator}, 84 {Text: `"abcd"`, Role: parser.TokenRoleString}, 85 {Text: `|`, Role: parser.TokenRoleOperator}, 86 {Text: `printf`, Role: parser.TokenRoleKeyword}, 87 {Text: `"%q"`, Role: parser.TokenRoleString}, 88 {Text: `}}`, Role: parser.TokenRoleOperator}, 89 }, 90 }, 91 { 92 name: "variable assignment", 93 text: `{{with $x := "abc"}}{{$x}}{{end}}`, 94 expected: []TokenWithText{ 95 {Text: `{{`, Role: parser.TokenRoleOperator}, 96 {Text: `with`, Role: parser.TokenRoleKeyword}, 97 {Text: `$`, Role: parser.TokenRoleOperator}, 98 {Text: `:=`, Role: parser.TokenRoleOperator}, 99 {Text: `"abc"`, Role: parser.TokenRoleString}, 100 {Text: `}}`, Role: parser.TokenRoleOperator}, 101 {Text: `{{`, Role: parser.TokenRoleOperator}, 102 {Text: `$`, Role: parser.TokenRoleOperator}, 103 {Text: `}}`, Role: parser.TokenRoleOperator}, 104 {Text: `{{`, Role: parser.TokenRoleOperator}, 105 {Text: `end`, Role: parser.TokenRoleKeyword}, 106 {Text: `}}`, Role: parser.TokenRoleOperator}, 107 }, 108 }, 109 { 110 name: "binary comparison operators", 111 text: `{{ if or (.A eq .B) (.C gt 123) }}`, 112 expected: []TokenWithText{ 113 {Text: `{{`, Role: parser.TokenRoleOperator}, 114 {Text: `if`, Role: parser.TokenRoleKeyword}, 115 {Text: `or`, Role: parser.TokenRoleKeyword}, 116 {Text: `eq`, Role: parser.TokenRoleKeyword}, 117 {Text: `gt`, Role: parser.TokenRoleKeyword}, 118 {Text: `}}`, Role: parser.TokenRoleOperator}, 119 }, 120 }, 121 { 122 name: "nested template definition", 123 text: `{{define "foo"}}foo{{end}}`, 124 expected: []TokenWithText{ 125 {Text: `{{`, Role: parser.TokenRoleOperator}, 126 {Text: `define`, Role: parser.TokenRoleKeyword}, 127 {Text: `"foo"`, Role: parser.TokenRoleString}, 128 {Text: `}}`, Role: parser.TokenRoleOperator}, 129 {Text: `{{`, Role: parser.TokenRoleOperator}, 130 {Text: `end`, Role: parser.TokenRoleKeyword}, 131 {Text: `}}`, Role: parser.TokenRoleOperator}, 132 }, 133 }, 134 } 135 136 for _, tc := range testCases { 137 t.Run(tc.name, func(t *testing.T) { 138 tokens := ParseTokensWithText(GoTemplateParseFunc(), tc.text) 139 assert.Equal(t, tc.expected, tokens) 140 }) 141 } 142 }