github.com/quantumghost/awgo@v0.15.0/magic_test.go (about)

     1  //
     2  // Copyright (c) 2017 Dean Jackson <deanishe@deanishe.net>
     3  //
     4  // MIT Licence. See http://opensource.org/licenses/MIT
     5  //
     6  // Created on 2017-08-12
     7  //
     8  
     9  package aw
    10  
    11  import (
    12  	"errors"
    13  	"testing"
    14  )
    15  
    16  // Mock magic action
    17  type testMA struct {
    18  	keyCalled, descCalled, runTextCalled, runCalled bool
    19  	returnError                                     bool
    20  }
    21  
    22  func (a *testMA) Keyword() string {
    23  	a.keyCalled = true
    24  	return "test"
    25  }
    26  func (a *testMA) Description() string {
    27  	a.descCalled = true
    28  	return "Just a test"
    29  }
    30  func (a *testMA) RunText() string {
    31  	a.runTextCalled = true
    32  	return "Performing test…"
    33  }
    34  func (a *testMA) Run() error {
    35  	a.runCalled = true
    36  	if a.returnError {
    37  		return errors.New("requested error")
    38  	}
    39  	return nil
    40  }
    41  
    42  // Returns an error if the MA wasn't "shown".
    43  // That means MagicActions didn't show a list of actions.
    44  func (a *testMA) ValidateShown() error {
    45  
    46  	if !a.keyCalled {
    47  		return errors.New("Keyword() not called")
    48  	}
    49  
    50  	if !a.descCalled {
    51  		return errors.New("Description() not called")
    52  	}
    53  
    54  	if a.runCalled {
    55  		return errors.New("Run() called")
    56  	}
    57  
    58  	if a.runTextCalled {
    59  		return errors.New("RunText() called")
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  // Returns an error if the MA wasn't run.
    66  func (a *testMA) ValidateRun() error {
    67  
    68  	if !a.keyCalled {
    69  		return errors.New("Keyword() not called")
    70  	}
    71  
    72  	if a.descCalled {
    73  		return errors.New("Description() called")
    74  	}
    75  
    76  	if !a.runCalled {
    77  		return errors.New("Run() not called")
    78  	}
    79  
    80  	if !a.runTextCalled {
    81  		return errors.New("RunText() not called")
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  // TestNonMagicArgs tests that normal arguments aren't ignored
    88  func TestNonMagicArgs(t *testing.T) {
    89  
    90  	data := []struct {
    91  		in, out []string
    92  	}{
    93  		{[]string{"a", "b", "c"}, []string{"a", "b", "c"}},
    94  	}
    95  
    96  	for _, td := range data {
    97  
    98  		wf := New()
    99  		ma := wf.MagicActions
   100  
   101  		args, handled := ma.handleArgs(td.in, DefaultMagicPrefix)
   102  
   103  		if handled {
   104  			t.Error("handled")
   105  		}
   106  
   107  		if !slicesEqual(args, td.out) {
   108  			t.Errorf("not equal. Expected=%v, Got=%v", td.out, args)
   109  		}
   110  	}
   111  
   112  }
   113  
   114  func TestMagicDefaults(t *testing.T) {
   115  	wf := New()
   116  	ma := wf.MagicActions
   117  
   118  	x := 6
   119  	v := len(ma.actions)
   120  	if v != x {
   121  		t.Errorf("Bad MagicAction count. Expected=%d, Got=%d", x, v)
   122  	}
   123  }
   124  
   125  func TestMagicActions(t *testing.T) {
   126  
   127  	wf := New()
   128  	ma := wf.MagicActions
   129  	ta := &testMA{}
   130  
   131  	ma.Register(ta)
   132  	// Incomplete keyword = search query
   133  	_, v := ma.handleArgs([]string{"workflow:tes"}, DefaultMagicPrefix)
   134  	if !v {
   135  		t.Errorf("Bad handled. Expected=%v, Got=%v", true, v)
   136  	}
   137  
   138  	if err := ta.ValidateShown(); err != nil {
   139  		t.Errorf("Bad MagicAction: %v", err)
   140  	}
   141  
   142  	// Test unregister
   143  	ma.Unregister(ta)
   144  
   145  	n := len(ma.actions)
   146  	x := 6
   147  	if n != x {
   148  		t.Errorf("Bad MagicActions length. Expected=%v, Got=%v", x, n)
   149  	}
   150  
   151  	// Register a new action
   152  	ta = &testMA{}
   153  	ma.Register(ta)
   154  
   155  	// Keyword of test MA
   156  	_, v = ma.handleArgs([]string{"workflow:test"}, DefaultMagicPrefix)
   157  	if !v {
   158  		t.Errorf("Bad handled. Expected=%v, Got=%v", true, v)
   159  	}
   160  
   161  	if err := ta.ValidateRun(); err != nil {
   162  		t.Errorf("Bad MagicAction: %v", err)
   163  	}
   164  }