You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.4 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. package clients
  2. import (
  3. "fmt"
  4. "net/http"
  5. "reflect"
  6. "testing"
  7. "github.com/matrix-org/go-neb/database"
  8. "github.com/matrix-org/go-neb/types"
  9. "github.com/matrix-org/gomatrix"
  10. )
  11. var commandParseTests = []struct {
  12. body string
  13. expectArgs []string
  14. }{
  15. {"!test word", []string{"word"}},
  16. {"!test two words", []string{"two", "words"}},
  17. {`!test "words with double quotes"`, []string{"words with double quotes"}},
  18. {"!test 'words with single quotes'", []string{"words with single quotes"}},
  19. {`!test 'single quotes' "double quotes"`, []string{"single quotes", "double quotes"}},
  20. {`!test ‘smart single quotes’ “smart double quotes”`, []string{"smart single quotes", "smart double quotes"}},
  21. }
  22. type MockService struct {
  23. types.DefaultService
  24. commands []types.Command
  25. }
  26. func (s *MockService) Commands(cli *gomatrix.Client) []types.Command {
  27. return s.commands
  28. }
  29. type MockStore struct {
  30. database.NopStorage
  31. service types.Service
  32. }
  33. func (d *MockStore) LoadServicesForUser(userID string) ([]types.Service, error) {
  34. return []types.Service{d.service}, nil
  35. }
  36. type MockTransport struct {
  37. roundTrip func(*http.Request) (*http.Response, error)
  38. }
  39. func (t MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
  40. return t.roundTrip(req)
  41. }
  42. func TestCommandParsing(t *testing.T) {
  43. var executedCmdArgs []string
  44. cmds := []types.Command{
  45. types.Command{
  46. Path: []string{"test"},
  47. Command: func(roomID, userID string, args []string) (interface{}, error) {
  48. executedCmdArgs = args
  49. return nil, nil
  50. },
  51. },
  52. }
  53. s := MockService{commands: cmds}
  54. store := MockStore{service: &s}
  55. database.SetServiceDB(&store)
  56. trans := struct{ MockTransport }{}
  57. trans.roundTrip = func(*http.Request) (*http.Response, error) {
  58. return nil, fmt.Errorf("unhandled test path")
  59. }
  60. cli := &http.Client{
  61. Transport: trans,
  62. }
  63. clients := New(&store, cli)
  64. mxCli, _ := gomatrix.NewClient("https://someplace.somewhere", "@service:user", "token")
  65. mxCli.Client = cli
  66. for _, input := range commandParseTests {
  67. executedCmdArgs = []string{}
  68. event := gomatrix.Event{
  69. Type: "m.room.message",
  70. Sender: "@someone:somewhere",
  71. RoomID: "!foo:bar",
  72. Content: map[string]interface{}{
  73. "body": input.body,
  74. "msgtype": "m.text",
  75. },
  76. }
  77. clients.onMessageEvent(mxCli, &event)
  78. if !reflect.DeepEqual(executedCmdArgs, input.expectArgs) {
  79. t.Errorf("TestCommandParsing want %s, got %s", input.expectArgs, executedCmdArgs)
  80. }
  81. }
  82. }