File: input_test.go

package info (click to toggle)
coyim 0.3.7-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,064 kB
  • ctags: 4,528
  • sloc: xml: 5,120; sh: 328; python: 286; makefile: 235; ruby: 51
file content (178 lines) | stat: -rw-r--r-- 3,725 bytes parent folder | download
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package cli

import (
	"io/ioutil"
	"log"
	"reflect"
	"testing"

	"github.com/twstrike/gotk3adapter/glib_mock"
	"github.com/twstrike/coyim/i18n"

	. "gopkg.in/check.v1"
)

const (
	opFind = iota
	opNext
)

var priorityListTests = []struct {
	op      int
	in, out string
}{
	{opFind, "a", "anchor"},
	{opFind, "a", "anchor"},
	{opNext, "", "anvil"},
	{opNext, "", "anchor"},
	{opNext, "", "anvil"},
	{opNext, "", "anchor"},
	{opFind, "a", "anchor"},
	{opNext, "", "anvil"},
	{opFind, "a", "anvil"},
	{opFind, "a", "anvil"},

	{opFind, "bo", "bob"},
	{opNext, "", "boom"},
	{opNext, "", "bop"},

	{opFind, "a", "anvil"},
	{opFind, "b", "bop"},

	{opFind, "c", "charlie"},
	{opNext, "", "charlie"},
	{opNext, "", "charlie"},
}

func Test(t *testing.T) { TestingT(t) }

func init() {
	log.SetOutput(ioutil.Discard)
	i18n.InitLocalization(&glib_mock.Mock{})
}

type CLISuite struct{}

var _ = Suite(&CLISuite{})

func (s *CLISuite) TestPriorityList(c *C) {
	var pl priorityList

	for _, word := range []string{"bop", "boom", "bob", "anvil", "anchor", "charlie"} {
		pl.Insert(word)
	}

	for _, step := range priorityListTests {
		var out string

		switch step.op {
		case opFind:
			out, _ = pl.Find(step.in)
		case opNext:
			out = pl.Next()
		default:
			panic("unknown op")
		}

		c.Check(out, Equals, step.out)
	}
}

var testCommands = []uiCommand{
	{"a", aCommand{}, "Desc"},
	{"b", bCommand{}, "Desc"},
}

type aCommand struct {
}

type bCommand struct {
	A string
	B string "uid"
	C string
}

var parseForCompletionTests = []struct {
	in             string
	ok             bool
	before, prefix string
	isCommand      bool
}{
	{"", false, "", "", false},
	{"/", true, "/", "", true},
	{"/a", true, "/", "a", true},
	{"/ab", true, "/", "ab", true},
	{"/a b", false, "", "", false},
	{"/a b ", false, "", "", false},
	{"/a ba", false, "", "", false},
	{"/a ba c", false, "", "", false},
	{"/a ba c d", false, "", "", false},

	{"/b c", false, "", "", false},
	{"/b c ", false, "", "", false},
	{"/b c d", true, "/b c ", "d", false},
	{"/b c de", true, "/b c ", "de", false},
	{"/b c de ", false, "", "", false},
	{"/b c de f", false, "", "", false},
}

func (s *CLISuite) TestParseCommandForCompletion(t *C) {
	for i, test := range parseForCompletionTests {
		before, prefix, isCommand, ok := parseCommandForCompletion(testCommands, test.in)
		if ok != test.ok {
			t.Errorf("#%d: result mismatch (should be %t)", i, test.ok)
			continue
		}

		if !ok {
			continue
		}

		if before != test.before {
			t.Errorf("#%d: mismatch with 'before': got '%s', want '%s'", i, string(before), test.before)
		}
		if prefix != test.prefix {
			t.Errorf("#%d: mismatch with 'prefix': got '%s', want '%s'", i, string(before), test.before)
		}
		if isCommand != test.isCommand {
			t.Errorf("#%d: isCommand incorrect, wanted %t", i, test.isCommand)
		}
	}
}

var parseCommandTests = []struct {
	in  string
	ok  bool
	out interface{}
}{
	{"/", false, nil},
	{"/bob", false, nil},
	{"/a", true, aCommand{}},
	{"/a b", false, nil},
	{"/a b c", false, nil},
	{"/b a", false, nil},
	{"/b a b", false, nil},
	{"/b a b ", false, nil},
	{"/b a b c", true, bCommand{"a", "b", "c"}},
	{"/b a b\\  c", true, bCommand{"a", "b ", "c"}},
	{"/b a \"b b\" c", true, bCommand{"a", "b b", "c"}},
	{"/b a \"b \\\"b\" c", true, bCommand{"a", "b \"b", "c"}},
}

func (s *CLISuite) TestParseCommand(t *C) {
	for i, test := range parseCommandTests {
		v, err := parseCommand(testCommands, []byte(test.in))
		if (len(err) == 0) != test.ok {
			t.Errorf("#%d: bad parse result, expected %t", i, test.ok)
			continue
		}

		if !test.ok {
			continue
		}

		if !reflect.DeepEqual(v, test.out) {
			t.Errorf("#%d: bad result: got %v, want %v", i, v, test.out)
		}
	}
}