1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-02 15:27:46 +01:00
liquid/expressions/scanner_test.go

96 lines
2.4 KiB
Go
Raw Normal View History

2017-07-14 02:18:23 +02:00
package expressions
2017-06-25 18:36:28 +02:00
import (
2017-07-07 17:55:41 +02:00
"fmt"
2017-06-25 18:36:28 +02:00
"testing"
"github.com/stretchr/testify/require"
)
type testSymbol struct {
tok int
typ yySymType
}
2017-07-07 17:55:41 +02:00
func (s testSymbol) String() string {
return fmt.Sprintf("%d:%v", s.tok, s.typ)
}
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-07-07 17:55:41 +02:00
// identifiers
ts, _ = scanExpression(`abc ab_c ab-c abc?`)
require.Len(t, ts, 4)
require.Equal(t, IDENTIFIER, ts[0].tok)
require.Equal(t, IDENTIFIER, ts[1].tok)
require.Equal(t, IDENTIFIER, ts[2].tok)
require.Equal(t, IDENTIFIER, ts[3].tok)
require.Equal(t, "abc", ts[0].typ.name)
require.Equal(t, "ab_c", ts[1].typ.name)
require.Equal(t, "ab-c", ts[2].typ.name)
require.Equal(t, "abc?", ts[3].typ.name)
2017-07-12 20:25:41 +02:00
ts, _ = scanExpression(`{%cycle 'a', 'b'`)
require.Len(t, ts, 4)
2017-07-13 20:58:36 +02:00
ts, _ = scanExpression(`%loop i in (3 .. 5)`)
2017-07-16 18:54:20 +02:00
require.Len(t, ts, 8)
2017-07-13 20:58:36 +02:00
// ts, _ = scanExpression(`%loop i in (3..5)`)
// require.Len(t, ts, 9)
2017-06-25 22:21:31 +02:00
}