1# SPDX-License-Identifier: Apache-2.0
2
3cmake_minimum_required(VERSION 3.20.0)
4
5find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6project(zephyr_yaml_test)
7target_sources(app PRIVATE ${ZEPHYR_BASE}/misc/empty_file.c)
8
9set_property(GLOBAL PROPERTY EXPECTED_ERROR 0)
10
11macro(message)
12  if(DEFINED expect_failure)
13  if(${ARGV0} STREQUAL FATAL_ERROR)
14    if("${ARGV1}" STREQUAL "${expect_failure}")
15      # This is an expected error.
16      get_property(error_count GLOBAL PROPERTY EXPECTED_ERROR)
17      math(EXPR error_count "${error_count} + 1")
18      set_property(GLOBAL PROPERTY EXPECTED_ERROR ${error_count})
19      return()
20    else()
21      _message("Unexpected error occurred")
22    endif()
23    endif()
24  endif()
25  _message(${ARGN})
26endmacro()
27
28macro(test_assert)
29  cmake_parse_arguments(TA_ARG "" "COMMENT" "TEST" ${ARGN})
30  if(${TA_ARG_TEST})
31    message("${CMAKE_CURRENT_FUNCTION}(): Passed")
32  else()
33    message(FATAL_ERROR
34      "${CMAKE_CURRENT_FUNCTION}(): Failed\n"
35      "Test: ${TA_ARG_TEST}\n"
36      "${TA_ARG_COMMENT}"
37    )
38  endif()
39endmacro()
40
41function(test_reading_string)
42  set(expected "Simple string")
43  yaml_get(actual NAME yaml-test KEY cmake test key-string)
44
45  test_assert(TEST ${expected} STREQUAL ${actual}
46              COMMENT "yaml key value does not match expectation."
47  )
48endfunction()
49
50function(test_reading_list_strings)
51  set(expected 4)
52  yaml_length(actual NAME yaml-test KEY cmake test key-list-string)
53  test_assert(TEST ${expected} EQUAL ${actual}
54              COMMENT "yaml list length does not match expectation."
55  )
56
57  set(expected "a" "list" "of" "strings")
58  yaml_get(actual NAME yaml-test KEY cmake test key-list-string)
59
60  foreach(a e IN ZIP_LISTS actual expected)
61    test_assert(TEST "${e}" STREQUAL "${a}"
62                COMMENT "list values mismatch."
63    )
64  endforeach()
65endfunction()
66
67function(test_reading_int)
68  set(expected 42)
69  yaml_get(actual NAME yaml-test KEY cmake test key-int)
70
71  test_assert(TEST ${expected} EQUAL ${actual}
72              COMMENT "yaml key value does not match expectation."
73  )
74endfunction()
75
76function(test_reading_list_int)
77  set(expected 3)
78  yaml_length(actual NAME yaml-test KEY cmake test key-list-int)
79  test_assert(TEST ${expected} EQUAL ${actual}
80              COMMENT "yaml list length does not match expectation."
81  )
82
83  set(expected 4 10 2)
84  yaml_get(actual NAME yaml-test KEY cmake test key-list-int)
85
86  foreach(a e IN ZIP_LISTS actual expected)
87    test_assert(TEST "${e}" STREQUAL "${a}"
88                COMMENT "list values mismatch."
89    )
90  endforeach()
91endfunction()
92
93function(test_reading_int)
94  set(expected 42)
95  yaml_get(actual NAME yaml-test KEY cmake test key-int)
96
97  test_assert(TEST ${expected} EQUAL ${actual}
98              COMMENT "yaml key value does not match expectation."
99  )
100endfunction()
101
102function(test_reading_not_found)
103  set(expected cmake-missing-NOTFOUND)
104  yaml_get(actual NAME yaml-test KEY cmake missing test key)
105
106  test_assert(TEST ${expected} STREQUAL ${actual}
107              COMMENT "Expected -NOTFOUND, but something was found."
108  )
109endfunction()
110
111function(test_reading_not_found_array)
112  set(expected cmake-missing-NOTFOUND)
113  yaml_length(actual NAME yaml-test KEY cmake missing test array list)
114
115  test_assert(TEST ${expected} STREQUAL ${actual}
116              COMMENT "Expected -NOTFOUND, but something was found."
117  )
118endfunction()
119
120function(test_reading_not_array)
121  set(expected -1)
122  yaml_length(actual NAME yaml-test KEY cmake test key-int)
123
124  test_assert(TEST ${expected} STREQUAL ${actual}
125              COMMENT "Not array expected, so length should be -1."
126  )
127endfunction()
128
129function(test_save_new_file)
130  yaml_save(NAME yaml-test FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_save.yaml)
131
132  # Read-back the yaml and verify the value.
133  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_save.yaml
134            NAME ${CMAKE_CURRENT_FUNCTION}_readback
135  )
136  set(expected "Simple string")
137  yaml_get(actual NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test key-string)
138
139  test_assert(TEST ${expected} STREQUAL ${actual}
140              COMMENT "yaml key value does not match expectation."
141  )
142
143  set(expected 42)
144  yaml_get(actual NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test key-int)
145
146  test_assert(TEST ${expected} EQUAL ${actual}
147              COMMENT "yaml key value does not match expectation."
148  )
149endfunction()
150
151function(test_setting_string)
152  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
153              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
154  )
155
156  set(new_value "A new string")
157  yaml_set(actual NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
158           KEY cmake test set key-string VALUE ${new_value}
159  )
160
161  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
162
163  # Read-back the yaml and verify the value.
164  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
165            NAME ${CMAKE_CURRENT_FUNCTION}_readback
166  )
167
168  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-string)
169
170  test_assert(TEST ${new_value} STREQUAL ${readback}
171              COMMENT "new yaml value does not match readback value."
172  )
173endfunction()
174
175function(test_setting_list_strings)
176  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
177              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
178  )
179
180  set(new_value "A" "new" "list" "of" "strings")
181  yaml_set(actual NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
182           KEY cmake test set key-list-string LIST ${new_value}
183  )
184
185  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
186
187  # Read-back the yaml and verify the value.
188  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
189            NAME ${CMAKE_CURRENT_FUNCTION}_readback
190  )
191
192  yaml_length(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-string)
193
194  test_assert(TEST 5 EQUAL ${readback}
195              COMMENT "readback yaml list length does not match original."
196  )
197
198  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-string)
199
200  foreach(a e IN ZIP_LISTS readback new_value)
201    test_assert(TEST "${e}" STREQUAL "${a}"
202                COMMENT "list values mismatch."
203    )
204  endforeach()
205endfunction()
206
207function(test_setting_int)
208  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
209              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
210  )
211
212  set(new_value 42)
213  yaml_set(actual NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
214           KEY cmake test set key-int VALUE ${new_value}
215  )
216
217  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
218
219  # Read-back the yaml and verify the value.
220  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
221            NAME ${CMAKE_CURRENT_FUNCTION}_readback
222  )
223
224  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-int)
225
226  test_assert(TEST ${new_value} STREQUAL ${readback}
227              COMMENT "new yaml value does not match readback value."
228  )
229endfunction()
230
231function(test_setting_list_int)
232  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
233              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
234  )
235
236  set(new_value 42 41 40 2 10)
237  yaml_set(actual NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
238           KEY cmake test set key-list-int LIST ${new_value}
239  )
240
241  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
242
243  # Read-back the yaml and verify the value.
244  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
245            NAME ${CMAKE_CURRENT_FUNCTION}_readback
246  )
247
248  yaml_length(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-int)
249
250  test_assert(TEST 5 EQUAL ${readback}
251              COMMENT "readback yaml list length does not match original."
252  )
253
254  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-int)
255
256  foreach(a e IN ZIP_LISTS readback new_value)
257    test_assert(TEST "${e}" STREQUAL "${a}"
258                COMMENT "list values mismatch."
259    )
260  endforeach()
261endfunction()
262
263function(test_setting_empty_value)
264  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
265              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
266  )
267
268  set(new_value)
269  yaml_set(actual NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
270           KEY cmake test set key-int VALUE ${new_value}
271  )
272
273  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
274
275  # Read-back the yaml and verify the value.
276  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
277            NAME ${CMAKE_CURRENT_FUNCTION}_readback
278  )
279
280  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-int)
281
282  if(DEFINED readback)
283    message(FATAL_ERROR "${CMAKE_CURRENT_FUNCTION}(): Failed\n"
284                        "Empty value expected, but got: ${readback}"
285    )
286  else()
287    message("${CMAKE_CURRENT_FUNCTION}(): Passed")
288  endif()
289endfunction()
290
291function(test_setting_empty_list)
292  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
293              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
294  )
295
296  set(new_value)
297  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
298           KEY cmake test set key-list-int LIST ${new_value}
299  )
300
301  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
302
303  # Read-back the yaml and verify the value.
304  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
305            NAME ${CMAKE_CURRENT_FUNCTION}_readback
306  )
307
308  yaml_length(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-int)
309
310  test_assert(TEST 0 EQUAL ${readback}
311              COMMENT "readback yaml list length does not match original."
312  )
313
314  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-int)
315
316  foreach(a e IN ZIP_LISTS readback new_value)
317    test_assert(TEST "${e}" STREQUAL "${a}"
318                COMMENT "list values mismatch."
319    )
320  endforeach()
321endfunction()
322
323function(test_set_remove_int)
324  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
325              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
326  )
327
328  set(new_value 42)
329  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
330           KEY cmake test set key-int VALUE ${new_value}
331  )
332
333  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
334
335  # Read-back the yaml and verify the value.
336  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
337            NAME ${CMAKE_CURRENT_FUNCTION}_readback
338  )
339
340  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-int)
341
342  test_assert(TEST ${new_value} STREQUAL ${readback}
343              COMMENT "new yaml value does not match readback value."
344  )
345
346  # Remove the setting and write the file again.
347  yaml_remove(NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-int)
348  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_readback)
349
350  # Read-back again and verify the value has been removed.
351  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
352            NAME ${CMAKE_CURRENT_FUNCTION}_readback_removed
353  )
354
355  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback_removed KEY cmake test set key-int)
356
357  set(expected cmake-test-set-key-int-NOTFOUND)
358
359  test_assert(TEST ${expected} STREQUAL ${readback}
360              COMMENT "Expected -NOTFOUND, but something was found."
361  )
362endfunction()
363
364function(test_fail_missing_filename)
365  yaml_create(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
366
367  set(new_value 42)
368  yaml_set(actual NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
369           KEY cmake test set key-int VALUE ${new_value}
370  )
371
372  set(expect_failure "yaml_save(...) missing a required argument: FILE")
373  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
374
375  get_property(errors GLOBAL PROPERTY EXPECTED_ERROR)
376  test_assert(TEST 1 EQUAL ${errors}
377              COMMENT "No error occurred when error was expected.\nExpected error: ${expect_failure}"
378  )
379  set_property(GLOBAL PROPERTY EXPECTED_ERROR 0)
380endfunction()
381
382yaml_load(FILE ${CMAKE_CURRENT_LIST_DIR}/test.yaml NAME yaml-test)
383test_reading_string()
384test_reading_int()
385test_reading_list_strings()
386test_reading_list_int()
387test_reading_not_found()
388test_reading_not_found_array()
389test_reading_not_array()
390
391test_save_new_file()
392
393test_setting_int()
394test_setting_string()
395test_setting_list_strings()
396test_setting_list_int()
397
398test_setting_empty_value()
399test_setting_empty_list()
400
401test_set_remove_int()
402
403test_fail_missing_filename()
404
405target_sources(app PRIVATE src/main.c)
406