forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_test.go
112 lines (88 loc) · 2.02 KB
/
table_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package engine
import (
"bytes"
"encoding/json"
"testing"
)
func TestTableWriteTo(t *testing.T) {
table := NewTable("", 0)
e := &Env{}
e.Set("foo", "bar")
table.Add(e)
var buf bytes.Buffer
if _, err := table.WriteTo(&buf); err != nil {
t.Fatal(err)
}
output := make(map[string]string)
if err := json.Unmarshal(buf.Bytes(), &output); err != nil {
t.Fatal(err)
}
if len(output) != 1 {
t.Fatalf("Incorrect output: %v", output)
}
if val, exists := output["foo"]; !exists || val != "bar" {
t.Fatalf("Inccorect output: %v", output)
}
}
func TestTableSortStringValue(t *testing.T) {
table := NewTable("Key", 0)
e := &Env{}
e.Set("Key", "A")
table.Add(e)
e = &Env{}
e.Set("Key", "D")
table.Add(e)
e = &Env{}
e.Set("Key", "B")
table.Add(e)
e = &Env{}
e.Set("Key", "C")
table.Add(e)
table.Sort()
if len := table.Len(); len != 4 {
t.Fatalf("Expected 4, got %d", len)
}
if value := table.Data[0].Get("Key"); value != "A" {
t.Fatalf("Expected A, got %s", value)
}
if value := table.Data[1].Get("Key"); value != "B" {
t.Fatalf("Expected B, got %s", value)
}
if value := table.Data[2].Get("Key"); value != "C" {
t.Fatalf("Expected C, got %s", value)
}
if value := table.Data[3].Get("Key"); value != "D" {
t.Fatalf("Expected D, got %s", value)
}
}
func TestTableReverseSortStringValue(t *testing.T) {
table := NewTable("Key", 0)
e := &Env{}
e.Set("Key", "A")
table.Add(e)
e = &Env{}
e.Set("Key", "D")
table.Add(e)
e = &Env{}
e.Set("Key", "B")
table.Add(e)
e = &Env{}
e.Set("Key", "C")
table.Add(e)
table.ReverseSort()
if len := table.Len(); len != 4 {
t.Fatalf("Expected 4, got %d", len)
}
if value := table.Data[0].Get("Key"); value != "D" {
t.Fatalf("Expected D, got %s", value)
}
if value := table.Data[1].Get("Key"); value != "C" {
t.Fatalf("Expected B, got %s", value)
}
if value := table.Data[2].Get("Key"); value != "B" {
t.Fatalf("Expected C, got %s", value)
}
if value := table.Data[3].Get("Key"); value != "A" {
t.Fatalf("Expected A, got %s", value)
}
}