Skip to content

Commit 150e065

Browse files
committed
Add full test coverage for the sexp package. Also uncovered a nasty
issue with peek and io.EOF.
1 parent 9e7052a commit 150e065

8 files changed

+167
-1
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ _testmain.go
2222
*.exe
2323
*.test
2424
*.prof
25+
26+
coverage.out

Diff for: sexp/bignum_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package sexp
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"math/big"
7+
"testing"
8+
)
9+
10+
func Test_BigNum_First_generatesAPanic(t *testing.T) {
11+
defer checkForPanic(t, "not valid to call First on a BigNum")
12+
NewBigNum("ABCD").First()
13+
}
14+
15+
func Test_BigNum_Second_generatesAPanic(t *testing.T) {
16+
defer checkForPanic(t, "not valid to call Second on a BigNum")
17+
NewBigNum("ABCD").Second()
18+
}
19+
20+
func Test_BigNum_String_generatesAValidRepresentation(t *testing.T) {
21+
res := NewBigNum("ABCD").String()
22+
assertEquals(t, res, "#ABCD#")
23+
}
24+
25+
func Test_BigNum_Value_returnsTheBigNumInside(t *testing.T) {
26+
res := NewBigNum("ABCD").Value().(*big.Int)
27+
assertDeepEquals(t, res, new(big.Int).SetBytes([]byte{0xAB, 0xCD}))
28+
}
29+
30+
func Test_ReadBigNum_returnsNilIfAskedToReadNonBigNum(t *testing.T) {
31+
res := ReadBigNum(bufio.NewReader(bytes.NewReader([]byte(""))))
32+
assertEquals(t, res, nil)
33+
}
34+
35+
func Test_ReadBigNum_returnsNilIfAskedToReadBigNumThatDoesntEndCorrectly(t *testing.T) {
36+
res := ReadBigNum(bufio.NewReader(bytes.NewReader([]byte("#ABCD"))))
37+
assertEquals(t, res, nil)
38+
}

Diff for: sexp/cons_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package sexp
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"testing"
7+
)
8+
9+
func Test_Cons_First_ReturnsTheFirstElement(t *testing.T) {
10+
c := Cons{Sstring("First"), Sstring("Second")}
11+
assertEquals(t, c.First(), Sstring("First"))
12+
}
13+
14+
func Test_Cons_Second_ReturnsTheSecondElement(t *testing.T) {
15+
c := Cons{Sstring("First"), Sstring("Second")}
16+
assertEquals(t, c.Second(), Sstring("Second"))
17+
}
18+
19+
func Test_Cons_Value_ReturnsTheFirstElement(t *testing.T) {
20+
c := Cons{Sstring("First"), Sstring("Second")}
21+
assertEquals(t, c.Value(), Sstring("First"))
22+
}
23+
24+
func Test_Cons_String_ReturnsTheConsFormatted(t *testing.T) {
25+
c := Cons{Sstring("First"), Sstring("Second")}
26+
assertEquals(t, c.String(), "(\"First\" . \"Second\")")
27+
}
28+
29+
func Test_ReadList_returnsNilIfAskedToReadNonList(t *testing.T) {
30+
res := ReadList(bufio.NewReader(bytes.NewReader([]byte("a"))))
31+
assertEquals(t, res, nil)
32+
}
33+
34+
func Test_ReadList_returnsNilIfAskedToReadListThatDoesntEndWell(t *testing.T) {
35+
res := ReadList(bufio.NewReader(bytes.NewReader([]byte("("))))
36+
assertEquals(t, res, nil)
37+
}

Diff for: sexp/test_helpers.go renamed to sexp/helpers_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,11 @@ func assertDeepEquals(t *testing.T, actual, expected interface{}) {
1616
t.Errorf("Expected %v to equal %v", actual, expected)
1717
}
1818
}
19+
20+
func checkForPanic(t *testing.T, s string) {
21+
if r := recover(); r != nil {
22+
assertEquals(t, r, s)
23+
} else {
24+
t.Errorf("Expected panic with message %v to be invoked", s)
25+
}
26+
}

Diff for: sexp/sexp.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ type Value interface {
1515

1616
func peek(r *bufio.Reader) (c byte, e error) {
1717
c, e = r.ReadByte()
18-
r.UnreadByte()
18+
if e != io.EOF {
19+
r.UnreadByte()
20+
}
1921
return
2022
}
2123

Diff for: sexp/snil_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package sexp
2+
3+
import "testing"
4+
5+
func Test_Snil_First_ReturnsItself(t *testing.T) {
6+
assertEquals(t, Snil{}, Snil{}.First())
7+
}
8+
9+
func Test_Snil_Second_ReturnsItself(t *testing.T) {
10+
assertEquals(t, Snil{}, Snil{}.Second())
11+
}
12+
13+
func Test_Snil_Value_ReturnsNil(t *testing.T) {
14+
assertEquals(t, nil, Snil{}.Value())
15+
}
16+
17+
func Test_Snil_String_ReturnsAStringRepresentation(t *testing.T) {
18+
assertEquals(t, "()", Snil{}.String())
19+
}

Diff for: sexp/str_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package sexp
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"testing"
7+
)
8+
9+
func Test_Sstring_First_generatesAPanic(t *testing.T) {
10+
defer checkForPanic(t, "not valid to call First on an SString")
11+
Sstring("ABCD").First()
12+
}
13+
14+
func Test_Sstring_Second_generatesAPanic(t *testing.T) {
15+
defer checkForPanic(t, "not valid to call Second on an SString")
16+
Sstring("ABCD").Second()
17+
}
18+
19+
func Test_Sstring_Value_returnsTheStringRepresentation(t *testing.T) {
20+
res := Sstring("ABB").Value()
21+
assertDeepEquals(t, res, "ABB")
22+
}
23+
24+
func Test_Sstring_String_returnsTheStringRepresentation(t *testing.T) {
25+
res := Sstring("ABB").String()
26+
assertDeepEquals(t, res, "\"ABB\"")
27+
}
28+
29+
func Test_ReadString_returnsNilIfAskedToReadNonString(t *testing.T) {
30+
res := ReadString(bufio.NewReader(bytes.NewReader([]byte("a"))))
31+
assertEquals(t, res, nil)
32+
}
33+
34+
func Test_ReadString_returnsNilIfAskedToReadNonFinishedString(t *testing.T) {
35+
res := ReadString(bufio.NewReader(bytes.NewReader([]byte("\"a"))))
36+
assertEquals(t, res, nil)
37+
}

Diff for: sexp/symbol_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package sexp
2+
3+
import "testing"
4+
5+
func Test_Symbol_First_generatesAPanic(t *testing.T) {
6+
defer checkForPanic(t, "not valid to call First on a Symbol")
7+
Symbol("ABCD").First()
8+
}
9+
10+
func Test_Symbol_Second_generatesAPanic(t *testing.T) {
11+
defer checkForPanic(t, "not valid to call Second on a Symbol")
12+
Symbol("ABCD").Second()
13+
}
14+
15+
func Test_Symbol_Value_returnsTheStringRepresentation(t *testing.T) {
16+
res := Symbol("ABB").Value()
17+
assertDeepEquals(t, res, "ABB")
18+
}
19+
20+
func Test_Symbol_String_returnsTheStringRepresentation(t *testing.T) {
21+
res := Symbol("ABB").String()
22+
assertDeepEquals(t, res, "ABB")
23+
}

0 commit comments

Comments
 (0)