Repository navigation
Expand file tree
/
Copy pathbufio.echo
More file actions
97 lines (89 loc) · 1.72 KB
/
Copy pathbufio.echo
File metadata and controls
97 lines (89 loc) · 1.72 KB
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
; std/bufio — line splitting helpers (thin; pure Echo over strings).
;
; Public API:
; lines(s) — list of lines (split on \n)
; read_lines(path) — result of lines from whole-file read, or ! io err
;
; Suite: xo test std/bufio.echo
/ std/str
/ std/fs
/ std/list
/ std/test
$ lines = (s) {
^ str.split(s, "\n")
}
$ read_lines = (path) {
| fs.read(path) {
$ b {
$ t = str.from_bytes(b)
^ lines(t)
}
! e {
! e
}
}
}
test.it("lines_basic", () {
$ ls = lines("a\nb\nc")
test.eq(list.len(ls), 3)
| list.get(ls, 0) {
$ a {
test.eq(a, "a")
}
! e {
test.fail(e)
}
}
| list.get(ls, 1) {
$ b {
test.eq(b, "b")
}
! e {
test.fail(e)
}
}
})
test.it("lines_len", () {
$ ls = lines("x\ny")
test.eq(list.len(ls), 2)
})
test.it("read_lines_missing", () {
| read_lines("/tmp/echo_bufio_absent_zzz") {
$ ls {
test.fail("expected err")
}
! e {
test.eq(e, "read failed")
}
}
})
test.it("read_lines_file", () {
$ p = fs.join(fs.temp_dir(), "echo_bufio_suite.txt")
| fs.write(p, "a\nb") {
$ ok {}
! e {
test.fail(e)
}
}
| read_lines(p) {
$ ls {
test.eq(list.len(ls), 2)
| list.get(ls, 0) {
$ a {
test.eq(a, "a")
}
! e {
test.fail(e)
}
}
}
! e {
test.fail(e)
}
}
| fs.remove(p) {
$ ok {}
! e {}
}
})
\ lines, read_lines