Skip to content

Commit 17edb88

Browse files
committed
Some renaming occurred
1 parent 6230ab6 commit 17edb88

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

mock.lua

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
local Mock = {}
2+
3+
4+
15
local subscriber
26

37
local function unexpectedCall()
4-
error('unexpected mock function called')
8+
error('unexpected Mock function called')
59
end
610

711
local function mockHandle(callback, thunk)
@@ -122,7 +126,7 @@ end
122126

123127

124128

125-
local function createMockFunction()
129+
function Mock:newFunction()
126130
local f
127131

128132
function f(...)
@@ -132,7 +136,7 @@ local function createMockFunction()
132136
return f
133137
end
134138

135-
local function createMockMethod()
139+
function Mock:newMethod()
136140
local m
137141

138142
function m(...)
@@ -144,37 +148,30 @@ local function createMockMethod()
144148
return m
145149
end
146150

147-
local function createMockTable(t)
151+
function Mock:newTable(t)
148152
local mocked = {}
149153

150154
for k, v in pairs(t) do
151155
if type(v) == 'function' then
152-
mocked[k] = createMockFunction()
156+
mocked[k] = self:newFunction()
153157
end
154158
end
155159

156160
return mocked
157161
end
158162

159-
local function createMockObject(o)
163+
function Mock:newObject(o)
160164
local mocked = {}
161165

162166
for k, v in pairs(o) do
163167
if type(v) == 'function' then
164-
mocked[k] = createMockMethod()
168+
mocked[k] = self:newMethod()
165169
end
166170
end
167171

168172
return mocked
169173
end
170174

171-
mock = {
172-
createMockFunction = createMockFunction,
173-
createMockTable = createMockTable,
174-
createMockMethod = createMockMethod,
175-
createMockObject = createMockObject
176-
}
177-
178-
setmetatable(mock, {__call = function(_, ...) return MockExpectation:new(...) end})
175+
setmetatable(Mock, {__call = function(_, ...) return MockExpectation:new(...) end})
179176

180-
return mock
177+
return Mock

