Created
February 2, 2015 22:09
-
-
Save n2westman/984d449af83441eaed38 to your computer and use it in GitHub Desktop.
hw3-tests.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This function is used by the test harness to pretty-print values. | |
| // Right now it doesn't handle undefined, functions, NaN, Number.POSITIVE_INFINITY, etc. | |
| // Feel free to modify / extend it as you see fit. | |
| // (FYI, pretty-printing is not used by our grading scripts.) | |
| function prettyPrintValue(value) { | |
| return JSON.stringify(value); | |
| } | |
| // Helper functions used in the tests | |
| function greaterThan(n) { | |
| return function(x) { return x > n; }; | |
| } | |
| function lessThan(n) { | |
| return function(x) { return x < n; }; | |
| } | |
| function isNumber(x) { | |
| return typeof x === 'number'; | |
| } | |
| function isString(x) { | |
| return typeof x === 'string'; | |
| } | |
| // Tests! | |
| tests( | |
| { | |
| name: 'wildcard', | |
| code: 'match(123,\n' + | |
| ' _, function(x) { return x + 2; }\n' + | |
| ')', | |
| expected: 125 | |
| }, | |
| { | |
| name: 'literal pattern', | |
| code: 'match(123,\n' + | |
| ' 42, function() { return "aaa"; },\n' + | |
| ' 123, function() { return "bbb"; },\n' + | |
| ' 444, function() { return "ccc"; }\n' + | |
| ')', | |
| expected: "bbb" | |
| }, | |
| { | |
| name: 'array pattern', | |
| code: 'match(["+", 5, 7],\n' + | |
| ' ["+", _, _], function(x, y) { return x + y; }\n' + | |
| ')', | |
| expected: 12 | |
| }, | |
| { | |
| name: 'many', | |
| code: 'match(["sum", 1, 2, 3, 4],\n' + | |
| ' ["sum", many(when(isNumber))], function(ns) {\n' + | |
| ' return ns.reduce(function(x, y) { return x + y; });\n' + | |
| ' }\n' + | |
| ')', | |
| expected: 10 | |
| }, | |
| { | |
| name: 'many2', | |
| code: 'match([1, 2, 3, 4],\n' + | |
| ' many(_), function(ns) {\n' + | |
| ' return ns;\n' + | |
| ' }\n' + | |
| ')', | |
| expected: [1,2,3,4] | |
| }, | |
| { | |
| name: 'many3', | |
| code: 'match([1, 2, 3, 4],\n' + | |
| ' many(when(isNumber)), function(ns) {\n' + | |
| ' return ns;\n' + | |
| ' }\n' + | |
| ')', | |
| expected: [1,2,3,4] | |
| }, | |
| { | |
| name: 'many pairs', | |
| code: 'match([[1, 2], [3, 4], [5, 6]],\n' + | |
| ' [many([_, _])], function(pts) { return JSON.stringify(pts); }\n' + | |
| ')', | |
| expected: "[1,2,3,4,5,6]" | |
| }, | |
| { | |
| name: 'many pairs and then some', | |
| code: 'match([[1, 2], [3, 4], [5, 6], "hello "],\n' + | |
| ' [many([_, _]), _], function(pts, s) { return s + JSON.stringify(pts); }\n' + | |
| ')', | |
| expected: "hello [1,2,3,4,5,6]" | |
| }, | |
| { | |
| name: 'when', | |
| code: 'match(5,\n' + | |
| ' when(greaterThan(8)), function(x) { return x + " is greater than 8"; },\n' + | |
| ' when(greaterThan(2)), function(x) { return x + " is greater than 2"; }\n' + | |
| ')', | |
| expected: "5 is greater than 2" | |
| }, | |
| { | |
| name: 'first match wins', | |
| code: 'match(123,\n' + | |
| ' _, function(x) { return x; },\n' + | |
| ' 123, function() { return 4; }\n' + | |
| ')', | |
| expected: 123 | |
| }, | |
| { | |
| name: 'match failed', | |
| code: 'match(3,\n' + | |
| ' 1, function() { return 1; },\n' + | |
| ' 2, function() { return 2; },\n' + | |
| ' [3], function() { return 3; }\n' + | |
| ')', | |
| shouldThrow: true | |
| }, | |
| { | |
| name: 'many on 0', | |
| code: 'match([], many(_), function(l) {return l.length === 0})', | |
| expected: true | |
| }, | |
| { | |
| name: 'many on 1', | |
| code: 'match([1], many(_), function(l) {return l.length === 1})', | |
| expected: true | |
| }, | |
| { | |
| name: 'many on 2', | |
| code: 'match([1,2], many(_), function(l) {return l.length === 2})', | |
| expected: true | |
| }, | |
| { | |
| name: 'many on 2 of four', | |
| code: 'match([1,2,3,4], [many(when(lessThan(3))),3,4], function(l) {return l.length === 2})', | |
| expected: true | |
| }, | |
| { | |
| name: 'string number flip', | |
| code: 'match([3,2,"hello","world"], \n' + | |
| '[many(when(isNumber)), many(when(isString))], function(l1, l2) { return l2.concat(l1); })', | |
| expected: ['hello','world',3,2] | |
| }, | |
| { | |
| name: 'string number flip fail', | |
| code: 'match([3,2,"hello","world", 4], \n' + | |
| '[many(when(isNumber)), many(when(isString))], function(l1, l2) { return l2.concat(l1); })', | |
| shouldThrow: true | |
| } | |
| ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment