"""Tests for the transpiled sx_ref.py evaluator. Runs the same test cases as test_evaluator.py and test_html.py but against the bootstrap-compiled evaluator to verify correctness. """ import pytest from shared.sx.parser import parse from shared.sx.types import Symbol, Keyword, NIL, Lambda, Component, Macro, PageDef from shared.sx.ref import sx_ref # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- def ev(text, env=None): """Parse and evaluate a single expression via sx_ref.""" return sx_ref.evaluate(parse(text), env) def render(text, env=None): """Parse and render via sx_ref.""" return sx_ref.render(parse(text), env) # --------------------------------------------------------------------------- # Literals and lookups # --------------------------------------------------------------------------- class TestLiterals: def test_int(self): assert ev("42") == 42 def test_string(self): assert ev('"hello"') == "hello" def test_true(self): assert ev("true") is True def test_nil(self): assert ev("nil") is NIL def test_symbol_lookup(self): assert ev("x", {"x": 10}) == 10 def test_undefined_symbol(self): with pytest.raises(sx_ref.EvalError, match="Undefined symbol"): ev("xyz") def test_keyword_evaluates_to_name(self): assert ev(":foo") == "foo" # --------------------------------------------------------------------------- # Arithmetic # --------------------------------------------------------------------------- class TestArithmetic: def test_add(self): assert ev("(+ 1 2 3)") == 6 def test_sub(self): assert ev("(- 10 3)") == 7 def test_mul(self): assert ev("(* 2 3 4)") == 24 def test_div(self): assert ev("(/ 10 4)") == 2.5 def test_mod(self): assert ev("(mod 7 3)") == 1 # --------------------------------------------------------------------------- # Special forms # --------------------------------------------------------------------------- class TestSpecialForms: def test_if_true(self): assert ev("(if true 1 2)") == 1 def test_if_false(self): assert ev("(if false 1 2)") == 2 def test_if_no_else(self): assert ev("(if false 1)") is NIL def test_when_true(self): assert ev("(when true 42)") == 42 def test_when_false(self): assert ev("(when false 42)") is NIL def test_and_short_circuit(self): assert ev("(and true true 3)") == 3 assert ev("(and true false 3)") is False def test_or_short_circuit(self): assert ev("(or false false 3)") == 3 assert ev("(or false 2 3)") == 2 def test_let_scheme_style(self): assert ev("(let ((x 10) (y 20)) (+ x y))") == 30 def test_let_clojure_style(self): assert ev("(let (x 10 y 20) (+ x y))") == 30 def test_let_sequential(self): assert ev("(let ((x 1) (y (+ x 1))) y)") == 2 def test_begin(self): assert ev("(begin 1 2 3)") == 3 def test_quote(self): result = ev("(quote (a b c))") assert result == [Symbol("a"), Symbol("b"), Symbol("c")] def test_cond_clojure(self): assert ev("(cond false 1 true 2 :else 3)") == 2 def test_cond_else(self): assert ev("(cond false 1 false 2 :else 99)") == 99 def test_case(self): assert ev('(case 2 1 "one" 2 "two" :else "other")') == "two" def test_thread_first(self): assert ev("(-> 5 (+ 3) (* 2))") == 16 def test_define(self): env = {} ev("(define x 42)", env) assert env["x"] == 42 # --------------------------------------------------------------------------- # Lambda # --------------------------------------------------------------------------- class TestLambda: def test_create_and_call(self): assert ev("((fn (x) (* x x)) 5)") == 25 def test_closure(self): result = ev("(let ((a 10)) ((fn (x) (+ x a)) 5))") assert result == 15 def test_higher_order(self): result = ev("(let ((double (fn (x) (* x 2)))) (double 7))") assert result == 14 # --------------------------------------------------------------------------- # Collections # --------------------------------------------------------------------------- class TestCollections: def test_list_constructor(self): assert ev("(list 1 2 3)") == [1, 2, 3] def test_dict_constructor(self): assert ev("(dict :a 1 :b 2)") == {"a": 1, "b": 2} def test_get_dict(self): assert ev('(get {:a 1 :b 2} "a")') == 1 def test_get_list(self): assert ev("(get (list 10 20 30) 1)") == 20 def test_first_last_rest(self): assert ev("(first (list 1 2 3))") == 1 assert ev("(last (list 1 2 3))") == 3 assert ev("(rest (list 1 2 3))") == [2, 3] def test_len(self): assert ev("(len (list 1 2 3))") == 3 def test_concat(self): assert ev("(concat (list 1 2) (list 3 4))") == [1, 2, 3, 4] def test_cons(self): assert ev("(cons 0 (list 1 2))") == [0, 1, 2] def test_merge(self): assert ev("(merge {:a 1} {:b 2} {:a 3})") == {"a": 3, "b": 2} def test_empty(self): assert ev("(empty? (list))") is True assert ev("(empty? (list 1))") is False assert ev("(empty? nil)") is True # --------------------------------------------------------------------------- # Higher-order forms # --------------------------------------------------------------------------- class TestHigherOrder: def test_map(self): assert ev("(map (fn (x) (* x x)) (list 1 2 3 4))") == [1, 4, 9, 16] def test_filter(self): assert ev("(filter (fn (x) (> x 2)) (list 1 2 3 4))") == [3, 4] def test_reduce(self): assert ev("(reduce (fn (acc x) (+ acc x)) 0 (list 1 2 3))") == 6 def test_some(self): assert ev("(some (fn (x) (> x 3)) (list 1 2 3 4 5))") is True def test_for_each(self): result = ev("(for-each (fn (x) x) (list 1 2 3))") assert result is NIL # --------------------------------------------------------------------------- # String ops # --------------------------------------------------------------------------- class TestStrings: def test_str(self): assert ev('(str "hello" " " "world")') == "hello world" def test_upper_lower(self): assert ev('(upper "hello")') == "HELLO" assert ev('(lower "HELLO")') == "hello" def test_split_join(self): assert ev('(split "a,b,c" ",")') == ["a", "b", "c"] assert ev('(join "-" (list "a" "b"))') == "a-b" def test_starts_ends(self): assert ev('(starts-with? "hello" "hel")') is True assert ev('(ends-with? "hello" "llo")') is True # --------------------------------------------------------------------------- # Components # --------------------------------------------------------------------------- class TestComponents: def test_defcomp_and_render(self): env = {} ev("(defcomp ~box (&key title) (div :class \"box\" title))", env) result = render("(~box :title \"hi\")", env) assert result == '
hello
" def test_attributes(self): result = render('(a :href "/about" "link")') assert result == 'link' def test_void_element(self): result = render('(br)') assert result == "a
b