2017-02-06 07:45:31 +00:00
// Copyright (c) Liam Stanley <me@liamstanley.io>. All rights reserved. Use
// of this source code is governed by the MIT license that can be found in
// the LICENSE file.
2016-11-22 04:52:18 +00:00
2016-11-18 20:17:09 +00:00
package girc
2016-11-22 04:35:51 +00:00
import (
"reflect"
"testing"
)
2016-11-18 20:17:09 +00:00
2017-02-22 05:25:04 +00:00
func mockEvent ( ) * Event {
return & Event {
Source : & Source { Name : "nick" , Ident : "user" , Host : "host.com" } ,
Command : "PRIVMSG" ,
Params : [ ] string { "#channel" } ,
Trailing : "1 2 3" ,
}
}
2016-11-22 04:35:51 +00:00
func TestParseSource ( t * testing . T ) {
2016-11-18 20:17:09 +00:00
type args struct {
2016-11-22 04:35:51 +00:00
raw string
2016-11-18 20:17:09 +00:00
}
tests := [ ] struct {
2016-11-22 04:35:51 +00:00
name string
args args
wantSrc * Source
2016-11-18 20:17:09 +00:00
} {
2016-11-22 04:35:51 +00:00
{ name : "full" , args : args { raw : "nick!user@hostname.com" } , wantSrc : & Source {
2016-12-09 11:34:38 +00:00
Name : "nick" , Ident : "user" , Host : "hostname.com" ,
2016-11-22 04:35:51 +00:00
} } ,
{ name : "special chars" , args : args { raw : "^[]nick!~user@test.host---name.com" } , wantSrc : & Source {
2016-12-09 11:34:38 +00:00
Name : "^[]nick" , Ident : "~user" , Host : "test.host---name.com" ,
2016-11-22 04:35:51 +00:00
} } ,
{ name : "short" , args : args { raw : "a!b@c" } , wantSrc : & Source {
2016-12-09 11:34:38 +00:00
Name : "a" , Ident : "b" , Host : "c" ,
2016-11-22 04:35:51 +00:00
} } ,
{ name : "short" , args : args { raw : "a!b" } , wantSrc : & Source {
2016-12-09 11:34:38 +00:00
Name : "a" , Ident : "b" , Host : "" ,
2016-11-22 04:35:51 +00:00
} } ,
{ name : "short" , args : args { raw : "a@b" } , wantSrc : & Source {
2016-12-09 11:34:38 +00:00
Name : "a" , Ident : "" , Host : "b" ,
2016-11-22 04:35:51 +00:00
} } ,
{ name : "short" , args : args { raw : "test" } , wantSrc : & Source {
2016-12-09 11:34:38 +00:00
Name : "test" , Ident : "" , Host : "" ,
2016-11-22 04:35:51 +00:00
} } ,
2016-11-18 20:17:09 +00:00
}
for _ , tt := range tests {
2016-11-22 04:35:51 +00:00
gotSrc := ParseSource ( tt . args . raw )
if ! reflect . DeepEqual ( gotSrc , tt . wantSrc ) {
t . Errorf ( "ParseSource() = %v, want %v" , gotSrc , tt . wantSrc )
2016-11-18 20:17:09 +00:00
}
2016-11-18 22:10:58 +00:00
2016-11-22 04:35:51 +00:00
if gotSrc . Len ( ) != tt . wantSrc . Len ( ) {
t . Errorf ( "ParseSource().Len() = %v, want %v" , gotSrc . Len ( ) , tt . wantSrc . Len ( ) )
}
if gotSrc . String ( ) != tt . wantSrc . String ( ) {
t . Errorf ( "ParseSource().String() = %v, want %v" , gotSrc . String ( ) , tt . wantSrc . String ( ) )
}
if gotSrc . IsServer ( ) != tt . wantSrc . IsServer ( ) {
t . Errorf ( "ParseSource().IsServer() = %v, want %v" , gotSrc . IsServer ( ) , tt . wantSrc . IsServer ( ) )
}
if gotSrc . IsHostmask ( ) != tt . wantSrc . IsHostmask ( ) {
t . Errorf ( "ParseSource().IsHostmask() = %v, want %v" , gotSrc . IsHostmask ( ) , tt . wantSrc . IsHostmask ( ) )
}
if ! reflect . DeepEqual ( gotSrc . Bytes ( ) , tt . wantSrc . Bytes ( ) ) {
t . Errorf ( "ParseSource().Bytes() = %v, want %v" , gotSrc , tt . wantSrc )
2016-11-18 22:10:58 +00:00
}
}
}
2017-04-18 18:45:52 +00:00
func TestParseEvent ( t * testing . T ) {
tests := [ ] struct {
in string
want string
} {
{ in : "" , want : "" } ,
{ in : ":host.domain.com TEST" , want : ":host.domain.com TEST" } ,
{ in : ":host.domain.com TEST\r\n" , want : ":host.domain.com TEST" } ,
{ in : ":host.domain.com TEST arg1 arg2" , want : ":host.domain.com TEST arg1 arg2" } ,
{ in : ":host.domain.com TEST :" , want : ":host.domain.com TEST :" } ,
{ in : ":host.domain.com TEST :test1" , want : ":host.domain.com TEST :test1" } ,
{ in : ":host.domain.com TEST arg1 arg2 :test1" , want : ":host.domain.com TEST arg1 arg2 :test1" } ,
2017-06-12 11:29:10 +00:00
{ in : ":host.domain.com TEST arg1 arg=:10 :test1" , want : ":host.domain.com TEST arg1 arg=:10 :test1" } ,
2017-04-18 18:45:52 +00:00
{ in : ":nick!user@host TEST :test1" , want : ":nick!user@host TEST :test1" } ,
{ in : ":nick!user@host TEST :test0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000LONG TEXT TRUNCATED HERE" , want : ":nick!user@host TEST :test0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" } ,
{ in : "@aaa=bbb;ccc;example.com/ddd=eee :nick!user@host TEST :test0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000LONG TEXT TRUNCATED HERE" , want : "@aaa=bbb;ccc;example.com/ddd=eee :nick!user@host TEST :test0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" } ,
2017-06-12 22:49:54 +00:00
{ in : "@aaa=bbb :nick!user@host TEST :test1" , want : "@aaa=bbb :nick!user@host TEST :test1" } ,
{ in : "@aaa=bbb;+ccc;example.com/ddd=eee :nick!user@host TEST :test1" , want : "@aaa=bbb;+ccc;example.com/ddd=eee :nick!user@host TEST :test1" } ,
{ in : "@bbb=aaa;aaa :nick!user@host TEST :test1" , want : "@aaa;bbb=aaa :nick!user@host TEST :test1" } ,
2017-04-18 18:45:52 +00:00
}
for _ , tt := range tests {
got := ParseEvent ( tt . in )
if got == nil && tt . want == "" {
continue
}
if got == nil {
t . Errorf ( "ParseEvent: got nil, want: %s" , tt . want )
}
if got . String ( ) != tt . want {
if got . Tags != nil {
if len ( got . String ( ) ) != len ( tt . want ) {
t . Fatalf ( "ParseEvent: length exception in tag parse: got %q, want %q" , got . String ( ) , tt . want )
}
} else {
t . Fatalf ( "ParseEvent: got %q, want %q" , got . String ( ) , tt . want )
}
}
if got . Len ( ) != len ( tt . want ) {
if got . Len ( ) > 510 {
continue
}
t . Fatalf ( "Event.Len: got %d from %q, want %d" , got . Len ( ) , got . String ( ) , len ( tt . want ) )
}
}
}
func TestEventCopy ( t * testing . T ) {
var nilEvent * Event
if event := nilEvent . Copy ( ) ; event != nil {
t . Fatalf ( "Event.Copy: returned non-nil on nil event: %#v" , event )
}
msg := "@aaa=bbb;ccc;example.com/ddd=eee :nick!user@host TEST arg1 arg2 :test1"
event := ParseEvent ( msg )
eventCopy := event . Copy ( )
if ! reflect . DeepEqual ( event , eventCopy ) {
t . Fatalf ( "Event.Copy: want %#v, got %#v" , event , eventCopy )
}
// Since Event.Copy() calls Source.Copy()...
if ! reflect . DeepEqual ( event . Source , eventCopy . Source ) {
t . Fatalf ( "Source.Copy: want %#v, got %#v" , event . Source , eventCopy . Source )
}
event . Source = nil
if src := event . Source . Copy ( ) ; src != nil {
t . Fatalf ( "Source.Copy: returned non-nil on nil source: %#v" , src )
}
}
func TestEventIs ( t * testing . T ) {
event := ParseEvent ( ":nick!user@host PRIVMSG #test :\x01ACTION this is a test\x01" )
if ! event . IsAction ( ) {
t . Fatalf ( "Event.IsAction: returned false on %#v" , event )
}
event . Command = "TEST"
if event . IsAction ( ) {
t . Fatalf ( "Event.IsAction: returned true though not privmsg; %#v" , event )
}
event . Command = "PRIVMSG"
event . Trailing = event . StripAction ( )
if event . IsAction ( ) || event . Trailing != "this is a test" {
t . Fatalf ( "Event.IsAction: returned true on %#v" , event )
}
if ! event . IsFromChannel ( ) {
t . Fatalf ( "Event.IsFromChannel: returned false on %#v" , event )
}
event . Command = "TEST"
if event . IsFromChannel ( ) {
t . Fatalf ( "Event.IsFromChannel: returned true though not privmsg; %#v" , event )
}
event . Params [ 0 ] = "user1"
if event . IsFromUser ( ) {
t . Fatalf ( "Event.IsFromUser: returned true when not privmsg; %#v" , event )
}
event . Command = "PRIVMSG"
if ! event . IsFromUser ( ) {
t . Fatalf ( "Event.IsFromUser: returned false on %#v" , event )
}
}
2018-01-27 12:00:17 +00:00
func TestEventSourceTagEquals ( t * testing . T ) {
// This should test events themselves, as well as tags and sources.
cases := [ ] struct {
before , after string
equals bool
} {
{
before : ":nick!user@host PRIVMSG #test :This is a test" ,
after : ":nick!user@host PRIVMSG #test :This is a test" ,
equals : true ,
} ,
{
before : ":nick!user@host PRIVMSG #test :This is a test" ,
after : ":nick!user@host1 PRIVMSG #test :This is a test" ,
equals : false ,
} ,
{
before : ":nick!user@host PRIVMSG #test :This is a test" ,
after : ":nick!user@host PRIVMSG #tes :This is a test" ,
equals : false ,
} ,
{
before : "@aaa=bbb;ccc;example.com/ddd=eee :nick!user@host PRIVMSG #test :This is a test" ,
after : "@aaa=bbb;ccc;example.com/ddd=eee :nick!user@host PRIVMSG #test :This is a test" ,
equals : true ,
} ,
{
before : "@aaa=bbb;ccc;example.com/ddd=eee :nick!user@host PRIVMSG #test :This is a test" ,
after : "@aaa=bbb;ccc :nick!user@host PRIVMSG #test :This is a test" ,
2018-01-28 03:44:44 +00:00
equals : true ,
2018-01-27 12:00:17 +00:00
} ,
{
before : ":nick!user@host PRIVMSG #test :This is a test" ,
after : "@aaa=bbb;ccc :nick!user@host PRIVMSG #test :This is a test" ,
2018-01-28 03:44:44 +00:00
equals : true ,
} ,
{
before : "@account=bbb;ccc :nick!user@host PRIVMSG #test :This is a test" ,
after : "@aaa=bbb;ccc :nick!user@host PRIVMSG #test :This is a test" ,
2018-01-27 12:00:17 +00:00
equals : false ,
} ,
}
for _ , tt := range cases {
before := ParseEvent ( tt . before )
after := ParseEvent ( tt . after )
equals := before . Equals ( after )
// It should be equal the opposite direction too.
if op := after . Equals ( before ) ; equals != op {
t . Fatalf ( "Event.Equals reverse order doesn't match forward order. before: %#v, after: %#v" , before , after )
}
if equals != tt . equals {
t . Fatalf ( "Event.Equals: returned %t (wanted %t) on copied event. before: %#v, after: %#v" , equals , tt . equals , before , after )
}
}
}