1# SPDX-License-Identifier: Apache-2.0
2
3cmake_minimum_required(VERSION 3.20.0)
4
5if(NOT CMAKE_SCRIPT_MODE_FILE)
6  # Project mode initialization (main CMake invocation)
7  find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
8  project(zephyr_yaml_test)
9  target_sources(app PRIVATE ${ZEPHYR_BASE}/misc/empty_file.c)
10  message(STATUS "Run 1 -------------\n  CMake PROJECT mode\n----------------------")
11else()
12  # Script mode initialization (re-run)
13  set(ZEPHYR_BASE ${CMAKE_CURRENT_LIST_DIR}/../../../)
14  list(APPEND CMAKE_MODULE_PATH "${ZEPHYR_BASE}/cmake/modules")
15  include(yaml)
16  message(STATUS "Run 2 ------------\n  CMake SCRIPT mode\n---------------------")
17endif()
18
19set_property(GLOBAL PROPERTY EXPECTED_ERROR 0)
20
21macro(message)
22  if(DEFINED expect_failure)
23    if(${ARGV0} STREQUAL FATAL_ERROR)
24      if("${ARGV1}" STREQUAL "${expect_failure}")
25        # This is an expected error.
26        get_property(error_count GLOBAL PROPERTY EXPECTED_ERROR)
27        math(EXPR error_count "${error_count} + 1")
28        set_property(GLOBAL PROPERTY EXPECTED_ERROR ${error_count})
29        return()
30      else()
31        _message("Unexpected error occurred")
32      endif()
33    endif()
34  endif()
35  _message(${ARGN})
36endmacro()
37
38macro(test_assert)
39  cmake_parse_arguments(TA_ARG "" "COMMENT" "TEST" ${ARGN})
40  if(${TA_ARG_TEST})
41    message("${CMAKE_CURRENT_FUNCTION}(): Passed")
42  else()
43    message(FATAL_ERROR
44      "${CMAKE_CURRENT_FUNCTION}(): Failed\n"
45      "Test: ${TA_ARG_TEST}\n"
46      "${TA_ARG_COMMENT}"
47    )
48  endif()
49endmacro()
50
51function(test_reading_string)
52  set(expected "Simple string")
53  yaml_get(actual NAME yaml-test KEY cmake test key-string)
54
55  test_assert(TEST ${expected} STREQUAL ${actual}
56              COMMENT "yaml key value does not match expectation."
57  )
58endfunction()
59
60function(test_reading_list_strings)
61  set(expected 4)
62  yaml_length(actual NAME yaml-test KEY cmake test key-list-string)
63  test_assert(TEST ${expected} EQUAL ${actual}
64              COMMENT "yaml list length does not match expectation."
65  )
66
67  set(expected "a" "list" "of" "strings")
68  yaml_get(actual NAME yaml-test KEY cmake test key-list-string)
69
70  foreach(a e IN ZIP_LISTS actual expected)
71    test_assert(TEST "${e}" STREQUAL "${a}"
72                COMMENT "list values mismatch."
73    )
74  endforeach()
75endfunction()
76
77function(test_reading_int)
78  set(expected 42)
79  yaml_get(actual NAME yaml-test KEY cmake test key-int)
80
81  test_assert(TEST ${expected} EQUAL ${actual}
82              COMMENT "yaml key value does not match expectation."
83  )
84endfunction()
85
86function(test_reading_list_int)
87  set(expected 3)
88  yaml_length(actual NAME yaml-test KEY cmake test key-list-int)
89  test_assert(TEST ${expected} EQUAL ${actual}
90              COMMENT "yaml list length does not match expectation."
91  )
92
93  set(expected 4 10 2)
94  yaml_get(actual NAME yaml-test KEY cmake test key-list-int)
95
96  foreach(a e IN ZIP_LISTS actual expected)
97    test_assert(TEST "${e}" STREQUAL "${a}"
98                COMMENT "list values mismatch."
99    )
100  endforeach()
101endfunction()
102
103function(test_reading_int)
104  set(expected 42)
105  yaml_get(actual NAME yaml-test KEY cmake test key-int)
106
107  test_assert(TEST ${expected} EQUAL ${actual}
108              COMMENT "yaml key value does not match expectation."
109  )
110endfunction()
111
112function(test_reading_map_list_entry)
113  set(expected_length 2)
114  set(expected_name "MapEntry1")
115  set(expected_int  5)
116  yaml_length(actual_length NAME yaml-test KEY cmake test map-list)
117  yaml_get(actual_name NAME yaml-test KEY cmake test map-list 0 map-entry-name)
118  yaml_get(actual_int NAME yaml-test KEY cmake test map-list 0 map-entry-int)
119
120  test_assert(TEST ${expected_length} EQUAL ${actual_length}
121              COMMENT "yaml key value does not match expectation."
122  )
123  test_assert(TEST ${expected_name} STREQUAL ${actual_name}
124              COMMENT "yaml key value does not match expectation."
125  )
126  test_assert(TEST ${expected_int} EQUAL ${actual_int}
127              COMMENT "yaml key value does not match expectation."
128  )
129endfunction()
130
131function(test_reading_not_found)
132  set(expected cmake-missing-NOTFOUND)
133  yaml_get(actual NAME yaml-test KEY cmake missing test key)
134
135  test_assert(TEST ${expected} STREQUAL ${actual}
136              COMMENT "Expected -NOTFOUND, but something was found."
137  )
138endfunction()
139
140function(test_reading_not_found_array)
141  set(expected cmake-missing-NOTFOUND)
142  yaml_length(actual NAME yaml-test KEY cmake missing test array list)
143
144  test_assert(TEST ${expected} STREQUAL ${actual}
145              COMMENT "Expected -NOTFOUND, but something was found."
146  )
147endfunction()
148
149function(test_reading_not_array)
150  set(expected -1)
151  yaml_length(actual NAME yaml-test KEY cmake test key-int)
152
153  test_assert(TEST ${expected} STREQUAL ${actual}
154              COMMENT "Not array expected, so length should be -1."
155  )
156endfunction()
157
158function(test_reading_not_found_map_list_entry)
159  set(expected cmake-test-map-list-3-NOTFOUND)
160  yaml_get(actual NAME yaml-test KEY cmake test map-list 3 map-entry-name)
161
162  test_assert(TEST ${expected} STREQUAL ${actual}
163              COMMENT "Expected -NOTFOUND, but something was found."
164  )
165endfunction()
166
167function(test_save_new_file)
168  yaml_save(NAME yaml-test FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_save.yaml)
169
170  # Read-back the yaml and verify the value.
171  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_save.yaml
172            NAME ${CMAKE_CURRENT_FUNCTION}_readback
173  )
174  set(expected "Simple string")
175  yaml_get(actual NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test key-string)
176
177  test_assert(TEST ${expected} STREQUAL ${actual}
178              COMMENT "yaml key value does not match expectation."
179  )
180
181  set(expected 42)
182  yaml_get(actual NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test key-int)
183
184  test_assert(TEST ${expected} EQUAL ${actual}
185              COMMENT "yaml key value does not match expectation."
186  )
187endfunction()
188
189function(test_setting_string)
190  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
191              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
192  )
193
194  set(new_value "A new string")
195  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
196           KEY cmake test set key-string VALUE ${new_value}
197  )
198
199  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
200
201  # Read-back the yaml and verify the value.
202  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
203            NAME ${CMAKE_CURRENT_FUNCTION}_readback
204  )
205
206  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-string)
207
208  test_assert(TEST ${new_value} STREQUAL ${readback}
209              COMMENT "new yaml value does not match readback value."
210  )
211endfunction()
212
213function(test_setting_list_strings)
214  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
215              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
216  )
217
218  set(new_value "A" "new" "list" "of" "strings")
219  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
220           KEY cmake test set key-list-string LIST ${new_value}
221  )
222
223  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
224
225  # Read-back the yaml and verify the value.
226  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
227            NAME ${CMAKE_CURRENT_FUNCTION}_readback
228  )
229
230  yaml_length(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-string)
231
232  test_assert(TEST 5 EQUAL ${readback}
233              COMMENT "readback yaml list length does not match original."
234  )
235
236  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-string)
237
238  foreach(a e IN ZIP_LISTS readback new_value)
239    test_assert(TEST "${e}" STREQUAL "${a}"
240                COMMENT "list values mismatch."
241    )
242  endforeach()
243endfunction()
244
245function(test_append_list_strings)
246  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
247              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
248  )
249
250  # start with entries that will be overwritten
251  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
252           KEY cmake test set key-list-string
253           LIST dropped entries
254  )
255
256  set(new_value "A" "new" "list" "of" "strings")
257  list(GET new_value 0 first)
258  list(SUBLIST new_value 1 -1 others)
259
260  # re-initialize the list with the first correct value
261  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
262           KEY cmake test set key-list-string
263           LIST ${first}
264  )
265
266  # append the rest of the values
267  foreach(entry IN LISTS others)
268    yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
269             KEY cmake test set key-list-string
270             APPEND LIST ${entry}
271    )
272  endforeach()
273
274  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
275
276  # Read-back the yaml and verify the value.
277  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
278            NAME ${CMAKE_CURRENT_FUNCTION}_readback
279  )
280
281  yaml_length(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-string)
282
283  test_assert(TEST 5 EQUAL ${readback}
284              COMMENT "readback yaml list length does not match original."
285  )
286
287  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-string)
288
289  foreach(a e IN ZIP_LISTS readback new_value)
290    test_assert(TEST "${e}" STREQUAL "${a}"
291                COMMENT "list values mismatch."
292    )
293  endforeach()
294endfunction()
295
296function(test_setting_int)
297  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
298              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
299  )
300
301  set(new_value 42)
302  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
303           KEY cmake test set key-int VALUE ${new_value}
304  )
305
306  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
307
308  # Read-back the yaml and verify the value.
309  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
310            NAME ${CMAKE_CURRENT_FUNCTION}_readback
311  )
312
313  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-int)
314
315  test_assert(TEST ${new_value} STREQUAL ${readback}
316              COMMENT "new yaml value does not match readback value."
317  )
318endfunction()
319
320function(test_setting_list_int)
321  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
322              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
323  )
324
325  set(new_value 42 41 40 2 10)
326  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
327           KEY cmake test set key-list-int LIST ${new_value}
328  )
329
330  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
331
332  # Read-back the yaml and verify the value.
333  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
334            NAME ${CMAKE_CURRENT_FUNCTION}_readback
335  )
336
337  yaml_length(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-int)
338
339  test_assert(TEST 5 EQUAL ${readback}
340              COMMENT "readback yaml list length does not match original."
341  )
342
343  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-int)
344
345  foreach(a e IN ZIP_LISTS readback new_value)
346    test_assert(TEST "${e}" STREQUAL "${a}"
347                COMMENT "list values mismatch."
348    )
349  endforeach()
350endfunction()
351
352function(test_setting_map_list_entry)
353  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
354              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
355  )
356
357  set(new_entry_name_0 MapEntryNew1)
358  set(new_entry_int_0  42)
359  set(new_entry_name_1 MapEntryNew2)
360  set(new_entry_int_1  24)
361  set(new_entry_name_2 MapEntryNew3)
362  set(new_entry_int_2  4224)
363  yaml_set(actual NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
364           KEY cmake test set map-list LIST
365           MAP "map-entry-name: ${new_entry_name_0}, map-entry-int: ${new_entry_int_0}"
366           MAP "map-entry-name: ${new_entry_name_1}, map-entry-int: ${new_entry_int_1}"
367           MAP "map-entry-name: ${new_entry_name_2}, map-entry-int: ${new_entry_int_2}"
368  )
369
370  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
371
372  # Read-back the yaml and verify the values.
373  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
374            NAME ${CMAKE_CURRENT_FUNCTION}_readback
375  )
376
377  yaml_length(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set map-list)
378
379  test_assert(TEST 3 EQUAL ${readback}
380              COMMENT "readback yaml list length does not match original."
381  )
382
383  foreach(index 0 1 2)
384    yaml_get(readback_name NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set map-list ${index} map-entry-name)
385    yaml_get(readback_int  NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set map-list ${index} map-entry-int)
386
387    test_assert(TEST "${readback_name}" STREQUAL "${new_entry_name_${index}}"
388                COMMENT "list values mismatch."
389    )
390    test_assert(TEST "${readback_int}" EQUAL "${new_entry_int_${index}}"
391                COMMENT "list values mismatch."
392    )
393  endforeach()
394endfunction()
395
396function(test_setting_map_list_entry_windows)
397  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
398              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
399  )
400
401  set(new_entry_name_0 MapEntryWindowsPath1)
402  set(new_entry_path_0  "c:/tmp/zephyr")
403  set(new_entry_name_1 MapEntryWindowsPath2)
404  set(new_entry_path_1  "c:/program files/space")
405  set(new_entry_name_2 MapEntryWindowsPath3)
406  set(new_entry_path_2  "D:/alternative/drive")
407  yaml_set(actual NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
408           KEY cmake test set map-list LIST
409           MAP "map-entry-name: ${new_entry_name_0}, map-entry-path: ${new_entry_path_0}"
410           MAP "map-entry-name: ${new_entry_name_1}, map-entry-path: ${new_entry_path_1}"
411           MAP "map-entry-name: ${new_entry_name_2}, map-entry-path: ${new_entry_path_2}"
412  )
413
414  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
415
416  # Read-back the yaml and verify the values.
417  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
418            NAME ${CMAKE_CURRENT_FUNCTION}_readback
419  )
420
421  yaml_length(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set map-list)
422
423  test_assert(TEST 3 EQUAL ${readback}
424              COMMENT "readback yaml list length does not match original."
425  )
426
427  foreach(index 0 1 2)
428    yaml_get(readback_name NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set map-list ${index} map-entry-name)
429    yaml_get(readback_path  NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set map-list ${index} map-entry-path)
430
431    test_assert(TEST "${readback_name}" STREQUAL "${new_entry_name_${index}}"
432                COMMENT "list values mismatch."
433    )
434    test_assert(TEST "${readback_path}" STREQUAL "${new_entry_path_${index}}"
435                COMMENT "list values mismatch."
436    )
437  endforeach()
438endfunction()
439
440function(test_setting_map_list_entry_commas)
441  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
442              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
443  )
444
445  set(new_entry_name_0 TestString1)
446  set(new_entry_str_0  "'A\\,string'")
447  set(new_entry_name_1 TestString2)
448  set(new_entry_str_1  "'\\, is first'")
449  set(new_entry_name_2 TestString3)
450  set(new_entry_str_2  "'\\, and : is\\,everywhere\\,'")
451  yaml_set(actual NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
452           KEY cmake test set map-list LIST
453           MAP "map-entry-name: ${new_entry_name_0}, map-entry-str: ${new_entry_str_0}"
454           MAP "map-entry-name: ${new_entry_name_1}, map-entry-str: ${new_entry_str_1}"
455           MAP "map-entry-name: ${new_entry_name_2}, map-entry-str: ${new_entry_str_2}"
456  )
457
458  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
459
460  # Read-back the yaml and verify the values.
461  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
462            NAME ${CMAKE_CURRENT_FUNCTION}_readback
463  )
464
465  yaml_length(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set map-list)
466
467  test_assert(TEST 3 EQUAL ${readback}
468              COMMENT "readback yaml list length does not match original."
469  )
470
471  foreach(index 0 1 2)
472    yaml_get(readback_name NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set map-list ${index} map-entry-name)
473    yaml_get(readback_str  NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set map-list ${index} map-entry-str)
474
475    test_assert(TEST "${readback_name}" STREQUAL "${new_entry_name_${index}}"
476                COMMENT "list values mismatch."
477    )
478    test_assert(TEST "'${readback_str}'" STREQUAL "${new_entry_str_${index}}"
479                COMMENT "list values mismatch."
480    )
481  endforeach()
482endfunction()
483function(test_setting_empty_value)
484  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
485              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
486  )
487
488  set(new_value)
489  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
490           KEY cmake test set key-int VALUE ${new_value}
491  )
492
493  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
494
495  # Read-back the yaml and verify the value.
496  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
497            NAME ${CMAKE_CURRENT_FUNCTION}_readback
498  )
499
500  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-int)
501
502  if(DEFINED readback)
503    message(FATAL_ERROR "${CMAKE_CURRENT_FUNCTION}(): Failed\n"
504                        "Empty value expected, but got: ${readback}"
505    )
506  else()
507    message("${CMAKE_CURRENT_FUNCTION}(): Passed")
508  endif()
509endfunction()
510
511function(test_setting_empty_list)
512  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
513              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
514  )
515
516  set(new_value)
517  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
518           KEY cmake test set key-list-int LIST ${new_value}
519  )
520
521  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
522
523  # Read-back the yaml and verify the value.
524  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
525            NAME ${CMAKE_CURRENT_FUNCTION}_readback
526  )
527
528  yaml_length(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-int)
529
530  test_assert(TEST 0 EQUAL ${readback}
531              COMMENT "readback yaml list length does not match original."
532  )
533
534  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-int)
535
536  foreach(a e IN ZIP_LISTS readback new_value)
537    test_assert(TEST "${e}" STREQUAL "${a}"
538                COMMENT "list values mismatch."
539    )
540  endforeach()
541endfunction()
542
543function(test_setting_genexes)
544  set(file ${CMAKE_BINARY_DIR}/test_setting_genexes.yaml)
545  yaml_create(FILE ${file}
546              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
547  )
548
549  set(new_value "string before changes")
550  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
551           KEY cmake test set key-string
552           VALUE ${new_value}
553  )
554
555  set_property(TARGET app PROPERTY expanding_str "expanded genex")
556  set(new_value "$<TARGET_PROPERTY:app,expanding_str>")
557  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
558           KEY cmake test set key-string-genex
559           GENEX VALUE ${new_value}
560  )
561
562  # create the list by appending in several steps to test conversion
563  # from JSON list to genex stringified list
564  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
565           KEY cmake test set key-list-string-genex
566           LIST "A"
567  )
568  set_property(TARGET app PROPERTY expanding_list "temporary" "list")
569  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
570           KEY cmake test set key-list-string-genex
571           APPEND GENEX LIST "$<TARGET_PROPERTY:app,expanding_list>" "of"
572  )
573  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
574           KEY cmake test set key-list-string-genex
575           APPEND LIST "strings"
576  )
577
578  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
579
580  # Read-back the yaml immediately and verify the genex values are NOT present
581  # (genexes are expanded at generation time)
582  yaml_load(FILE ${file}
583            NAME ${CMAKE_CURRENT_FUNCTION}_readback
584  )
585
586  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-string)
587  set(expected "string before changes")
588
589  test_assert(TEST ${expected} STREQUAL ${readback}
590              COMMENT "yaml key value does not match expectation."
591  )
592
593  yaml_length(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-string-genex)
594  set(expected cmake-test-set-key-list-string-genex-NOTFOUND)
595
596  test_assert(TEST ${expected} STREQUAL ${readback}
597              COMMENT "Expected -NOTFOUND, but something was found."
598  )
599
600  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-string-genex)
601  set(expected cmake-test-set-key-string-genex-NOTFOUND)
602
603  test_assert(TEST ${expected} STREQUAL ${readback}
604              COMMENT "Expected -NOTFOUND, but something was found."
605  )
606
607  # modify the expanding_list property and the string, then save the file once
608  # more to check the final values are used in the output yaml file
609  set_property(TARGET app PROPERTY expanding_list "new" "list")
610  set(new_value "string after changes")
611  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
612           KEY cmake test set key-string
613           VALUE ${new_value}
614  )
615
616  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
617endfunction()
618
619function(test_verify_genexes)
620  set(file ${CMAKE_BINARY_DIR}/test_setting_genexes.yaml)
621  yaml_load(FILE ${file}
622      NAME ${CMAKE_CURRENT_FUNCTION}_readback
623  )
624
625  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-string)
626  set(expected "string after changes")
627
628  test_assert(TEST ${expected} STREQUAL ${readback}
629              COMMENT "yaml key value does not match expectation."
630  )
631
632  set(expected "expanded genex")
633  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-string-genex)
634
635  test_assert(TEST ${expected} STREQUAL ${readback}
636        COMMENT "new yaml value does not match readback value."
637  )
638
639  set(expected "A" "new" "list" "of" "strings")
640  list(LENGTH expected exp_len)
641
642  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-string-genex)
643  yaml_length(act_len NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-string-genex)
644
645  test_assert(TEST ${exp_len} EQUAL ${act_len}
646              COMMENT "yaml list length does not match expectation."
647  )
648
649  foreach(a e IN ZIP_LISTS readback expected)
650    test_assert(TEST "${e}" STREQUAL "${a}"
651                COMMENT "list values mismatch."
652    )
653  endforeach()
654endfunction()
655
656function(test_set_remove_int)
657  yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
658              NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
659  )
660
661  set(new_value 42)
662  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
663           KEY cmake test set key-int VALUE ${new_value}
664  )
665
666  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
667
668  # Read-back the yaml and verify the value.
669  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
670            NAME ${CMAKE_CURRENT_FUNCTION}_readback
671  )
672
673  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-int)
674
675  test_assert(TEST ${new_value} STREQUAL ${readback}
676              COMMENT "new yaml value does not match readback value."
677  )
678
679  # Remove the setting and write the file again.
680  yaml_remove(NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-int)
681  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_readback)
682
683  # Read-back again and verify the value has been removed.
684  yaml_load(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
685            NAME ${CMAKE_CURRENT_FUNCTION}_readback_removed
686  )
687
688  yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback_removed KEY cmake test set key-int)
689
690  set(expected cmake-test-set-key-int-NOTFOUND)
691
692  test_assert(TEST ${expected} STREQUAL ${readback}
693              COMMENT "Expected -NOTFOUND, but something was found."
694  )
695endfunction()
696
697function(test_fail_missing_filename)
698  yaml_create(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
699
700  set(new_value 42)
701  yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
702           KEY cmake test set key-int VALUE ${new_value}
703  )
704
705  set(expect_failure "yaml_save(...) missing a required argument: FILE")
706  yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create)
707
708  get_property(errors GLOBAL PROPERTY EXPECTED_ERROR)
709  test_assert(TEST 1 EQUAL ${errors}
710              COMMENT "No error occurred when error was expected.\nExpected error: ${expect_failure}"
711  )
712  set_property(GLOBAL PROPERTY EXPECTED_ERROR 0)
713endfunction()
714
715yaml_load(FILE ${CMAKE_CURRENT_LIST_DIR}/test.yaml NAME yaml-test)
716test_reading_string()
717test_reading_int()
718test_reading_list_strings()
719test_reading_list_int()
720test_reading_map_list_entry()
721test_reading_not_found()
722test_reading_not_found_array()
723test_reading_not_array()
724test_reading_not_found_map_list_entry()
725
726test_save_new_file()
727
728test_setting_int()
729test_setting_string()
730test_setting_list_strings()
731test_setting_list_int()
732test_setting_map_list_entry()
733test_setting_map_list_entry_windows()
734test_setting_map_list_entry_commas()
735
736test_setting_empty_value()
737test_setting_empty_list()
738
739test_append_list_strings()
740
741test_set_remove_int()
742
743test_fail_missing_filename()
744
745# Generator expressions cannot be tested immediately, since they are expanded
746# at project generation time. The script mode re-run is delayed until after
747# the associated target has been built, and the verification is performed at
748# that time.
749if(NOT CMAKE_SCRIPT_MODE_FILE)
750  # create a file containing genexes
751  test_setting_genexes()
752
753  # Spawn a new CMake instance to re-run the whole test suite in script mode
754  # and verify the genex values once the associated target is built.
755  add_custom_command(TARGET app POST_BUILD
756    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_LIST_FILE}
757  )
758else()
759  # verify the contents of the created genex file
760  test_verify_genexes()
761endif()
762