-
Notifications
You must be signed in to change notification settings - Fork 0
/
pumpkin_test.go
60 lines (52 loc) · 1.37 KB
/
pumpkin_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
package main
import (
"github.com/stretchr/testify/assert"
"testing"
)
var basicPumpkin string = `
{
"pattern": ".*\\.go",
"commands": [
"echo hello"
]
}`
func PumpkinToTest() Pumpkin {
return NewPumpkin([]byte(basicPumpkin))
}
func TestExtractingAnInvalidPumpkin(t *testing.T) {
pumpkin := NewPumpkin([]byte(`{"pattern": ".*"}`))
valid, message := pumpkin.Validate()
assert.Equal(t, false, valid)
assert.Equal(t, "commands cannot be blank", message)
}
func TestExtractingAPumpkin(t *testing.T) {
pumpkin := PumpkinToTest()
assert.Equal(t, ".*\\.go", pumpkin.Regex.String())
}
func TestCheckingAFilename(t *testing.T) {
pumpkin := PumpkinToTest()
assert.Equal(t, true, pumpkin.Check("main.go"))
assert.Equal(t, false, pumpkin.Check("main.c"))
assert.Equal(t, false, pumpkin.Check("maindgo"))
}
func TestRunningCommands(t *testing.T) {
var msg Message
pumpkin := PumpkinToTest()
output := make(chan Message)
go func() {
pumpkin.Process(output)
}()
validateMessage(t, <-output, yellow, "---------------")
validateMessage(t, <-output, yellow, "echo hello")
validateMessage(t, <-output, green, "hello\n")
select {
case msg = <-output:
assert.Fail(t, "Received an unexpected message", msg)
default:
// Success
}
}
func validateMessage(t *testing.T, msg Message, color, content string) {
assert.Equal(t, color, msg.Color)
assert.Equal(t, content, msg.Content)
}