-
Notifications
You must be signed in to change notification settings - Fork 15
/
macro_test.go
57 lines (47 loc) · 1.56 KB
/
macro_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package rivescript_test
// This test file contains the unit tests that had to be segregated from the
// others in the `src/` package.
//
// The only one here so far is an object macro test. It needed to use the public
// RiveScript API because the JavaScript handler expects an object of that type,
// and so it couldn't be in the `src/` package or it would create a dependency
// cycle.
import (
"testing"
rivescript "github.com/aichaos/rivescript-go"
"github.com/aichaos/rivescript-go/lang/javascript"
)
// This one has to test the public interface because of the JavaScript handler
// expecting a *RiveScript of the correct color.
func TestJavaScript(t *testing.T) {
rs := rivescript.New(nil)
rs.SetHandler("javascript", javascript.New(rs))
rs.Stream(`
> object reverse javascript
var msg = args.join(" ");
return msg.split("").reverse().join("");
< object
> object nolang
return "No language provided!"
< object
+ reverse *
- <call>reverse <star></call>
+ no lang
- <call>nolang</call>
`)
rs.SortReplies()
// Helper function to assert replies via the public interface.
assert := func(input, expected string) {
reply, err := rs.Reply("local-user", input)
if err != nil {
t.Errorf("Got error when trying to get a reply: %v", err)
} else if reply != expected {
t.Errorf("Got unexpected reply. Expected %s, got %s", expected, reply)
}
}
assert("reverse hello world", "dlrow olleh")
assert("no lang", "[ERR: Object Not Found]")
// Disable support.
rs.RemoveHandler("javascript")
assert("reverse hello world", "[ERR: Object Not Found]")
}