1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-02 21:57:47 +01:00
liquid/expression/scanner_test.go

78 lines
1.8 KiB
Go
Raw Normal View History

2017-06-25 18:36:28 +02:00
//go:generate ragel -Z scanner.rl
2017-06-26 15:33:07 +02:00
//go:generate goyacc expressions.y
2017-06-25 18:36:28 +02:00
2017-07-04 17:12:40 +02:00
package expression
2017-06-25 18:36:28 +02:00
import (
"testing"
"github.com/stretchr/testify/require"
)
// var lexerTests = []struct{}{
// {"{{var}}", "value"},
// {"{{x}}", "1"},
// }
type testSymbol struct {
tok int
typ yySymType
}
func scanExpression(data string) ([]testSymbol, error) {
var (
lex = newLexer([]byte(data))
symbols []testSymbol
s yySymType
)
2017-06-25 22:21:31 +02:00
for {
tok := lex.Lex(&s)
if tok == 0 {
2017-06-25 22:21:31 +02:00
break
}
symbols = append(symbols, testSymbol{tok, s})
2017-06-25 22:21:31 +02:00
}
return symbols, nil
}
2017-07-07 15:32:24 +02:00
func TestLex(t *testing.T) {
ts, err := scanExpression("abc > 123")
2017-06-25 18:36:28 +02:00
require.NoError(t, err)
require.Len(t, ts, 3)
require.Equal(t, IDENTIFIER, ts[0].tok)
require.Equal(t, "abc", ts[0].typ.name)
require.Equal(t, LITERAL, ts[2].tok)
require.Equal(t, 123, ts[2].typ.val)
// verify these don't match "for", "or", or "false"
ts, _ = scanExpression("forage")
require.Len(t, ts, 1)
ts, _ = scanExpression("orange")
require.Len(t, ts, 1)
ts, _ = scanExpression("falsehood")
require.Len(t, ts, 1)
2017-07-02 13:51:24 +02:00
ts, _ = scanExpression("a.b-c")
require.Len(t, ts, 2)
require.Equal(t, PROPERTY, ts[1].tok)
require.Equal(t, "b-c", ts[1].typ.name)
2017-07-02 13:51:24 +02:00
// literals
ts, _ = scanExpression(`true false nil 2 2.3 "abc" 'abc'`)
require.Len(t, ts, 7)
require.Equal(t, LITERAL, ts[0].tok)
require.Equal(t, LITERAL, ts[1].tok)
require.Equal(t, LITERAL, ts[2].tok)
require.Equal(t, LITERAL, ts[3].tok)
require.Equal(t, LITERAL, ts[4].tok)
require.Equal(t, LITERAL, ts[5].tok)
require.Equal(t, LITERAL, ts[6].tok)
require.Equal(t, true, ts[0].typ.val)
require.Equal(t, false, ts[1].typ.val)
require.Equal(t, nil, ts[2].typ.val)
require.Equal(t, 2, ts[3].typ.val)
require.Equal(t, 2.3, ts[4].typ.val)
require.Equal(t, "abc", ts[5].typ.val)
2017-07-07 15:32:24 +02:00
require.Equal(t, "abc", ts[6].typ.val)
2017-06-25 22:21:31 +02:00
}