-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsourceDocument_test.go
102 lines (75 loc) · 2.45 KB
/
sourceDocument_test.go
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
package guerillaradio
import "testing"
import "math/rand"
func TestReadFile(t *testing.T) {
source := SourceDocument{FileName: "fixtures/oneline.txt"}
err := source.ReadFile()
length := len(source.Lines)
expected_length := 1
if expected_length != length {
t.Errorf("Expected length to be %v, was %v", expected_length, length)
}
expected := "first line\n"
actual := source.Lines[0]
if expected != string(actual) {
t.Errorf("expected '%v' , read '%v'", expected, actual)
}
if err != nil {
t.Errorf("ReadFile had an unexpcted err %v", err)
}
}
func TestReadFileWithTwoLines(t *testing.T) {
source := SourceDocument{FileName: "fixtures/twolines.txt"}
err := source.ReadFile()
if err != nil {
t.Errorf("ReadFile had an unexpcted err %v", err)
}
length := len(source.Lines)
expected_length := 2
if expected_length != length {
t.Errorf("Expected length to be %v, was %v", expected_length, length)
}
}
func TestNoFileToRead(t *testing.T) {
source := SourceDocument{FileName: "/noway/this/works"}
err := source.ReadFile()
if err == nil {
t.Errorf("Error not returned for fake File")
}
expected_size := 0
if expected_size != len(source.Lines) {
t.Errorf("Expected to Slice to be empty")
}
}
func TestRetrievesOnlyOneLine(t *testing.T) {
source := SourceDocument{FileName: "fixtures/oneline.txt"}
err := source.ReadFile()
if err != nil {
t.Errorf("Test could not read file unexpectedly: %v", err)
}
number_of_lines_requested := 2
lines, lines_returned := source.RetrieveLines(number_of_lines_requested)
if len(lines) != lines_returned {
t.Errorf("Number of lines returned does not match reported number of lines. %v != %v", len(lines), lines_returned)
}
if lines_returned != 1 {
t.Errorf("%v Lines returned and we expected only a single one since the file only has one")
}
}
func TestRetrieveMultipleLines(t *testing.T) {
rand.Seed(1)
source := SourceDocument{FileName: "fixtures/sixlines.txt"}
err := source.ReadFile()
if err != nil {
t.Errorf("Test could not read file unexpectedly: %v", err)
}
number_of_lines_requested := 5
lines, lines_returned := source.RetrieveLines(number_of_lines_requested)
if len(lines) != lines_returned {
t.Errorf("Number of lines returned does not match reported number of lines. %v != %v", len(lines), lines_returned)
}
expected_lines := 1
if lines_returned != expected_lines {
t.Errorf("%v Lines returned and we expected %v with the rand seed value", lines_returned, expected_lines)
}
}