-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_helpers_test.go
46 lines (40 loc) · 1.23 KB
/
template_helpers_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
package main
import (
"reflect"
"sort"
"testing"
)
// ByAge implements sort.Interface for []*Group based on
// the Name field.
type ByName []*Group
func (n ByName) Len() int { return len(n) }
func (n ByName) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
func (n ByName) Less(i, j int) bool { return n[i].Name < n[j].Name }
// TestGroups checks that the correct []*Group is returned for a
// substantive input.
func TestGroups(t *testing.T) {
input := []*InstanceInfo{
{Name: "i_a", Address: "192.168.1.1", Groups: []string{"g1", "g2"}},
{Name: "i_b", Address: "192.168.1.2", Groups: []string{"g1", "g3"}},
}
expect := []*Group{
&Group{Name: "g1", Hosts: []string{"192.168.1.1", "192.168.1.2"}},
&Group{Name: "g2", Hosts: []string{"192.168.1.1"}},
&Group{Name: "g3", Hosts: []string{"192.168.1.2"}},
&Group{Name: "i_a", Hosts: []string{"192.168.1.1"}},
&Group{Name: "i_b", Hosts: []string{"192.168.1.2"}},
}
got := Groups(input)
sort.Sort(ByName(got))
if !reflect.DeepEqual(got, expect) {
t.Fail()
}
}
// TestTmptyInput checks that calling groups with
// []*InstanceInfo{} returns an empty slice of results.
func TestEmptyInput(t *testing.T) {
got := Groups([]*InstanceInfo{})
if len(got) != 0 {
t.Fail()
}
}