1+ include ("rsp/testing" )
2+ include ("rsp/helpers" )
3+
4+ define_test_case(
5+ "Types Test"
6+ LABELS "types;helpers"
7+ )
8+
9+ # -------------------------------------------------------------------------------------------------------------- #
10+ # Data Providers
11+ # -------------------------------------------------------------------------------------------------------------- #
12+
13+ function (provides_values output )
14+ # Format:
15+ # <value> | <expected type>
16+ # <value>[@<value>] | <expected type> - list
17+ set ("${output} "
18+ "foo|string"
19+ "|string"
20+ "?|string"
21+ "0|int"
22+ "-2|int"
23+ "0.1|float"
24+ "-0.1|float"
25+ "true|bool"
26+ "false|bool"
27+ "a@b@c|list" # "@" must be replaced with ";" in test!
28+ "define_test_case|command"
29+ "define_test|command"
30+ )
31+ return (PROPAGATE "${output} " )
32+ endfunction ()
33+
34+ # -------------------------------------------------------------------------------------------------------------- #
35+ # Actual tests
36+ # -------------------------------------------------------------------------------------------------------------- #
37+
38+ define_test("can determine if integer" "can_determine_if_int" )
39+ function (can_determine_if_int)
40+
41+ is_int(22 a)
42+ assert_truthy(a MESSAGE "a should be an int" )
43+
44+ is_int(0 b)
45+ assert_truthy(b MESSAGE "b should be an int" )
46+
47+ set (value 8)
48+ is_int(value c)
49+ assert_truthy(c MESSAGE "c should be an int" )
50+
51+ set (value -8)
52+ is_int(value d)
53+ assert_truthy(d MESSAGE "d should be an int" )
54+
55+ is_int(2.34 e)
56+ assert_falsy(e MESSAGE "e should NOT be an int" )
57+
58+ set (value 1.123)
59+ is_int(value f)
60+ assert_falsy(f MESSAGE "f should NOT be an int" )
61+
62+ is_int("str" g)
63+ assert_falsy(g MESSAGE "g should NOT be an int" )
64+
65+ is_int(true h)
66+ assert_falsy(h MESSAGE "h should NOT be an int" )
67+
68+ is_int(false i)
69+ assert_falsy(i MESSAGE "i should NOT be an int" )
70+
71+ set (my_list "foo;bar;zim" )
72+ is_int(my_list j)
73+ assert_falsy(j MESSAGE "j should NOT be an int" )
74+ endfunction ()
75+
76+ define_test("can determine if float" "can_determine_if_float" )
77+ function (can_determine_if_float)
78+
79+ is_float(22 a)
80+ assert_falsy(a MESSAGE "a should NOT be a float" )
81+
82+ set (value 8)
83+ is_float(value b)
84+ assert_falsy(b MESSAGE "b should NOT be a float" )
85+
86+ is_float(2.34 c)
87+ assert_truthy(c MESSAGE "c should be a float" )
88+
89+ set (value 1.123)
90+ is_float(value d)
91+ assert_truthy(d MESSAGE "d should be a float" )
92+
93+ is_float("str" e)
94+ assert_falsy(e MESSAGE "e should NOT be a float" )
95+
96+ is_float(true f)
97+ assert_falsy(f MESSAGE "f should NOT be a float" )
98+
99+ is_float(false g)
100+ assert_falsy(g MESSAGE "g should NOT be a float" )
101+
102+ set (my_list "foo;bar;zim" )
103+ is_float(my_list h)
104+ assert_falsy(h MESSAGE "g should NOT be a float" )
105+
106+ set (value -1.02)
107+ is_float(value i)
108+ assert_truthy(i MESSAGE "i should be a float" )
109+ endfunction ()
110+
111+ define_test("can determine if numeric" "can_determine_if_numeric" )
112+ function (can_determine_if_numeric)
113+
114+ is_numeric(22 a)
115+ assert_truthy(a MESSAGE "a should be numeric" )
116+
117+ set (value 8)
118+ is_numeric(value b)
119+ assert_truthy(b MESSAGE "b should be numeric" )
120+
121+ is_numeric(2.34 c)
122+ assert_truthy(c MESSAGE "c should be numeric" )
123+
124+ set (value 1.123)
125+ is_numeric(value d)
126+ assert_truthy(d MESSAGE "d should be numeric" )
127+
128+ is_numeric("str" e)
129+ assert_falsy(e MESSAGE "e should NOT be numeric" )
130+
131+ is_numeric(true f)
132+ assert_falsy(f MESSAGE "f should NOT be numeric" )
133+
134+ is_numeric(false g)
135+ assert_falsy(g MESSAGE "g should NOT be numeric" )
136+
137+ set (my_list "foo;bar;zim" )
138+ is_numeric(my_list h)
139+ assert_falsy(h MESSAGE "g should NOT be numeric" )
140+
141+ set (value -22.02)
142+ is_numeric(value i)
143+ assert_truthy(i MESSAGE "i should be numeric" )
144+ endfunction ()
145+
146+ define_test("can determine if boolean" "can_determine_if_bool" )
147+ function (can_determine_if_bool)
148+
149+ is_bool(22 a)
150+ assert_falsy(a MESSAGE "a should NOT be a bool" )
151+
152+ set (value 8)
153+ is_bool(value b)
154+ assert_falsy(b MESSAGE "b should NOT be a bool" )
155+
156+ is_bool(2.34 c)
157+ assert_falsy(c MESSAGE "c should NOT be a bool" )
158+
159+ set (value 1.123)
160+ is_bool(value d)
161+ assert_falsy(d MESSAGE "d should NOT be a bool" )
162+
163+ is_bool("str" e)
164+ assert_falsy(e MESSAGE "e should NOT be a bool" )
165+
166+ is_bool(true f)
167+ assert_truthy(f MESSAGE "f should be a bool" )
168+
169+ is_bool(false g)
170+ assert_truthy(g MESSAGE "g should be a bool" )
171+
172+ set (my_list "foo;bar;zim" )
173+ is_bool(my_list h)
174+ assert_falsy(h MESSAGE "g should NOT be a bool" )
175+ endfunction ()
176+
177+ define_test("can determine if boolean like" "can_determine_if_bool_like" )
178+ function (can_determine_if_bool_like)
179+
180+ set (valid "1;on;yes;true;y;0;off;no;false;n;ignore;notfound" )
181+ foreach (v IN LISTS value )
182+ is_bool_like(v result)
183+ assert_truthy(result MESSAGE "${v} should be bool like" )
184+ endforeach ()
185+
186+ # ------------------------------------------------------------ #
187+
188+ is_bool_like("" empty_str)
189+ assert_truthy(empty_str MESSAGE "empty string should be bool like" )
190+
191+ # ------------------------------------------------------------ #
192+
193+ is_bool_like("foo-NOTFOUND" end_with_not_found)
194+ assert_truthy(end_with_not_found MESSAGE "string with '-NOTFOUND' should be bool like" )
195+
196+ # ------------------------------------------------------------ #
197+
198+ is_bool_like(22 a)
199+ assert_truthy(a MESSAGE "a should be bool like" )
200+
201+ set (value 8)
202+ is_bool_like(value b)
203+ assert_truthy(b MESSAGE "b should be bool like" )
204+
205+ is_bool_like(2.34 c)
206+ assert_truthy(c MESSAGE "c should be bool like" )
207+
208+ set (value 1.123)
209+ is_bool_like(value d)
210+ assert_truthy(d MESSAGE "d should be bool like" )
211+
212+ # ------------------------------------------------------------ #
213+
214+ is_bool_like("str" e)
215+ assert_falsy(e MESSAGE "e should NOT be bool like" )
216+
217+ set (my_list "foo;bar;zim" )
218+ is_bool_like(my_list f)
219+ assert_falsy(h MESSAGE "f should NOT be bool like" )
220+
221+ # ------------------------------------------------------------ #
222+
223+ # NOTE: Negative numbers DO NOT evaluate to false in cmake!
224+ set (my_negative_value "-1" )
225+ # if (NOT my_negative_value)
226+ # message("Expected this to work...")
227+ # endif ()
228+ is_bool_like(my_negative_value g)
229+ assert_falsy(g MESSAGE "g should NOT be bool like" )
230+
231+ endfunction ()
232+
233+ define_test("can determine if list" "can_determine_if_list" )
234+ function (can_determine_if_list)
235+
236+ set (list_a "aa;bbb;ccc" )
237+ is_list(list_a a)
238+ assert_truthy(a MESSAGE "a should be a list" )
239+
240+ set (list_b aa bbb ccc)
241+ is_list(list_b b)
242+ assert_truthy(b MESSAGE "b should be a list" )
243+
244+ set (not_list "aa bbb ccc" )
245+ is_list(not_list c)
246+ assert_falsy(c MESSAGE "c should NOT be a list" )
247+
248+ is_list("aa;bbb;ccc" d)
249+ assert_truthy(d MESSAGE "d should be a list" )
250+
251+ is_list("aa bbb ccc" e)
252+ assert_falsy(e MESSAGE "e should NOT be a list" )
253+
254+ is_list("foo" f)
255+ assert_falsy(f MESSAGE "f should NOT be a list" )
256+
257+ is_list(1 g)
258+ assert_falsy(g MESSAGE "g should NOT be a list" )
259+
260+ is_list(1.32 h)
261+ assert_falsy(h MESSAGE "h should NOT be a list" )
262+
263+ is_list(-1 i)
264+ assert_falsy(i MESSAGE "i should NOT be a list" )
265+
266+ is_list(false j)
267+ assert_falsy(j MESSAGE "j should NOT be a list" )
268+ endfunction ()
269+
270+
271+ define_test("can determine if command" "can_determine_if_cmd" )
272+ function (can_determine_if_cmd)
273+
274+ macro (my_macro)
275+ endmacro ()
276+ is_command(my_macro a)
277+ assert_truthy(a MESSAGE "a should be a command (macro)" )
278+
279+ function (my_function)
280+ endfunction ()
281+ is_command(my_function b)
282+ assert_truthy(b MESSAGE "b should be a command (function)" )
283+
284+ # Not sure if this even can be tested...
285+ # add_custom_command(TARGET my_command
286+ # PRE_BUILD
287+ # COMMAND ${CMAKE_COMMAND} -E echo hello
288+ # COMMENT "FOR TESTING PURPOSES ONLY"
289+ # VERBATIM
290+ # )
291+ # is_command(my_command c)
292+ # assert_truthy(c MESSAGE "c should be a command (custom command)")
293+
294+ set (my_var "" )
295+ is_command(my_var d)
296+ assert_falsy(d MESSAGE "d should NOT be a command" )
297+
298+ set (fn_ref "my_function" )
299+ is_command(fn_ref e)
300+ assert_truthy(e MESSAGE "e should be a command (reference)" )
301+
302+ endfunction ()
303+
304+ define_test("can determine if string" "can_determine_if_str" )
305+ function (can_determine_if_str)
306+
307+ is_string("abc" a)
308+ assert_truthy(a MESSAGE "a should be a string" )
309+
310+ set (my_str "foo" )
311+ is_string(my_str b)
312+ assert_truthy(b MESSAGE "b should be a string" )
313+
314+ set (my_empty_str "" )
315+ is_string(my_empty_str c)
316+ assert_truthy(c MESSAGE "c should be a string" )
317+
318+ is_string(my_undefined_var d)
319+ assert_truthy(d MESSAGE "d should be a string" )
320+
321+ is_string(42 e)
322+ assert_falsy(e MESSAGE "e should NOT be a string" )
323+
324+ is_string(-42.1 f)
325+ assert_falsy(f MESSAGE "f should NOT be a string" )
326+
327+ set (list_a "a;b;c" )
328+ is_string(list_a g)
329+ assert_falsy(g MESSAGE "g should NOT be a string" )
330+
331+ set (list_b aa bb cc)
332+ is_string(list_b h)
333+ assert_falsy(h MESSAGE "h should NOT be a string" )
334+
335+ set (not_list "aa bb cc" )
336+ is_string(not_list i)
337+ assert_truthy(i MESSAGE "i should be a string" )
338+
339+ macro (my_macro)
340+ endmacro ()
341+ is_string(my_macro j)
342+ assert_falsy(j MESSAGE "j should NOT be a string" )
343+
344+ function (my_function)
345+ endfunction ()
346+ is_string(my_function k)
347+ assert_falsy(k MESSAGE "k should NOT be a string" )
348+
349+ set (fn_ref "my_function" )
350+ is_string(fn_ref l)
351+ assert_falsy(l MESSAGE "l should NOT be a string" )
352+ endfunction ()
353+
354+ define_test("can determine type" "can_determine_type" DATA_PROVIDER "provides_values" )
355+ function (can_determine_type item)
356+
357+ string (REPLACE "|" ";" parts "${item} " )
358+ list (GET parts 0 value )
359+ list (GET parts 1 expected)
360+
361+ string (FIND "${value} " "@" is_sublist)
362+ if (NOT is_sublist EQUAL -1)
363+ string (REPLACE "@" ";" value "${value} " )
364+ endif ()
365+
366+ message ("Value: ${value} " )
367+
368+ get_type(value result)
369+ assert_string_equals("${expected} " "${result} " MESSAGE "incorrect '${result} ' type for value: ${value} " )
370+ endfunction ()
0 commit comments