Debian Codegraph @

rdfloader/xmlreader/readerUtils_test.go

raw symbols SCIP
1 package rdfloader
2
3 import (
4 "bufio"
5 "bytes"
6 "io"
7 "strings"
8 "testing"
9 )
10
11 func xmlreaderFromString(fileContent string) XMLReader {
12 return XMLReaderFromFileObject(bufio.NewReader(io.Reader(bytes.NewReader([]byte(fileContent)))))
13 }
14
15 func TestXMLReader_ignoreWhiteSpace(t *testing.T) {
16 // string starting with 4 blank characters.
17 fileContent1 := "\n \r\tsample string"
18
19 // string without any blank space at the start.
20 fileContent2 := "sample string"
21
22 // new reader with above string which starts with blank chars.
23 xmlReader1 := xmlreaderFromString(fileContent1)
24 // reader for string without any blank char at start
25 xmlReader2 := xmlreaderFromString(fileContent2)
26
27 // Test Case 1
28 nWS, err := xmlReader1.ignoreWhiteSpace()
29 if err != nil {
30 t.Error(err)
31 }
32 if nWS != 4 {
33 t.Errorf("ignored %v spaces, expected it to ignore %v spaces", nWS, 4)
34 }
35
36 // Test Case 2
37 nWS, err = xmlReader2.ignoreWhiteSpace()
38 if err != nil {
39 t.Error(err)
40 }
41 if nWS != 0 {
42 t.Errorf("ignored %v spaces, expected it to ignore %v spaces", nWS, 0)
43 }
44 }
45
46 func TestXMLReader_peekARune(t *testing.T) {
47 testCases := []string{
48 "string - alphabets",
49 "123456 - numbers",
50 "@#$%%^ - non-alphanumeric chars",
51 }
52
53 for _, testCase := range testCases {
54 xmlReader := xmlreaderFromString(testCase)
55 r, err := xmlReader.peekARune()
56 if err != nil {
57 t.Error(err)
58 } else if expected := rune(testCase[0]); r != expected {
59 t.Errorf("Expected %v, Found %v", expected, r)
60 }
61 }
62
63 // testing if the function returns an error upon eof
64 xmlReader := xmlreaderFromString("")
65 _, err := xmlReader.peekARune()
66 if err == nil {
67 t.Error("expected function to raise an error")
68 }
69 }
70
71 func TestXMLReader_peekNBytes(t *testing.T) {
72 type TestCase struct {
73 input string
74 numberChars int
75 output []byte
76 err error
77 }
78 testCases := []TestCase{
79 TestCase{"string", 3, []byte("str"), nil},
80 TestCase{"string", 6, []byte("string"), nil},
81 TestCase{"string", 0, []byte{}, nil},
82 TestCase{"string", 7, []byte("string"), io.EOF},
83 }
84 for _, testCase := range testCases {
85 xmlReader := xmlreaderFromString(testCase.input)
86 nBytes, err := xmlReader.peekNBytes(testCase.numberChars)
87 if bytes.Compare(nBytes, testCase.output) != 0 {
88 t.Errorf("Expected %v, Found %v", string(nBytes), string(testCase.output))
89 }
90 if err != testCase.err {
91 t.Errorf("expected to raise %v error", testCase.err)
92 }
93 }
94 }
95
96 func TestXMLReader_readARune(t *testing.T) {
97 type TestCase struct {
98 input string
99 numberChars int
100 output []byte
101 err error
102 }
103 testCases := []TestCase{
104 TestCase{"string", 3, []byte("str"), nil},
105 TestCase{"string", 6, []byte("string"), nil},
106 TestCase{"string", 0, []byte{}, nil},
107 TestCase{"string", 7, []byte("string"), io.EOF},
108 }
109 for _, testCase := range testCases {
110 xmlReader := xmlreaderFromString(testCase.input)
111 nBytes, err := xmlReader.peekNBytes(testCase.numberChars)
112 if bytes.Compare(nBytes, testCase.output) != 0 {
113 t.Errorf("Expected %v, Found %v", string(nBytes), string(testCase.output))
114 }
115 if err != testCase.err {
116 t.Errorf("expected to raise %v error", testCase.err)
117 }
118 }
119 }
120
121 func TestXMLReader_readNBytes(t *testing.T) {
122 xmlReader := xmlreaderFromString("string")
123 op, err := xmlReader.readNBytes(4)
124 if err != nil {
125 t.Errorf("unexpected error: %v", err)
126 }
127 if bytes.Compare(op, []byte("stri")) != 0 {
128 t.Errorf("Expected: %v, Found: %v", "stri", string(op))
129 }
130 }
131
132 func TestXMLReader_readTill(t *testing.T) {
133 xmlReader := xmlreaderFromString("hello world")
134 op, err := xmlReader.readTill(1 << ' ')
135 if string(op) != "hello" {
136 t.Errorf("Expected %v, Found: %v", "hello", string(op))
137 }
138
139 // next character must be a space.
140 // That is, delimiter mustn't be consumed while reading.
141 r, err := xmlReader.readARune()
142 if err != nil {
143 t.Error(err)
144 }
145 if r != ' ' {
146 t.Errorf("Expected %v, Found %v", ' ', r)
147 }
148 }
149
150 func TestXMLReader_readTillString(t *testing.T) {
151 // TestCase 1: searching in an empty file must raise an eof error
152 xmlReader := xmlreaderFromString("")
153 _, err := xmlReader.readTillString("any string")
154 if err == nil {
155 t.Error("expected an eof error")
156 }
157
158 // TestCase 2: searching in a file with delimiter at the end.
159 // it shouldn't raise any error.
160 delimiter := "example"
161 fileContent := delimiter
162 xmlReader = xmlreaderFromString(fileContent)
163 output, err := xmlReader.readTillString(delimiter)
164 if err != nil {
165 t.Errorf("unexpected error %v", err)
166 }
167 if len(output) > 0 {
168 t.Error("expected output to be empty as the file didn't had any chars before the delimiter")
169 }
170
171 // TestCase 3: delimiter not in the file must return an empty output with an eof error.
172 fileContent = "sample content"
173 delimiter = "delim"
174 xmlReader = xmlreaderFromString(fileContent)
175 output, err = xmlReader.readTillString(delimiter)
176 if err == nil {
177 t.Error("expected an eof error finding delimiter")
178 }
179 if len(output) > 0 {
180 t.Errorf("output should be empty when the delimiter is not found which ran into an error. found %v", output)
181 }
182
183 // TestCase 4: Valid case: delimiter present in the string.
184 delimiter = "delimiter"
185 fileContent = "some random chars" + delimiter
186 xmlReader = xmlreaderFromString(fileContent)
187 output, _ = xmlReader.readTillString(delimiter)
188 expected := strings.Split(fileContent, delimiter)[0]
189 if string(output) != expected {
190 t.Errorf("faulty parsing. expected: %s, found: %s", expected, string(output))
191 }
192 }