test-json-schema-to-grammar.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. #ifdef NDEBUG
  2. #undef NDEBUG
  3. #endif
  4. #include "json-schema-to-grammar.h"
  5. #include "llama-grammar.h"
  6. #include <cassert>
  7. #include <fstream>
  8. #include <sstream>
  9. #include <regex>
  10. static std::string trim(const std::string & source) {
  11. std::string s(source);
  12. s.erase(0,s.find_first_not_of(" \n\r\t"));
  13. s.erase(s.find_last_not_of(" \n\r\t")+1);
  14. return std::regex_replace(s, std::regex("(^|\n)[ \t]+"), "$1");
  15. }
  16. enum TestCaseStatus {
  17. SUCCESS,
  18. FAILURE
  19. };
  20. struct TestCase {
  21. TestCaseStatus expected_status;
  22. std::string name;
  23. std::string schema;
  24. std::string expected_grammar;
  25. void _print_failure_header() const {
  26. fprintf(stderr, "#\n# Test '%s' failed.\n#\n%s\n", name.c_str(), schema.c_str());
  27. }
  28. void verify(const std::string & actual_grammar) const {
  29. if (trim(actual_grammar) != trim(expected_grammar)) {
  30. _print_failure_header();
  31. fprintf(stderr, "# EXPECTED:\n%s\n# ACTUAL:\n%s\n", expected_grammar.c_str(), actual_grammar.c_str());
  32. assert(false);
  33. }
  34. }
  35. void verify_expectation_parseable() const {
  36. try {
  37. llama_grammar_parser state;
  38. state.parse(expected_grammar.c_str());
  39. if (state.symbol_ids.find("root") == state.symbol_ids.end()) {
  40. throw std::runtime_error("Grammar failed to parse:\n" + expected_grammar);
  41. }
  42. } catch (const std::runtime_error & ex) {
  43. _print_failure_header();
  44. fprintf(stderr, "# GRAMMAR ERROR: %s\n", ex.what());
  45. assert(false);
  46. }
  47. }
  48. void verify_status(TestCaseStatus status) const {
  49. if (status != expected_status) {
  50. _print_failure_header();
  51. fprintf(stderr, "# EXPECTED STATUS: %s\n", expected_status == SUCCESS ? "SUCCESS" : "FAILURE");
  52. fprintf(stderr, "# ACTUAL STATUS: %s\n", status == SUCCESS ? "SUCCESS" : "FAILURE");
  53. assert(false);
  54. }
  55. }
  56. };
  57. static void write(const std::string & file, const std::string & content) {
  58. std::ofstream f;
  59. f.open(file.c_str());
  60. f << content.c_str();
  61. f.close();
  62. }
  63. static std::string read(const std::string & file) {
  64. std::ostringstream actuals;
  65. actuals << std::ifstream(file.c_str()).rdbuf();
  66. return actuals.str();
  67. }
  68. static void test_all(const std::string & lang, std::function<void(const TestCase &)> runner) {
  69. fprintf(stderr, "#\n# Testing JSON schema conversion (%s)\n#\n", lang.c_str());
  70. auto test = [&](const TestCase & tc) {
  71. fprintf(stderr, "- %s%s\n", tc.name.c_str(), tc.expected_status == FAILURE ? " (failure expected)" : "");
  72. runner(tc);
  73. };
  74. test({
  75. SUCCESS,
  76. "min 0",
  77. R"""({
  78. "type": "integer",
  79. "minimum": 0
  80. })""",
  81. R"""(
  82. root ::= ([0] | [1-9] [0-9]{0,15}) space
  83. space ::= | " " | "\n" [ \t]{0,20}
  84. )"""
  85. });
  86. test({
  87. SUCCESS,
  88. "min 1",
  89. R"""({
  90. "type": "integer",
  91. "minimum": 1
  92. })""",
  93. R"""(
  94. root ::= ([1-9] [0-9]{0,15}) space
  95. space ::= | " " | "\n" [ \t]{0,20}
  96. )"""
  97. });
  98. test({
  99. SUCCESS,
  100. "min 3",
  101. R"""({
  102. "type": "integer",
  103. "minimum": 3
  104. })""",
  105. R"""(
  106. root ::= ([1-2] [0-9]{1,15} | [3-9] [0-9]{0,15}) space
  107. space ::= | " " | "\n" [ \t]{0,20}
  108. )"""
  109. });
  110. test({
  111. SUCCESS,
  112. "min 9",
  113. R"""({
  114. "type": "integer",
  115. "minimum": 9
  116. })""",
  117. R"""(
  118. root ::= ([1-8] [0-9]{1,15} | [9] [0-9]{0,15}) space
  119. space ::= | " " | "\n" [ \t]{0,20}
  120. )"""
  121. });
  122. test({
  123. SUCCESS,
  124. "min 10",
  125. R"""({
  126. "type": "integer",
  127. "minimum": 10
  128. })""",
  129. R"""(
  130. root ::= ([1] ([0-9]{1,15}) | [2-9] [0-9]{1,15}) space
  131. space ::= | " " | "\n" [ \t]{0,20}
  132. )"""
  133. });
  134. test({
  135. SUCCESS,
  136. "min 25",
  137. R"""({
  138. "type": "integer",
  139. "minimum": 25
  140. })""",
  141. R"""(
  142. root ::= ([1] [0-9]{2,15} | [2] ([0-4] [0-9]{1,14} | [5-9] [0-9]{0,14}) | [3-9] [0-9]{1,15}) space
  143. space ::= | " " | "\n" [ \t]{0,20}
  144. )"""
  145. });
  146. test({
  147. SUCCESS,
  148. "max 30",
  149. R"""({
  150. "type": "integer",
  151. "maximum": 30
  152. })""",
  153. R"""(
  154. root ::= ("-" [1-9] [0-9]{0,15} | [0-9] | ([1-2] [0-9] | [3] "0")) space
  155. space ::= | " " | "\n" [ \t]{0,20}
  156. )"""
  157. });
  158. test({
  159. SUCCESS,
  160. "min -5",
  161. R"""({
  162. "type": "integer",
  163. "minimum": -5
  164. })""",
  165. R"""(
  166. root ::= ("-" ([0-5]) | [0] | [1-9] [0-9]{0,15}) space
  167. space ::= | " " | "\n" [ \t]{0,20}
  168. )"""
  169. });
  170. test({
  171. SUCCESS,
  172. "min -123",
  173. R"""({
  174. "type": "integer",
  175. "minimum": -123
  176. })""",
  177. R"""(
  178. root ::= ("-" ([0-9] | ([1-8] [0-9] | [9] [0-9]) | "1" ([0-1] [0-9] | [2] [0-3])) | [0] | [1-9] [0-9]{0,15}) space
  179. space ::= | " " | "\n" [ \t]{0,20}
  180. )"""
  181. });
  182. test({
  183. SUCCESS,
  184. "max -5",
  185. R"""({
  186. "type": "integer",
  187. "maximum": -5
  188. })""",
  189. R"""(
  190. root ::= ("-" ([0-4] [0-9]{1,15} | [5-9] [0-9]{0,15})) space
  191. space ::= | " " | "\n" [ \t]{0,20}
  192. )"""
  193. });
  194. test({
  195. SUCCESS,
  196. "max 1",
  197. R"""({
  198. "type": "integer",
  199. "maximum": 1
  200. })""",
  201. R"""(
  202. root ::= ("-" [1-9] [0-9]{0,15} | [0-1]) space
  203. space ::= | " " | "\n" [ \t]{0,20}
  204. )"""
  205. });
  206. test({
  207. SUCCESS,
  208. "max 100",
  209. R"""({
  210. "type": "integer",
  211. "maximum": 100
  212. })""",
  213. R"""(
  214. root ::= ("-" [1-9] [0-9]{0,15} | [0-9] | ([1-8] [0-9] | [9] [0-9]) | "100") space
  215. space ::= | " " | "\n" [ \t]{0,20}
  216. )"""
  217. });
  218. test({
  219. SUCCESS,
  220. "min 0 max 23",
  221. R"""({
  222. "type": "integer",
  223. "minimum": 0,
  224. "maximum": 23
  225. })""",
  226. R"""(
  227. root ::= ([0-9] | ([1] [0-9] | [2] [0-3])) space
  228. space ::= | " " | "\n" [ \t]{0,20}
  229. )"""
  230. });
  231. test({
  232. SUCCESS,
  233. "min 15 max 300",
  234. R"""({
  235. "type": "integer",
  236. "minimum": 15,
  237. "maximum": 300
  238. })""",
  239. R"""(
  240. root ::= (([1] ([5-9]) | [2-9] [0-9]) | ([1-2] [0-9]{2} | [3] "00")) space
  241. space ::= | " " | "\n" [ \t]{0,20}
  242. )"""
  243. });
  244. test({
  245. SUCCESS,
  246. "min 5 max 30",
  247. R"""({
  248. "type": "integer",
  249. "minimum": 5,
  250. "maximum": 30
  251. })""",
  252. R"""(
  253. root ::= ([5-9] | ([1-2] [0-9] | [3] "0")) space
  254. space ::= | " " | "\n" [ \t]{0,20}
  255. )"""
  256. });
  257. test({
  258. SUCCESS,
  259. "min -123 max 42",
  260. R"""({
  261. "type": "integer",
  262. "minimum": -123,
  263. "maximum": 42
  264. })""",
  265. R"""(
  266. root ::= ("-" ([0-9] | ([1-8] [0-9] | [9] [0-9]) | "1" ([0-1] [0-9] | [2] [0-3])) | [0-9] | ([1-3] [0-9] | [4] [0-2])) space
  267. space ::= | " " | "\n" [ \t]{0,20}
  268. )"""
  269. });
  270. test({
  271. SUCCESS,
  272. "min -10 max 10",
  273. R"""({
  274. "type": "integer",
  275. "minimum": -10,
  276. "maximum": 10
  277. })""",
  278. R"""(
  279. root ::= ("-" ([0-9] | "10") | [0-9] | "10") space
  280. space ::= | " " | "\n" [ \t]{0,20}
  281. )"""
  282. });
  283. test({
  284. FAILURE,
  285. "unknown type",
  286. R"""({
  287. "type": "kaboom"
  288. })""",
  289. ""
  290. });
  291. test({
  292. FAILURE,
  293. "invalid type",
  294. R"""({
  295. "type": 123
  296. })""",
  297. ""
  298. });
  299. test({
  300. SUCCESS,
  301. "empty schema (object)",
  302. "{}",
  303. R"""(
  304. array ::= "[" space ( value ("," space value)* )? "]" space
  305. boolean ::= ("true" | "false") space
  306. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  307. decimal-part ::= [0-9]{1,16}
  308. integral-part ::= [0] | [1-9] [0-9]{0,15}
  309. null ::= "null" space
  310. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  311. object ::= "{" space ( string ":" space value ("," space string ":" space value)* )? "}" space
  312. root ::= object
  313. space ::= | " " | "\n" [ \t]{0,20}
  314. string ::= "\"" char* "\"" space
  315. value ::= object | array | string | number | boolean | null
  316. )"""
  317. });
  318. test({
  319. SUCCESS,
  320. "exotic formats",
  321. R"""({
  322. "items": [
  323. { "format": "date" },
  324. { "format": "uuid" },
  325. { "format": "time" },
  326. { "format": "date-time" }
  327. ]
  328. })""",
  329. R"""(
  330. date ::= [0-9]{4} "-" ( "0" [1-9] | "1" [0-2] ) "-" ( "0" [1-9] | [1-2] [0-9] | "3" [0-1] )
  331. date-string ::= "\"" date "\"" space
  332. date-time ::= date "T" time
  333. date-time-string ::= "\"" date-time "\"" space
  334. root ::= "[" space tuple-0 "," space uuid "," space tuple-2 "," space tuple-3 "]" space
  335. space ::= | " " | "\n" [ \t]{0,20}
  336. time ::= ([01] [0-9] | "2" [0-3]) ":" [0-5] [0-9] ":" [0-5] [0-9] ( "." [0-9]{3} )? ( "Z" | ( "+" | "-" ) ( [01] [0-9] | "2" [0-3] ) ":" [0-5] [0-9] )
  337. time-string ::= "\"" time "\"" space
  338. tuple-0 ::= date-string
  339. tuple-2 ::= time-string
  340. tuple-3 ::= date-time-string
  341. uuid ::= "\"" [0-9a-fA-F]{8} "-" [0-9a-fA-F]{4} "-" [0-9a-fA-F]{4} "-" [0-9a-fA-F]{4} "-" [0-9a-fA-F]{12} "\"" space
  342. )"""
  343. });
  344. test({
  345. SUCCESS,
  346. "string",
  347. R"""({
  348. "type": "string"
  349. })""",
  350. R"""(
  351. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  352. root ::= "\"" char* "\"" space
  353. space ::= | " " | "\n" [ \t]{0,20}
  354. )"""
  355. });
  356. test({
  357. SUCCESS,
  358. "string w/ min length 1",
  359. R"""({
  360. "type": "string",
  361. "minLength": 1
  362. })""",
  363. R"""(
  364. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  365. root ::= "\"" char+ "\"" space
  366. space ::= | " " | "\n" [ \t]{0,20}
  367. )"""
  368. });
  369. test({
  370. SUCCESS,
  371. "string w/ min length 3",
  372. R"""({
  373. "type": "string",
  374. "minLength": 3
  375. })""",
  376. R"""(
  377. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  378. root ::= "\"" char{3,} "\"" space
  379. space ::= | " " | "\n" [ \t]{0,20}
  380. )"""
  381. });
  382. test({
  383. SUCCESS,
  384. "string w/ max length",
  385. R"""({
  386. "type": "string",
  387. "maxLength": 3
  388. })""",
  389. R"""(
  390. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  391. root ::= "\"" char{0,3} "\"" space
  392. space ::= | " " | "\n" [ \t]{0,20}
  393. )"""
  394. });
  395. test({
  396. SUCCESS,
  397. "string w/ min & max length",
  398. R"""({
  399. "type": "string",
  400. "minLength": 1,
  401. "maxLength": 4
  402. })""",
  403. R"""(
  404. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  405. root ::= "\"" char{1,4} "\"" space
  406. space ::= | " " | "\n" [ \t]{0,20}
  407. )"""
  408. });
  409. test({
  410. SUCCESS,
  411. "boolean",
  412. R"""({
  413. "type": "boolean"
  414. })""",
  415. R"""(
  416. root ::= ("true" | "false") space
  417. space ::= | " " | "\n" [ \t]{0,20}
  418. )"""
  419. });
  420. test({
  421. SUCCESS,
  422. "integer",
  423. R"""({
  424. "type": "integer"
  425. })""",
  426. R"""(
  427. integral-part ::= [0] | [1-9] [0-9]{0,15}
  428. root ::= ("-"? integral-part) space
  429. space ::= | " " | "\n" [ \t]{0,20}
  430. )"""
  431. });
  432. test({
  433. SUCCESS,
  434. "string const",
  435. R"""({
  436. "const": "foo"
  437. })""",
  438. R"""(
  439. root ::= "\"foo\"" space
  440. space ::= | " " | "\n" [ \t]{0,20}
  441. )"""
  442. });
  443. test({
  444. SUCCESS,
  445. "non-string const",
  446. R"""({
  447. "const": 123
  448. })""",
  449. R"""(
  450. root ::= "123" space
  451. space ::= | " " | "\n" [ \t]{0,20}
  452. )"""
  453. });
  454. test({
  455. SUCCESS,
  456. "non-string enum",
  457. R"""({
  458. "enum": ["red", "amber", "green", null, 42, ["foo"]]
  459. })""",
  460. R"""(
  461. root ::= ("\"red\"" | "\"amber\"" | "\"green\"" | "null" | "42" | "[\"foo\"]") space
  462. space ::= | " " | "\n" [ \t]{0,20}
  463. )"""
  464. });
  465. test({
  466. SUCCESS,
  467. "string array",
  468. R"""({
  469. "type": "array",
  470. "prefixItems": { "type": "string" }
  471. })""",
  472. R"""(
  473. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  474. root ::= "[" space (string ("," space string)*)? "]" space
  475. space ::= | " " | "\n" [ \t]{0,20}
  476. string ::= "\"" char* "\"" space
  477. )"""
  478. });
  479. test({
  480. SUCCESS,
  481. "nullable string array",
  482. R"""({
  483. "type": ["array", "null"],
  484. "prefixItems": { "type": "string" }
  485. })""",
  486. R"""(
  487. alternative-0 ::= "[" space (string ("," space string)*)? "]" space
  488. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  489. null ::= "null" space
  490. root ::= alternative-0 | null
  491. space ::= | " " | "\n" [ \t]{0,20}
  492. string ::= "\"" char* "\"" space
  493. )"""
  494. });
  495. test({
  496. SUCCESS,
  497. "tuple1",
  498. R"""({
  499. "prefixItems": [{ "type": "string" }]
  500. })""",
  501. R"""(
  502. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  503. root ::= "[" space string "]" space
  504. space ::= | " " | "\n" [ \t]{0,20}
  505. string ::= "\"" char* "\"" space
  506. )"""
  507. });
  508. test({
  509. SUCCESS,
  510. "tuple2",
  511. R"""({
  512. "prefixItems": [{ "type": "string" }, { "type": "number" }]
  513. })""",
  514. R"""(
  515. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  516. decimal-part ::= [0-9]{1,16}
  517. integral-part ::= [0] | [1-9] [0-9]{0,15}
  518. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  519. root ::= "[" space string "," space number "]" space
  520. space ::= | " " | "\n" [ \t]{0,20}
  521. string ::= "\"" char* "\"" space
  522. )"""
  523. });
  524. test({
  525. SUCCESS,
  526. "number",
  527. R"""({
  528. "type": "number"
  529. })""",
  530. R"""(
  531. decimal-part ::= [0-9]{1,16}
  532. integral-part ::= [0] | [1-9] [0-9]{0,15}
  533. root ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  534. space ::= | " " | "\n" [ \t]{0,20}
  535. )"""
  536. });
  537. test({
  538. SUCCESS,
  539. "minItems",
  540. R"""({
  541. "items": {
  542. "type": "boolean"
  543. },
  544. "minItems": 2
  545. })""",
  546. R"""(
  547. boolean ::= ("true" | "false") space
  548. root ::= "[" space boolean ("," space boolean)+ "]" space
  549. space ::= | " " | "\n" [ \t]{0,20}
  550. )"""
  551. });
  552. test({
  553. SUCCESS,
  554. "maxItems 1",
  555. R"""({
  556. "items": {
  557. "type": "boolean"
  558. },
  559. "maxItems": 1
  560. })""",
  561. R"""(
  562. boolean ::= ("true" | "false") space
  563. root ::= "[" space boolean? "]" space
  564. space ::= | " " | "\n" [ \t]{0,20}
  565. )"""
  566. });
  567. test({
  568. SUCCESS,
  569. "maxItems 2",
  570. R"""({
  571. "items": {
  572. "type": "boolean"
  573. },
  574. "maxItems": 2
  575. })""",
  576. R"""(
  577. boolean ::= ("true" | "false") space
  578. root ::= "[" space (boolean ("," space boolean)?)? "]" space
  579. space ::= | " " | "\n" [ \t]{0,20}
  580. )"""
  581. });
  582. test({
  583. SUCCESS,
  584. "min + maxItems",
  585. R"""({
  586. "items": {
  587. "type": ["number", "integer"]
  588. },
  589. "minItems": 3,
  590. "maxItems": 5
  591. })""",
  592. R"""(
  593. decimal-part ::= [0-9]{1,16}
  594. integer ::= ("-"? integral-part) space
  595. integral-part ::= [0] | [1-9] [0-9]{0,15}
  596. item ::= number | integer
  597. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  598. root ::= "[" space item ("," space item){2,4} "]" space
  599. space ::= | " " | "\n" [ \t]{0,20}
  600. )"""
  601. });
  602. test({
  603. SUCCESS,
  604. "min + max items with min + max values across zero",
  605. R"""({
  606. "items": {
  607. "type": "integer",
  608. "minimum": -12,
  609. "maximum": 207
  610. },
  611. "minItems": 3,
  612. "maxItems": 5
  613. })""",
  614. R"""(
  615. item ::= ("-" ([0-9] | "1" [0-2]) | [0-9] | ([1-8] [0-9] | [9] [0-9]) | ([1] [0-9]{2} | [2] "0" [0-7])) space
  616. root ::= "[" space item ("," space item){2,4} "]" space
  617. space ::= | " " | "\n" [ \t]{0,20}
  618. )"""
  619. });
  620. test({
  621. SUCCESS,
  622. "min + max items with min + max values",
  623. R"""({
  624. "items": {
  625. "type": "integer",
  626. "minimum": 12,
  627. "maximum": 207
  628. },
  629. "minItems": 3,
  630. "maxItems": 5
  631. })""",
  632. R"""(
  633. item ::= (([1] ([2-9]) | [2-9] [0-9]) | ([1] [0-9]{2} | [2] "0" [0-7])) space
  634. root ::= "[" space item ("," space item){2,4} "]" space
  635. space ::= | " " | "\n" [ \t]{0,20}
  636. )"""
  637. });
  638. test({
  639. SUCCESS,
  640. "simple regexp",
  641. R"""({
  642. "type": "string",
  643. "pattern": "^abc?d*efg+(hij)?kl$"
  644. })""",
  645. R"""(
  646. root ::= "\"" "ab" "c"? "d"* "ef" "g"+ ("hij")? "kl" "\"" space
  647. space ::= | " " | "\n" [ \t]{0,20}
  648. )"""
  649. });
  650. test({
  651. SUCCESS,
  652. "regexp escapes",
  653. R"""({
  654. "type": "string",
  655. "pattern": "^\\[\\]\\{\\}\\(\\)\\|\\+\\*\\?$"
  656. })""",
  657. R"""(
  658. root ::= "\"" "[]{}()|+*?" "\"" space
  659. space ::= | " " | "\n" [ \t]{0,20}
  660. )"""
  661. });
  662. test({
  663. SUCCESS,
  664. "regexp quote",
  665. R"""({
  666. "type": "string",
  667. "pattern": "^\"$"
  668. })""",
  669. R"""(
  670. root ::= "\"" "\"" "\"" space
  671. space ::= | " " | "\n" [ \t]{0,20}
  672. )"""
  673. });
  674. test({
  675. SUCCESS,
  676. "regexp",
  677. R"""({
  678. "type": "string",
  679. "pattern": "^(\\([0-9]{1,3}\\))?[0-9]{3}-[0-9]{4} a{3,5}nd...$"
  680. })""",
  681. R"""(
  682. dot ::= [^\x0A\x0D]
  683. root ::= "\"" ("(" root-1{1,3} ")")? root-1{3,3} "-" root-1{4,4} " " "a"{3,5} "nd" dot dot dot "\"" space
  684. root-1 ::= [0-9]
  685. space ::= | " " | "\n" [ \t]{0,20}
  686. )"""
  687. });
  688. test({
  689. SUCCESS,
  690. "required props in original order",
  691. R"""({
  692. "type": "object",
  693. "properties": {
  694. "b": {"type": "string"},
  695. "c": {"type": "string"},
  696. "a": {"type": "string"}
  697. },
  698. "required": [
  699. "a",
  700. "b",
  701. "c"
  702. ],
  703. "additionalProperties": false,
  704. "definitions": {}
  705. })""",
  706. R"""(
  707. a-kv ::= "\"a\"" space ":" space string
  708. b-kv ::= "\"b\"" space ":" space string
  709. c-kv ::= "\"c\"" space ":" space string
  710. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  711. root ::= "{" space b-kv "," space c-kv "," space a-kv "}" space
  712. space ::= | " " | "\n" [ \t]{0,20}
  713. string ::= "\"" char* "\"" space
  714. )"""
  715. });
  716. test({
  717. SUCCESS,
  718. "1 optional prop",
  719. R"""({
  720. "properties": {
  721. "a": {
  722. "type": "string"
  723. }
  724. },
  725. "additionalProperties": false
  726. })""",
  727. R"""(
  728. a-kv ::= "\"a\"" space ":" space string
  729. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  730. root ::= "{" space (a-kv )? "}" space
  731. space ::= | " " | "\n" [ \t]{0,20}
  732. string ::= "\"" char* "\"" space
  733. )"""
  734. });
  735. test({
  736. SUCCESS,
  737. "N optional props",
  738. R"""({
  739. "properties": {
  740. "a": {"type": "string"},
  741. "b": {"type": "string"},
  742. "c": {"type": "string"}
  743. },
  744. "additionalProperties": false
  745. })""",
  746. R"""(
  747. a-kv ::= "\"a\"" space ":" space string
  748. a-rest ::= ( "," space b-kv )? b-rest
  749. b-kv ::= "\"b\"" space ":" space string
  750. b-rest ::= ( "," space c-kv )?
  751. c-kv ::= "\"c\"" space ":" space string
  752. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  753. root ::= "{" space (a-kv a-rest | b-kv b-rest | c-kv )? "}" space
  754. space ::= | " " | "\n" [ \t]{0,20}
  755. string ::= "\"" char* "\"" space
  756. )"""
  757. });
  758. test({
  759. SUCCESS,
  760. "required + optional props each in original order",
  761. R"""({
  762. "properties": {
  763. "b": {"type": "string"},
  764. "a": {"type": "string"},
  765. "d": {"type": "string"},
  766. "c": {"type": "string"}
  767. },
  768. "required": ["a", "b"],
  769. "additionalProperties": false
  770. })""",
  771. R"""(
  772. a-kv ::= "\"a\"" space ":" space string
  773. b-kv ::= "\"b\"" space ":" space string
  774. c-kv ::= "\"c\"" space ":" space string
  775. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  776. d-kv ::= "\"d\"" space ":" space string
  777. d-rest ::= ( "," space c-kv )?
  778. root ::= "{" space b-kv "," space a-kv ( "," space ( d-kv d-rest | c-kv ) )? "}" space
  779. space ::= | " " | "\n" [ \t]{0,20}
  780. string ::= "\"" char* "\"" space
  781. )"""
  782. });
  783. test({
  784. SUCCESS,
  785. "additional props",
  786. R"""({
  787. "type": "object",
  788. "additionalProperties": {"type": "array", "items": {"type": "number"}}
  789. })""",
  790. R"""(
  791. additional-kv ::= string ":" space additional-value
  792. additional-value ::= "[" space (number ("," space number)*)? "]" space
  793. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  794. decimal-part ::= [0-9]{1,16}
  795. integral-part ::= [0] | [1-9] [0-9]{0,15}
  796. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  797. root ::= "{" space (additional-kv ( "," space additional-kv )* )? "}" space
  798. space ::= | " " | "\n" [ \t]{0,20}
  799. string ::= "\"" char* "\"" space
  800. )"""
  801. });
  802. test({
  803. SUCCESS,
  804. "additional props (true)",
  805. R"""({
  806. "type": "object",
  807. "additionalProperties": true
  808. })""",
  809. R"""(
  810. array ::= "[" space ( value ("," space value)* )? "]" space
  811. boolean ::= ("true" | "false") space
  812. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  813. decimal-part ::= [0-9]{1,16}
  814. integral-part ::= [0] | [1-9] [0-9]{0,15}
  815. null ::= "null" space
  816. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  817. object ::= "{" space ( string ":" space value ("," space string ":" space value)* )? "}" space
  818. root ::= object
  819. space ::= | " " | "\n" [ \t]{0,20}
  820. string ::= "\"" char* "\"" space
  821. value ::= object | array | string | number | boolean | null
  822. )"""
  823. });
  824. test({
  825. SUCCESS,
  826. "additional props (implicit)",
  827. R"""({
  828. "type": "object"
  829. })""",
  830. R"""(
  831. array ::= "[" space ( value ("," space value)* )? "]" space
  832. boolean ::= ("true" | "false") space
  833. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  834. decimal-part ::= [0-9]{1,16}
  835. integral-part ::= [0] | [1-9] [0-9]{0,15}
  836. null ::= "null" space
  837. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  838. object ::= "{" space ( string ":" space value ("," space string ":" space value)* )? "}" space
  839. root ::= object
  840. space ::= | " " | "\n" [ \t]{0,20}
  841. string ::= "\"" char* "\"" space
  842. value ::= object | array | string | number | boolean | null
  843. )"""
  844. });
  845. test({
  846. SUCCESS,
  847. "empty w/o additional props",
  848. R"""({
  849. "type": "object",
  850. "additionalProperties": false
  851. })""",
  852. R"""(
  853. root ::= "{" space "}" space
  854. space ::= | " " | "\n" [ \t]{0,20}
  855. )"""
  856. });
  857. test({
  858. SUCCESS,
  859. "required + additional props",
  860. R"""({
  861. "type": "object",
  862. "properties": {
  863. "a": {"type": "number"}
  864. },
  865. "required": ["a"],
  866. "additionalProperties": {"type": "string"}
  867. })""",
  868. R"""(
  869. a-kv ::= "\"a\"" space ":" space number
  870. additional-k ::= ["] ( [a] char+ | [^"a] char* )? ["] space
  871. additional-kv ::= additional-k ":" space string
  872. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  873. decimal-part ::= [0-9]{1,16}
  874. integral-part ::= [0] | [1-9] [0-9]{0,15}
  875. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  876. root ::= "{" space a-kv ( "," space ( additional-kv ( "," space additional-kv )* ) )? "}" space
  877. space ::= | " " | "\n" [ \t]{0,20}
  878. string ::= "\"" char* "\"" space
  879. )"""
  880. });
  881. test({
  882. SUCCESS,
  883. "optional + additional props",
  884. R"""({
  885. "type": "object",
  886. "properties": {
  887. "a": {"type": "number"}
  888. },
  889. "additionalProperties": {"type": "number"}
  890. })""",
  891. R"""(
  892. a-kv ::= "\"a\"" space ":" space number
  893. a-rest ::= ( "," space additional-kv )*
  894. additional-k ::= ["] ( [a] char+ | [^"a] char* )? ["] space
  895. additional-kv ::= additional-k ":" space number
  896. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  897. decimal-part ::= [0-9]{1,16}
  898. integral-part ::= [0] | [1-9] [0-9]{0,15}
  899. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  900. root ::= "{" space (a-kv a-rest | additional-kv ( "," space additional-kv )* )? "}" space
  901. space ::= | " " | "\n" [ \t]{0,20}
  902. )"""
  903. });
  904. test({
  905. SUCCESS,
  906. "required + optional + additional props",
  907. R"""({
  908. "type": "object",
  909. "properties": {
  910. "and": {"type": "number"},
  911. "also": {"type": "number"}
  912. },
  913. "required": ["and"],
  914. "additionalProperties": {"type": "number"}
  915. })""",
  916. R"""(
  917. additional-k ::= ["] ( [a] ([l] ([s] ([o] char+ | [^"o] char*) | [^"s] char*) | [n] ([d] char+ | [^"d] char*) | [^"ln] char*) | [^"a] char* )? ["] space
  918. additional-kv ::= additional-k ":" space number
  919. also-kv ::= "\"also\"" space ":" space number
  920. also-rest ::= ( "," space additional-kv )*
  921. and-kv ::= "\"and\"" space ":" space number
  922. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  923. decimal-part ::= [0-9]{1,16}
  924. integral-part ::= [0] | [1-9] [0-9]{0,15}
  925. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  926. root ::= "{" space and-kv ( "," space ( also-kv also-rest | additional-kv ( "," space additional-kv )* ) )? "}" space
  927. space ::= | " " | "\n" [ \t]{0,20}
  928. )"""
  929. });
  930. test({
  931. SUCCESS,
  932. "optional props with empty name",
  933. R"""({
  934. "properties": {
  935. "": {"type": "integer"},
  936. "a": {"type": "integer"}
  937. },
  938. "additionalProperties": {"type": "integer"}
  939. })""",
  940. R"""(
  941. -kv ::= "\"\"" space ":" space root
  942. -rest ::= ( "," space a-kv )? a-rest
  943. a-kv ::= "\"a\"" space ":" space integer
  944. a-rest ::= ( "," space additional-kv )*
  945. additional-k ::= ["] ( [a] char+ | [^"a] char* ) ["] space
  946. additional-kv ::= additional-k ":" space integer
  947. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  948. integer ::= ("-"? integral-part) space
  949. integral-part ::= [0] | [1-9] [0-9]{0,15}
  950. root ::= ("-"? integral-part) space
  951. root0 ::= "{" space (-kv -rest | a-kv a-rest | additional-kv ( "," space additional-kv )* )? "}" space
  952. space ::= | " " | "\n" [ \t]{0,20}
  953. )"""
  954. });
  955. test({
  956. SUCCESS,
  957. "optional props with nested names",
  958. R"""({
  959. "properties": {
  960. "a": {"type": "integer"},
  961. "aa": {"type": "integer"}
  962. },
  963. "additionalProperties": {"type": "integer"}
  964. })""",
  965. R"""(
  966. a-kv ::= "\"a\"" space ":" space integer
  967. a-rest ::= ( "," space aa-kv )? aa-rest
  968. aa-kv ::= "\"aa\"" space ":" space integer
  969. aa-rest ::= ( "," space additional-kv )*
  970. additional-k ::= ["] ( [a] ([a] char+ | [^"a] char*) | [^"a] char* )? ["] space
  971. additional-kv ::= additional-k ":" space integer
  972. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  973. integer ::= ("-"? integral-part) space
  974. integral-part ::= [0] | [1-9] [0-9]{0,15}
  975. root ::= "{" space (a-kv a-rest | aa-kv aa-rest | additional-kv ( "," space additional-kv )* )? "}" space
  976. space ::= | " " | "\n" [ \t]{0,20}
  977. )"""
  978. });
  979. test({
  980. SUCCESS,
  981. "optional props with common prefix",
  982. R"""({
  983. "properties": {
  984. "ab": {"type": "integer"},
  985. "ac": {"type": "integer"}
  986. },
  987. "additionalProperties": {"type": "integer"}
  988. })""",
  989. R"""(
  990. ab-kv ::= "\"ab\"" space ":" space integer
  991. ab-rest ::= ( "," space ac-kv )? ac-rest
  992. ac-kv ::= "\"ac\"" space ":" space integer
  993. ac-rest ::= ( "," space additional-kv )*
  994. additional-k ::= ["] ( [a] ([b] char+ | [c] char+ | [^"bc] char*) | [^"a] char* )? ["] space
  995. additional-kv ::= additional-k ":" space integer
  996. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  997. integer ::= ("-"? integral-part) space
  998. integral-part ::= [0] | [1-9] [0-9]{0,15}
  999. root ::= "{" space (ab-kv ab-rest | ac-kv ac-rest | additional-kv ( "," space additional-kv )* )? "}" space
  1000. space ::= | " " | "\n" [ \t]{0,20}
  1001. )"""
  1002. });
  1003. test({
  1004. SUCCESS,
  1005. "top-level $ref",
  1006. R"""({
  1007. "$ref": "#/definitions/foo",
  1008. "definitions": {
  1009. "foo": {
  1010. "type": "object",
  1011. "properties": {
  1012. "a": {
  1013. "type": "string"
  1014. }
  1015. },
  1016. "required": [
  1017. "a"
  1018. ],
  1019. "additionalProperties": false
  1020. }
  1021. }
  1022. })""",
  1023. R"""(
  1024. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  1025. foo ::= "{" space foo-a-kv "}" space
  1026. foo-a-kv ::= "\"a\"" space ":" space string
  1027. root ::= foo
  1028. space ::= | " " | "\n" [ \t]{0,20}
  1029. string ::= "\"" char* "\"" space
  1030. )"""
  1031. });
  1032. test({
  1033. SUCCESS,
  1034. "anyOf",
  1035. R"""({
  1036. "anyOf": [
  1037. {"$ref": "#/definitions/foo"},
  1038. {"$ref": "#/definitions/bar"}
  1039. ],
  1040. "definitions": {
  1041. "foo": {
  1042. "properties": {"a": {"type": "number"}}
  1043. },
  1044. "bar": {
  1045. "properties": {"b": {"type": "number"}}
  1046. }
  1047. },
  1048. "type": "object"
  1049. })""",
  1050. R"""(
  1051. alternative-0 ::= foo
  1052. alternative-1 ::= bar
  1053. bar ::= "{" space (bar-b-kv )? "}" space
  1054. bar-b-kv ::= "\"b\"" space ":" space number
  1055. decimal-part ::= [0-9]{1,16}
  1056. foo ::= "{" space (foo-a-kv )? "}" space
  1057. foo-a-kv ::= "\"a\"" space ":" space number
  1058. integral-part ::= [0] | [1-9] [0-9]{0,15}
  1059. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  1060. root ::= alternative-0 | alternative-1
  1061. space ::= | " " | "\n" [ \t]{0,20}
  1062. )"""
  1063. });
  1064. test({
  1065. SUCCESS,
  1066. "mix of allOf, anyOf and $ref (similar to https://json.schemastore.org/tsconfig.json)",
  1067. R"""({
  1068. "allOf": [
  1069. {"$ref": "#/definitions/foo"},
  1070. {"$ref": "#/definitions/bar"},
  1071. {
  1072. "anyOf": [
  1073. {"$ref": "#/definitions/baz"},
  1074. {"$ref": "#/definitions/bam"}
  1075. ]
  1076. }
  1077. ],
  1078. "definitions": {
  1079. "foo": {
  1080. "properties": {"a": {"type": "number"}}
  1081. },
  1082. "bar": {
  1083. "properties": {"b": {"type": "number"}}
  1084. },
  1085. "bam": {
  1086. "properties": {"c": {"type": "number"}}
  1087. },
  1088. "baz": {
  1089. "properties": {"d": {"type": "number"}}
  1090. }
  1091. },
  1092. "type": "object"
  1093. })""",
  1094. R"""(
  1095. a-kv ::= "\"a\"" space ":" space number
  1096. b-kv ::= "\"b\"" space ":" space number
  1097. c-kv ::= "\"c\"" space ":" space number
  1098. d-kv ::= "\"d\"" space ":" space number
  1099. d-rest ::= ( "," space c-kv )?
  1100. decimal-part ::= [0-9]{1,16}
  1101. integral-part ::= [0] | [1-9] [0-9]{0,15}
  1102. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  1103. root ::= "{" space a-kv "," space b-kv ( "," space ( d-kv d-rest | c-kv ) )? "}" space
  1104. space ::= | " " | "\n" [ \t]{0,20}
  1105. )"""
  1106. });
  1107. test({
  1108. SUCCESS,
  1109. "conflicting names",
  1110. R"""({
  1111. "type": "object",
  1112. "properties": {
  1113. "number": {
  1114. "type": "object",
  1115. "properties": {
  1116. "number": {
  1117. "type": "object",
  1118. "properties": {
  1119. "root": {
  1120. "type": "number"
  1121. }
  1122. },
  1123. "required": [
  1124. "root"
  1125. ],
  1126. "additionalProperties": false
  1127. }
  1128. },
  1129. "required": [
  1130. "number"
  1131. ],
  1132. "additionalProperties": false
  1133. }
  1134. },
  1135. "required": [
  1136. "number"
  1137. ],
  1138. "additionalProperties": false,
  1139. "definitions": {}
  1140. })""",
  1141. R"""(
  1142. decimal-part ::= [0-9]{1,16}
  1143. integral-part ::= [0] | [1-9] [0-9]{0,15}
  1144. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  1145. number- ::= "{" space number-number-kv "}" space
  1146. number-kv ::= "\"number\"" space ":" space number-
  1147. number-number ::= "{" space number-number-root-kv "}" space
  1148. number-number-kv ::= "\"number\"" space ":" space number-number
  1149. number-number-root-kv ::= "\"root\"" space ":" space number
  1150. root ::= "{" space number-kv "}" space
  1151. space ::= | " " | "\n" [ \t]{0,20}
  1152. )"""
  1153. });
  1154. }
  1155. int main() {
  1156. fprintf(stderr, "LLAMA_NODE_AVAILABLE = %s\n", getenv("LLAMA_NODE_AVAILABLE") ? "true" : "false");
  1157. fprintf(stderr, "LLAMA_PYTHON_AVAILABLE = %s\n", getenv("LLAMA_PYTHON_AVAILABLE") ? "true" : "false");
  1158. test_all("C++", [](const TestCase & tc) {
  1159. try {
  1160. tc.verify(json_schema_to_grammar(nlohmann::ordered_json::parse(tc.schema)));
  1161. tc.verify_status(SUCCESS);
  1162. } catch (const std::runtime_error & ex) {
  1163. fprintf(stderr, "Error: %s\n", ex.what());
  1164. tc.verify_status(FAILURE);
  1165. }
  1166. });
  1167. if (getenv("LLAMA_SKIP_TESTS_SLOW_ON_EMULATOR")) {
  1168. fprintf(stderr, "\033[33mWARNING: Skipping slow tests on emulator.\n\033[0m");
  1169. } else {
  1170. if (getenv("LLAMA_PYTHON_AVAILABLE") || (std::system("python -c \"import sys; exit(1) if sys.version_info < (3, 8) else print('Python version is sufficient')\"") == 0)) {
  1171. test_all("Python", [](const TestCase & tc) {
  1172. write("test-json-schema-input.tmp", tc.schema);
  1173. tc.verify_status(std::system(
  1174. "python ./examples/json_schema_to_grammar.py test-json-schema-input.tmp > test-grammar-output.tmp") == 0 ? SUCCESS : FAILURE);
  1175. tc.verify(read("test-grammar-output.tmp"));
  1176. });
  1177. } else {
  1178. fprintf(stderr, "\033[33mWARNING: Python not found (min version required is 3.8), skipping Python JSON schema -> grammar tests.\n\033[0m");
  1179. }
  1180. if (getenv("LLAMA_NODE_AVAILABLE") || (std::system("node --version") == 0)) {
  1181. test_all("JavaScript", [](const TestCase & tc) {
  1182. write("test-json-schema-input.tmp", tc.schema);
  1183. tc.verify_status(std::system(
  1184. "node ./tests/run-json-schema-to-grammar.mjs test-json-schema-input.tmp > test-grammar-output.tmp") == 0 ? SUCCESS : FAILURE);
  1185. tc.verify(read("test-grammar-output.tmp"));
  1186. });
  1187. } else {
  1188. fprintf(stderr, "\033[33mWARNING: Node not found, skipping JavaScript JSON schema -> grammar tests.\n\033[0m");
  1189. }
  1190. }
  1191. test_all("Check Expectations Validity", [](const TestCase & tc) {
  1192. if (tc.expected_status == SUCCESS) {
  1193. tc.verify_expectation_parseable();
  1194. }
  1195. });
  1196. }