#!/usr/bin/ruby

#
## Test for nested evals.
#

var s1 = "1 + 2";
var s2 = "x";

10.times {
    s1 = %Q<eval(%Q<#{s1}>)>;
    s2 = %Q{eval(%Q{#{s2}})};
}

do {
    var x = 42;
    assert_eq(eval(s1), 3);
    assert_eq(eval(s2), x);

    x = "foo";
    assert_eq(eval(s1), 3);
    assert_eq(eval(s2), "foo");
}

var s3 = "0"
var strs = []

for i in (1..10) {
    s3 = %Q<#{i} + eval(%Q<#{s3}>)>
    strs << s3;
}

assert_eq(eval(s3), 55);
assert_eq(strs.map {|s| eval(s) }, 10.of { |i| (i+1) * (i+2) / 2 });
assert_eq(eval(s3), 55);

say "** Test passed!";
