mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04: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 == '*'
|
||||
end
|
||||
|
||||
# def test_lexing_if_statement
|
||||
# code = "clap_your_hands() if happy"
|
||||
# assert @lex.tokenize(code) == [[:IDENTIFIER, "clap_your_hands"], ["(", "("],
|
||||
# [")", ")"], [:IF, "if"], [:IDENTIFIER, "happy"]]
|
||||
# end
|
||||
#
|
||||
# def test_lexing
|
||||
# tokens = @lex.tokenize(File.read('test/fixtures/each.cs'))
|
||||
# assert tokens.inspect == File.read('test/fixtures/each.tokens')
|
||||
# end
|
||||
def test_lexing_if_statement
|
||||
the_if = @par.parse("clap_your_hands() if happy").expressions.first
|
||||
assert the_if.is_a? IfNode
|
||||
assert the_if.condition.literal == 'happy'
|
||||
assert the_if.body.is_a? CallNode
|
||||
assert the_if.body.variable.literal == 'clap_your_hands'
|
||||
end
|
||||
|
||||
def test_parsing
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user