Add API Framework Revel Source Files
[iec.git] / src / foundation / api / revel / invoker_test.go
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.
4
5 package revel
6
7 import (
8         "net/url"
9         "reflect"
10         "testing"
11 )
12
13 // These tests verify that Controllers are initialized properly, given the range
14 // of embedding possibilities..
15
16 type P struct{ *Controller }
17
18 type PN struct{ P }
19
20 type PNN struct{ PN }
21
22 // Embedded via two paths
23 type P2 struct{ *Controller }
24 type PP2 struct {
25         *Controller // Need to embed this explicitly to avoid duplicate selector.
26         P
27         P2
28         PNN
29 }
30
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)
37
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}})
43 }
44
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)
49         }
50 }
51
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"}})
57
58         // Test that all *revel.Controllers are initialized.
59         c := &Controller{Name: "Test"}
60         if err := c.SetAction("P", "Method"); err != nil {
61                 t.Error(err)
62         } else if c.AppController.(*P).Controller != c {
63                 t.Errorf("P not initialized")
64         }
65
66         if err := c.SetAction("PNN", "Method"); err != nil {
67                 t.Error(err)
68         } else if c.AppController.(*PNN).Controller != c {
69                 t.Errorf("PNN not initialized")
70         }
71
72         // PP2 has 4 different slots for *Controller.
73         if err := c.SetAction("PP2", "Method"); err != nil {
74                 t.Error(err)
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")
80         }
81 }
82
83 func BenchmarkSetAction(b *testing.B) {
84         type Mixin1 struct {
85                 *Controller
86                 x, y int
87                 foo  string
88         }
89         type Mixin2 struct {
90                 *Controller
91                 a, b float64
92                 bar  string
93         }
94
95         type Benchmark struct {
96                 *Controller
97                 Mixin1
98                 Mixin2
99                 user interface{}
100                 guy  string
101         }
102
103         RegisterController((*Mixin1)(nil), []*MethodType{{Name: "Method"}})
104         RegisterController((*Mixin2)(nil), []*MethodType{{Name: "Method"}})
105         RegisterController((*Benchmark)(nil), []*MethodType{{Name: "Method"}})
106         c := Controller{
107                 ViewArgs: make(map[string]interface{}),
108         }
109
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)
113                         return
114                 }
115         }
116 }
117
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)
124                 return
125         }
126
127         c.Params = &Params{Values: make(url.Values)}
128         c.Params.Set("id", "3")
129
130         b.ResetTimer()
131         for i := 0; i < b.N; i++ {
132                 ActionInvoker(c, nil)
133         }
134 }