1--[[
2	Tests for the libraryUtil module
3
4	@license GNU GPL v2+
5	@author Mr. Stradivarius < misterstrad@gmail.com >
6]]
7
8local testframework = require 'Module:TestFramework'
9
10local util = require( 'libraryUtil' )
11local checkType = util.checkType
12local checkTypeMulti = util.checkTypeMulti
13local checkTypeForIndex = util.checkTypeForIndex
14local checkTypeForNamedArg = util.checkTypeForNamedArg
15local makeCheckSelfFunction = util.makeCheckSelfFunction
16
17local function testExpectTypes( arg, expectTypes )
18	pcall( checkTypeMulti, 'myFunc', 1, arg, expectTypes )
19	return unpack( expectTypes )
20end
21
22local function testCheckSelf( self, method, ... )
23	local checkSelf = makeCheckSelfFunction( ... )
24	return checkSelf( self, method )
25end
26
27local testObject = {}
28
29-- Tests
30local tests = {
31	-- checkType
32	{ name = 'checkType, valid', func = checkType, type='ToString',
33		args = { 'myFunc', 1, 'foo', 'string' },
34		expect = { nil }
35	},
36	{ name = 'checkType, invalid', func = checkType, type='ToString',
37		args = { 'myFunc', 1, 9, 'string' },
38		expect = "bad argument #1 to 'myFunc' (string expected, got number)"
39	},
40	{ name = 'checkType, nil valid', func = checkType, type='ToString',
41		args = { 'myFunc', 1, nil, 'string', true },
42		expect = { nil }
43	},
44	{ name = 'checkType, nil invalid', func = checkType, type='ToString',
45		args = { 'myFunc', 1, nil, 'string', false },
46		expect = "bad argument #1 to 'myFunc' (string expected, got nil)"
47	},
48	{ name = 'checkType, boolean', func = checkType, type='ToString',
49		args = { 'myFunc', 1, true, 'boolean' },
50		expect = { nil }
51	},
52	{ name = 'checkType, table', func = checkType, type='ToString',
53		args = { 'myFunc', 1, {}, 'table' },
54		expect = { nil }
55	},
56	{ name = 'checkType, function', func = checkType, type='ToString',
57		args = { 'myFunc', 1, function () return end, 'function' },
58		expect = { nil }
59	},
60	{ name = 'checkType, argument #2', func = checkType, type='ToString',
61		args = { 'myFunc', 2, 9, 'string' },
62		expect = "bad argument #2 to 'myFunc' (string expected, got number)"
63	},
64	{ name = 'checkType, name', func = checkType, type='ToString',
65		args = { 'otherFunc', 1, 9, 'string' },
66		expect = "bad argument #1 to 'otherFunc' (string expected, got number)"
67	},
68
69	-- checkTypeMulti
70	{ name = 'checkTypeMulti, single valid', func = checkTypeMulti, type='ToString',
71		args = { 'myFunc', 1, 'foo', { 'string' } },
72		expect = { nil }
73	},
74	{ name = 'checkTypeMulti, single type invalid (1)', func = checkTypeMulti, type='ToString',
75		args = { 'myFunc', 1, 9, { 'string' } },
76		expect = "bad argument #1 to 'myFunc' (string expected, got number)"
77	},
78	{ name = 'checkTypeMulti, single type invalid (2)', func = checkTypeMulti, type='ToString',
79		args = { 'myFunc', 1, nil, { 'string' } },
80		expect = "bad argument #1 to 'myFunc' (string expected, got nil)"
81	},
82	{ name = 'checkTypeMulti, multiple types valid (1)', func = checkTypeMulti, type='ToString',
83		args = { 'myFunc', 1, 'foo', { 'string', 'number', 'table' } },
84		expect = { nil }
85	},
86	{ name = 'checkTypeMulti, multiple types valid (2)', func = checkTypeMulti, type='ToString',
87		args = { 'myFunc', 1, 9, { 'string', 'number', 'table' } },
88		expect = { nil }
89	},
90	{ name = 'checkTypeMulti, multiple types valid (3)', func = checkTypeMulti, type='ToString',
91		args = { 'myFunc', 1, {}, { 'string', 'number', 'table' } },
92		expect = { nil }
93	},
94	{ name = 'checkTypeMulti, multiple types invalid (1)', func = checkTypeMulti, type='ToString',
95		args = { 'myFunc', 1, true, { 'string', 'number', 'table' } },
96		expect = "bad argument #1 to 'myFunc' (string, number or table expected, got boolean)"
97	},
98	{ name = 'checkTypeMulti, multiple types invalid (2)', func = checkTypeMulti, type='ToString',
99		args = { 'myFunc', 1, nil, { 'string', 'number', 'table' } },
100		expect = "bad argument #1 to 'myFunc' (string, number or table expected, got nil)"
101	},
102	{ name = 'checkTypeMulti, multiple types invalid (3)', func = checkTypeMulti, type='ToString',
103		args = { 'myFunc', 1, function () return end, { 'string', 'number', 'table' } },
104		expect = "bad argument #1 to 'myFunc' (string, number or table expected, got function)"
105	},
106	{ name = 'checkTypeMulti, two types invalid', func = checkTypeMulti, type='ToString',
107		args = { 'myFunc', 1, {}, { 'string', 'number' } },
108		expect = "bad argument #1 to 'myFunc' (string or number expected, got table)"
109	},
110	{ name = 'checkTypeMulti, type order', func = checkTypeMulti, type='ToString',
111		args = { 'myFunc', 1, true, { 'table', 'number', 'string' } },
112		expect = "bad argument #1 to 'myFunc' (table, number or string expected, got boolean)"
113	},
114	{ name = 'checkTypeMulti, argument #2', func = checkTypeMulti, type='ToString',
115		args = { 'myFunc', 2, 9, { 'string' } },
116		expect = "bad argument #2 to 'myFunc' (string expected, got number)"
117	},
118	{ name = 'checkTypeMulti, other name', func = checkTypeMulti, type='ToString',
119		args = { 'otherFunc', 1, 9, { 'string' } },
120		expect = "bad argument #1 to 'otherFunc' (string expected, got number)"
121	},
122	{ name = 'checkTypeMulti, expectTypes not altered (1)', func = testExpectTypes, type='ToString',
123		args = { 'foo', { 'string', 'number', 'table' } },
124		expect = { 'string', 'number', 'table' }
125	},
126	{ name = 'checkTypeMulti, expectTypes not altered (2)', func = testExpectTypes, type='ToString',
127		args = { true, { 'string', 'number', 'table' } },
128		expect = { 'string', 'number', 'table' }
129	},
130	{ name = 'checkTypeMulti, expectTypes not altered (3)', func = testExpectTypes, type='ToString',
131		args = { 'foo', { 'string' } },
132		expect = { 'string' }
133	},
134	{ name = 'checkTypeMulti, expectTypes not altered (4)', func = testExpectTypes, type='ToString',
135		args = { true, { 'string' } },
136		expect = { 'string' }
137	},
138
139	-- checkTypeForIndex
140	{ name = 'checkTypeForIndex, valid', func = checkTypeForIndex, type='ToString',
141		args = { 'foo', 'bar', 'string' },
142		expect = { nil }
143	},
144	{ name = 'checkTypeForIndex, invalid (1)', func = checkTypeForIndex, type='ToString',
145		args = { 'foo', 9, 'string' },
146		expect = "value for index 'foo' must be string, number given"
147	},
148	{ name = 'checkTypeForIndex, invalid (2)', func = checkTypeForIndex, type='ToString',
149		args = { 'foo', 9, 'string' },
150		expect = "value for index 'foo' must be string, number given"
151	},
152	{ name = 'checkTypeForIndex, other index', func = checkTypeForIndex, type='ToString',
153		args = { 'bar', 9, 'string' },
154		expect = "value for index 'bar' must be string, number given"
155	},
156
157	-- checkTypeForNamedArg
158	{ name = 'checkTypeForNamedArg, valid', func = checkTypeForNamedArg, type='ToString',
159		args = { 'myFunc', 'myArg', 'foo', 'string' },
160		expect = { nil }
161	},
162	{ name = 'checkTypeForNamedArg, invalid', func = checkTypeForNamedArg, type='ToString',
163		args = { 'myFunc', 'myArg', 9, 'string' },
164		expect = "bad named argument myArg to 'myFunc' (string expected, got number)"
165	},
166	{ name = 'checkTypeForNamedArg, nil valid', func = checkTypeForNamedArg, type='ToString',
167		args = { 'myFunc', 'myArg', nil, 'string', true },
168		expect = { nil }
169	},
170	{ name = 'checkTypeForNamedArg, nil invalid', func = checkTypeForNamedArg, type='ToString',
171		args = { 'myFunc', 'myArg', nil, 'string', false },
172		expect = "bad named argument myArg to 'myFunc' (string expected, got nil)"
173	},
174	{ name = 'checkTypeForNamedArg, other function', func = checkTypeForNamedArg, type='ToString',
175		args = { 'otherFunc', 'myArg', 9, 'string' },
176		expect = "bad named argument myArg to 'otherFunc' (string expected, got number)"
177	},
178	{ name = 'checkTypeForNamedArg, other argument', func = checkTypeForNamedArg, type='ToString',
179		args = { 'myFunc', 'otherArg', 9, 'string' },
180		expect = "bad named argument otherArg to 'myFunc' (string expected, got number)"
181	},
182
183	-- makeCheckSelfFunction
184	{ name = 'makeCheckSelfFunction, valid', func = testCheckSelf, type='ToString',
185		args = { testObject, 'myMethod', 'myLibrary', 'myObject', testObject, 'test object' },
186		expect = { nil }
187	},
188	{ name = 'makeCheckSelfFunction, invalid (1)', func = testCheckSelf, type='ToString',
189		args = { {}, 'myMethod', 'myLibrary', 'myObject', testObject, 'test object' },
190		expect = 'myLibrary: invalid test object. Did you call myMethod with a dot instead ' ..
191		'of a colon, i.e. myObject.myMethod() instead of myObject:myMethod()?'
192	},
193	{ name = 'makeCheckSelfFunction, invalid (2)', func = testCheckSelf, type='ToString',
194		args = { 'foo', 'myMethod', 'myLibrary', 'myObject', testObject, 'test object' },
195		expect = 'myLibrary: invalid test object. Did you call myMethod with a dot instead ' ..
196		'of a colon, i.e. myObject.myMethod() instead of myObject:myMethod()?'
197	},
198	{ name = 'makeCheckSelfFunction, other method', func = testCheckSelf, type='ToString',
199		args = { {}, 'otherMethod', 'myLibrary', 'myObject', testObject, 'test object' },
200		expect = 'myLibrary: invalid test object. Did you call otherMethod with a dot instead ' ..
201		'of a colon, i.e. myObject.otherMethod() instead of myObject:otherMethod()?'
202	},
203	{ name = 'makeCheckSelfFunction, other library', func = testCheckSelf, type='ToString',
204		args = { {}, 'myMethod', 'otherLibrary', 'myObject', testObject, 'test object' },
205		expect = 'otherLibrary: invalid test object. Did you call myMethod with a dot instead ' ..
206		'of a colon, i.e. myObject.myMethod() instead of myObject:myMethod()?'
207	},
208	{ name = 'makeCheckSelfFunction, other object', func = testCheckSelf, type='ToString',
209		args = { {}, 'myMethod', 'otherLibrary', 'otherObject', testObject, 'test object' },
210		expect = 'otherLibrary: invalid test object. Did you call myMethod with a dot instead ' ..
211		'of a colon, i.e. otherObject.myMethod() instead of otherObject:myMethod()?'
212	},
213	{ name = 'makeCheckSelfFunction, other description', func = testCheckSelf, type='ToString',
214		args = { {}, 'myMethod', 'myLibrary', 'myObject', testObject, 'test object' },
215		expect = 'myLibrary: invalid test object. Did you call myMethod with a dot instead ' ..
216		'of a colon, i.e. myObject.myMethod() instead of myObject:myMethod()?'
217	},
218}
219
220return testframework.getTestProvider( tests )
221