github.com/aretext/aretext@v1.3.0/syntax/parser/tracking_test.go (about) 1 package parser 2 3 import ( 4 "io" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 10 "github.com/aretext/aretext/text" 11 ) 12 13 func TestTrackingRuneIter(t *testing.T) { 14 tree, err := text.NewTreeFromString("abcd") 15 require.NoError(t, err) 16 reader := tree.ReaderAtPosition(0) 17 trackingIter := NewTrackingRuneIter(reader) 18 assert.Equal(t, uint64(0), trackingIter.MaxRead()) 19 20 r, err := trackingIter.NextRune() 21 require.NoError(t, err) 22 assert.Equal(t, 'a', r) 23 assert.Equal(t, uint64(1), trackingIter.MaxRead()) 24 25 cloneIter := trackingIter 26 r, err = cloneIter.NextRune() 27 require.NoError(t, err) 28 assert.Equal(t, 'b', r) 29 assert.Equal(t, uint64(2), trackingIter.MaxRead()) 30 31 r, err = trackingIter.NextRune() 32 require.NoError(t, err) 33 assert.Equal(t, 'b', r) 34 assert.Equal(t, uint64(2), trackingIter.MaxRead()) 35 36 r, err = trackingIter.NextRune() 37 require.NoError(t, err) 38 assert.Equal(t, 'c', r) 39 assert.Equal(t, uint64(3), trackingIter.MaxRead()) 40 41 r, err = trackingIter.NextRune() 42 require.NoError(t, err) 43 assert.Equal(t, 'd', r) 44 assert.Equal(t, uint64(4), trackingIter.MaxRead()) 45 46 // EOF should include one position past the end of text. 47 _, err = trackingIter.NextRune() 48 assert.ErrorIs(t, err, io.EOF) 49 assert.Equal(t, uint64(5), trackingIter.MaxRead()) 50 51 r, err = cloneIter.NextRune() 52 require.NoError(t, err) 53 assert.Equal(t, 'c', r) 54 assert.Equal(t, uint64(5), trackingIter.MaxRead()) 55 56 // Read EOF again, shouldn't affect max read. 57 _, err = trackingIter.NextRune() 58 assert.ErrorIs(t, err, io.EOF) 59 assert.Equal(t, uint64(5), trackingIter.MaxRead()) 60 } 61 62 func TestTrackingRuneIterLimit(t *testing.T) { 63 tree, err := text.NewTreeFromString("abcd") 64 require.NoError(t, err) 65 reader := tree.ReaderAtPosition(0) 66 trackingIter := NewTrackingRuneIter(reader) 67 68 // Set a limit of two runes. 69 trackingIter.Limit(2) 70 71 // Read the first rune. 72 r, err := trackingIter.NextRune() 73 require.NoError(t, err) 74 assert.Equal(t, 'a', r) 75 76 // Read the second rune. 77 r, err = trackingIter.NextRune() 78 require.NoError(t, err) 79 assert.Equal(t, 'b', r) 80 81 // Hit the limit, so expect EOF. 82 _, err = trackingIter.NextRune() 83 require.ErrorIs(t, err, io.EOF) 84 }