Skip to content

Instantly share code, notes, and snippets.

@rfc1459
Created February 3, 2013 10:21
Show Gist options
  • Select an option

  • Save rfc1459/4701175 to your computer and use it in GitHub Desktop.

Select an option

Save rfc1459/4701175 to your computer and use it in GitHub Desktop.
gocheck sample
/*
* io/parse_test.go - Tests for Moebius parser
* Copyright (C) 2013 Matteo Panella <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
package io
import (
. "launchpad.net/gocheck"
"testing"
)
// Hook our test suite into 'go test'
func Test(t *testing.T) {
TestingT(t)
}
// Define and register our test suite
type ParserSuite struct{}
var _ = Suite(&ParserSuite{})
// Base case: well-formed packet
func (s *ParserSuite) TestWellFormedPacket(c *C) {
sourceMessage := []byte(":[email protected] PRIVMSG NickServ :identify morph trollface")
referencePacket := &Packet{
Prefix: []byte("[email protected]"),
Command: []byte("PRIVMSG"),
Args: [][]byte{
[]byte("NickServ"),
[]byte("identify morph trollface"),
},
}
packet, err := Parse(sourceMessage)
c.Assert(err, IsNil) // Packet should be parsed successfully
c.Assert(packet, DeepEquals, referencePacket) // Verify that parsed packet matches the reference packet
}
// Edge case 1 (failure): empty packet
func (p *ParserSuite) TestEmptyPacket(c *C) {
_, err := Parse([]byte(""))
c.Assert(err, NotNil) // Packet should *NOT* be parsed successfully
}
// Edge case 2 (failure): malformed packet
func (p *ParserSuite) TestMalformedPacket(c *C) {
sourceMessage := []byte(": PING")
_, err := Parse(sourceMessage)
c.Assert(err, NotNil) // Packet should *NOT* be parsed successfully
}
// Edge case 3 (success): command without prefix/arguments and surrounded with excess whitespace
func (p *ParserSuite) TestLoneCommand(c *C) {
sourceMessage := []byte(" PING ")
pkt, err := Parse(sourceMessage)
c.Assert(err, IsNil) // Packet should be parsed successfully
c.Assert(pkt.Prefix, IsNil) // Prefix must be nil
c.Assert(pkt.Command, DeepEquals, []byte("PING")) // Command should be equal to the original packet with whitespace stripped
c.Assert(pkt.Args, HasLen, 0) // Args should be nil/empty
}
// Edge case 4 (success): MAXPARA overflow
func (p *ParserSuite) TestMaxParamEdgeCase(c *C) {
sourceMessages := [][]byte{
[]byte("NICK morph 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 :17"),
[]byte("NICK morph 2 3 4 5 6 7 8 9 10 11 12 13 14 :15 16 :17"),
}
referencePacket := &Packet{
Command: []byte("NICK"),
Args: [][]byte{
[]byte("morph"),
[]byte("2"),
[]byte("3"),
[]byte("4"),
[]byte("5"),
[]byte("6"),
[]byte("7"),
[]byte("8"),
[]byte("9"),
[]byte("10"),
[]byte("11"),
[]byte("12"),
[]byte("13"),
[]byte("14"),
[]byte("15 16 :17"),
},
}
for i := 0; i < len(sourceMessages); i++ {
pkt, err := Parse(sourceMessages[i])
c.Assert(err, IsNil) // Packet should be parsed successfully
c.Assert(pkt, DeepEquals, referencePacket) // Check last param behaviour
}
}
// Benchmark Parse() against a well-formed packet
func (s *ParserSuite) BenchmarkParser(c *C) {
sourceMessage := []byte(":[email protected] PRIVMSG NickServ :identify morph trollface")
for i := 0; i < c.N; i++ {
_, _ = Parse(sourceMessage)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment