forked from deanishe/awgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow_test.go
124 lines (112 loc) · 3.82 KB
/
workflow_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
//
// Copyright (c) 2016 Dean Jackson <[email protected]>
//
// MIT Licence. See http://opensource.org/licenses/MIT
//
package aw
import (
"encoding/json"
"fmt"
"testing"
)
// Opens workflow's log file.
type testMagicAction struct{}
func (a testMagicAction) Keyword() string { return "test" }
func (a testMagicAction) Description() string { return "Just a test" }
func (a testMagicAction) RunText() string { return "Performing test…" }
func (a testMagicAction) Run() error { return nil }
var testOptions = []struct {
opt Option
test func(wf *Workflow) bool
desc string
}{
{HelpURL("http://www.example.com"), func(wf *Workflow) bool { return wf.HelpURL == "http://www.example.com" }, "Set HelpURL"},
{MaxResults(10), func(wf *Workflow) bool { return wf.MaxResults == 10 }, "Set MaxResults"},
{LogPrefix("blah"), func(wf *Workflow) bool { return wf.LogPrefix == "blah" }, "Set LogPrefix"},
{SortOptions(), func(wf *Workflow) bool { return wf.SortOptions == nil }, "Set SortOptions"},
{MagicPrefix("aw:"), func(wf *Workflow) bool { return wf.magicPrefix == "aw:" }, "Set MagicPrefix"},
{MaxLogSize(2048), func(wf *Workflow) bool { return wf.MaxLogSize == 2048 }, "Set MaxLogSize"},
{TextErrors(true), func(wf *Workflow) bool { return wf.TextErrors == true }, "Set TextErrors"},
{AddMagic(testMagicAction{}), func(wf *Workflow) bool { return wf.MagicActions["test"] != nil }, "Add Magic"},
{RemoveMagic(openLogMagic{}), func(wf *Workflow) bool { return wf.MagicActions["log"] == nil }, "Remove Magic"},
}
func TestOptions(t *testing.T) {
for _, td := range testOptions {
wf := New(td.opt)
if !td.test(wf) {
t.Errorf("option %s failed", td.desc)
}
}
}
// TestWorkflowValues tests workflow name, bundle ID etc.
func TestWorkflowValues(t *testing.T) {
name := "AwGo"
bundleID := "net.deanishe.awgo"
wf := New()
if wf.Name() != name {
t.Errorf("wrong name. Expected=%s, Got=%s", name, wf.Name())
}
if wf.BundleID() != bundleID {
t.Errorf("wrong bundle ID. Expected=%s, Got=%s", bundleID, wf.BundleID())
}
}
// New initialises a Workflow with the default settings. Name,
// bundle ID, version etc. are read from the environment variables set by Alfred.
func ExampleNew() {
wf := New()
// BundleID is read from environment or info.plist
fmt.Println(wf.BundleID())
// Version is from info.plist
fmt.Println(wf.Version())
// Output:
// net.deanishe.awgo
// 0.2.2
}
// Pass one or more Options to New() to configure the created Workflow.
func ExampleNew_withOptions() {
wf := New(HelpURL("http://www.example.com"), MaxResults(200))
fmt.Println(wf.HelpURL)
fmt.Println(wf.MaxResults)
// Output:
// http://www.example.com
// 200
}
// Temporarily change Workflow's behaviour then revert it.
func ExampleOption() {
wf := New()
// Default settings (false and 0)
fmt.Println(wf.TextErrors)
fmt.Println(wf.MaxResults)
// Turn text errors on, set max results and save Option to revert
// to previous configuration
previous := wf.Configure(TextErrors(true), MaxResults(200))
fmt.Println(wf.TextErrors)
fmt.Println(wf.MaxResults)
// Revert to previous configuration
wf.Configure(previous)
fmt.Println(wf.TextErrors)
fmt.Println(wf.MaxResults)
// Output:
// false
// 0
// true
// 200
// false
// 0
}
// The normal way to create a new Item, but not the normal way to use it.
//
// Typically, when you're done adding Items, you call SendFeedback() to
// send the results to Alfred.
func ExampleNewItem() {
// Create a new item via the default Workflow object, which will
// track the Item and send it to Alfred when you call SendFeedback()
//
// Title is the only required value.
it := NewItem("First Result").
Subtitle("Some details here")
// Just to see what it looks like...
data, _ := json.Marshal(it)
fmt.Println(string(data))
// Output: {"title":"First Result","subtitle":"Some details here","valid":false}
}