From 4c1c29361e8b514735fa8f16c1ca0d919b353ea2 Mon Sep 17 00:00:00 2001 From: Olivier Chafik Date: Sat, 8 Jun 2024 20:33:40 +0100 Subject: [PATCH] json: fix negative min (w/ more than 1 digit) --- common/json-schema-to-grammar.cpp | 4 ++-- tests/test-grammar-integration.cpp | 24 ++++++++++++++++++++++++ tests/test-json-schema-to-grammar.cpp | 16 +++++++++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/common/json-schema-to-grammar.cpp b/common/json-schema-to-grammar.cpp index 61bdd1bf9..79b037f27 100644 --- a/common/json-schema-to-grammar.cpp +++ b/common/json-schema-to-grammar.cpp @@ -276,9 +276,9 @@ static void _generate_min_max_int(int min_value, int max_value, std::stringstrea if (has_min) { if (min_value < 0) { - out << "\"-\" "; + out << "\"-\" ("; _generate_min_max_int(std::numeric_limits::min(), -min_value, out, decimals_left, /* top_level= */ false); - out << " | [0] | [1-9] "; + out << ") | [0] | [1-9] "; more_digits(0, decimals_left - 1); } else if (min_value == 0) { if (top_level) { diff --git a/tests/test-grammar-integration.cpp b/tests/test-grammar-integration.cpp index 2d78c61d3..da5c5f98d 100644 --- a/tests/test-grammar-integration.cpp +++ b/tests/test-grammar-integration.cpp @@ -199,6 +199,30 @@ static void test_simple_grammar() { "123", } ); + test_schema( + "simple min -123", + R"""({ + "type": "integer", + "minimum": -123 + })""", + // Passing strings + { + "-123", + "-122", + "-11", + "-1", + "0", + "1", + "123", + "1234", + "2345", + }, + // Failing strings + { + "-1234", + "-124", + } + ); // Test case for a simple grammar test_grammar( diff --git a/tests/test-json-schema-to-grammar.cpp b/tests/test-json-schema-to-grammar.cpp index c0c67c9b3..2134eac4b 100755 --- a/tests/test-json-schema-to-grammar.cpp +++ b/tests/test-json-schema-to-grammar.cpp @@ -179,7 +179,21 @@ static void test_all(const std::string & lang, std::function