forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 2
/
intercept_test.go
85 lines (72 loc) · 2.72 KB
/
intercept_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package revel
import (
"reflect"
"testing"
)
var funcP = func(c *Controller) Result { return nil }
var funcP2 = func(c *Controller) Result { return nil }
type InterceptController struct{ *Controller }
type InterceptControllerN struct{ InterceptController }
type InterceptControllerP struct{ *InterceptController }
type InterceptControllerNP struct {
*Controller
InterceptControllerN
InterceptControllerP
}
func (c InterceptController) methN() Result { return nil }
func (c *InterceptController) methP() Result { return nil }
func (c InterceptControllerN) methNN() Result { return nil }
func (c *InterceptControllerN) methNP() Result { return nil }
func (c InterceptControllerP) methPN() Result { return nil }
func (c *InterceptControllerP) methPP() Result { return nil }
// Methods accessible from InterceptControllerN
var MethodN = []interface{}{
InterceptController.methN,
(*InterceptController).methP,
InterceptControllerN.methNN,
(*InterceptControllerN).methNP,
}
// Methods accessible from InterceptControllerP
var MethodP = []interface{}{
InterceptController.methN,
(*InterceptController).methP,
InterceptControllerP.methPN,
(*InterceptControllerP).methPP,
}
// This checks that all the various kinds of interceptor functions/methods are
// properly invoked.
func TestInvokeArgType(t *testing.T) {
n := InterceptControllerN{InterceptController{&Controller{}}}
p := InterceptControllerP{&InterceptController{&Controller{}}}
np := InterceptControllerNP{&Controller{}, n, p}
testInterceptorController(t, reflect.ValueOf(&n), MethodN)
testInterceptorController(t, reflect.ValueOf(&p), MethodP)
testInterceptorController(t, reflect.ValueOf(&np), MethodN)
testInterceptorController(t, reflect.ValueOf(&np), MethodP)
}
func testInterceptorController(t *testing.T, appControllerPtr reflect.Value, methods []interface{}) {
interceptors = []*Interception{}
InterceptFunc(funcP, BEFORE, appControllerPtr.Elem().Interface())
InterceptFunc(funcP2, BEFORE, AllControllers)
for _, m := range methods {
InterceptMethod(m, BEFORE)
}
ints := getInterceptors(BEFORE, appControllerPtr)
if len(ints) != 6 {
t.Fatalf("N: Expected 6 interceptors, got %d.", len(ints))
}
testInterception(t, ints[0], reflect.ValueOf(&Controller{}))
testInterception(t, ints[1], reflect.ValueOf(&Controller{}))
for i := range methods {
testInterception(t, ints[i+2], appControllerPtr)
}
}
func testInterception(t *testing.T, intc *Interception, arg reflect.Value) {
val := intc.Invoke(arg)
if !val.IsNil() {
t.Errorf("Failed (%v): Expected nil got %v", intc, val)
}
}