forked from deanishe/awgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alfred_test.go
158 lines (132 loc) · 5.76 KB
/
alfred_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
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
// Copyright (c) 2018 Dean Jackson <[email protected]>
// MIT Licence - http://opensource.org/licenses/MIT
package aw
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestAlfred verifies scripts generated for Alfred's JXA API.
func TestAlfred(t *testing.T) {
a := NewAlfred()
defer func() {
if err := os.Setenv("alfred_version", ""); err != nil {
panic(fmt.Sprintf("setenv failed: %v", err))
}
}()
t.Run("setup", func(t *testing.T) {
require.Nil(t, os.Setenv("alfred_version", ""), "setenv failed")
a.noRunScripts = true
})
t.Run("empty action", func(t *testing.T) {
// run this first because a.lastScript won't be set
assert.Nil(t, a.Action(), "call empty action failed")
assert.Equal(t, "", a.lastScript, "run empty action failed")
})
t.Run("empty search", func(t *testing.T) {
x := `Application("com.runningwithcrayons.Alfred").search("");`
assert.Nil(t, a.Search(""), "call empty search failed")
assert.Equal(t, x, a.lastScript, "empty search failed")
})
t.Run("search", func(t *testing.T) {
x := `Application("com.runningwithcrayons.Alfred").search("awgo alfred");`
assert.Nil(t, a.Search("awgo alfred"), "call search failed")
assert.Equal(t, x, a.lastScript, "search failed")
})
t.Run("action", func(t *testing.T) {
x := `Application("com.runningwithcrayons.Alfred").action(["/","/Volumes"]);`
assert.Nil(t, a.Action("/", "/Volumes"), "call action failed")
assert.Equal(t, x, a.lastScript, "action failed")
})
t.Run("action as file", func(t *testing.T) {
x := `Application("com.runningwithcrayons.Alfred").action(["/","/Volumes"], {"asType":"file"});`
assert.Nil(t, a.ActionAsType(TypeFile, "/", "/Volumes"), "call action on files failed")
assert.Equal(t, x, a.lastScript, "action failed")
})
t.Run("action as URL", func(t *testing.T) {
x := `Application("com.runningwithcrayons.Alfred").action(["https://example.com/","https://alfredapp.com/"], {"asType":"url"});`
assert.Nil(t, a.ActionAsType(TypeURL, "https://example.com/", "https://alfredapp.com/"), "call action on URLs failed")
assert.Equal(t, x, a.lastScript, "action failed")
})
t.Run("browse", func(t *testing.T) {
x := `Application("com.runningwithcrayons.Alfred").browse("/Users");`
assert.Nil(t, a.Browse("/Users"), "call browse failed")
assert.Equal(t, x, a.lastScript, "browse failed")
})
t.Run("run trigger", func(t *testing.T) {
x := `Application("com.runningwithcrayons.Alfred").runTrigger("test", {"inWorkflow":"net.deanishe.awgo","withArgument":"AwGo, yo!"});`
assert.Nil(t, a.RunTrigger("test", "AwGo, yo!"), "call trigger failed")
assert.Equal(t, x, a.lastScript, "run trigger failed")
})
t.Run("run 3rd-party trigger", func(t *testing.T) {
x := `Application("com.runningwithcrayons.Alfred").runTrigger("test", {"inWorkflow":"com.example.workflow","withArgument":"AwGo, yo!"});`
assert.Nil(t, a.RunTrigger("test", "AwGo, yo!", "com.example.workflow"), "call 3rd-party trigger failed")
assert.Equal(t, x, a.lastScript, "run trigger in other workflow failed")
})
t.Run("set theme", func(t *testing.T) {
x := `Application("com.runningwithcrayons.Alfred").setTheme("Alfred Notepad");`
assert.Nil(t, a.SetTheme("Alfred Notepad"), "call set theme failed")
assert.Equal(t, x, a.lastScript, "set theme failed")
})
t.Run("reload workflow", func(t *testing.T) {
x := `Application("com.runningwithcrayons.Alfred").reloadWorkflow("net.deanishe.awgo");`
assert.Nil(t, a.ReloadWorkflow(), "call reload workflow failed")
assert.Equal(t, x, a.lastScript, "reload workflow failed")
})
t.Run("reload 3rd-party workflow", func(t *testing.T) {
x := `Application("com.runningwithcrayons.Alfred").reloadWorkflow("com.example.workflow");`
assert.Nil(t, a.ReloadWorkflow("com.example.workflow"), "call reload workflow failed")
assert.Equal(t, x, a.lastScript, "reload workflow failed")
})
// run a do-nothing script
t.Run("do-nothing script", func(t *testing.T) {
a.noRunScripts = false
js := `function run(argv) { return %s }`
assert.Nil(t, a.runScript(js), "run script failed")
})
}
// TestAlfred3 verifies scripts generated for Alfred 3's JXA API.
func TestAlfred3(t *testing.T) {
a := NewAlfred()
defer func() {
if err := os.Setenv("alfred_version", ""); err != nil {
panic(fmt.Sprintf("setenv failed: %v", err))
}
}()
a.noRunScripts = true
t.Run("set env", func(t *testing.T) {
require.Nil(t, os.Setenv("alfred_version", "3.8.1"), "setenv failed")
})
t.Run("empty search", func(t *testing.T) {
x := `Application("Alfred 3").search("");`
assert.Nil(t, a.Search(""), "Alfred call failed")
assert.Equal(t, x, a.lastScript, "search failed")
})
t.Run("search", func(t *testing.T) {
x := `Application("Alfred 3").search("awgo alfred");`
assert.Nil(t, a.Search("awgo alfred"), "Alfred call failed")
assert.Equal(t, x, a.lastScript, "search failed")
})
t.Run("action", func(t *testing.T) {
x := `Application("Alfred 3").action(["/","/Volumes"]);`
assert.Nil(t, a.Action("/", "/Volumes"), "Alfred call failed")
assert.Equal(t, x, a.lastScript, "action failed")
})
t.Run("browser", func(t *testing.T) {
x := `Application("Alfred 3").browse("/Users");`
assert.Nil(t, a.Browse("/Users"), "Alfred call failed")
assert.Equal(t, x, a.lastScript, "browse failed")
})
t.Run("run trigger", func(t *testing.T) {
x := `Application("Alfred 3").runTrigger("test", {"inWorkflow":"net.deanishe.awgo","withArgument":"AwGo, yo!"});`
assert.Nil(t, a.RunTrigger("test", "AwGo, yo!"), "Alfred call failed")
assert.Equal(t, x, a.lastScript, "run trigger failed")
})
t.Run("set theme", func(t *testing.T) {
x := `Application("Alfred 3").setTheme("Alfred Notepad");`
assert.Nil(t, a.SetTheme("Alfred Notepad"), "Alfred call failed")
assert.Equal(t, x, a.lastScript, "run trigger failed")
})
}