...
1 package tokens_test
2
3 import (
4 "testing"
5
6 "github.com/noirbizarre/gonja/tokens"
7 "github.com/stretchr/testify/assert"
8 )
9
10 const multilineSample = `Hello
11 {#
12 Multiline comment
13 #}
14 World
15 `
16
17 var readablePositionsCases = []struct {
18 name string
19 pos int
20 line int
21 col int
22 char byte
23 }{
24 {"First char", 0, 1, 1, 'H'},
25 {"Last char", len(multilineSample) - 1, 5, 6, '\n'},
26 {"Anywhere", 14, 3, 5, 'M'},
27 }
28
29 func TestReadablePosition(t *testing.T) {
30 for _, rp := range readablePositionsCases {
31 test := rp
32 t.Run(test.name, func(t *testing.T) {
33 assert := assert.New(t)
34 assert.Equalf(test.char, multilineSample[test.pos],
35 `Invalid test, expected "%#U" rune at pos %d, got "%#U"`,
36 test.char, test.pos, multilineSample[test.pos])
37 line, col := tokens.ReadablePosition(test.pos, multilineSample)
38 assert.Equalf(test.line, line, "Expected line %d, got %d", test.line, line)
39 assert.Equalf(test.col, col, "Expected col %d, got %d", test.col, col)
40 })
41 }
42 }
43
View as plain text