Skip to content

Commit

Permalink
homogeneous test with gocheck
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Feb 22, 2015
1 parent 48da7da commit e19d606
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 29 deletions.
12 changes: 12 additions & 0 deletions format/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package format

import (
"bufio"

"github.com/jeromer/syslogparser"
)

type Format interface {
GetParser([]byte) syslogparser.LogParser
GetSplitFunc() bufio.SplitFunc
}
13 changes: 13 additions & 0 deletions format/format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package format

import (
"testing"

. "launchpad.net/gocheck"
)

func Test(t *testing.T) { TestingT(t) }

type FormatSuite struct{}

var _ = Suite(&FormatSuite{})
18 changes: 18 additions & 0 deletions format/rfc3164.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package format

import (
"bufio"

"github.com/jeromer/syslogparser"
"github.com/jeromer/syslogparser/rfc3164"
)

type RFC3164 struct{}

func (f *RFC3164) GetParser(line []byte) syslogparser.LogParser {
return rfc3164.NewParser(line)
}

func (f *RFC3164) GetSplitFunc() bufio.SplitFunc {
return nil
}
10 changes: 10 additions & 0 deletions format/rfc3164_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package format

import (
. "launchpad.net/gocheck"
)

func (s *FormatSuite) TestRFC3164_SingleSplit(c *C) {
f := RFC3164{}
c.Assert(f.GetSplitFunc(), IsNil)
}
18 changes: 18 additions & 0 deletions format/rfc5424.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package format

import (
"bufio"

"github.com/jeromer/syslogparser"
"github.com/jeromer/syslogparser/rfc5424"
)

type RFC5424 struct{}

func (f *RFC5424) GetParser(line []byte) syslogparser.LogParser {
return rfc5424.NewParser(line)
}

func (f *RFC5424) GetSplitFunc() bufio.SplitFunc {
return nil
}
10 changes: 10 additions & 0 deletions format/rfc5424_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package format

import (
. "launchpad.net/gocheck"
)

func (s *FormatSuite) TestRFC5424_SingleSplit(c *C) {
f := RFC5424{}
c.Assert(f.GetSplitFunc(), IsNil)
}
42 changes: 42 additions & 0 deletions format/rfc6587.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package format

import (
"bufio"
"bytes"
"strconv"

"github.com/jeromer/syslogparser"
"github.com/jeromer/syslogparser/rfc5424"
)

type RFC6587 struct{}

func (f *RFC6587) GetParser(line []byte) syslogparser.LogParser {
return rfc5424.NewParser(line)
}

func (f *RFC6587) GetSplitFunc() bufio.SplitFunc {
return rfc6587ScannerSplit
}

func rfc6587ScannerSplit(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}

if i := bytes.IndexByte(data, ' '); i > 0 {
pLength := data[0:i]
length, err := strconv.Atoi(string(pLength))
if err != nil {
return 0, nil, err
}
end := length + i + 1
if len(data) >= end {
//Return the frame with the length removed
return end, data[i+1 : end], nil
}
}

// Request more data
return 0, nil, nil
}
49 changes: 20 additions & 29 deletions format/rfc6587_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@ import (
"bytes"
"fmt"
"strings"
"testing"

. "launchpad.net/gocheck"
)

func TestRFC6587_GetSplitFuncSingleSplit(t *testing.T) {
func (s *FormatSuite) TestRFC6587_GetSplitFuncSingleSplit(c *C) {
f := RFC6587{}

find := "I am test."
buf := strings.NewReader("10 " + find)
buf := strings.NewReader("10 I am test.")
scanner := bufio.NewScanner(buf)
scanner.Split(f.GetSplitFunc())
if r := scanner.Scan(); !r {
t.Error("Expected Scan() to return true, but didn't")
}
if found := scanner.Text(); found != find {
t.Errorf("Expected the right ('%s') token, but got: '%s'\n", find, found)
}

r := scanner.Scan()
c.Assert(r, NotNil)
c.Assert(scanner.Text(), Equals, "I am test.")
}

func TestRFC6587_GetSplitFuncMultiSplit(t *testing.T) {
func (s *FormatSuite) TestRFC6587_GetSplitFuncMultiSplit(c *C) {
f := RFC6587{}

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

i := 0
for scanner.Scan() {
c.Assert(scanner.Text(), Equals, find[i])
i++
}

if i != len(find) {
t.Errorf("Expected to find %d items, but found: %d\n", len(find), i)
}
c.Assert(i, Equals, len(find))
}

func TestRFC6587_GetSplitBadSplit(t *testing.T) {
func (s *FormatSuite) TestRFC6587_GetSplitBadSplit(c *C) {
f := RFC6587{}

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

if r := scanner.Scan(); !r {
t.Error("Expected Scan() to return true, but didn't")
}
if found := scanner.Text(); found != find[0:9] {
t.Errorf("Expected to find %s, but found %s.", find[0:9], found)
}
if r := scanner.Scan(); r {
t.Error("Expected Scan() to return false, but didn't")
}
if err := scanner.Err(); err == nil {
t.Error("Expected an error, but didn't get one")
} else {
t.Log("Error was: ", err)
}
r := scanner.Scan()
c.Assert(r, NotNil)
c.Assert(scanner.Text(), Equals, find[0:9])

r = scanner.Scan()
c.Assert(r, NotNil)

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

0 comments on commit e19d606

Please sign in to comment.