File: mocking_test.go

package info (click to toggle)
snapd 2.71-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,536 kB
  • sloc: ansic: 16,114; sh: 16,105; python: 9,941; makefile: 1,890; exp: 190; awk: 40; xml: 22
file content (141 lines) | stat: -rw-r--r-- 2,988 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package testutil_test

import (
	"fmt"

	"github.com/snapcore/snapd/testutil"

	. "gopkg.in/check.v1"
)

func ExampleBackup_mockingSimple() {

	mockable := func() {
		fmt.Println("Original")
	}

	// Mock
	restore := testutil.Backup(&mockable)
	mockable = func() {
		fmt.Println("Mock")
	}

	// Restore
	restore()

	mockable()

	// Output: Original
}

func ExampleBackup_mockingMultiple() {
	mockableFunc := func() {
		fmt.Println("Original function")
	}
	mockableNumber := 17.53
	mockableString := "Original string"
	mockableStruct := struct {
		first  string
		second string
	}{
		first:  "Original",
		second: "struct",
	}

	// Mock
	restore := testutil.BackupMany(&mockableFunc, &mockableNumber, &mockableString, &mockableStruct)
	mockableFunc = func() {
		fmt.Println("Mock")
	}
	mockableNumber = 37
	mockableString = "Mock"
	mockableStruct.first, mockableStruct.second = "mocked", "value"

	// Restore
	restore()

	mockableFunc()
	fmt.Println(mockableNumber, mockableString, mockableStruct)

	// Output:
	// Original function
	// 17.53 Original string {Original struct}
}

var _ = Suite(&MockingTestSuite{})

type MockingTestSuite struct{}

func (s *MockingTestSuite) TestBackup(c *C) {
	mockableInt := 2
	mockableString := "foo"
	mockableFunc := func() string { return "original" }

	restore := testutil.Backup(&mockableInt)
	mockableInt = 42
	restore()
	c.Check(mockableInt, Equals, 2)

	restore = testutil.Backup(&mockableString)
	mockableString = "bar"
	restore()
	c.Check(mockableString, Equals, "foo")

	restore = testutil.Backup(&mockableFunc)
	mockableFunc = func() string { return "mock" }
	restore()
	c.Check(mockableFunc(), Equals, "original")
}

func (s *MockingTestSuite) TestBackupMany(c *C) {
	mockable1 := "foo"
	mockable2 := 2
	mockable3 := func() string { return "foo" }

	restore := testutil.BackupMany(&mockable1, &mockable2, &mockable3)
	mockable1 = "bar"
	mockable2 = 42
	mockable3 = func() string { return "bar" }

	restore()
	c.Check(mockable1, Equals, "foo")
	c.Check(mockable2, Equals, 2)
	c.Check(mockable3(), Equals, "foo")
}

func (s *MockingTestSuite) TestMock(c *C) {
	mockableInt := 2
	mockableString := "foo"
	mockableFunc := func() string { return "foo" }

	type Foo struct {
		Bar int
	}

	mockableStruct := Foo{Bar: 9}

	restore := testutil.Mock(&mockableInt, 5)
	c.Check(mockableInt, Equals, 5)
	restore()
	c.Check(mockableInt, Equals, 2)

	restore = testutil.Mock(&mockableString, "bar")
	c.Check(mockableString, Equals, "bar")
	restore()
	c.Check(mockableString, Equals, "foo")

	restore = testutil.Mock(&mockableFunc, func() string { return "bar" })
	c.Check(mockableFunc(), Equals, "bar")
	restore()
	c.Check(mockableFunc(), Equals, "foo")

	restore = testutil.Mock(&mockableStruct, Foo{Bar: 12})
	c.Check(mockableStruct, Equals, Foo{Bar: 12})
	restore()
	c.Check(mockableStruct, Equals, Foo{Bar: 9})

	// XXX does not build because the types don't match

	// restore = testutil.Mock(&mockableFunc, func() int { return 555 })
	// restore()
}