Skip to content

Commit 50182c4

Browse files
author
Ronan Chauvin
committed
1 parent 4878735 commit 50182c4

File tree

6 files changed

+790
-1
lines changed

6 files changed

+790
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Module: cjson
3+
Purpose: Provides the cjson library compileable as a Bazel target. Includes general and unity tests through Bazel
4+
Note: cjson_utils target is also available
5+
"""
6+
7+
module(
8+
name = "cjson",
9+
version = "1.7.19-0.20240923110858-12c4bf1986c2.bcr.2",
10+
compatibility_level = 1,
11+
)
12+
13+
bazel_dep(name = "rules_cc", version = "0.1.1")
14+
bazel_dep(name = "platforms", version = "0.0.11")
Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
diff --git a/BUILD.bazel b/BUILD.bazel
2+
new file mode 100644
3+
index 0000000..60e3a76
4+
--- /dev/null
5+
+++ b/BUILD.bazel
6+
@@ -0,0 +1,82 @@
7+
+load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
8+
+
9+
+CJSON_COPTS = select({
10+
+ "@bazel_tools//src/conditions:darwin": [
11+
+ "-std=c89", # Standards compliance
12+
+ "-Wc++-compat", # C++ compatibility
13+
+ "-fvisibility=hidden", # Symbol visibility
14+
+ ],
15+
+ "@bazel_tools//src/conditions:linux": [
16+
+ "-std=c89", # Standards compliance
17+
+ "-Wc++-compat", # C++ compatibility
18+
+ "-fstack-protector-strong", # Security (GCC/Clang on Linux supports this)
19+
+ "-fvisibility=hidden", # Symbol visibility
20+
+ ],
21+
+ "@bazel_tools//src/conditions:windows": [
22+
+ "/Za", # Equivalent to -std=c89 (enforce ANSI C)
23+
+ "/W3", # Moderate warning level (instead of -Wc++-compat)
24+
+ "/GS", # Stack protection (equivalent to -fstack-protect-strong)
25+
+ ],
26+
+ "//conditions:default": [ # Used for baremetal
27+
+ "-std=c89", # Standards compliance
28+
+ "-Wc++-compat", # C++ compatibility
29+
+ ],
30+
+})
31+
+
32+
+cc_library(
33+
+ name = "cjson",
34+
+ srcs = ["cJSON.c"],
35+
+ hdrs = ["include/cJSON.h"],
36+
+ includes = ["include"],
37+
+ copts = CJSON_COPTS + [
38+
+ "-DCJSON_EXPORT_SYMBOLS",
39+
+ "-DCJSON_API_VISIBILITY",
40+
+ ],
41+
+ linkopts = select({
42+
+ "@bazel_tools//src/conditions:windows": [],
43+
+ "//conditions:default": ["-lm"], # Link math library on non-Windows
44+
+ }),
45+
+ visibility = ["//visibility:public"],
46+
+)
47+
+
48+
+cc_library(
49+
+ name = "cjson_utils",
50+
+ srcs = ["cJSON_Utils.c"],
51+
+ hdrs = ["include/cJSON_Utils.h"],
52+
+ includes = ["include"],
53+
+ copts = CJSON_COPTS + [
54+
+ "-DCJSON_EXPORT_SYMBOLS",
55+
+ "-DCJSON_API_VISIBILITY",
56+
+ ],
57+
+ visibility = ["//visibility:public"],
58+
+ deps = [":cjson"],
59+
+)
60+
+
61+
+cc_library(
62+
+ name = "cjson_tests", # Some tests directly include the .c file
63+
+ hdrs = ["cJSON.c"],
64+
+ visibility = ["//tests:__pkg__"],
65+
+)
66+
+
67+
+cc_test(
68+
+ name = "cjson_test",
69+
+ size = "small",
70+
+ srcs = ["test.c"],
71+
+ copts = CJSON_COPTS + select({
72+
+ "@platforms//os:windows": [],
73+
+ "//conditions:default": [
74+
+ "-fno-sanitize=float-divide-by-zero",
75+
+ ],
76+
+ }),
77+
+ deps = [":cjson"],
78+
+)
79+
+
80+
+test_suite(
81+
+ name = "all_tests",
82+
+ tests = [
83+
+ ":cjson_test",
84+
+ "//tests:json_patch_tests",
85+
+ "//tests:parse_examples",
86+
+ "//tests:unity_tests",
87+
+ ],
88+
+)
89+
diff --git a/MODULE.bazel b/MODULE.bazel
90+
new file mode 100644
91+
index 0000000..06f600d
92+
--- /dev/null
93+
+++ b/MODULE.bazel
94+
@@ -0,0 +1,14 @@
95+
+"""
96+
+Module: cjson
97+
+Purpose: Provides the cjson library compileable as a Bazel target. Includes general and unity tests through Bazel
98+
+Note: cjson_utils target is also available
99+
+"""
100+
+
101+
+module(
102+
+ name = "cjson",
103+
+ version = "1.7.19-0.20240923110858-12c4bf1986c2.bcr.2",
104+
+ compatibility_level = 1,
105+
+)
106+
+
107+
+bazel_dep(name = "rules_cc", version = "0.1.1")
108+
+bazel_dep(name = "platforms", version = "0.0.11")
109+
diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel
110+
new file mode 100644
111+
index 0000000..baed64d
112+
--- /dev/null
113+
+++ b/tests/BUILD.bazel
114+
@@ -0,0 +1,70 @@
115+
+load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
116+
+load(":tools.bzl", "test_set")
117+
+
118+
+DEPS = [
119+
+ "//:cjson",
120+
+ "//:cjson_tests",
121+
+ "//:cjson_utils",
122+
+ "//tests/unity",
123+
+ "//tests/unity:unity-examples",
124+
+] + select({
125+
+ "@platforms//os:windows": [":unity-win"],
126+
+ "//conditions:default": [],
127+
+})
128+
+
129+
+DATA = [
130+
+ ":patch-test_inputs",
131+
+ ":test_inputs",
132+
+]
133+
+
134+
+HDRS = glob(["*.h"])
135+
+
136+
+cc_library(
137+
+ name = "unity-win",
138+
+ srcs = ["unity_setup.c"],
139+
+)
140+
+
141+
+filegroup(
142+
+ name = "test_inputs",
143+
+ srcs = glob(["inputs/*"]),
144+
+ visibility = ["//tests:__pkg__"],
145+
+)
146+
+
147+
+filegroup(
148+
+ name = "patch-test_inputs",
149+
+ srcs = glob(["json-patch-tests/*.json"]),
150+
+ visibility = ["//tests:__pkg__"],
151+
+)
152+
+
153+
+test_suite(
154+
+ name = "unity_tests",
155+
+ tests = test_set(
156+
+ srcs = HDRS,
157+
+ data = DATA,
158+
+ test_files = glob(
159+
+ ["*.c"],
160+
+ exclude = [
161+
+ "unity_setup.c",
162+
+ "parse_examples.c",
163+
+ "json_patch_tests.c",
164+
+ ],
165+
+ ),
166+
+ deps = DEPS,
167+
+ ),
168+
+)
169+
+
170+
+cc_test(
171+
+ name = "parse_examples",
172+
+ srcs = HDRS + ["parse_examples.c"],
173+
+ copts = ['-DTEST_DIR_PATH=\\"tests/inputs/\\"'],
174+
+ data = DATA,
175+
+ deps = DEPS,
176+
+)
177+
+
178+
+cc_test(
179+
+ name = "json_patch_tests",
180+
+ srcs = HDRS + ["json_patch_tests.c"],
181+
+ copts = ['-DTEST_DIR_PATH=\\"tests/json-patch-tests/\\"'],
182+
+ data = DATA,
183+
+ deps = DEPS,
184+
+)
185+
diff --git a/tests/json_patch_tests.c b/tests/json_patch_tests.c
186+
index 7c3d6ae..731da6e 100644
187+
--- a/tests/json_patch_tests.c
188+
+++ b/tests/json_patch_tests.c
189+
@@ -31,0 +32,4 @@
190+
+#ifndef TEST_DIR_PATH
191+
+#define TEST_DIR_PATH "json-patch-tests/"
192+
+#endif
193+
+
194+
@@ -185 +189 @@ static void cjson_utils_should_pass_json_patch_test_tests(void)
195+
- cJSON *tests = parse_test_file("json-patch-tests/tests.json");
196+
+ cJSON *tests = parse_test_file(TEST_DIR_PATH "tests.json");
197+
@@ -202 +206 @@ static void cjson_utils_should_pass_json_patch_test_spec_tests(void)
198+
- cJSON *tests = parse_test_file("json-patch-tests/spec_tests.json");
199+
+ cJSON *tests = parse_test_file(TEST_DIR_PATH "spec_tests.json");
200+
@@ -219 +223 @@ static void cjson_utils_should_pass_json_patch_test_cjson_utils_tests(void)
201+
- cJSON *tests = parse_test_file("json-patch-tests/cjson-utils-tests.json");
202+
+ cJSON *tests = parse_test_file(TEST_DIR_PATH "cjson-utils-tests.json");
203+
diff --git a/tests/parse_examples.c b/tests/parse_examples.c
204+
index d35d6cf..1e12e89 100644
205+
--- a/tests/parse_examples.c
206+
+++ b/tests/parse_examples.c
207+
@@ -60,0 +61 @@ static void do_test(const char *test_name)
208+
+#ifndef TEST_DIR_PATH
209+
@@ -61,0 +63 @@ static void do_test(const char *test_name)
210+
+#endif
211+
@@ -139 +141,6 @@ static void file_test6_should_not_be_parsed(void)
212+
- test6 = read_file("inputs/test6");
213+
+ char *test_path = NULL;
214+
+ const char * test_name = "test6";
215+
+ test_path = (char*)malloc(sizeof(TEST_DIR_PATH) + strlen(test_name));
216+
+ TEST_ASSERT_NOT_NULL_MESSAGE(test_path, "Failed to allocate test_path buffer.");
217+
+ sprintf(test_path, TEST_DIR_PATH"%s", test_name);
218+
+ test6 = read_file(test_path);
219+
diff --git a/tests/tools.bzl b/tests/tools.bzl
220+
new file mode 100644
221+
index 0000000..6098b6c
222+
--- /dev/null
223+
+++ b/tests/tools.bzl
224+
@@ -0,0 +1,44 @@
225+
+"""Tool for generating C++ test targets efficiently using Bazel rules.
226+
+"""
227+
+
228+
+load("@rules_cc//cc:defs.bzl", "cc_test")
229+
+
230+
+def test_set(
231+
+ test_files = None,
232+
+ size = "small",
233+
+ srcs = [],
234+
+ file_extensions = ".c",
235+
+ **kwargs):
236+
+ """Creates C++ test targets from a list of test files.
237+
+
238+
+ Args:
239+
+ test_files: List of test file paths to process. Defaults to None.
240+
+ size: Test size parameter for cc_test rule. Defaults to "small".
241+
+ srcs: Additional source files to include in all tests. Defaults to empty list.
242+
+ file_extensions: Expected extension of test files. Defaults to ".cpp".
243+
+ **kwargs: Additional arguments to pass to cc_test rule.
244+
+
245+
+ Returns:
246+
+ List of test target names (e.g., [":test1", ":test2"]).
247+
+
248+
+ Note:
249+
+ Only files ending with the specified file_extensions are processed.
250+
+ Each test target is created with the filename (without extension) as its name.
251+
+ """
252+
+ test_targets = []
253+
+
254+
+ # Process positive tests
255+
+ for file in test_files:
256+
+ if not file.endswith(file_extensions):
257+
+ continue
258+
+ name = file[:-len(file_extensions)]
259+
+ target = ":" + name
260+
+ cc_test(
261+
+ name = name,
262+
+ size = size,
263+
+ srcs = srcs + [file],
264+
+ **kwargs
265+
+ )
266+
+ test_targets.append(target)
267+
+
268+
+ return test_targets
269+
diff --git a/tests/unity/BUILD.bazel b/tests/unity/BUILD.bazel
270+
new file mode 100644
271+
index 0000000..e8c4d59
272+
--- /dev/null
273+
+++ b/tests/unity/BUILD.bazel
274+
@@ -0,0 +1,27 @@
275+
+load("@rules_cc//cc:defs.bzl", "cc_library")
276+
+
277+
+cc_library(
278+
+ name = "unity",
279+
+ srcs = glob(["src/*.c"]),
280+
+ hdrs = glob(["src/*.h"]),
281+
+ copts = select({
282+
+ "@platforms//os:windows": [
283+
+ "/wd4100", # Disable warning C4100: unreferenced formal parameter (similar to -Wno-error)
284+
+ "/wd4065", # Disable warning C4065: switch statement contains default but no case (similar to -Wno-switch-enum)
285+
+ ],
286+
+ "//conditions:default": [
287+
+ "-fvisibility=default",
288+
+ "-fno-sanitize=float-divide-by-zero",
289+
+ "-Wno-switch-enum",
290+
+ ],
291+
+ }),
292+
+ includes = ["src"],
293+
+ visibility = ["//tests:__pkg__"],
294+
+ deps = [":unity-examples"],
295+
+)
296+
+
297+
+cc_library(
298+
+ name = "unity-examples",
299+
+ hdrs = ["examples/unity_config.h"],
300+
+ visibility = ["//tests:__pkg__"],
301+
+)

0 commit comments

Comments
 (0)