#include #include #include using mbs::EvalErr; using mbs::EvalOk; using mbs::eval_true; using mbs::eval_val; using mbs::test; using nlohmann::json; TEST_CASE("single-line comments are skipped", "[lexer]") { auto r = eval_val( R"( // comment 1 + 2 // trailing )", json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value == 3.0); } TEST_CASE("string times integer repeats like Python", "[ops]") { auto r = eval_val(R"("ab" * 3)", json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value == "ababab"); } TEST_CASE("integer times string repeats", "[ops]") { auto r = eval_val(R"(4 * "x")", json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value == "xxxx"); } TEST_CASE("string times non-integral number is invalid (null)", "[ops]") { auto r = eval_val(R"("a" * 2.5)", json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value.is_null()); } TEST_CASE("string times zero yields empty string", "[ops]") { auto r = eval_val(R"("hi" * 0)", json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value == ""); } TEST_CASE("power requires numeric operands", "[ops]") { SECTION("string ^ number -> null") { auto r = eval_val(R"("a" ^ 2)", json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value.is_null()); } SECTION("number ^ string -> null") { auto r = eval_val(R"(2 ^ "b")", json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value.is_null()); } SECTION("valid number power") { auto r = eval_val(R"(2 ^ 3)", json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value == 8.0); } } TEST_CASE("invalid mixed arithmetic returns null", "[ops]") { auto r = eval_val(R"("a" + 1)", json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value.is_null()); } TEST_CASE("string + string concatenation", "[ops]") { auto r = eval_val(R"("foo" + "bar")", json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value == "foobar"); } TEST_CASE("test() rejects unknown methods", "[test]") { auto t = test(R"(@x.unknown())"); REQUIRE_FALSE(t.ok); REQUIRE(t.error.find("unknown method") != std::string::npos); } TEST_CASE("test() accepts known script", "[test]") { auto t = test(R"(@user.age > 0 && ['a'].has("a"))"); REQUIRE(t.ok); } TEST_CASE("eval_true uses truthiness", "[eval]") { json ctx = {{"flag", false}}; auto b = eval_true("@flag", ctx); REQUIRE(b.ok); REQUIRE_FALSE(b.value); } TEST_CASE("context variable missing is null", "[eval]") { auto r = eval_val("@missing", json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value.is_null()); } TEST_CASE("len on number returns number", "[builtin]") { auto r = eval_val(R"( (5).len() )", json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value == 5.0); } TEST_CASE("array literal has membership", "[eval]") { json ctx = {{"role", "student"}}; auto r = eval_val(R"(['student','teacher'].has(@role))", ctx); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value == true); } TEST_CASE("short-circuit and skips rhs", "[eval]") { json ctx = json::object(); auto r = eval_val(R"(false && @nope.foo)", ctx); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value == false); } TEST_CASE("string repeat with huge count returns null", "[security]") { auto r = eval_val(R"("x" * 999999999)", json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value.is_null()); } TEST_CASE("number * string with huge count returns null", "[security]") { auto r = eval_val(R"(999999999 * "y")", json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value.is_null()); } TEST_CASE("deeply nested parentheses throw parse depth error", "[security]") { std::string expr(2000, '('); expr += "1"; expr += std::string(2000, ')'); auto r = eval_val(expr, json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).error.find("deeply nested") != std::string::npos); } TEST_CASE("deeply nested unary chains throw parse depth error", "[security]") { std::string expr(2000, '!'); expr += "true"; auto r = eval_val(expr, json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).error.find("deeply nested") != std::string::npos); } TEST_CASE("evaluation depth exceeded throws error", "[security]") { std::string expr(600, '('); expr += "1"; expr += std::string(600, ')'); auto r = eval_val(expr, json::object()); REQUIRE(std::holds_alternative(r)); auto& err = std::get(r).error; bool is_depth_error = err.find("deeply nested") != std::string::npos || err.find("depth exceeded") != std::string::npos; REQUIRE(is_depth_error); } TEST_CASE("moderate string repeat still works", "[security]") { auto r = eval_val(R"("ab" * 100)", json::object()); REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value.get().size() == 200); }