From 1c113a2f7a92fffd5d778c8a3c8a46d81b99218b Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Wed, 8 Jul 2015 20:36:56 -0700 Subject: [PATCH] [test] Tests for 'block' and 'return' added. All tests passing. W00t! --- test/test_chapter3-cont.coffee | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 test/test_chapter3-cont.coffee diff --git a/test/test_chapter3-cont.coffee b/test/test_chapter3-cont.coffee new file mode 100644 index 0000000..acfab74 --- /dev/null +++ b/test/test_chapter3-cont.coffee @@ -0,0 +1,34 @@ +chai = require 'chai' +chai.should() +expect = chai.expect + +{cons} = require "cons-lists/lists" +olisp = require '../chapter3/interpreter' +{read, readForms} = require '../chapter1/reader' + +the_false_value = (cons "false", "boolean") + +lisp = (ast) -> + ret = undefined + olisp ast, (i) -> ret = i + return ret + +describe "Core interpreter #3: Blocks", -> + it "Should handle simple blocks", -> + expect(lisp read "(block foo 33)").to.equal(33) + it "Should handle the last blocks", -> + expect(lisp read "(block foo 1 2 3)").to.equal(3) + it "Should handle expressive blocks", -> + expect(lisp read "(block foo (+ 5 5))").to.equal(10) + it "Should handle basic returns blocks", -> + expect(lisp read "(block foo (+ 1 (return-from foo 2)))").to.equal(2) + it "Should handle complex returns blocks", -> + expect(lisp read "(block foo ((lambda (exit)(* 2 (block foo (* 3 (exit 5)) )) ) (lambda (x) (return-from foo x)) ) )").to.equal(5) + it "Expects an uninitialized return-from to fail", -> + expect(-> lisp read "(return-from foo 3)").to.throw("Unknown block label foo") + it "Expects to see an obsolete block when called late", -> + expect(-> lisp read "((block foo (lambda (x) (return-from foo x))) 3 )").to.throw("Obsolete continuation") + it "Expects to see an obsolete block when called late", -> + expect(-> lisp read "((block a (* 2 (block b (return-from a (lambda (x) (return-from b x))))) )3 )").to.throw("Obsolete continuation") + it "Expects to see an obsolete block when called late", -> + expect(-> lisp read "((block a (* 2 (block b (return-from a (lambda (x) (return-from a x))))) ) 3 )").to.throw("Obsolete continuation")