1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
13 // These tests verify that Controllers are initialized properly, given the range
14 // of embedding possibilities..
16 type P struct{ *Controller }
22 // Embedded via two paths
23 type P2 struct{ *Controller }
25 *Controller // Need to embed this explicitly to avoid duplicate selector.
31 func TestFindControllers(t *testing.T) {
32 controllers = make(map[string]*ControllerType)
33 RegisterController((*P)(nil), nil)
34 RegisterController((*PN)(nil), nil)
35 RegisterController((*PNN)(nil), nil)
36 RegisterController((*PP2)(nil), nil)
38 // Test construction of indexes to each *Controller
39 checkSearchResults(t, P{}, [][]int{{0}})
40 checkSearchResults(t, PN{}, [][]int{{0, 0}})
41 checkSearchResults(t, PNN{}, [][]int{{0, 0, 0}})
42 checkSearchResults(t, PP2{}, [][]int{{0}, {1, 0}, {2, 0}, {3, 0, 0, 0}})
45 func checkSearchResults(t *testing.T, obj interface{}, expected [][]int) {
46 actual := findControllers(reflect.TypeOf(obj))
47 if !reflect.DeepEqual(expected, actual) {
48 t.Errorf("Indexes do not match. expected %v actual %v", expected, actual)
52 func TestSetAction(t *testing.T) {
53 controllers = make(map[string]*ControllerType)
54 RegisterController((*P)(nil), []*MethodType{{Name: "Method"}})
55 RegisterController((*PNN)(nil), []*MethodType{{Name: "Method"}})
56 RegisterController((*PP2)(nil), []*MethodType{{Name: "Method"}})
58 // Test that all *revel.Controllers are initialized.
59 c := &Controller{Name: "Test"}
60 if err := c.SetAction("P", "Method"); err != nil {
62 } else if c.AppController.(*P).Controller != c {
63 t.Errorf("P not initialized")
66 if err := c.SetAction("PNN", "Method"); err != nil {
68 } else if c.AppController.(*PNN).Controller != c {
69 t.Errorf("PNN not initialized")
72 // PP2 has 4 different slots for *Controller.
73 if err := c.SetAction("PP2", "Method"); err != nil {
75 } else if pp2 := c.AppController.(*PP2); pp2.Controller != c ||
76 pp2.P.Controller != c ||
77 pp2.P2.Controller != c ||
78 pp2.PNN.Controller != c {
79 t.Errorf("PP2 not initialized")
83 func BenchmarkSetAction(b *testing.B) {
95 type Benchmark struct {
103 RegisterController((*Mixin1)(nil), []*MethodType{{Name: "Method"}})
104 RegisterController((*Mixin2)(nil), []*MethodType{{Name: "Method"}})
105 RegisterController((*Benchmark)(nil), []*MethodType{{Name: "Method"}})
107 ViewArgs: make(map[string]interface{}),
110 for i := 0; i < b.N; i++ {
111 if err := c.SetAction("Benchmark", "Method"); err != nil {
112 b.Errorf("Failed to set action: %s", err)
118 func BenchmarkInvoker(b *testing.B) {
119 startFakeBookingApp()
120 c := NewTestController(nil, showRequest)
121 c.ViewArgs = make(map[string]interface{})
122 if err := c.SetAction("Hotels", "Show"); err != nil {
123 b.Errorf("Failed to set action: %s", err)
127 c.Params = &Params{Values: make(url.Values)}
128 c.Params.Set("id", "3")
131 for i := 0; i < b.N; i++ {
132 ActionInvoker(c, nil)