File: intercept_test.go

package info (click to toggle)
golang-github-revel-revel 0.12.0+dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 828 kB
  • sloc: xml: 7; makefile: 4
file content (81 lines) | stat: -rw-r--r-- 2,603 bytes parent folder | download | duplicates (2)
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
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 METHODS_N = []interface{}{
	InterceptController.methN,
	(*InterceptController).methP,
	InterceptControllerN.methNN,
	(*InterceptControllerN).methNP,
}

// Methods accessible from InterceptControllerP
var METHODS_P = []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), METHODS_N)
	testInterceptorController(t, reflect.ValueOf(&p), METHODS_P)
	testInterceptorController(t, reflect.ValueOf(&np), METHODS_N)
	testInterceptorController(t, reflect.ValueOf(&np), METHODS_P)
}

func testInterceptorController(t *testing.T, appControllerPtr reflect.Value, methods []interface{}) {
	interceptors = []*Interception{}
	InterceptFunc(funcP, BEFORE, appControllerPtr.Elem().Interface())
	InterceptFunc(funcP2, BEFORE, ALL_CONTROLLERS)
	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 (%s): Expected nil got %v", intc, val)
	}
}