1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #include <zephyr/ztest.h>
7 #include <zephyr/devicetree.h>
8 
9 /* Initial test config values set by `prj.conf` */
10 #define TEST_FOO_VAL_INIT		(774392)
11 #define TEST_BAR_VAL_INIT		(182834)
12 #define TEST_COMMON_VAL_INIT		(588411)
13 
14 /* Test config values set by the `foo` snippet */
15 #define TEST_FOO_VAL_FOO		(464372)
16 #define TEST_COMMON_VAL_FOO		(271983)
17 
18 /* Test config values set by the `bar` snippet */
19 #define TEST_BAR_VAL_BAR		(964183)
20 #define TEST_COMMON_VAL_BAR		(109234)
21 
22 ZTEST_SUITE(snippet_tests, NULL, NULL, NULL, NULL, NULL);
23 
ZTEST(snippet_tests,test_overlay_config)24 ZTEST(snippet_tests, test_overlay_config)
25 {
26 	if (IS_ENABLED(CONFIG_TEST_TYPE_NONE)) {
27 		/*
28 		 * When no snippet is applied, test that the config values
29 		 * correspond to the initial values set by `prj.conf`.
30 		 */
31 		zassert_equal(CONFIG_TEST_FOO_VAL, TEST_FOO_VAL_INIT);
32 		zassert_equal(CONFIG_TEST_BAR_VAL, TEST_BAR_VAL_INIT);
33 		zassert_equal(CONFIG_TEST_COMMON_VAL, TEST_COMMON_VAL_INIT);
34 	} else if (IS_ENABLED(CONFIG_TEST_TYPE_FOO)) {
35 		/*
36 		 * When `foo` snippet is applied, test that the foo and common
37 		 * config values correspond to the values set by the `foo`
38 		 * snippet.
39 		 */
40 		zassert_equal(CONFIG_TEST_FOO_VAL, TEST_FOO_VAL_FOO);
41 		zassert_equal(CONFIG_TEST_BAR_VAL, TEST_BAR_VAL_INIT);
42 		zassert_equal(CONFIG_TEST_COMMON_VAL, TEST_COMMON_VAL_FOO);
43 	} else if (IS_ENABLED(CONFIG_TEST_TYPE_BAR)) {
44 		/*
45 		 * When `bar` snippet is applied, test that the bar and common
46 		 * config values correspond to the values set by the `bar`
47 		 * snippet.
48 		 */
49 		zassert_equal(CONFIG_TEST_FOO_VAL, TEST_FOO_VAL_INIT);
50 		zassert_equal(CONFIG_TEST_BAR_VAL, TEST_BAR_VAL_BAR);
51 		zassert_equal(CONFIG_TEST_COMMON_VAL, TEST_COMMON_VAL_BAR);
52 	} else if (IS_ENABLED(CONFIG_TEST_TYPE_FOO_BAR)) {
53 		/*
54 		 * When `foo` and `bar` snippets are applied, with `bar`
55 		 * specified after `foo`, test that the common config value
56 		 * corresponds to the value set by the `bar` snippet.
57 		 */
58 		zassert_equal(CONFIG_TEST_FOO_VAL, TEST_FOO_VAL_FOO);
59 		zassert_equal(CONFIG_TEST_BAR_VAL, TEST_BAR_VAL_BAR);
60 		zassert_equal(CONFIG_TEST_COMMON_VAL, TEST_COMMON_VAL_BAR);
61 	} else if (IS_ENABLED(CONFIG_TEST_TYPE_BAR_FOO)) {
62 		/*
63 		 * When `foo` and `bar` snippets are applied, with `foo`
64 		 * specified after `bar`, test that the common config value
65 		 * corresponds to the value set by the `foo` snippet.
66 		 */
67 		zassert_equal(CONFIG_TEST_FOO_VAL, TEST_FOO_VAL_FOO);
68 		zassert_equal(CONFIG_TEST_BAR_VAL, TEST_BAR_VAL_BAR);
69 		zassert_equal(CONFIG_TEST_COMMON_VAL, TEST_COMMON_VAL_FOO);
70 	} else {
71 		zassert(false, "Invalid test type");
72 	}
73 }
74 
ZTEST(snippet_tests,test_dtc_overlay)75 ZTEST(snippet_tests, test_dtc_overlay)
76 {
77 	if (IS_ENABLED(CONFIG_TEST_TYPE_NONE)) {
78 		/*
79 		 * When no snippet is applied, test that both `deleted-by-foo`
80 		 * and `deleted-by-bar` nodes exist, and that none of the nodes
81 		 * added by the `foo` and `bar` snippets exist.
82 		 */
83 		zassert_true(DT_NODE_EXISTS(DT_PATH(deleted_by_foo)));
84 		zassert_true(DT_NODE_EXISTS(DT_PATH(deleted_by_bar)));
85 		zassert_false(DT_NODE_EXISTS(DT_PATH(added_by_foo)));
86 		zassert_false(DT_NODE_EXISTS(DT_PATH(added_by_bar)));
87 	} else if (IS_ENABLED(CONFIG_TEST_TYPE_FOO)) {
88 		/*
89 		 * When `foo` snippet is applied, test that `deleted-by-foo` is
90 		 * deleted and `added-by-foo` is added.
91 		 */
92 		zassert_false(DT_NODE_EXISTS(DT_PATH(deleted_by_foo)));
93 		zassert_true(DT_NODE_EXISTS(DT_PATH(deleted_by_bar)));
94 		zassert_true(DT_NODE_EXISTS(DT_PATH(added_by_foo)));
95 		zassert_false(DT_NODE_EXISTS(DT_PATH(added_by_bar)));
96 	} else if (IS_ENABLED(CONFIG_TEST_TYPE_BAR)) {
97 		/*
98 		 * When `bar` snippet is applied, test that `deleted-by-bar` is
99 		 * deleted and `added-by-bar` is added.
100 		 */
101 		zassert_true(DT_NODE_EXISTS(DT_PATH(deleted_by_foo)));
102 		zassert_false(DT_NODE_EXISTS(DT_PATH(deleted_by_bar)));
103 		zassert_false(DT_NODE_EXISTS(DT_PATH(added_by_foo)));
104 		zassert_true(DT_NODE_EXISTS(DT_PATH(added_by_bar)));
105 	} else if (IS_ENABLED(CONFIG_TEST_TYPE_FOO_BAR)) {
106 		/*
107 		 * When `foo` and `bar` snippets are applied, with `bar`
108 		 * specified after `foo`, test that both `deleted-by-foo` and
109 		 * `deleted-by-bar` are deleted, and that the `added-by-foo` is
110 		 * deleted by the `bar` snippet.
111 		 */
112 		zassert_false(DT_NODE_EXISTS(DT_PATH(deleted_by_foo)));
113 		zassert_false(DT_NODE_EXISTS(DT_PATH(deleted_by_bar)));
114 		zassert_false(DT_NODE_EXISTS(DT_PATH(added_by_foo)));
115 		zassert_true(DT_NODE_EXISTS(DT_PATH(added_by_bar)));
116 	} else if (IS_ENABLED(CONFIG_TEST_TYPE_BAR_FOO)) {
117 		/*
118 		 * When `foo` and `bar` snippets are applied, with `foo`
119 		 * specified after `bar`, test that both `deleted-by-foo` and
120 		 * `deleted-by-bar` are deleted, and that the `added-by-bar` is
121 		 * deleted by the `foo` snippet.
122 		 */
123 		zassert_false(DT_NODE_EXISTS(DT_PATH(deleted_by_foo)));
124 		zassert_false(DT_NODE_EXISTS(DT_PATH(deleted_by_bar)));
125 		zassert_true(DT_NODE_EXISTS(DT_PATH(added_by_foo)));
126 		zassert_false(DT_NODE_EXISTS(DT_PATH(added_by_bar)));
127 	} else {
128 		zassert(false, "Invalid test type");
129 	}
130 }
131 
ZTEST(snippet_tests,test_cmake_include)132 ZTEST(snippet_tests, test_cmake_include)
133 {
134 	if (IS_ENABLED(CONFIG_TEST_TYPE_FOO) ||
135 	    IS_ENABLED(CONFIG_TEST_TYPE_FOO_BAR) ||
136 	    IS_ENABLED(CONFIG_TEST_TYPE_BAR_FOO)) {
137 		zassert_true(DT_NODE_EXISTS(DT_PATH(cmake_dts_configure)));
138 	} else {
139 		zassert_false(DT_NODE_EXISTS(DT_PATH(cmake_dts_configure)));
140 	}
141 }
142