Merge pull request 'schemagen: add C++20 keywords to the C++ keyword allow-list' (#22) from weaselbot/weaseljson:weaselbot/issue-18 into main

Reviewed-on: weaselab/weaseljson#22
This commit is contained in:
2026-06-22 00:54:04 +00:00
2 changed files with 47 additions and 0 deletions
+38
View File
@@ -49,5 +49,43 @@ class SchemagenEnumTest(unittest.TestCase):
self.assertIn("enum class Role : int { admin, user, guest };", stdout)
class SchemagenKeywordTest(unittest.TestCase):
def run_schemagen(self, schema, args=None):
"""Run schemagen on a schema dict. Returns (returncode, stdout, stderr)."""
with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False) as fp:
json.dump(schema, fp)
schema_path = fp.name
try:
cmd = [sys.executable, SCRIPT, schema_path]
if args:
cmd.extend(args)
result = subprocess.run(cmd, capture_output=True, text=True, check=False)
return result.returncode, result.stdout, result.stderr
finally:
os.unlink(schema_path)
def test_cpp20_keywords_are_sanitized(self):
"""C++20 keywords used as JSON property names must be suffixed."""
keywords = [
"concept",
"consteval",
"constinit",
"co_await",
"co_return",
"co_yield",
"requires",
"module",
"import",
]
schema = {"type": "object", "properties": {}}
for kw in keywords:
schema["properties"][kw] = {"type": "string"}
rc, stdout, stderr = self.run_schemagen(schema)
self.assertEqual(rc, 0, msg=stderr)
for kw in keywords:
self.assertIn(f"std::optional<std::string> {kw}_;", stdout)
self.assertNotIn(f"std::optional<std::string> {kw};", stdout)
if __name__ == "__main__":
unittest.main()
@@ -133,9 +133,15 @@ _CPP_KEYWORDS = {
"catch",
"char",
"class",
"concept",
"const",
"consteval",
"constinit",
"constexpr",
"continue",
"co_await",
"co_return",
"co_yield",
"decltype",
"default",
"delete",
@@ -152,9 +158,11 @@ _CPP_KEYWORDS = {
"friend",
"goto",
"if",
"import",
"inline",
"int",
"long",
"module",
"namespace",
"new",
"not",
@@ -166,6 +174,7 @@ _CPP_KEYWORDS = {
"public",
"register",
"return",
"requires",
"short",
"signed",
"sizeof",