git.deanishe.net/deanishe/awgo.git@v0.15.0/workflow_test.go (about)

     1  //
     2  // Copyright (c) 2016 Dean Jackson <deanishe@deanishe.net>
     3  //
     4  // MIT Licence. See http://opensource.org/licenses/MIT
     5  //
     6  
     7  package aw
     8  
     9  import (
    10  	"encoding/json"
    11  	"fmt"
    12  	"os"
    13  	"testing"
    14  )
    15  
    16  // TestWorkflowValues tests workflow name, bundle ID etc.
    17  func TestWorkflowValues(t *testing.T) {
    18  
    19  	withTestWf(func(wf *Workflow) {
    20  
    21  		if wf.Name() != tName {
    22  			t.Errorf("Bad Name. Expected=%s, Got=%s", tName, wf.Name())
    23  		}
    24  		if wf.BundleID() != tBundleID {
    25  			t.Errorf("Bad BundleID. Expected=%s, Got=%s", tBundleID, wf.BundleID())
    26  		}
    27  	})
    28  }
    29  
    30  // TestOptions verifies that options correctly alter Workflow.
    31  func TestOptions(t *testing.T) {
    32  
    33  	data := []struct {
    34  		opt  Option                  // option to set
    35  		test func(wf *Workflow) bool // function to verify change was made
    36  		desc string                  // test title
    37  	}{
    38  		{
    39  			HelpURL("http://www.example.com"),
    40  			func(wf *Workflow) bool { return wf.helpURL == "http://www.example.com" },
    41  			"Set HelpURL"},
    42  		{
    43  			MaxResults(10),
    44  			func(wf *Workflow) bool { return wf.maxResults == 10 },
    45  			"Set MaxResults"},
    46  		{
    47  			LogPrefix("blah"),
    48  			func(wf *Workflow) bool { return wf.logPrefix == "blah" },
    49  			"Set LogPrefix"},
    50  		{
    51  			SessionName("SESH"),
    52  			func(wf *Workflow) bool { return wf.sessionName == "SESH" },
    53  			"Set SessionName"},
    54  		{
    55  			SortOptions(),
    56  			func(wf *Workflow) bool { return wf.sortOptions == nil },
    57  			"Set SortOptions"},
    58  		{
    59  			SuppressUIDs(true),
    60  			func(wf *Workflow) bool { return wf.Feedback.NoUIDs == true },
    61  			"Set SuppressUIDs"},
    62  		{
    63  			MagicPrefix("aw:"),
    64  			func(wf *Workflow) bool { return wf.magicPrefix == "aw:" },
    65  			"Set MagicPrefix"},
    66  		{
    67  			MaxLogSize(2048),
    68  			func(wf *Workflow) bool { return wf.maxLogSize == 2048 },
    69  			"Set MaxLogSize"},
    70  		{
    71  			TextErrors(true),
    72  			func(wf *Workflow) bool { return wf.textErrors == true },
    73  			"Set TextErrors"},
    74  		{
    75  			AddMagic(&testMA{}),
    76  			func(wf *Workflow) bool { return wf.MagicActions.actions["test"] != nil },
    77  			"Add Magic"},
    78  		{
    79  			RemoveMagic(logMA{}),
    80  			func(wf *Workflow) bool { return wf.MagicActions.actions["log"] == nil },
    81  			"Remove Magic"},
    82  	}
    83  
    84  	for _, td := range data {
    85  
    86  		wf := New(td.opt)
    87  
    88  		if !td.test(wf) {
    89  			t.Errorf("option %s failed", td.desc)
    90  		}
    91  	}
    92  }
    93  
    94  func TestWorkflowRun(t *testing.T) {
    95  
    96  	withTestWf(func(wf *Workflow) {
    97  
    98  		var called bool
    99  
   100  		run := func() {
   101  			called = true
   102  		}
   103  
   104  		wf.Run(run)
   105  
   106  		if !called {
   107  			t.Errorf("run wasn't called")
   108  		}
   109  	})
   110  }
   111  
   112  // TestWorkflowDir verifies that AwGo finds the right directory.
   113  func TestWorkflowDir(t *testing.T) {
   114  
   115  	withTestWf(func(wf *Workflow) {
   116  
   117  		// Set up environment
   118  		cwd, err := os.Getwd()
   119  		if err != nil {
   120  			t.Fatal(err)
   121  		}
   122  
   123  		subdir := "sub"
   124  		if err := os.Mkdir(subdir, 0700); err != nil {
   125  			t.Fatal(err)
   126  		}
   127  
   128  		// workflow root (alongside info.plist)
   129  		if wf.Dir() != cwd {
   130  			t.Errorf("Bad Dir (root). Expected=%v, Got=%v", cwd, wf.Dir())
   131  		}
   132  
   133  		// Change to subdirectory
   134  		if err := os.Chdir(subdir); err != nil {
   135  			t.Fatal(err)
   136  		}
   137  
   138  		// Reset cached path
   139  		wf.dir = ""
   140  		// Should find parent directory (where info.plist is)
   141  		if wf.Dir() != cwd {
   142  			t.Errorf("Bad Dir (sub). Expected=%v, Got=%v", cwd, wf.Dir())
   143  		}
   144  	})
   145  }
   146  
   147  // New initialises a Workflow with the default settings. Name,
   148  // bundle ID, version etc. are read from the environment variables set by Alfred.
   149  func ExampleNew() {
   150  	wf := New()
   151  	// Name is read from environment
   152  	fmt.Println(wf.Name())
   153  	// BundleID is read from environment
   154  	fmt.Println(wf.BundleID())
   155  	// Version is from info.plist
   156  	fmt.Println(wf.Version())
   157  	// Output:
   158  	// AwGo
   159  	// net.deanishe.awgo
   160  	// 0.14
   161  }
   162  
   163  // Pass one or more Options to New() to configure the created Workflow.
   164  func ExampleNew_withOptions() {
   165  	wf := New(HelpURL("http://www.example.com"), MaxResults(200))
   166  	fmt.Println(wf.helpURL)
   167  	fmt.Println(wf.maxResults)
   168  	// Output:
   169  	// http://www.example.com
   170  	// 200
   171  }
   172  
   173  // The normal way to create a new Item, but not the normal way to use it.
   174  //
   175  // Typically, when you're done adding Items, you call SendFeedback() to
   176  // send the results to Alfred.
   177  func ExampleWorkflow_NewItem() {
   178  	wf := New()
   179  	// Create a new item via the Workflow object, which will
   180  	// track the Item and send it to Alfred when you call
   181  	// Workflow.SendFeedback()
   182  	//
   183  	// Title is the only required value.
   184  	it := wf.NewItem("First Result").
   185  		Subtitle("Some details here")
   186  
   187  	// Just to see what it looks like...
   188  	data, _ := json.Marshal(it)
   189  	fmt.Println(string(data))
   190  	// Output: {"title":"First Result","subtitle":"Some details here","valid":false}
   191  }
   192  
   193  // Change Workflow's configuration after creation, then revert it.
   194  func ExampleWorkflow_Configure() {
   195  	wf := New()
   196  	// Default settings (false and 0)
   197  	fmt.Println(wf.textErrors)
   198  	fmt.Println(wf.maxResults)
   199  	// Turn text errors on, set max results and save Option to revert
   200  	// to previous configuration
   201  	previous := wf.Configure(TextErrors(true), MaxResults(200))
   202  	fmt.Println(wf.textErrors)
   203  	fmt.Println(wf.maxResults)
   204  	// Revert to previous configuration
   205  	wf.Configure(previous)
   206  	fmt.Println(wf.textErrors)
   207  	fmt.Println(wf.maxResults)
   208  	// Output:
   209  	// false
   210  	// 0
   211  	// true
   212  	// 200
   213  	// false
   214  	// 0
   215  }
   216  
   217  func ExampleArgVars() {
   218  	// Set workflow variables from Alfred's Run Script Action
   219  	av := NewArgVars()
   220  	av.Arg("baz")        // Set output (i.e. next action's {query}) to "baz"
   221  	av.Var("foo", "bar") // Set workflow variable "foo" to "bar"
   222  	av.Send()
   223  	// Output: {"alfredworkflow":{"arg":"baz","variables":{"foo":"bar"}}}
   224  }