github.com/naoina/kocha@v0.7.1-0.20171129072645-78c7a531f799/router_test.go (about)

     1  package kocha_test
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/naoina/kocha"
     9  )
    10  
    11  func TestRouter_Reverse(t *testing.T) {
    12  	app := kocha.NewTestApp()
    13  	for _, v := range []struct {
    14  		name   string
    15  		args   []interface{}
    16  		expect string
    17  	}{
    18  		{"root", []interface{}{}, "/"},
    19  		{"user", []interface{}{77}, "/user/77"},
    20  		{"date", []interface{}{2013, 10, 26, "naoina"}, "/2013/10/26/user/naoina"},
    21  		{"static", []interface{}{"/hoge.png"}, "/static/hoge.png"},
    22  		{"static", []interface{}{"hoge.png"}, "/static/hoge.png"},
    23  	} {
    24  		r, err := app.Router.Reverse(v.name, v.args...)
    25  		if err != nil {
    26  			t.Errorf(`Router.Reverse(%#v, %#v) => (_, %#v); want (_, %#v)`, v.name, v.args, err, err)
    27  			continue
    28  		}
    29  		actual := r
    30  		expect := v.expect
    31  		if !reflect.DeepEqual(actual, expect) {
    32  			t.Errorf(`Router.Reverse(%#v, %#v) => (%#v, %#v); want (%#v, %#v)`, v.name, v.args, actual, err, expect, err)
    33  		}
    34  	}
    35  }
    36  
    37  func TestRouter_Reverse_withUnknownRouteName(t *testing.T) {
    38  	app := kocha.NewTestApp()
    39  	name := "unknown"
    40  	_, err := app.Router.Reverse(name)
    41  	actual := err
    42  	expect := fmt.Errorf("kocha: no match route found: %s ()", name)
    43  	if !reflect.DeepEqual(actual, expect) {
    44  		t.Errorf("Router.Reverse(%#v) => (_, %#v); want (_, %#v)", name, actual, expect)
    45  	}
    46  }
    47  
    48  func TestRouter_Reverse_withFewArguments(t *testing.T) {
    49  	app := kocha.NewTestApp()
    50  	name := "user"
    51  	_, err := app.Router.Reverse(name)
    52  	actual := err
    53  	expect := fmt.Errorf("kocha: too few arguments: %s (controller is %T)", name, &kocha.FixtureUserTestCtrl{})
    54  	if !reflect.DeepEqual(actual, expect) {
    55  		t.Errorf(`Router.Reverse(%#v) => (_, %#v); want (_, %#v)`, name, actual, expect)
    56  	}
    57  }
    58  
    59  func TestRouter_Reverse_withManyArguments(t *testing.T) {
    60  	app := kocha.NewTestApp()
    61  	name := "user"
    62  	args := []interface{}{77, 100}
    63  	_, err := app.Router.Reverse(name, args...)
    64  	actual := err
    65  	expect := fmt.Errorf("kocha: too many arguments: %s (controller is %T)", name, &kocha.FixtureUserTestCtrl{})
    66  	if !reflect.DeepEqual(actual, expect) {
    67  		t.Errorf(`Router.Reverse(%#v, %#v) => (_, %#v); want (_, %#v)`, name, args, actual, expect)
    68  	}
    69  }