void test_if_elif_else() { Branch branch; branch.compile("if true { a = 1 } elif true { a = 2 } else { a = 3 } a=a"); evaluate_branch(&branch); test_assert(branch.contains("a")); test_equals(branch["a"]->asInt(), 1); branch.compile( "if false { b = 'apple' } elif false { b = 'orange' } else { b = 'pineapple' } b=b"); evaluate_branch(&branch); test_assert(branch.contains("b")); test_assert(branch["b"]->asString() == "pineapple"); // try one without 'else' branch.clear(); branch.compile("c = 0"); branch.compile("if false { c = 7 } elif true { c = 8 }; c=c"); evaluate_branch(&branch); test_assert(branch.contains("c")); test_assert(branch["c"]->asInt() == 8); // try with some more complex conditions branch.clear(); branch.compile("x = 5"); branch.compile("if x > 6 { compare = 1 } elif x < 6 { compare = -1 } else { compare = 0}"); branch.compile("compare=compare"); evaluate_branch(&branch); test_assert(branch.contains("compare")); test_assert(branch["compare"]->asInt() == -1); }
void test_dont_always_rebind_inner_names() { Branch branch; branch.compile("if false { b = 1 } elif false { c = 1 } elif false { d = 1 } else { e = 1 }"); evaluate_branch(&branch); test_assert(!branch.contains("b")); test_assert(!branch.contains("c")); test_assert(!branch.contains("d")); test_assert(!branch.contains("e")); }
void test_if_joining() { Branch branch; // Test that a name defined in one branch is not rebound in outer scope branch.eval("if true { apple = 5 }"); test_assert(!branch.contains("apple")); // Test that a name which exists in the outer scope is rebound Term* original_banana = create_int(&branch, 10, "banana"); branch.eval("if true { banana = 15 }"); test_assert(branch["banana"] != original_banana); // Test that if a name is defined in both 'if' and 'else' branches, that it gets // defined in the outer scope. branch.eval("if true { Cardiff = 5 } else { Cardiff = 11 }"); test_assert(branch.contains("Cardiff")); // Test that the type of the joined name is correct branch.compile("if true { a = 4 } else { a = 5 }; a = a"); test_equals(get_output_type(branch["a"])->name, "int"); }