github.com/dahs81/otto@v0.2.1-0.20160126165905-6400716cf085/helper/router/router_test.go (about)

     1  package router
     2  
     3  import (
     4  	"flag"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/otto/ui"
     8  )
     9  
    10  func TestRouter_default(t *testing.T) {
    11  	var called bool
    12  	executeFunc := func(ctx Context) error {
    13  		called = true
    14  		return nil
    15  	}
    16  
    17  	r := &Router{
    18  		Actions: map[string]Action{
    19  			"": &SimpleAction{
    20  				ExecuteFunc: executeFunc,
    21  			},
    22  		},
    23  	}
    24  
    25  	err := r.Route(&stubContext{})
    26  	if err != nil {
    27  		t.Fatalf("err: %s", err)
    28  	}
    29  
    30  	if !called {
    31  		t.Fatal("should be called")
    32  	}
    33  }
    34  
    35  func TestRouter_specific(t *testing.T) {
    36  	var called bool
    37  	executeFunc := func(ctx Context) error {
    38  		called = true
    39  		return nil
    40  	}
    41  
    42  	r := &Router{
    43  		Actions: map[string]Action{
    44  			"foo": &SimpleAction{
    45  				ExecuteFunc: executeFunc,
    46  			},
    47  		},
    48  	}
    49  
    50  	err := r.Route(&stubContext{routeName: "foo"})
    51  	if err != nil {
    52  		t.Fatalf("err: %s", err)
    53  	}
    54  
    55  	if !called {
    56  		t.Fatal("should be called")
    57  	}
    58  }
    59  
    60  func TestRouter_helpErr(t *testing.T) {
    61  	var called bool
    62  	executeFunc := func(ctx Context) error {
    63  		called = true
    64  		return nil
    65  	}
    66  
    67  	r := &Router{
    68  		Actions: map[string]Action{
    69  			"help": &SimpleAction{
    70  				ExecuteFunc: executeFunc,
    71  			},
    72  
    73  			"foo": &SimpleAction{
    74  				ExecuteFunc: func(Context) error { return ErrHelp },
    75  			},
    76  		},
    77  	}
    78  
    79  	err := r.Route(&stubContext{routeName: "foo"})
    80  	if err != nil {
    81  		t.Fatalf("err: %s", err)
    82  	}
    83  
    84  	if !called {
    85  		t.Fatal("should be called")
    86  	}
    87  }
    88  
    89  func TestRouter_flagHelpErr(t *testing.T) {
    90  	var called bool
    91  	executeFunc := func(ctx Context) error {
    92  		called = true
    93  		return nil
    94  	}
    95  
    96  	flagExecuteFunc := func(ctx Context) error {
    97  		fs := flag.NewFlagSet("foo", flag.ContinueOnError)
    98  		return fs.Parse(ctx.RouteArgs())
    99  	}
   100  
   101  	r := &Router{
   102  		Actions: map[string]Action{
   103  			"help": &SimpleAction{
   104  				ExecuteFunc: executeFunc,
   105  			},
   106  
   107  			"foo": &SimpleAction{
   108  				ExecuteFunc: flagExecuteFunc,
   109  			},
   110  		},
   111  	}
   112  
   113  	err := r.Route(&stubContext{
   114  		routeName: "foo",
   115  		routeArgs: []string{"-help"},
   116  	})
   117  	if err != nil {
   118  		t.Fatalf("err: %s", err)
   119  	}
   120  
   121  	if !called {
   122  		t.Fatal("should be called")
   123  	}
   124  }
   125  
   126  func TestSimpleAction_impl(t *testing.T) {
   127  	var _ Action = new(SimpleAction)
   128  }
   129  
   130  type stubContext struct {
   131  	routeName string
   132  	routeArgs []string
   133  	ui        ui.Ui
   134  }
   135  
   136  func (mc *stubContext) RouteName() string {
   137  	return mc.routeName
   138  }
   139  
   140  func (mc *stubContext) RouteArgs() []string {
   141  	return mc.routeArgs
   142  }
   143  
   144  func (mc *stubContext) UI() ui.Ui {
   145  	return mc.ui
   146  }