Skip to content

Instantly share code, notes, and snippets.

@manveru
Created June 20, 2010 05:00
Show Gist options
  • Save manveru/445573 to your computer and use it in GitHub Desktop.
Save manveru/445573 to your computer and use it in GitHub Desktop.
require('../oxid')
bacon: require('../bacon')
sys: require('sys')
assert: require('assert')
Array.prototype.compareArrays: (arr) ->
if @length != arr.length
return false
for elem, i in arr
if elem.compareArrays # likely nested array
if !elem.compareArrays(arr[i])
return false
else
continue
if elem != arr[i]
return false
parse: (code, expect) ->
ast: OxidParser.parse(code)
if ast.compareArrays(expect)
bacon.pass sys.inspect(code)
else
bacon.fail code, error, expect, ast
bacon.describe 'Decimal Literals', ->
parse "0", [0]
parse "1", [1]
parse "2", [2]
parse "10", [10]
bacon.describe 'Hexadecimal Literals', ->
parse "0x0", [0]
parse "0x1", [1]
parse "0xf", [15]
parse "0xff", [255]
parse "0xdeadbeefcafe", [244837814094590]
bacon.describe 'Octal Literals', ->
parse "00", [0]
parse "01", [1]
parse "0777", [511]
bacon.describe 'Float Literals', ->
parse "0.0", [0.0]
parse "1.1", [1.1]
parse "333.333", [333.333]
bacon.describe 'Uninterpolated String Literals', ->
parse "'foo'", ["foo"]
parse "'\\a'", ['\\a']
parse "'\\b'", ['\\b']
parse "'\\f'", ['\\f']
parse "'\\n'", ['\\n']
parse "'\\r'", ['\\r']
parse "'\\t'", ['\\t']
parse "'\\v'", ['\\v']
parse "'\\\\'", ['\\']
parse "'\\\''", ["'"]
parse "'\"'", ['"']
bacon.describe 'Interpolated String Literals', ->
parse '"foo"', ["foo"]
parse '"\\a"', ["\u0007"]
parse '"\\b"', ["\b"]
parse '"\\f"', ["\f"]
parse '"\\n"', ["\n"]
parse '"\\r"', ["\r"]
parse '"\\t"', ["\t"]
parse '"\\v"', ["\v"]
parse '"\\\\"', ['\\']
parse '"\\\""', ['"']
parse '"\'"', ["'"]
bacon.describe 'Array literals', ->
parse '[]', [["array", []]]
parse '[1]', [["array", [1]]]
parse '[1,2]', [["array", [1,2]]]
parse '[1,2,3]', [["array", [1,2,3]]]
parse '[1,2]', [["array", [1,2]]]
parse '[1, 2]', [["array", [1,2]]]
parse '[1,2,3]', [["array", [1,2,3]]]
parse '[ 1 , 2 , 3 ]', [["array", [1,2,3]]]
bacon.describe 'Nested Array literals', ->
parse '[[]]', [["array", ["array", []]]]
parse '[[8]]', [["array", ["array", [8]]]]
parse '[[[8]]]', [["array", ["array", ["array", [8]]]]]
parse '[["omg"],["crazy"]]', [["array", [["array", []], ["array", []]]]]
bacon.describe 'Blocks', ->
parse '[|]', [["block", [], []]]
bacon.summary()
(function(){
var assert, bacon, parse, sys;
require('../oxid');
bacon = require('../bacon');
sys = require('sys');
assert = require('assert');
Array.prototype.compareArrays = function(arr) {
var _a, _b, _c, elem, i;
if (this.length !== arr.length) {
return false;
}
_a = []; _b = arr;
for (i = 0, _c = _b.length; i < _c; i++) {
elem = _b[i];
if (elem.compareArrays) {
// likely nested array
if (!elem.compareArrays(arr[i])) {
return false;
} else {
continue;
}
}
if (elem !== arr[i]) {
return false;
}
}
return _a;
};
parse = function(code, expect) {
var ast;
ast = OxidParser.parse(code);
if (ast.compareArrays(expect)) {
return bacon.pass(sys.inspect(code));
} else {
return bacon.fail(code, error, expect, ast);
}
};
bacon.describe('Decimal Literals', function() {
parse("0", [0]);
parse("1", [1]);
parse("2", [2]);
return parse("10", [10]);
});
bacon.describe('Hexadecimal Literals', function() {
parse("0x0", [0]);
parse("0x1", [1]);
parse("0xf", [15]);
parse("0xff", [255]);
return parse("0xdeadbeefcafe", [244837814094590]);
});
bacon.describe('Octal Literals', function() {
parse("00", [0]);
parse("01", [1]);
return parse("0777", [511]);
});
bacon.describe('Float Literals', function() {
parse("0.0", [0.0]);
parse("1.1", [1.1]);
return parse("333.333", [333.333]);
});
bacon.describe('Uninterpolated String Literals', function() {
parse("'foo'", ["foo"]);
parse("'\\a'", ['\\a']);
parse("'\\b'", ['\\b']);
parse("'\\f'", ['\\f']);
parse("'\\n'", ['\\n']);
parse("'\\r'", ['\\r']);
parse("'\\t'", ['\\t']);
parse("'\\v'", ['\\v']);
parse("'\\\\'", ['\\']);
parse("'\\\''", ["'"]);
return parse("'\"'", ['"']);
});
bacon.describe('Interpolated String Literals', function() {
parse('"foo"', ["foo"]);
parse('"\\a"', ["\u0007"]);
parse('"\\b"', ["\b"]);
parse('"\\f"', ["\f"]);
parse('"\\n"', ["\n"]);
parse('"\\r"', ["\r"]);
parse('"\\t"', ["\t"]);
parse('"\\v"', ["\v"]);
parse('"\\\\"', ['\\']);
parse('"\\\""', ['"']);
return parse('"\'"', ["'"]);
});
bacon.describe('Array literals', function() {
parse('[]', [["array", []]]);
parse('[1]', [["array", [1]]]);
parse('[1,2]', [["array", [1, 2]]]);
parse('[1,2,3]', [["array", [1, 2, 3]]]);
parse('[1,2]', [["array", [1, 2]]]);
parse('[1, 2]', [["array", [1, 2]]]);
parse('[1,2,3]', [["array", [1, 2, 3]]]);
return parse('[ 1 , 2 , 3 ]', [["array", [1, 2, 3]]]);
});
bacon.describe('Nested Array literals', function() {
parse('[[]]', [["array", ["array", []]]]);
parse('[[8]]', [["array", ["array", [8]]]]);
parse('[[[8]]]', [["array", ["array", ["array", [8]]]]]);
return parse('[["omg"],["crazy"]]', [["array", [["array", []], ["array", []]]]]);
});
bacon.describe('Blocks', function() {
return parse('[|]', [["block", [], []]]);
});
bacon.summary();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment