Repository Public License
1import Foundation2import Testing3import PrismCore4@testable import PrismLogicRuntime56@Suite struct ExpressionTests {7 let scope = ExpressionScope(8 roots: [9 "state": ["filter": "Food", "budget": 500, "form": ["amount": 12.5]],10 "row": ["amount": 42.5, "category": "Food", "date": "2026-03-14"],11 ],12 queryResolver: { id in13 id == "totals" ? [["month": "2026-01", "total": 300], ["month": "2026-02", "total": 450.5]] : []14 },15 locale: Locale(identifier: "en_US"), currencyCode: "USD",16 now: DateCoding.date(from: "2026-11-22")!.addingTimeInterval(17 * 3600)17 )1819 func eval(_ s: String) throws -> JSONValue { try Evaluator.evaluate(s, scope: scope) }2021 @Test func arithmeticAndPrecedence() throws {22 #expect(try eval("1 + 2 * 3") == 7)23 #expect(try eval("(1 + 2) * 3") == 9)24 #expect(try eval("10 / 4") == 2.5)25 #expect(try eval("7 % 4") == 3)26 #expect(try eval("-row.amount + 0.5") == -42)27 #expect(try eval("1 / 0") == .null)28 #expect(try eval("9223372036854775807 + 1").doubleValue != nil)29 }3031 @Test func comparisonsLogicAndConditionals() throws {32 #expect(try eval("row.amount > 40 && row.category == state.filter") == true)33 #expect(try eval("state.filter == 'All' || row.category == state.filter") == true)34 #expect(try eval("!(1 < 2)") == false)35 #expect(try eval("row.amount >= 100 ? 'big' : 'small'") == "small")36 #expect(try eval("if(state.budget > 400, 'ok', 'low')") == "ok")37 #expect(try eval("1 == 1.0") == true)38 #expect(try eval("'b' > 'a'") == true)39 #expect(try eval("row.missing == null") == true)40 }4142 @Test func stringsAndFunctions() throws {43 #expect(try eval("'Total: ' + 3") == "Total: 3")44 #expect(try eval("upper(state.filter)") == "FOOD")45 #expect(try eval("contains('Groceries', 'CER')") == true)46 #expect(try eval("concat(row.category, ' / ', row.amount)") == "Food / 42.5")47 #expect(try eval("length('abc')") == 3)48 #expect(try eval("coalesce(row.missing, '', 'fallback')") == "fallback")49 #expect(try eval("round(2.456, 2)") == 2.46)50 #expect(try eval("formatCurrency(1234.5)") == "$1,234.50")51 #expect(try eval("formatNumber(1234.567, 1)") == "1,234.6")52 }5354 @Test func datesAndQueries() throws {55 #expect(try eval("month(row.date)") == "2026-03")56 #expect(try eval("year(row.date)") == 2026)57 #expect(try eval("today()") == "2026-11-22")58 #expect(try eval("month('not a date')") == .null)59 #expect(try eval("sum(queries.totals, 'total')") == 750.5)60 #expect(try eval("count(queries.totals)") == 2)61 #expect(try eval("queries.totals[1].month") == "2026-02")62 #expect(try eval("sum(queries.totals, 'total') > state.budget") == true)63 #expect(try eval("max(queries.totals, 'total')") == 450.5)64 #expect(try eval("avg(queries.empty, 'x')") == .null)65 #expect(DateCoding.normalize("3/14/2026") == "2026-03-14")66 #expect(DateCoding.normalize("2026-02-30") == nil)67 }6869 @Test func errorsAreReportedNotCrashed() {70 for bad in ["", "1 +", "foo(", "'open", "a ? b", "1 2", "#", "unknownFn(1)", "nope.x"] {71 #expect(throws: ExpressionError.self, "\(bad)") { _ = try eval(bad) }72 }73 let deep = String(repeating: "(", count: 200) + "1" + String(repeating: ")", count: 200)74 #expect(throws: ExpressionError.self) { _ = try Expression(deep) }75 }7677 @Test func budgetStopsExpensiveEvaluation() throws {78 let expr = try Expression(Array(repeating: "1", count: 100).joined(separator: " + "))79 var cheap = Evaluator(scope: scope, budget: 50)80 #expect(throws: ExpressionError.self) { _ = try cheap.eval(expr.root) }81 #expect(try Evaluator.evaluate(expr, scope: scope) == 100)82 let huge = Array(repeating: "1", count: 900).joined(separator: " + ")83 #expect(throws: ExpressionError.self) { _ = try Expression(huge) }84 }8586 @Test func referencesAndFunctionsAreListed() throws {87 let e = try Expression("sum(queries.totals, 'total') > state.budget && row.amount > 0")88 #expect(e.references == ["queries.totals", "state.budget", "row.amount"])89 #expect(e.calledFunctions == ["sum"])90 }9192 @Test func dynamicValuesAndStatePaths() throws {93 #expect(DynamicValue(["$expr": "1+1"]) == .expression("1+1"))94 #expect(DynamicValue(["$bind": "state.form.amount"]) == .binding("form.amount"))95 #expect(DynamicValue(["a": 1]) == .literal(["a": 1]))96 #expect(try DynamicValue(["$bind": "form.amount"]).resolve(in: scope) == 12.5)97 #expect(try DynamicValue(["$expr": "state.budget * 2"]).resolve(in: scope) == 1000)98 var state: [String: JSONValue] = ["a": 1]99 StatePath.set("form.amount", to: 3, in: &state)100 StatePath.set("a", to: 2, in: &state)101 #expect(state == ["a": 2, "form": ["amount": 3]])102 #expect(StatePath.isValid("form.amount") && !StatePath.isValid("form..x") && !StatePath.isValid("1a"))103 }104}105