LispInSmallPieces/test/test_chapter1.coffee

41 lines
1.6 KiB
CoffeeScript
Raw Normal View History

2015-06-17 20:02:37 +00:00
chai = require 'chai'
chai.should()
expect = chai.expect
2015-06-17 20:02:37 +00:00
lisp = require '../chapter1/interpreter'
{read, readForms} = require '../chapter1/reader'
2015-06-17 19:34:31 +00:00
2015-06-17 20:02:37 +00:00
describe "Core interpreter", ->
it "Should handle if statements", ->
expect(lisp read "(begin (if (lt 0 1) #t #f))").to.equal(true)
expect(lisp read "(begin (if (lt 1 0) #t #f))").to.equal(false)
expect(lisp read '(begin (if (lt 1 0) "y" "n"))').to.equal("n")
expect(lisp read '(begin (if (lt 0 1) "y" "n"))').to.equal("y")
expect(lisp read '(begin (if (eq "y" "y") "y" "n"))').to.equal("y")
expect(lisp read '(begin (if (eq "y" "x") "y" "n"))').to.equal("n")
it "Should handle basic arithmetic", ->
expect(lisp read '(begin (+ 5 5))').to.equal(10)
expect(lisp read '(begin (* 5 5))').to.equal(25)
expect(lisp read '(begin (/ 5 5))').to.equal(1)
expect(lisp read '(begin (- 9 5))').to.equal(4)
2015-06-17 20:02:37 +00:00
it "Should handle some algebra", ->
expect(lisp read '(begin (* (+ 5 5) (* 2 3))').to.equal(60)
2015-06-17 20:02:37 +00:00
it "Should handle a basic setting", ->
expect(lisp read '(begin (set! fact 4) fact)').to.equal(4)
it "Should handle a zero arity thunk", ->
expect(lisp read '(begin (set! fact (lambda () (+ 5 5))) (fact))').to.equal(10)
it "Should handle a two arity thunk", ->
expect(lisp read '(begin (set! fact (lambda (a b) (+ a b))) (fact 4 6))').to.equal(10)
it "Should handle a recursive function", ->
expect(lisp read '(begin (set! fact (lambda (x) (if (eq? x 0) 1 (* x (fact (- x 1)))))) (fact 5))').to.equal(120)
it "Should handle an IIFE", ->
expect(lisp read '(begin ((lambda () (+ 5 5))))').to.equal(10)