aboutsummaryrefslogtreecommitdiffstats
path: root/formats/packp/pktline/scanner_test.go
diff options
context:
space:
mode:
authorAlberto Cortés <alcortesm@gmail.com>2016-10-19 22:08:59 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2016-10-19 22:08:59 +0200
commit8cd772a53e8ecd2687b739eea110fa9b179f1e0f (patch)
tree37911e19713b8f562c801efc172244795655cb8e /formats/packp/pktline/scanner_test.go
parent176cdac72c9c6eb8d875c90664a433d94e968438 (diff)
downloadgo-git-8cd772a53e8ecd2687b739eea110fa9b179f1e0f.tar.gz
Fix pktline api (#89)v4.0.0-rc1
* Change pktline API so you can add payloads The old pktline API only had one method: New, that receives the payloads and return the new PktLine, that was an io.Reader. This means you have to prepare the contents beforehand, in a [][]byte and then call the ctor to build the pktlines. Now, the construction of the pktlines and the method to add payloads are separated: New() // creates an empty PktLines AddFlush() Add(pp ...[]byte) AddString(pp ...string) and a PktLines has a public member R, which is the io.Reader of the pktlines added. * metalinter * change package name from pktlines to pktline * change package name from pktlines to pktline for true * make pktlines a reader instead of have a reader
Diffstat (limited to 'formats/packp/pktline/scanner_test.go')
-rw-r--r--formats/packp/pktline/scanner_test.go43
1 files changed, 23 insertions, 20 deletions
diff --git a/formats/packp/pktline/scanner_test.go b/formats/packp/pktline/scanner_test.go
index 08ca51f..de0f8df 100644
--- a/formats/packp/pktline/scanner_test.go
+++ b/formats/packp/pktline/scanner_test.go
@@ -41,9 +41,10 @@ func (s *SuiteScanner) TestEmptyReader(c *C) {
}
func (s *SuiteScanner) TestFlush(c *C) {
- r, err := pktline.NewFromStrings("")
- c.Assert(err, IsNil)
- sc := pktline.NewScanner(r)
+ p := pktline.New()
+ p.AddFlush()
+ sc := pktline.NewScanner(p)
+
c.Assert(sc.Scan(), Equals, true)
payload := sc.Bytes()
c.Assert(len(payload), Equals, 0)
@@ -69,9 +70,11 @@ func (s *SuiteScanner) TestScanAndPayload(c *C) {
strings.Repeat("a", pktline.MaxPayloadSize),
strings.Repeat("a", pktline.MaxPayloadSize-1) + "\n",
} {
- r, err := pktline.NewFromStrings(test)
- c.Assert(err, IsNil, Commentf("input len=%x, contents=%.10q\n", len(test), test))
- sc := pktline.NewScanner(r)
+ p := pktline.New()
+ err := p.AddString(test)
+ c.Assert(err, IsNil,
+ Commentf("input len=%x, contents=%.10q\n", len(test), test))
+ sc := pktline.NewScanner(p)
c.Assert(sc.Scan(), Equals, true,
Commentf("test = %.20q...", test))
@@ -91,8 +94,7 @@ func (s *SuiteScanner) TestSkip(c *C) {
input: []string{
"first",
"second",
- "third",
- ""},
+ "third"},
n: 1,
expected: []byte("second"),
},
@@ -100,15 +102,15 @@ func (s *SuiteScanner) TestSkip(c *C) {
input: []string{
"first",
"second",
- "third",
- ""},
+ "third"},
n: 2,
expected: []byte("third"),
},
} {
- r, err := pktline.NewFromStrings(test.input...)
+ p := pktline.New()
+ err := p.AddString(test.input...)
c.Assert(err, IsNil)
- sc := pktline.NewScanner(r)
+ sc := pktline.NewScanner(p)
for i := 0; i < test.n; i++ {
c.Assert(sc.Scan(), Equals, true,
Commentf("scan error = %s", sc.Err()))
@@ -123,9 +125,10 @@ func (s *SuiteScanner) TestSkip(c *C) {
}
func (s *SuiteScanner) TestEOF(c *C) {
- r, err := pktline.NewFromStrings("first", "second")
+ p := pktline.New()
+ err := p.AddString("first", "second")
c.Assert(err, IsNil)
- sc := pktline.NewScanner(r)
+ sc := pktline.NewScanner(p)
for sc.Scan() {
}
c.Assert(sc.Err(), IsNil)
@@ -161,19 +164,19 @@ func (s *SuiteScanner) TestReadSomeSections(c *C) {
// 0000
// and so on
func sectionsExample(c *C, nSections, nLines int) io.Reader {
- ss := []string{}
+ p := pktline.New()
for section := 0; section < nSections; section++ {
+ ss := []string{}
for line := 0; line < nLines; line++ {
line := fmt.Sprintf(" %d.%d\n", section, line)
ss = append(ss, line)
}
- ss = append(ss, "")
+ err := p.AddString(ss...)
+ c.Assert(err, IsNil)
+ p.AddFlush()
}
- ret, err := pktline.NewFromStrings(ss...)
- c.Assert(err, IsNil)
-
- return ret
+ return p
}
func ExampleScanner() {