Permalink
Browse files

adding tests for scheme

  • Loading branch information...
1 parent 27a1fa1 commit 17c7198c5f999c578cffd5f62f892d405072dfeb @ajb committed Apr 30, 2012
Showing with 218 additions and 93 deletions.
  1. +214 −85 Homework4/test/evalScheemTest.js
  2. +4 −8 Homework4/test/webtest.html
@@ -2,116 +2,245 @@ if (typeof module !== 'undefined') {
// In Node load required modules
var assert = require('chai').assert;
var evalScheem = require('../scheem').evalScheem;
+
+ var PEG = require('pegjs');
+ var fs = require('fs'); // for loading files
+
+ // Read file contents
+ var data = fs.readFileSync('parser.peg', 'utf-8');
+
+ // Create my parser
+ var parseScheme = PEG.buildParser(data).parse;
+
} else {
// In browser assume already loaded by <script> tags
- var assert = chai.assert;
+ var assert = chai.assert;
+ var parseScheme = parseScheme.parse;
}
-suite('arithmetic', function () {
- test('addition', function () {
- assert.deepEqual(
- evalScheem(['+', 1, 1], {}), 2);
- });
+var evalSchemeString = function(schemeString, env) {
+ return evalScheem(parseScheme(schemeString), env);
+}
- test('subtraction', function () {
- assert.deepEqual(
- evalScheem(['-', 3, 1], {}), 2);
+suite('scheem', function(){
+ suite('arithmetic', function () {
+ test('addition', function () {
+ assert.deepEqual(
+ evalScheem(['+', 1, 1], {}), 2);
+ });
+
+ test('subtraction', function () {
+ assert.deepEqual(
+ evalScheem(['-', 3, 1], {}), 2);
+ });
+
+ test('multiplication', function () {
+ assert.deepEqual(
+ evalScheem(['*', 4, 0.5], {}), 2);
+ });
+
+ test('division', function () {
+ assert.deepEqual(
+ evalScheem(['/', 4, 2], {}), 2);
+ });
});
- test('multiplication', function () {
- assert.deepEqual(
- evalScheem(['*', 4, 0.5], {}), 2);
+ suite('quote', function () {
+ test('a number', function () {
+ assert.deepEqual(
+ evalScheem(['quote', 3], {}), 3);
+ });
+ test('an atom', function () {
+ assert.deepEqual(
+ evalScheem(['quote', 'dog'], {}), 'dog');
+ });
+ test('a list', function () {
+ assert.deepEqual(
+ evalScheem(['quote', [1, 2, 3]], {}), [1, 2, 3]);
+ });
});
- test('division', function () {
- assert.deepEqual(
- evalScheem(['/', 4, 2], {}), 2);
+ suite('definitions', function () {
+ test('define returns 0', function () {
+ assert.deepEqual(
+ evalScheem(['define', 'c', 4], {}), 0);
+ });
+
+ test('set! returns 0', function () {
+ assert.deepEqual(
+ evalScheem(['set!', 'a', 1], {}), 0);
+ });
+
+ test('begin', function () {
+ assert.deepEqual(
+ evalScheem(['begin', 'x', 'y', 'x'], {
+ x: 1,
+ y: 2
+ }), 1);
+ });
+
+ test('begin and set', function () {
+ assert.deepEqual(
+ evalScheem(['begin', ['set!', 'x', 5],
+ ['set!', 'x', ['+', 'y', 'x']], 'x'], {
+ x: 1,
+ y: 2
+ }), 7);
+ });
});
-});
-suite('quote', function () {
- test('a number', function () {
- assert.deepEqual(
- evalScheem(['quote', 3], {}), 3);
- });
- test('an atom', function () {
- assert.deepEqual(
- evalScheem(['quote', 'dog'], {}), 'dog');
- });
- test('a list', function () {
- assert.deepEqual(
- evalScheem(['quote', [1, 2, 3]], {}), [1, 2, 3]);
- });
-});
+ suite('if', function () {
+ test('basic predicate', function () {
+ assert.deepEqual(
+ evalScheem(['<', 1, 2]), '#t');
-suite('definitions', function () {
- test('define returns 0', function () {
- assert.deepEqual(
- evalScheem(['define', 'c', 4], {}), 0);
- });
+ assert.deepEqual(
+ evalScheem(['<', 2, 1]), '#f');
+ });
- test('set! returns 0', function () {
- assert.deepEqual(
- evalScheem(['set!', 'a', 1], {}), 0);
- });
+ test('if statement with predicate', function () {
+ assert.deepEqual(
+ evalScheem(['if', ['<', 1, 2], 5, 6], {}), 5);
- test('begin', function () {
- assert.deepEqual(
- evalScheem(['begin', 'x', 'y', 'x'], {
- x: 1,
- y: 2
- }), 1);
- });
+ assert.deepEqual(
+ evalScheem(['if', ['<', 2, 1], 5, 6], {}), 6);
+ })
+
+ test('if statement with equals', function () {
+ assert.deepEqual(
+ evalScheem(['if', ['=', 1, 1], 5, 6], {}), 5);
- test('begin and set', function () {
- assert.deepEqual(
- evalScheem(['begin', ['set!', 'x', 5],
- ['set!', 'x', ['+', 'y', 'x']], 'x'], {
- x: 1,
- y: 2
- }), 7);
+ assert.deepEqual(
+ evalScheem(['if', ['=', 2, 1], 5, 6], {}), 6);
+ })
});
+
+ suite('list values', function () {
+ test('cons', function () {
+ assert.deepEqual(
+ evalScheem(['cons', 1, ['quote', [2, 3]]]), [1, 2, 3]);
+ });
+
+ test('car', function () {
+ assert.deepEqual(
+ evalScheem(['car', ['quote', [1, 2, 3]]]), 1);
+ });
+
+ test('cdr', function () {
+ assert.deepEqual(
+ evalScheem(['cdr', ['quote', [1, 2, 3]]]), [2, 3]);
+ });
+ });
});
-suite('if', function () {
- test('basic predicate', function () {
- assert.deepEqual(
- evalScheem(['<', 1, 2]), '#t');
+suite('scheme', function(){
+ suite('arithmetic', function () {
+ test('addition', function () {
+ assert.deepEqual(
+ evalSchemeString('(+ 2 1)', {}), 3);
+ });
+
+ test('subtraction', function () {
+ assert.deepEqual(
+ evalSchemeString('(- 2 1)', {}), 1);
+ });
+
+ test('multiplication', function () {
+ assert.deepEqual(
+ evalSchemeString('(* 2 1)', {}), 2);
+ });
+
+ test('division', function () {
+ assert.deepEqual(
+ evalSchemeString('(/ 2 1)', {}), 2);
+ });
+ });
+
+ suite('quote', function () {
+ test('a number', function () {
+ assert.deepEqual(
+ evalSchemeString("(quote 3)", {}), 3);
+ });
+ test('an atom', function () {
+ assert.deepEqual(
+ evalSchemeString('(quote dog)', {}), 'dog');
+ });
+ test('a list', function () {
+ assert.deepEqual(
+ evalSchemeString('(quote (1 2 3))', {}), [1, 2, 3]);
+ });
+ });
- assert.deepEqual(
- evalScheem(['<', 2, 1]), '#f');
+ suite('definitions', function () {
+ test('define returns 0', function () {
+ assert.deepEqual(
+ evalSchemeString('(define c 4)', {}), 0);
+ });
+
+ test('set! returns 0', function () {
+ assert.deepEqual(
+ evalSchemeString('(set! c 4)', {}), 0);
+ });
+
+ test('begin', function () {
+ assert.deepEqual(
+ evalSchemeString('(begin x y x)', {
+ x: 1,
+ y: 2
+ }), 1);
+ });
+
+ test('begin and set', function () {
+ assert.deepEqual(
+ evalSchemeString('(begin (set! x 5) (set! x (+ y x)) x)', {
+ x: 1,
+ y: 2
+ }), 7);
+ });
});
- test('if statement with predicate', function () {
- assert.deepEqual(
- evalScheem(['if', ['<', 1, 2], 5, 6], {}), 5);
+ suite('if', function () {
+ test('basic predicate', function () {
+ assert.deepEqual(
+ evalSchemeString('(< 1 2)'), '#t');
- assert.deepEqual(
- evalScheem(['if', ['<', 2, 1], 5, 6], {}), 6);
- })
+ assert.deepEqual(
+ evalSchemeString('(< 2 1)'), '#f');
+ });
- test('if statement with equals', function () {
- assert.deepEqual(
- evalScheem(['if', ['=', 1, 1], 5, 6], {}), 5);
+ test('if statement with predicate', function () {
+ assert.deepEqual(
+ evalSchemeString('(if (< 1 2) 5 6)', {}), 5);
- assert.deepEqual(
- evalScheem(['if', ['=', 2, 1], 5, 6], {}), 6);
- })
-});
+ assert.deepEqual(
+ evalSchemeString('(if (< 2 1) 5 6)', {}), 6);
+ })
-suite('list values', function () {
- test('cons', function () {
- assert.deepEqual(
- evalScheem(['cons', 1, ['quote', [2, 3]]]), [1, 2, 3]);
- });
+ test('if statement with equals', function () {
+ assert.deepEqual(
+ evalSchemeString('(if (= 1 1) 5 6)', {}), 5);
- test('car', function () {
- assert.deepEqual(
- evalScheem(['car', ['quote', [1, 2, 3]]]), 1);
+ assert.deepEqual(
+ evalSchemeString('(if (= 2 1) 5 6)', {}), 6);
+ })
});
+ suite('list values', function () {
+ test('cons', function () {
+ assert.deepEqual(
+ evalSchemeString('(cons 1 (quote (2 3)))'), [1, 2, 3]);
+
+ });
+
+ test('car', function () {
+ assert.deepEqual(
+ evalSchemeString('(car (quote (1 2 3)))'), 1);
+
+ });
+
+ test('cdr', function () {
+ assert.deepEqual(
+ evalSchemeString('(cdr (quote (1 2 3)))'), [2, 3]);
+ });
+ });
+});
- test('cdr', function () {
- assert.deepEqual(
- evalScheem(['cdr', ['quote', [1, 2, 3]]]), [2, 3]);
- });
-});
@@ -4,14 +4,10 @@
<title>Mocha Tests</title>
<link rel="stylesheet"
href="http://nathansuniversity.com/css/mocha.css" />
- <script src=
- "http://nathansuniversity.com/js/jquery-1.7.1.min.js">
- </script>
- <script src=
- "http://nathansuniversity.com/js/chai.js">
- </script>
- <script src="http://nathansuniversity.com/js/mocha.js">
- </script>
+ <script src="http://nathansuniversity.com/js/jquery-1.7.1.min.js"></script>
+ <script src="http://nathansuniversity.com/js/chai.js"></script>
+ <script src="http://nathansuniversity.com/js/mocha.js"></script>
+ <script src="../parser.js"></script>
<script>mocha.setup('tdd')</script>
<script src="../scheem.js"></script>
<script src="evalScheemTest.js"></script>

0 comments on commit 17c7198

Please sign in to comment.