2017-07-07 11:41:37 +02:00
|
|
|
package parser
|
2017-06-27 19:18:11 +02:00
|
|
|
|
|
|
|
import (
|
2017-06-28 04:34:46 +02:00
|
|
|
"fmt"
|
2017-06-27 19:18:11 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-06-28 04:34:46 +02:00
|
|
|
var scannerCountTests = []struct {
|
|
|
|
in string
|
|
|
|
len int
|
|
|
|
}{
|
|
|
|
{`{% tag arg %}`, 1},
|
|
|
|
{`{% tag arg %}{% tag %}`, 2},
|
|
|
|
{`{% tag arg %}{% tag arg %}{% tag %}`, 3},
|
|
|
|
{`{% tag %}{% tag %}`, 2},
|
|
|
|
{`{% tag arg %}{% tag arg %}{% tag %}{% tag %}`, 4},
|
|
|
|
{`{{ expr }}`, 1},
|
|
|
|
{`{{ expr arg }}`, 1},
|
|
|
|
{`{{ expr }}{{ expr }}`, 2},
|
|
|
|
{`{{ expr arg }}{{ expr arg }}`, 2},
|
|
|
|
}
|
|
|
|
|
2017-07-02 13:51:24 +02:00
|
|
|
func TestChunkScanner(t *testing.T) {
|
2017-06-27 19:18:11 +02:00
|
|
|
tokens := Scan("12", "")
|
|
|
|
require.NotNil(t, tokens)
|
|
|
|
require.Len(t, tokens, 1)
|
|
|
|
require.Equal(t, TextChunkType, tokens[0].Type)
|
|
|
|
require.Equal(t, "12", tokens[0].Source)
|
|
|
|
|
|
|
|
tokens = Scan("{{obj}}", "")
|
|
|
|
require.NotNil(t, tokens)
|
|
|
|
require.Len(t, tokens, 1)
|
|
|
|
require.Equal(t, ObjChunkType, tokens[0].Type)
|
2017-06-30 23:33:36 +02:00
|
|
|
require.Equal(t, "obj", tokens[0].Args)
|
2017-06-27 19:18:11 +02:00
|
|
|
|
|
|
|
tokens = Scan("{{ obj }}", "")
|
|
|
|
require.NotNil(t, tokens)
|
|
|
|
require.Len(t, tokens, 1)
|
|
|
|
require.Equal(t, ObjChunkType, tokens[0].Type)
|
2017-06-30 23:33:36 +02:00
|
|
|
require.Equal(t, "obj", tokens[0].Args)
|
2017-06-27 19:18:11 +02:00
|
|
|
|
|
|
|
tokens = Scan("{%tag args%}", "")
|
|
|
|
require.NotNil(t, tokens)
|
|
|
|
require.Len(t, tokens, 1)
|
|
|
|
require.Equal(t, TagChunkType, tokens[0].Type)
|
2017-06-29 18:20:16 +02:00
|
|
|
require.Equal(t, "tag", tokens[0].Name)
|
2017-06-30 23:33:36 +02:00
|
|
|
require.Equal(t, "args", tokens[0].Args)
|
2017-06-27 19:18:11 +02:00
|
|
|
|
|
|
|
tokens = Scan("{% tag args %}", "")
|
|
|
|
require.NotNil(t, tokens)
|
|
|
|
require.Len(t, tokens, 1)
|
|
|
|
require.Equal(t, TagChunkType, tokens[0].Type)
|
2017-06-29 18:20:16 +02:00
|
|
|
require.Equal(t, "tag", tokens[0].Name)
|
2017-06-30 23:33:36 +02:00
|
|
|
require.Equal(t, "args", tokens[0].Args)
|
2017-06-28 04:34:46 +02:00
|
|
|
|
2017-07-02 13:51:24 +02:00
|
|
|
tokens = Scan("pre{% tag args %}mid{{ object }}post", "")
|
|
|
|
require.Equal(t, `[TextChunkType{"pre"} TagChunkType{Tag:"tag", Args:"args"} TextChunkType{"mid"} ObjChunkType{"object"} TextChunkType{"post"}]`, fmt.Sprint(tokens))
|
|
|
|
|
2017-06-28 04:34:46 +02:00
|
|
|
for i, test := range scannerCountTests {
|
|
|
|
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
|
|
|
|
tokens := Scan(test.in, "")
|
|
|
|
require.Len(t, tokens, test.len)
|
|
|
|
})
|
|
|
|
}
|
2017-06-27 19:18:11 +02:00
|
|
|
}
|