mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 19:34:27 -05:00
finished the first draft of the parser test
This commit is contained in:
30
test/fixtures/each.js
vendored
Normal file
30
test/fixtures/each.js
vendored
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
(function(){
|
||||||
|
_.each = function(obj, iterator, context) {
|
||||||
|
var index = 0;
|
||||||
|
try {
|
||||||
|
if (obj.forEach) {
|
||||||
|
return obj.forEach(iterator, context);
|
||||||
|
}
|
||||||
|
if (_.isArray(obj) || _.isArguments(obj)) {
|
||||||
|
var a = obj;
|
||||||
|
var d = [];
|
||||||
|
for (var b=0, c=a.length; b<c; b++) {
|
||||||
|
var item = a[b];
|
||||||
|
var i = b;
|
||||||
|
d[b] = iterator.call(context, item, i, obj);
|
||||||
|
}
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
var e = _.keys(obj);
|
||||||
|
for (var f=0, g=e.length; f<g; f++) {
|
||||||
|
var key = e[f];
|
||||||
|
iterator.call(context, obj[key], key, obj);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
if (e !== breaker) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
};
|
||||||
|
})();
|
||||||
@@ -36,15 +36,22 @@ class ParserTest < Test::Unit::TestCase
|
|||||||
assert body.operator == '*'
|
assert body.operator == '*'
|
||||||
end
|
end
|
||||||
|
|
||||||
# def test_lexing_if_statement
|
def test_lexing_if_statement
|
||||||
# code = "clap_your_hands() if happy"
|
the_if = @par.parse("clap_your_hands() if happy").expressions.first
|
||||||
# assert @lex.tokenize(code) == [[:IDENTIFIER, "clap_your_hands"], ["(", "("],
|
assert the_if.is_a? IfNode
|
||||||
# [")", ")"], [:IF, "if"], [:IDENTIFIER, "happy"]]
|
assert the_if.condition.literal == 'happy'
|
||||||
# end
|
assert the_if.body.is_a? CallNode
|
||||||
#
|
assert the_if.body.variable.literal == 'clap_your_hands'
|
||||||
# def test_lexing
|
end
|
||||||
# tokens = @lex.tokenize(File.read('test/fixtures/each.cs'))
|
|
||||||
# assert tokens.inspect == File.read('test/fixtures/each.tokens')
|
def test_parsing
|
||||||
# end
|
nodes = @par.parse(File.read('test/fixtures/each.cs'))
|
||||||
|
assign = nodes.expressions.first
|
||||||
|
assert assign.is_a? AssignNode
|
||||||
|
assert assign.variable.literal == '_'
|
||||||
|
assert assign.value.is_a? CodeNode
|
||||||
|
assert assign.value.params == ['obj', 'iterator', 'context']
|
||||||
|
assert nodes.compile == File.read('test/fixtures/each.js')
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user