Skip to content

Commit

Permalink
Merge cd31d31 into 762af81
Browse files Browse the repository at this point in the history
  • Loading branch information
belak committed Jan 10, 2017
2 parents 762af81 + cd31d31 commit 9904fb9
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "testcases"]
path = testcases
url = https://github.com/DanielOaks/irc-parser-tests/
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ As a result of this, there are multiple import locations.
tagged as stable
* `github.com/go-irc/irc` should be used to develop against the master branch

## Development

In order to run the tests, make sure all submodules are up to date. If you are
just using this library, these are not needed.

## Example

```go
Expand Down
11 changes: 11 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ func ParseMessage(line string) (*Message, error) {
c.Command = strings.ToUpper(c.Params[0])
c.Params = c.Params[1:]

// If there are no params, set it to nil, to make writing tests and other
// things simpler.
if len(c.Params) == 0 {
c.Params = nil
}

return c, nil
}

Expand Down Expand Up @@ -354,6 +360,11 @@ func (m *Message) Copy() *Message {
// Copy the Params slice
newMessage.Params = append(make([]string, 0, len(m.Params)), m.Params...)

// Similar to parsing, if Params is empty, set it to nil
if len(newMessage.Params) == 0 {
newMessage.Params = nil
}

return newMessage
}

Expand Down
157 changes: 141 additions & 16 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package irc

import (
"io/ioutil"
"strings"
"testing"

yaml "gopkg.in/yaml.v2"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var messageTests = []struct {
Expand Down Expand Up @@ -43,7 +48,6 @@ var messageTests = []struct {
{ // Basic prefix test
Prefix: "server.kevlar.net",
Cmd: "PING",
Params: []string{},

Name: "server.kevlar.net",

Expand Down Expand Up @@ -146,8 +150,7 @@ var messageTests = []struct {
"tag": "value",
},

Params: []string{},
Cmd: "A",
Cmd: "A",

Expect: "@tag=value A\n",
},
Expand All @@ -156,8 +159,7 @@ var messageTests = []struct {
"tag": "\n",
},

Params: []string{},
Cmd: "A",
Cmd: "A",

Expect: "@tag=\\n A\n",
},
Expand All @@ -166,8 +168,7 @@ var messageTests = []struct {
"tag": "\\",
},

Params: []string{},
Cmd: "A",
Cmd: "A",

Expect: "@tag=\\ A\n",
ExpectIn: []string{"@tag=\\\\ A\n"},
Expand All @@ -177,8 +178,7 @@ var messageTests = []struct {
"tag": ";",
},

Params: []string{},
Cmd: "A",
Cmd: "A",

Expect: "@tag=\\: A\n",
},
Expand All @@ -187,8 +187,7 @@ var messageTests = []struct {
"tag": "",
},

Params: []string{},
Cmd: "A",
Cmd: "A",

Expect: "@tag A\n",
},
Expand All @@ -197,8 +196,7 @@ var messageTests = []struct {
"tag": "\\&",
},

Params: []string{},
Cmd: "A",
Cmd: "A",

Expect: "@tag=\\& A\n",
ExpectIn: []string{"@tag=\\\\& A\n"},
Expand All @@ -209,8 +207,7 @@ var messageTests = []struct {
"tag2": "asd",
},

Params: []string{},
Cmd: "A",
Cmd: "A",

Expect: "@tag=x;tag2=asd A\n",
ExpectIn: []string{"@tag=x;tag2=asd A\n", "@tag2=asd;tag=x A\n"},
Expand All @@ -220,7 +217,6 @@ var messageTests = []struct {
"tag": "; \\\r\n",
},

Params: []string{},
Cmd: "A",
Expect: "@tag=\\:\\s\\\\\\r\\n A\n",
},
Expand Down Expand Up @@ -443,3 +439,132 @@ func TestMessageTags(t *testing.T) {
assert.EqualValues(t, test.Tags, m.Tags, "%d. Tags don't match", i)
}
}

type MsgSplitTests struct {
Tests []struct {
Input string
Atoms struct {
Source *string
Verb string
Params []string
Tags map[string]interface{}
}
}
}

func TestMsgSplit(t *testing.T) {
data, err := ioutil.ReadFile("./testcases/tests/msg-split.yaml")
require.NoError(t, err)

var splitTests MsgSplitTests
err = yaml.Unmarshal(data, &splitTests)
require.NoError(t, err)

for _, test := range splitTests.Tests {
msg, err := ParseMessage(test.Input)
assert.NoError(t, err, "Failed to parse: %s (%s)", test.Input, err)

assert.Equal(t,
strings.ToUpper(test.Atoms.Verb), msg.Command,
"Wrong command for input: %s", test.Input,
)
assert.Equal(t,
test.Atoms.Params, msg.Params,
"Wrong params for input: %s", test.Input,
)

if test.Atoms.Source != nil {
assert.Equal(t, *test.Atoms.Source, msg.Prefix.String())
}

assert.Equal(t,
len(test.Atoms.Tags), len(msg.Tags),
"Wrong number of tags",
)

for k, v := range test.Atoms.Tags {
if v == nil {
assert.EqualValues(t, "", msg.Tags[k], "Tag differs")
} else {
assert.EqualValues(t, v, msg.Tags[k], "Tag differs")
}
}
}
}

type MsgJoinTests struct {
Tests []struct {
Atoms struct {
Source string
Verb string
Params []string
Tags map[string]interface{}
}
Matches []string
}
}

func TestMsgJoin(t *testing.T) {
data, err := ioutil.ReadFile("./testcases/tests/msg-join.yaml")
require.NoError(t, err)

var splitTests MsgJoinTests
err = yaml.Unmarshal(data, &splitTests)
require.NoError(t, err)

for _, test := range splitTests.Tests {
msg := &Message{
Prefix: ParsePrefix(test.Atoms.Source),
Command: test.Atoms.Verb,
Params: test.Atoms.Params,
Tags: make(map[string]TagValue),
}

for k, v := range test.Atoms.Tags {
if v == nil {
msg.Tags[k] = TagValue("")
} else {
msg.Tags[k] = TagValue(v.(string))
}
}

assert.Contains(t, test.Matches, msg.String())
}
}

type UserhostSplitTests struct {
Tests []struct {
Source string
Atoms struct {
Nick string
User string
Host string
}
}
}

func TestUserhostSplit(t *testing.T) {
data, err := ioutil.ReadFile("./testcases/tests/userhost-split.yaml")
require.NoError(t, err)

var userhostTests UserhostSplitTests
err = yaml.Unmarshal(data, &userhostTests)
require.NoError(t, err)

for _, test := range userhostTests.Tests {
prefix := ParsePrefix(test.Source)

assert.Equal(t,
test.Atoms.Nick, prefix.Name,
"Name did not match for input: %q", test.Source,
)
assert.Equal(t,
test.Atoms.User, prefix.User,
"User did not match for input: %q", test.Source,
)
assert.Equal(t,
test.Atoms.Host, prefix.Host,
"Host did not match for input: %q", test.Source,
)
}
}
1 change: 1 addition & 0 deletions testcases
Submodule testcases added at e1a2ee

0 comments on commit 9904fb9

Please sign in to comment.