aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/strings/textscanner/textscanner_test.v
blob: e9d248769a58920c184736ed04765e9ecb4a71a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import strings.textscanner

fn test_remaining() {
	mut s := textscanner.new('abc')
	assert s.remaining() == 3
	s.next()
	s.next()
	assert s.remaining() == 1
	s.next()
	assert s.remaining() == 0
	s.next()
	s.next()
	assert s.remaining() == 0
	s.reset()
	assert s.remaining() == 3
}

fn test_next() {
	mut s := textscanner.new('abc')
	assert s.next() == `a`
	assert s.next() == `b`
	assert s.next() == `c`
	assert s.next() == -1
	assert s.next() == -1
	assert s.next() == -1
}

fn test_skip() {
	mut s := textscanner.new('abc')
	assert s.next() == `a`
	s.skip()
	assert s.next() == `c`
	assert s.next() == -1
}

fn test_skip_n() {
	mut s := textscanner.new('abc')
	s.skip_n(2)
	assert s.next() == `c`
	assert s.next() == -1
}

fn test_peek() {
	mut s := textscanner.new('abc')
	assert s.peek() == `a`
	assert s.peek() == `a`
	assert s.peek() == `a`
	//
	assert s.next() == `a`
	assert s.next() == `b`
	assert s.next() == `c`
	assert s.next() == -1
}

fn test_peek_n() {
	mut s := textscanner.new('abc')
	assert s.peek_n(0) == `a`
	assert s.peek_n(1) == `b`
	assert s.peek_n(2) == `c`
	assert s.peek_n(3) == -1
	assert s.peek_n(4) == -1
	//
	assert s.next() == `a`
	assert s.next() == `b`
	assert s.next() == `c`
	assert s.next() == -1
}

fn test_back() {
	mut s := textscanner.new('abc')
	assert s.next() == `a`
	s.back()
	assert s.next() == `a`
	assert s.next() == `b`
	s.back()
	assert s.next() == `b`
	assert s.next() == `c`
	assert s.next() == -1
}

fn test_back_n() {
	mut s := textscanner.new('abc')
	assert s.next() == `a`
	s.back_n(10)
	assert s.next() == `a`
	assert s.next() == `b`
	assert s.next() == `c`
	s.back_n(2)
	assert s.next() == `b`
}

fn test_peek_back() {
	mut s := textscanner.new('abc')
	assert s.next() == `a`
	assert s.next() == `b`
	// check that calling .peek_back() multiple times
	// does not change the state:
	assert s.peek_back() == `a`
	assert s.peek_back() == `a`
	assert s.peek_back() == `a`
	// advance, then peek_back again:
	assert s.next() == `c`
	assert s.peek_back() == `b`
	// peeking before the start:
	s.reset()
	assert s.peek_back() == -1
	// peeking right at the end:
	s.goto_end()
	assert s.peek_back() == `b`
}

fn test_peek_back_n() {
	mut s := textscanner.new('abc')
	s.goto_end()
	assert s.peek_back_n(0) == `c`
	assert s.peek_back_n(1) == `b`
	assert s.peek_back_n(2) == `a`
	assert s.peek_back_n(3) == -1
	assert s.peek_back_n(4) == -1
}

fn test_reset() {
	mut s := textscanner.new('abc')
	assert s.next() == `a`
	s.next()
	s.next()
	assert s.next() == -1
	s.reset()
	assert s.next() == `a`
}

fn test_current() {
	mut s := textscanner.new('abc')
	assert s.current() == -1
	assert s.next() == `a`
	assert s.current() == `a`
	assert s.current() == `a`
	assert s.peek_back() == -1
	assert s.next() == `b`
	assert s.current() == `b`
	assert s.current() == `b`
	assert s.peek_back() == `a`
	assert s.next() == `c`
	assert s.current() == `c`
	assert s.next() == -1
	assert s.current() == `c`
	assert s.next() == -1
	assert s.current() == `c`
	s.reset()
	assert s.current() == -1
	assert s.next() == `a`
	assert s.current() == `a`
}

fn test_goto_end() {
	mut s := textscanner.new('abc')
	s.goto_end()
	assert s.current() == `c`
}