Skip to content

Commit e19d606

Browse files
committed
homogeneous test with gocheck
1 parent 48da7da commit e19d606

8 files changed

+143
-29
lines changed

format/format.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package format
2+
3+
import (
4+
"bufio"
5+
6+
"github.com/jeromer/syslogparser"
7+
)
8+
9+
type Format interface {
10+
GetParser([]byte) syslogparser.LogParser
11+
GetSplitFunc() bufio.SplitFunc
12+
}

format/format_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package format
2+
3+
import (
4+
"testing"
5+
6+
. "launchpad.net/gocheck"
7+
)
8+
9+
func Test(t *testing.T) { TestingT(t) }
10+
11+
type FormatSuite struct{}
12+
13+
var _ = Suite(&FormatSuite{})

format/rfc3164.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package format
2+
3+
import (
4+
"bufio"
5+
6+
"github.com/jeromer/syslogparser"
7+
"github.com/jeromer/syslogparser/rfc3164"
8+
)
9+
10+
type RFC3164 struct{}
11+
12+
func (f *RFC3164) GetParser(line []byte) syslogparser.LogParser {
13+
return rfc3164.NewParser(line)
14+
}
15+
16+
func (f *RFC3164) GetSplitFunc() bufio.SplitFunc {
17+
return nil
18+
}

format/rfc3164_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package format
2+
3+
import (
4+
. "launchpad.net/gocheck"
5+
)
6+
7+
func (s *FormatSuite) TestRFC3164_SingleSplit(c *C) {
8+
f := RFC3164{}
9+
c.Assert(f.GetSplitFunc(), IsNil)
10+
}

format/rfc5424.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package format
2+
3+
import (
4+
"bufio"
5+
6+
"github.com/jeromer/syslogparser"
7+
"github.com/jeromer/syslogparser/rfc5424"
8+
)
9+
10+
type RFC5424 struct{}
11+
12+
func (f *RFC5424) GetParser(line []byte) syslogparser.LogParser {
13+
return rfc5424.NewParser(line)
14+
}
15+
16+
func (f *RFC5424) GetSplitFunc() bufio.SplitFunc {
17+
return nil
18+
}

format/rfc5424_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package format
2+
3+
import (
4+
. "launchpad.net/gocheck"
5+
)
6+
7+
func (s *FormatSuite) TestRFC5424_SingleSplit(c *C) {
8+
f := RFC5424{}
9+
c.Assert(f.GetSplitFunc(), IsNil)
10+
}

format/rfc6587.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package format
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"strconv"
7+
8+
"github.com/jeromer/syslogparser"
9+
"github.com/jeromer/syslogparser/rfc5424"
10+
)
11+
12+
type RFC6587 struct{}
13+
14+
func (f *RFC6587) GetParser(line []byte) syslogparser.LogParser {
15+
return rfc5424.NewParser(line)
16+
}
17+
18+
func (f *RFC6587) GetSplitFunc() bufio.SplitFunc {
19+
return rfc6587ScannerSplit
20+
}
21+
22+
func rfc6587ScannerSplit(data []byte, atEOF bool) (advance int, token []byte, err error) {
23+
if atEOF && len(data) == 0 {
24+
return 0, nil, nil
25+
}
26+
27+
if i := bytes.IndexByte(data, ' '); i > 0 {
28+
pLength := data[0:i]
29+
length, err := strconv.Atoi(string(pLength))
30+
if err != nil {
31+
return 0, nil, err
32+
}
33+
end := length + i + 1
34+
if len(data) >= end {
35+
//Return the frame with the length removed
36+
return end, data[i+1 : end], nil
37+
}
38+
}
39+
40+
// Request more data
41+
return 0, nil, nil
42+
}

format/rfc6587_test.go

+20-29
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,23 @@ import (
55
"bytes"
66
"fmt"
77
"strings"
8-
"testing"
8+
9+
. "launchpad.net/gocheck"
910
)
1011

11-
func TestRFC6587_GetSplitFuncSingleSplit(t *testing.T) {
12+
func (s *FormatSuite) TestRFC6587_GetSplitFuncSingleSplit(c *C) {
1213
f := RFC6587{}
1314

14-
find := "I am test."
15-
buf := strings.NewReader("10 " + find)
15+
buf := strings.NewReader("10 I am test.")
1616
scanner := bufio.NewScanner(buf)
1717
scanner.Split(f.GetSplitFunc())
18-
if r := scanner.Scan(); !r {
19-
t.Error("Expected Scan() to return true, but didn't")
20-
}
21-
if found := scanner.Text(); found != find {
22-
t.Errorf("Expected the right ('%s') token, but got: '%s'\n", find, found)
23-
}
18+
19+
r := scanner.Scan()
20+
c.Assert(r, NotNil)
21+
c.Assert(scanner.Text(), Equals, "I am test.")
2422
}
2523

26-
func TestRFC6587_GetSplitFuncMultiSplit(t *testing.T) {
24+
func (s *FormatSuite) TestRFC6587_GetSplitFuncMultiSplit(c *C) {
2725
f := RFC6587{}
2826

2927
find := []string{
@@ -40,35 +38,28 @@ func TestRFC6587_GetSplitFuncMultiSplit(t *testing.T) {
4038

4139
i := 0
4240
for scanner.Scan() {
41+
c.Assert(scanner.Text(), Equals, find[i])
4342
i++
4443
}
4544

46-
if i != len(find) {
47-
t.Errorf("Expected to find %d items, but found: %d\n", len(find), i)
48-
}
45+
c.Assert(i, Equals, len(find))
4946
}
5047

51-
func TestRFC6587_GetSplitBadSplit(t *testing.T) {
48+
func (s *FormatSuite) TestRFC6587_GetSplitBadSplit(c *C) {
5249
f := RFC6587{}
5350

5451
find := "I am test.2 ab"
5552
buf := strings.NewReader("9 " + find)
5653
scanner := bufio.NewScanner(buf)
5754
scanner.Split(f.GetSplitFunc())
5855

59-
if r := scanner.Scan(); !r {
60-
t.Error("Expected Scan() to return true, but didn't")
61-
}
62-
if found := scanner.Text(); found != find[0:9] {
63-
t.Errorf("Expected to find %s, but found %s.", find[0:9], found)
64-
}
65-
if r := scanner.Scan(); r {
66-
t.Error("Expected Scan() to return false, but didn't")
67-
}
68-
if err := scanner.Err(); err == nil {
69-
t.Error("Expected an error, but didn't get one")
70-
} else {
71-
t.Log("Error was: ", err)
72-
}
56+
r := scanner.Scan()
57+
c.Assert(r, NotNil)
58+
c.Assert(scanner.Text(), Equals, find[0:9])
59+
60+
r = scanner.Scan()
61+
c.Assert(r, NotNil)
7362

63+
err := scanner.Err()
64+
c.Assert(err, ErrorMatches, "strconv.ParseInt: parsing \".2\": invalid syntax")
7465
}

0 commit comments

Comments
 (0)