diff --git a/pkg/irc/numerics_test.go b/pkg/irc/numerics_test.go new file mode 100644 index 0000000..8374db3 --- /dev/null +++ b/pkg/irc/numerics_test.go @@ -0,0 +1,163 @@ +package irc_test + +import ( + "errors" + "testing" + + "git.eeqj.de/sneak/neoirc/pkg/irc" +) + +func TestName(t *testing.T) { + t.Parallel() + + tests := []struct { + numeric irc.IRCMessageType + want string + }{ + {irc.RplWelcome, "RPL_WELCOME"}, + {irc.RplBounce, "RPL_BOUNCE"}, + {irc.RplLuserOp, "RPL_LUSEROP"}, + {irc.ErrCannotSendToChan, "ERR_CANNOTSENDTOCHAN"}, + {irc.ErrNicknameInUse, "ERR_NICKNAMEINUSE"}, + } + + for _, tc := range tests { + if got := tc.numeric.Name(); got != tc.want { + t.Errorf("IRCMessageType(%d).Name() = %q, want %q", tc.numeric.Int(), got, tc.want) + } + } +} + +func TestString(t *testing.T) { + t.Parallel() + + tests := []struct { + numeric irc.IRCMessageType + want string + }{ + {irc.RplWelcome, "RPL_WELCOME <001>"}, + {irc.RplBounce, "RPL_BOUNCE <005>"}, + {irc.RplLuserOp, "RPL_LUSEROP <252>"}, + {irc.ErrCannotSendToChan, "ERR_CANNOTSENDTOCHAN <404>"}, + } + + for _, tc := range tests { + if got := tc.numeric.String(); got != tc.want { + t.Errorf("IRCMessageType(%d).String() = %q, want %q", tc.numeric.Int(), got, tc.want) + } + } +} + +func TestCode(t *testing.T) { + t.Parallel() + + tests := []struct { + numeric irc.IRCMessageType + want string + }{ + {irc.RplWelcome, "001"}, + {irc.RplBounce, "005"}, + {irc.RplLuserOp, "252"}, + {irc.ErrCannotSendToChan, "404"}, + } + + for _, tc := range tests { + if got := tc.numeric.Code(); got != tc.want { + t.Errorf("IRCMessageType(%d).Code() = %q, want %q", tc.numeric.Int(), got, tc.want) + } + } +} + +func TestInt(t *testing.T) { + t.Parallel() + + tests := []struct { + numeric irc.IRCMessageType + want int + }{ + {irc.RplWelcome, 1}, + {irc.RplBounce, 5}, + {irc.RplLuserOp, 252}, + {irc.ErrCannotSendToChan, 404}, + } + + for _, tc := range tests { + if got := tc.numeric.Int(); got != tc.want { + t.Errorf("IRCMessageType(%d).Int() = %d, want %d", tc.want, got, tc.want) + } + } +} + +func TestFromInt_Known(t *testing.T) { + t.Parallel() + + tests := []struct { + code int + want irc.IRCMessageType + }{ + {1, irc.RplWelcome}, + {5, irc.RplBounce}, + {252, irc.RplLuserOp}, + {404, irc.ErrCannotSendToChan}, + {433, irc.ErrNicknameInUse}, + } + + for _, test := range tests { + got, err := irc.FromInt(test.code) + if err != nil { + t.Errorf("FromInt(%d) returned unexpected error: %v", test.code, err) + + continue + } + + if got != test.want { + t.Errorf("FromInt(%d) = %v, want %v", test.code, got, test.want) + } + } +} + +func TestFromInt_Unknown(t *testing.T) { + t.Parallel() + + unknowns := []int{0, 999, 600, -1} + for _, code := range unknowns { + _, err := irc.FromInt(code) + if err == nil { + t.Errorf("FromInt(%d) expected error, got nil", code) + + continue + } + + if !errors.Is(err, irc.ErrUnknownNumeric) { + t.Errorf("FromInt(%d) error = %v, want ErrUnknownNumeric", code, err) + } + } +} + +func TestUnknownNumeric_Name(t *testing.T) { + t.Parallel() + + unknown := irc.IRCMessageType(999) + if got := unknown.Name(); got != "" { + t.Errorf("IRCMessageType(999).Name() = %q, want empty string", got) + } +} + +func TestUnknownNumeric_String(t *testing.T) { + t.Parallel() + + unknown := irc.IRCMessageType(999) + want := "UNKNOWN <999>" + + if got := unknown.String(); got != want { + t.Errorf("IRCMessageType(999).String() = %q, want %q", got, want) + } +} + +func TestDeprecatedNameFunc(t *testing.T) { + t.Parallel() + + if got := irc.Name(irc.RplYourHost); got != "RPL_YOURHOST" { + t.Errorf("Name(RplYourHost) = %q, want %q", got, "RPL_YOURHOST") + } +}