This file is indexed.

/usr/share/lua/5.1/lemock.lua is in lua-lemock 0.6-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
------ THIS FILE IS TANGLED FROM LITERATE SOURCE FILES ------
-- Copyright (C) 2009 Tommy Pettersson <ptp@lysator.liu.se>
-- See terms in file COPYRIGHT, or at http://lemock.luaforge.net
module( 'lemock', package.seeall )
_VERSION   = "LeMock 0.6"
_COPYRIGHT = "Copyright (C) 2009 Tommy Pettersson <ptp@lysator.liu.se>"
local class, object, qtostring, sfmt, add_to_set
local elements_of_set, value_equal
function object (class)
	return setmetatable( {}, class )
end
function class (parent)
	local c = object(parent)
	c.__index = c
	return c
end
sfmt = string.format
function qtostring (v)
	if type(v) == 'string' then
		return sfmt( '%q', v )
	else
		return tostring( v )
	end
end
function add_to_set (o, setname, element)
	if not o[setname] then
		o[setname] = {}
	end
	local l = o[setname]
	for i = 1, #l do
		if l[i] == element then return end
	end
	l[#l+1] = element
end
function elements_of_set (o, setname)
	local l = o[setname]
	local i = l and #l+1 or 0
	return function ()
		i = i - 1
		if i > 0 then return l[i] end
	end
end
function value_equal (a, b)
	if a == b then return true end
	if a ~= a and b ~= b then return true end -- NaN == NaN
	return false
end
local mock_controller_map = setmetatable( {}, {__mode='k'} )
-- All the classes are private
local Action, Argv, Callable, Controller, Mock
Action = {}
-- abstract
Action.generic = class()
function Action.generic:add_close (label)
	add_to_set( self, 'closelist', label )
end
function Action.generic:add_depend (d)
	add_to_set( self, 'dependlist', d )
end
function Action.generic:add_label (label)
	add_to_set( self, 'labellist', label )
end
function Action.generic:assert_satisfied ()
	assert( self.replay_count <= self.max_replays, "lemock internal error" )
	if not (
self.min_replays <= self.replay_count
                                  ) then
		error( sfmt( "Wrong replay count %d (expected %d..%d) for %s"
		             , self.replay_count
		             , self.min_replays, self.max_replays
		             , self:tostring()
		       )
		       , 0
		)
	end
end
function Action.generic:blocks ()
	if self:is_satisfied() then
		return function () end
	end
	return elements_of_set( self, 'labellist' )
end
function Action.generic:closes ()
	return elements_of_set( self, 'closelist' )
end
function Action.generic:depends ()
	return elements_of_set( self, 'dependlist' )
end
function Action.generic:has_label (l)
	for x in elements_of_set( self, 'labellist' ) do
		if x == l then return true end
	end
	return false
end
function Action.generic:is_expected ()
	return self.replay_count < self.max_replays
	   and not self.is_blocked
	   and not self.is_closed
end
function Action.generic:is_satisfied ()
	return 
self.min_replays <= self.replay_count
end
function Action.generic:match (key)
	if getmetatable(self) ~= getmetatable(key)  then return false end
	if self.mock ~= key.mock                    then return false end
	return self:is_expected()
end
function Action.generic:new (mock)
	local a = object( self )
	a.mock         = mock
	a.replay_count = 0
	a.min_replays  = 1
	a.max_replays  = 1
	return a
end
function Action.generic:set_times (a, b)
	min = a or 1
	max = b or min
	min, max = tonumber(min), tonumber(max)
	if (not min) or (not max) or (min >= math.huge)
	             or (min ~= min) or (max ~= max) -- NaN
	             or (min < 0) or (max <= 0) or (min > max) then
		error( sfmt( "Unrealistic time arguments (%s, %s)"
		           , qtostring( min )
		           , qtostring( max )
		           )
		     , 0
		     )
	end
	self.min_replays = min
	self.max_replays = max
end
Action.generic_call = class( Action.generic )
Action.generic_call.can_return = true
function Action.generic_call:get_returnvalue ()
	if self.has_returnvalue then
		return self.returnvalue:unpack()
	end
end
function Action.generic_call:set_returnvalue (...)
	self.returnvalue = Argv:new(...)
	self.has_returnvalue = true
end
function Action.generic_call:match (q)
	if not Action.generic.match( self, q )  then return false end
	if not self.argv:equal( q.argv )        then return false end
	return true
end
function Action.generic_call:new (m, ...)
	local a = Action.generic.new( self, m )
	a.argv   = Argv:new(...)
	return a
end
-- concrete
Action.call = class( Action.generic_call )
function Action.call:match (q)
	if not Action.generic_call.match( self, q )  then return false end
	if self.key ~= q.key                         then return false end
	return true
end
function Action.call:new (m, key, ...)
	local a = Action.generic_call.new( self, m, ... )
	a.key = key
	return a
end
function Action.call:tostring ()
	if self.has_returnvalue then
		return sfmt( "call %s(%s) => %s"
		             , tostring(self.key)
		             , self.argv:tostring()
		             , self.returnvalue:tostring()
		       )
	else
		return sfmt( "call %s(%s)"
		             , tostring(self.key)
		             , self.argv:tostring()
		       )
	end
end
Action.index = class( Action.generic )
Action.index.can_return = true
function Action.index:get_returnvalue ()
	return self.returnvalue
end
function Action.index:set_returnvalue (v)
	self.returnvalue = v
	self.has_returnvalue = true
end
function Action.index:match (q)
	if not Action.generic.match( self, q )  then return false end
	if self.key ~= q.key                    then return false end
	return true
end
function Action.index:new (m, key)
	local a = Action.generic.new( self, m )
	a.key = key
	return a
end
function Action.index:tostring ()
	local key = 'index '..tostring( self.key )
	if self.has_returnvalue then
		return sfmt( "index %s => %s"
		             , tostring( self.key )
		             , qtostring( self.returnvalue )
		       )
	elseif self.is_callable then
		return sfmt( "index %s()"
		             , tostring( self.key )
		       )
	else
		return sfmt( "index %s"
		             , tostring( self.key )
		       )
	end
end
Action.newindex = class( Action.generic )
function Action.newindex:match (q)
	if not Action.generic.match( self, q )  then return false end
	if self.key ~= q.key                    then return false end
	if not value_equal( self.val, q.val )
	   and self.val ~= Argv.ANYARG
	   and q.val    ~= Argv.ANYARG          then return false end
	return true
end
function Action.newindex:new (m, key, val)
	local a = Action.generic.new( self, m )
	a.key    = key
	a.val    = val
	return a
end
function Action.newindex:tostring ()
	return sfmt( "newindex %s = %s"
	             , tostring(self.key)
	             , qtostring(self.val)
	       )
end
Action.selfcall = class( Action.generic_call )
function Action.selfcall:match (q)
	return Action.generic_call.match( self, q )
end
function Action.selfcall:new (m, ...)
	local a = Action.generic_call.new( self, m, ... )
	return a
end
function Action.selfcall:tostring ()
	if self.has_returnvalue then
		return sfmt( "selfcall (%s) => %s"
		             , self.argv:tostring()
		             , self.returnvalue:tostring()
		       )
	else
		return sfmt( "selfcall (%s)"
		             , self.argv:tostring()
		       )
	end
end
Argv = class()
Argv.ANYARGS = newproxy()  local ANYARGS = Argv.ANYARGS
Argv.ANYARG  = newproxy()  local ANYARG  = Argv.ANYARG
function Argv:equal (other)
	local a1, n1 = self.v,  self.len
	local a2, n2 = other.v, other.len
	if n1-1 <= n2 and a1[n1] == ANYARGS then
		n1 = n1-1
		n2 = n1
	elseif n2-1 <= n1 and a2[n2] == ANYARGS then
		n2 = n2-1
		n1 = n2
	end
	if n1 ~= n2 then
		return false
	end
	for i = 1, n1 do
		local v1, v2 = a1[i], a2[i]
		if not value_equal(v1,v2) and v1 ~= ANYARG and v2 ~= ANYARG then
			return false
		end
	end
	return true
end
function Argv:new (...)
	local av = object( self )
	av.v = {...}
	av.len = select('#',...)
	for i = 1, av.len - 1 do
		if av.v[i] == Argv.ANYARGS then
			error( "ANYARGS not at end.", 0 )
		end
	end
	return av
end
function Argv:tostring ()
	local res = {}
	local function w (v)
		res[#res+1] = qtostring( v )
	end
	local av, ac = self.v, self.len
	for i = 1, ac do
		if av[i] == Argv.ANYARG then
			res[#res+1] = 'ANYARG'
		elseif av[i] == Argv.ANYARGS then
			res[#res+1] = 'ANYARGS'
		else
			w( av[i] )
		end
		if i < ac then
			res[#res+1] = ',' -- can not use qtostring in w()
		end
	end
	return table.concat( res )
end
function Argv:unpack ()
	return unpack( self.v, 1, self.len )
end
Callable = {}
Callable.generic = class()
Callable.record  = class( Callable.generic )
Callable.replay  = class( Callable.generic )
function Callable.generic:new ( index_action )
	local f = object( self )
	f.action = index_action
	return f
end
function Callable.record:__call (...)
	local index_action = self.action
	local m = index_action.mock
	local mc = mock_controller_map[m]
	assert( mc.is_recording, "client uses cached callable from recording" )
	mc:make_callable( index_action )
	mc:add_action( Action.call:new( m, index_action.key, ... ))
end
function Callable.replay:__call (...)
	local index_action = self.action
	local m = index_action.mock
	local mc = mock_controller_map[m]
	local call_action = mc:lookup( Action.call:new( m, index_action.key, ... ))
	mc:replay_action( call_action )
	if call_action.throws_error then
		error( call_action.errorvalue, 2 )
	end
	return call_action:get_returnvalue()
end
Controller = class()
-- Exported methods
function Controller:close (...)
	if not self.is_recording then
		error( "Can not insert close in replay mode.", 2 )
	end
	local action = self:get_last_action()
	for _, close in ipairs{ ... } do
		action:add_close( close )
	end
	return self -- for chaining
end
function Controller:depend (...)
	if not self.is_recording then
		error( "Can not add dependency in replay mode.", 2 )
	end
	local action = self:get_last_action()
	for _, dependency in ipairs{ ... } do
		action:add_depend( dependency )
	end
	return self -- for chaining
end
function Controller:error (value)
	if not self.is_recording then
		error( "Error called during replay.", 2 )
	end
	local action = self:get_last_action()
	if action.has_returnvalue or action.throws_error then
		error( "Returns and/or Error called twice for same action.", 2 )
	end
	action.throws_error = true
	action.errorvalue = value
	return self -- for chaining
end
function Controller:label (...)
if not self.is_recording then
	error( "Can not add labels in replay mode.", 2 )
end
local action = self:get_last_action()
for _, label in ipairs{ ... } do
	action:add_label( label )
end
return self -- for chaining
end
function Controller:mock ()
	if not self.is_recording then
		error( "New mock during replay.", 2 )
	end
	local m = object( Mock.record )
	mock_controller_map[m] = self
	return m
end
function Controller:new ()
	local mc = object( self )
	mc.actionlist   = {}
	mc.is_recording = true
	return mc
end
function Controller:replay ()
	if not self.is_recording then
		error( "Replay called twice.", 2 )
	end
	self.is_recording = false
	for m, mc in pairs( mock_controller_map ) do
		if mc == self then
			setmetatable( m, Mock.replay )
		end
	end
	self:update_dependencies()
	self:assert_no_dependency_cycles()
end
function Controller:returns (...)
	if not self.is_recording then
		error( "Returns called during replay.", 2 )
	end
	local action = self:get_last_action()
	assert( not action.is_callable, "lemock internal error" )
	if not action.can_return then
		error( "Previous action can not return anything.", 2 )
	end
	if action.has_returnvalue or action.throws_error then
		error( "Returns and/or Error called twice for same action.", 2 )
	end
	action:set_returnvalue(...)
	return self -- for chaining
end
function Controller:times (min, max)
	if not self.is_recording then
		error( "Can not set times in replay mode.", 0 )
	end
	self:get_last_action():set_times( min, max )
	return self -- for chaining
end
-- convenience functions
function Controller:anytimes()    return self:times( 0, math.huge ) end
function Controller:atleastonce() return self:times( 1, math.huge ) end
function Controller:verify ()
	if self.is_recording then
		error( "Verify called during record.", 2 )
	end
	for a in self:actions() do
		a:assert_satisfied()
	end
end
-- Protected methods
function Controller:actions (q)
	local l = self.actionlist
	local i = 0
	return function ()
		i = i + 1
		return l[i]
	end				
end
function Controller:add_action (a)
	assert( a ~= nil, "lemock internal error" ) -- breaks array property
	table.insert( self.actionlist, a )
end
function Controller:assert_no_dependency_cycles ()
	local function is_in_path (label, path)
		if not path then return false end -- is root
		for _, l in ipairs( path ) do
			if l == label then return true end
		end
		if path.prev then return is_in_path( label, path.prev ) end
		return false
	end
	local function can_block (action, node)
		for _, label in ipairs( node ) do
			if action:has_label( label ) then return true end
		end
		return false
	end
	local function step (action, path)
		local new_head
		for label in action:depends() do
			if is_in_path( label, path ) then
				error( "Detected dependency cycle", 0 )
			end
			-- only create table if needed to reduce garbage
			if not new_head then new_head = { prev=path } end
			new_head[#new_head+1] = label
		end
		return new_head
	end
	local function search_depth_first (path)
		for action in self:actions() do
			if can_block( action, path ) then
				local new_head = step( action, path )
				if new_head then
					search_depth_first( new_head )
				end
			end
		end
	end
	for action in self:actions() do
		local root = step( action, nil )
		if root then search_depth_first( root ) end
	end
end
function Controller:close_actions( ... ) -- takes iterator
	for label in ... do
		for candidate in self:actions() do
			if candidate:has_label( label ) then
				if not candidate:is_satisfied() then
					error( "Closes unsatisfied action: "..candidate:tostring(), 0 )
				end
				candidate.is_closed = true
			end
		end
	end
end
function Controller:get_last_action ()
	local l = self.actionlist
	if #l == 0 then
		error( "No action is recorded yet.", 0 )
	end
	return l[#l]
end
function Controller:lookup (actual)
	for action in self:actions() do
		if action:match( actual ) then
			return action
		end
	end
local expected = {}
for _, a in ipairs( self.actionlist ) do
	if a:is_expected() and not a.is_callable then
		expected[#expected+1] = a:tostring()
	end
end
table.sort( expected )
if #expected == 0 then
	expected[1] = "(Nothing)"
end
	error( sfmt( "Unexpected action %s, expected:\n%s\n"
	             , actual:tostring()
	             , table.concat(expected,'\n')
	       )
	       , 0
	)
end
function Controller:make_callable (action)
	if action.has_returnvalue then
		error( "Can not call "..action.key..". It has a returnvalue.", 0 )
	end
	action.is_callable = true
	action.min_replays = 0
	action.max_replays = math.huge
end
function Controller:new ()
	local mc = object( self )
	mc.actionlist   = {}
	mc.is_recording = true
	return mc
end
function Controller:replay_action ( action )
	assert( action:is_expected(), "lemock internal error" )
	assert( action.replay_count < action.max_replays, "lemock internal error" )
	local was_satisfied = action:is_satisfied()
	action.replay_count = action.replay_count + 1
	if not was_satisfied and action.labellist and action:is_satisfied() then
		self:update_dependencies()
	end
	if action.closelist then
		self:close_actions( action:closes() )
	end
end
function Controller:update_dependencies ()
	local blocked = {}
	for action in self:actions() do
		for label in action:blocks() do
			blocked[label] = true
		end
	end
	local function is_blocked (action)
		for label in action:depends() do
			if blocked[label] then return true end
		end
		return false
	end
	for action in self:actions() do
		action.is_blocked = is_blocked( action )
	end
end
Mock = { record={}, replay={} } -- no self-referencing __index!
function Mock.record:__index (key)
	local mc = mock_controller_map[self]
	local action = Action.index:new( self, key )
	mc:add_action( action )
	return Callable.record:new( action )
end
function Mock.record:__newindex (key, val)
	local mc = mock_controller_map[self]
	mc:add_action( Action.newindex:new( self, key, val ))
end
function Mock.record:__call (...)
	local mc = mock_controller_map[self]
	mc:add_action( Action.selfcall:new( self, ... ))
end
function Mock.replay:__index (key)
	local mc = mock_controller_map[self]
	local index_action = mc:lookup( Action.index:new( self, key ))
	mc:replay_action( index_action )
	if index_action.throws_error then
		error( index_action.errorvalue, 2 )
	end
	if index_action.is_callable then
		return Callable.replay:new( index_action )
	else
		return index_action:get_returnvalue()
	end
end
function Mock.replay:__newindex (key, val)
	local mc = mock_controller_map[self]
	local newindex_action = mc:lookup( Action.newindex:new( self, key, val ))
	mc:replay_action( newindex_action )
	if newindex_action.throws_error then
		error( newindex_action.errorvalue, 2 )
	end
end
function Mock.replay:__call (...)
	local mc = mock_controller_map[self]
	local selfcall_action = mc:lookup( Action.selfcall:new( self, ... ))
	mc:replay_action( selfcall_action )
	if selfcall_action.throws_error then
		error( selfcall_action.errorvalue, 2 )
	end
	return selfcall_action:get_returnvalue()
end
function controller ()
	local exported_methods = {
		'anytimes',
		'atleastonce',
		'close',
		'depend',
		'error',
		'label',
		'mock',
		'new',
		'replay',
		'returns',
		'times',
		'verify',
	}
	local mc = Controller:new()
	local wrapper = {}
	for _, method in ipairs( exported_methods ) do
		wrapper[ method ] = function (self, ...)
			return mc[ method ]( mc, ... )
		end
	end
	wrapper.ANYARG  = Argv.ANYARG
	wrapper.ANYARGS = Argv.ANYARGS
	return wrapper
end
return _M