-
Notifications
You must be signed in to change notification settings - Fork 15
/
start_test.go
127 lines (109 loc) · 2.47 KB
/
start_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"testing"
"github.com/issadarkthing/gomu/anko"
"github.com/issadarkthing/gomu/hook"
"github.com/stretchr/testify/assert"
)
// Test default case
func TestGetArgsDefaults(t *testing.T) {
args := getArgs()
assert.Equal(t, *args.config, "~/.config/gomu/config")
assert.Equal(t, *args.empty, false)
assert.Equal(t, *args.music, "~/music")
assert.Equal(t, *args.version, false)
}
// Test non-standard flags/the empty/version flags
func TestGetArgs(t *testing.T) {
home, err := os.UserHomeDir()
if err != nil {
t.Error(err)
t.FailNow()
}
cfgDir, err := os.UserConfigDir()
if err != nil {
t.Error(err)
t.FailNow()
}
// Test setting config flag
testConfig := filepath.Join(cfgDir, ".tmp", "gomu")
_, err = os.Stat(testConfig)
if os.IsNotExist(err) {
os.MkdirAll(testConfig, 0755)
}
defer os.RemoveAll(testConfig)
//create a temporary config file
tmpCfgf, err := os.CreateTemp(testConfig, "config")
if err != nil {
t.Error(err)
t.FailNow()
}
testMusic := filepath.Join(home, ".tmp", "gomu")
_, err = os.Stat(testMusic)
if os.IsNotExist(err) {
os.MkdirAll(testMusic, 0755)
}
defer os.RemoveAll(testMusic)
boolChecks := []struct {
name string
arg bool
want bool
}{
{"empty", true, true},
{"version", true, true},
}
for _, check := range boolChecks {
t.Run("testing bool flag "+check.name, func(t *testing.T) {
flag.CommandLine.Set(check.name, strconv.FormatBool(check.arg))
flag.CommandLine.Parse(os.Args[1:])
assert.Equal(t, check.arg, check.want)
})
}
strChecks := []struct {
name string
arg string
want string
}{
{"config", tmpCfgf.Name(), tmpCfgf.Name()},
{"music", testMusic, testMusic},
}
for _, check := range strChecks {
t.Run("testing string flag "+check.name, func(t *testing.T) {
flag.CommandLine.Set(check.name, check.arg)
flag.CommandLine.Parse(os.Args[1:])
fmt.Println("flag value: ", check.arg)
assert.Equal(t, check.arg, check.want)
})
}
}
func TestSetupHooks(t *testing.T) {
gomu := newGomu()
gomu.anko = anko.NewAnko()
gomu.hook = hook.NewEventHook()
err := loadModules(gomu.anko)
if err != nil {
t.Error(err)
}
setupHooks(gomu.hook, gomu.anko)
const src = `
i = 0
Event.add_hook("skip", func() {
i++
})
`
_, err = gomu.anko.Execute(src)
if err != nil {
t.Error(err)
}
gomu.hook.RunHooks("enter")
for i := 0; i < 12; i++ {
gomu.hook.RunHooks("skip")
}
got := gomu.anko.GetInt("i")
assert.Equal(t, 12, got)
}