spec/mock_spec.lua

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ describe('The mock library', function()
88
end
99

1010
it('should allow you to verify that a function is called', function()
11-
local m = mock.createMockFunction()
11+
local m = mock:newFunction()
1212

1313
mock(m):shouldBeCalled():
1414
when(function() m() end)
1515
end)
1616

1717
it('should alert you when a function is not called', function()
18-
local m = mock.createMockFunction()
18+
local m = mock:newFunction()
1919

2020
shouldFail(function()
2121
mock(m):shouldBeCalled():
@@ -24,8 +24,8 @@ describe('The mock library', function()
2424
end)
2525

2626
it('should alert you when the wrong function is called', function()
27-
local m1 = mock.createMockFunction()
28-
local m2 = mock.createMockFunction()
27+
local m1 = mock:newFunction()
28+
local m2 = mock:newFunction()
2929

3030
shouldFail(function()
3131
mock(m1):shouldBeCalled():
@@ -34,22 +34,22 @@ describe('The mock library', function()
3434
end)
3535

3636
it('should alert you when a function is called unexpectedly', function()
37-
local m = mock.createMockFunction()
37+
local m = mock:newFunction()
3838

3939
shouldFail(function()
4040
m()
4141
end)
4242
end)
4343

4444
it('should allow you to verify that a function has been called with the correct arguments', function()
45-
local m = mock.createMockFunction()
45+
local m = mock:newFunction()
4646

4747
mock(m):shouldBeCalledWith(1, '2'):
4848
when(function() m(1, '2') end)
4949
end)
5050

5151
it('should alert you when a function has been called with incorrect arguments', function()
52-
local m = mock.createMockFunction()
52+
local m = mock:newFunction()
5353

5454
shouldFail(function()
5555
mock(m):shouldBeCalledWith(1, '2'):
@@ -58,7 +58,7 @@ describe('The mock library', function()
5858
end)
5959

6060
it('should allow you to specify the return value of a mocked function', function()
61-
local m = mock.createMockFunction()
61+
local m = mock:newFunction()
6262

6363
mock(m):shouldBeCalled():andWillReturn(4):
6464
when(function()
@@ -67,7 +67,7 @@ describe('The mock library', function()
6767
end)
6868

6969
it('should allow you to specify multiple return values for a mocked function', function()
70-
local m = mock.createMockFunction()
70+
local m = mock:newFunction()
7171

7272
mock(m):shouldBeCalled():andWillReturn(1, 2):
7373
when(function()
@@ -78,7 +78,7 @@ describe('The mock library', function()
7878
end)
7979

8080
it('should allow you to check that a function has been called multiple times', function()
81-
local m = mock.createMockFunction()
81+
local m = mock:newFunction()
8282

8383
mock(m):shouldBeCalled():
8484
andAlso(mock(m):shouldBeCalledWith(1, 2, 3)):
@@ -89,8 +89,8 @@ describe('The mock library', function()
8989
end)
9090

9191
it('should allow you to check that multiple functions are called', function()
92-
local m1 = mock.createMockFunction()
93-
local m2 = mock.createMockFunction()
92+
local m1 = mock:newFunction()
93+
local m2 = mock:newFunction()
9494

9595
mock(m1):shouldBeCalled():
9696
andAlso(mock(m2):shouldBeCalledWith(1, 2, 3)):
@@ -101,8 +101,8 @@ describe('The mock library', function()
101101
end)
102102

103103
it('should allow you to mix and match call types', function()
104-
local m1 = mock.createMockFunction()
105-
local m2 = mock.createMockFunction()
104+
local m1 = mock:newFunction()
105+
local m2 = mock:newFunction()
106106

107107
mock(m1):shouldBeCalled():
108108
andAlso(mock(m2):shouldBeCalledWith(1, 2, 3)):
@@ -115,8 +115,8 @@ describe('The mock library', function()
115115
end)
116116

117117
it('should allow functions to be used to improve readability', function()
118-
local m1 = mock.createMockFunction()
119-
local m2 = mock.createMockFunction()
118+
local m1 = mock:newFunction()
119+
local m2 = mock:newFunction()
120120

121121
function somethingShouldHappen()
122122
return mock(m1):shouldBeCalled()
@@ -142,7 +142,7 @@ describe('The mock library', function()
142142
bar = function() end
143143
}
144144

145-
mockedTable = mock.createMockTable(someTable)
145+
mockedTable = mock:newTable(someTable)
146146

147147
mock(mockedTable.foo):shouldBeCalledWith(1):andWillReturn(2):
148148
andAlso(mock(mockedTable.bar):shouldBeCalled()):
@@ -158,7 +158,7 @@ describe('The mock library', function()
158158
foo = function() end
159159
}
160160

161-
mockedTable = mock.createMockTable(someTable)
161+
mockedTable = mock:newTable(someTable)
162162

163163
mock(mockedTable.foo):shouldBeCalledWith(1):andWillReturn(2):
164164
when(function()
@@ -173,7 +173,7 @@ describe('The mock library', function()
173173
function someObject:foo() end
174174
function someObject:bar() end
175175

176-
mockedObject = mock.createMockObject(someObject)
176+
mockedObject = mock:newObject(someObject)
177177

178178
mock(mockedObject.foo):shouldBeCalledWith(1):andWillReturn(2):
179179
andAlso(mock(mockedObject.bar):shouldBeCalled()):
@@ -189,7 +189,7 @@ describe('The mock library', function()
189189

190190
function someObject:foo() end
191191

192-
mockedObject = mock.createMockObject(someObject)
192+
mockedObject = mock:newObject(someObject)
193193

194194
mock(mockedObject.foo):shouldBeCalledWith(1):andWillReturn(2):
195195
when(function()

0 commit comments

Comments
 (0)