forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_go_map_test.go
More file actions
40 lines (33 loc) · 843 Bytes
/
type_go_map_test.go
File metadata and controls
40 lines (33 loc) · 843 Bytes
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
package otto
import (
"fmt"
"sort"
"testing"
)
type GoMapTest map[string]int
func (s GoMapTest) Join() string {
joinedStr := ""
// Ordering the map takes some effort
// because map iterators in golang are unordered by definition.
// So we need to extract keys, sort them, and then generate K/V pairs
// All of this is meant to ensure that the test is predictable.
keys := make([]string, len(s))
i := 0
for key, _ := range s {
keys[i] = key
i++
}
sort.Strings(keys)
for _, key := range keys {
joinedStr += key + ": " + fmt.Sprintf("%d", s[key]) + " "
}
return joinedStr
}
func TestGoMap(t *testing.T) {
tt(t, func() {
test, vm := test()
vm.Set("TestMap", GoMapTest{"one": 1, "two": 2, "three": 3})
is(test(`TestMap["one"]`).export(), 1)
is(test(`TestMap.Join()`).export(), "one: 1 three: 3 two: 2 ")
})
}