-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_reader.rs
72 lines (67 loc) · 1.89 KB
/
test_reader.rs
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
use text_reader::TextReader;
#[test]
fn test_reader() {
let mut reader = TextReader::new("華文\ndef");
assert_eq!(6, reader.len());
assert_eq!(Some('華'), reader.next());
assert_eq!(Some('文'), reader.next());
assert_eq!(2, reader.position());
assert_eq!(Some("華文".to_string()), reader.this_line());
assert_eq!(1, reader.line());
assert_eq!(2, reader.cursor());
assert_eq!(Some('\n'), reader.next());
assert_eq!(2, reader.line());
assert_eq!(0, reader.cursor());
assert_eq!(Some("def".to_string()), reader.this_line());
reader.back().back();
assert_eq!(Some('文'), reader.next());
assert_eq!(true, reader.has_next());
assert_eq!(Some('\n'), reader.next());
assert_eq!(Some('d'), reader.next());
assert_eq!(Some('e'), reader.next());
assert_eq!(Some('f'), reader.next());
assert_eq!(false, reader.has_next());
}
#[test]
fn test_while() {
let mut reader = TextReader::new("華文\ndef");
while reader.has_next() {
let position = reader.position();
match reader.next() {
Some(ch) => match position {
0 => assert_eq!('華', ch),
1 => assert_eq!('文', ch),
2 => assert_eq!('\n', ch),
3 => assert_eq!('d', ch),
4 => assert_eq!('e', ch),
5 => assert_eq!('f', ch),
_ => {}
},
None => panic!("None")
}
}
}
#[test]
fn test_stat() {
let mut reader = TextReader::new("abc\ndef");
println!("{:?}", reader);
reader.next();
println!("{:?}", reader);
reader.back();
println!("{:?}", reader);
let line_text = reader.this_line();
println!("{:?}", line_text);
let position = reader.position();
println!("{:?}", position);
println!("{:?}", reader);
reader.next();
reader.next();
reader.next();
let line = reader.line(); // 1
assert_eq!(1, line);
println!("{:?}", reader);
reader.next();
let line = reader.line(); // 2
assert_eq!(2, line);
println!("{:?}", reader);
}