mirror of https://github.com/matrix-org/go-neb.git
Browse Source
Merge pull request #130 from matrix-org/kegan/smart-quotes
Merge pull request #130 from matrix-org/kegan/smart-quotes
Fix #72: Support smart quotespull/131/head
Kegsay
8 years ago
committed by
GitHub
2 changed files with 104 additions and 3 deletions
-
12src/github.com/matrix-org/go-neb/clients/clients.go
-
95src/github.com/matrix-org/go-neb/clients/clients_test.go
@ -0,0 +1,95 @@ |
|||||
|
package clients |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"github.com/matrix-org/go-neb/database" |
||||
|
"github.com/matrix-org/go-neb/matrix" |
||||
|
"github.com/matrix-org/go-neb/types" |
||||
|
"net/http" |
||||
|
"net/url" |
||||
|
"reflect" |
||||
|
"testing" |
||||
|
) |
||||
|
|
||||
|
var commandParseTests = []struct { |
||||
|
body string |
||||
|
expectArgs []string |
||||
|
}{ |
||||
|
{"!test word", []string{"word"}}, |
||||
|
{"!test two words", []string{"two", "words"}}, |
||||
|
{`!test "words with double quotes"`, []string{"words with double quotes"}}, |
||||
|
{"!test 'words with single quotes'", []string{"words with single quotes"}}, |
||||
|
{`!test 'single quotes' "double quotes"`, []string{"single quotes", "double quotes"}}, |
||||
|
{`!test ‘smart single quotes’ “smart double quotes”`, []string{"smart single quotes", "smart double quotes"}}, |
||||
|
} |
||||
|
|
||||
|
type MockService struct { |
||||
|
types.DefaultService |
||||
|
commands []types.Command |
||||
|
} |
||||
|
|
||||
|
func (s *MockService) Commands(cli *matrix.Client) []types.Command { |
||||
|
return s.commands |
||||
|
} |
||||
|
|
||||
|
type MockStore struct { |
||||
|
database.NopStorage |
||||
|
service types.Service |
||||
|
} |
||||
|
|
||||
|
func (d *MockStore) LoadServicesForUser(userID string) ([]types.Service, error) { |
||||
|
return []types.Service{d.service}, nil |
||||
|
} |
||||
|
|
||||
|
type MockTransport struct { |
||||
|
roundTrip func(*http.Request) (*http.Response, error) |
||||
|
} |
||||
|
|
||||
|
func (t MockTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
||||
|
return t.roundTrip(req) |
||||
|
} |
||||
|
|
||||
|
func TestCommandParsing(t *testing.T) { |
||||
|
var executedCmdArgs []string |
||||
|
cmds := []types.Command{ |
||||
|
types.Command{ |
||||
|
Path: []string{"test"}, |
||||
|
Command: func(roomID, userID string, args []string) (interface{}, error) { |
||||
|
executedCmdArgs = args |
||||
|
return nil, nil |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
s := MockService{commands: cmds} |
||||
|
store := MockStore{service: &s} |
||||
|
database.SetServiceDB(&store) |
||||
|
|
||||
|
trans := struct{ MockTransport }{} |
||||
|
trans.roundTrip = func(*http.Request) (*http.Response, error) { |
||||
|
return nil, fmt.Errorf("unhandled test path") |
||||
|
} |
||||
|
cli := &http.Client{ |
||||
|
Transport: trans, |
||||
|
} |
||||
|
clients := New(&store, cli) |
||||
|
hsURL, _ := url.Parse("https://someplace.somewhere") |
||||
|
mxCli := matrix.NewClient(cli, hsURL, "token", "@service:user") |
||||
|
|
||||
|
for _, input := range commandParseTests { |
||||
|
executedCmdArgs = []string{} |
||||
|
event := matrix.Event{ |
||||
|
Type: "m.room.message", |
||||
|
Sender: "@someone:somewhere", |
||||
|
RoomID: "!foo:bar", |
||||
|
Content: map[string]interface{}{ |
||||
|
"body": input.body, |
||||
|
"msgtype": "m.text", |
||||
|
}, |
||||
|
} |
||||
|
clients.onMessageEvent(mxCli, &event) |
||||
|
if !reflect.DeepEqual(executedCmdArgs, input.expectArgs) { |
||||
|
t.Errorf("TestCommandParsing want %s, got %s", input.expectArgs, executedCmdArgs) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue