File: type_go_map_test.go

package info (click to toggle)
golang-github-robertkrimen-otto 0.0~git20200922.ef014fd-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,796 kB
  • sloc: perl: 1,227; makefile: 79
file content (40 lines) | stat: -rw-r--r-- 843 bytes parent folder | download | duplicates (2)
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 ")
	})
}