package bot import "testing" func TestParseCommand(t *testing.T) { cases := []struct { text string ok bool name string target string argument string }{ {text: "/cep 01001000", ok: true, name: "cep", argument: "01001000"}, {text: "/cep@cepisbot 01001-000", ok: true, name: "cep", target: "cepisbot", argument: "01001-000"}, {text: " /CEP@CepisBot 01001000 ", ok: true, name: "cep", target: "CepisBot", argument: "01001000"}, {text: "/start", ok: true, name: "start"}, {text: "/help@cepisbot", ok: true, name: "help", target: "cepisbot"}, {text: "01001000", ok: false}, {text: "", ok: false}, {text: "/", ok: false}, {text: "não é comando /cep", ok: false}, } for _, tc := range cases { command, ok := ParseCommand(tc.text) if ok != tc.ok { t.Fatalf("ParseCommand(%q) ok = %v, want %v", tc.text, ok, tc.ok) } if !ok { continue } if command.Name != tc.name || command.Target != tc.target || command.Argument != tc.argument { t.Fatalf("ParseCommand(%q) = %+v, want {%q %q %q}", tc.text, command, tc.name, tc.target, tc.argument) } } } func TestAddressedTo(t *testing.T) { cases := []struct { text string want bool }{ {"/cep 01001000", true}, {"/cep@cepisbot 01001000", true}, {"/cep@CEPISBOT 01001000", true}, {"/cep@outrobot 01001000", false}, } for _, tc := range cases { command, ok := ParseCommand(tc.text) if !ok { t.Fatalf("ParseCommand(%q) did not parse", tc.text) } if got := command.AddressedTo("cepisbot"); got != tc.want { t.Fatalf("AddressedTo(%q) = %v, want %v", tc.text, got, tc.want) } } }