[as2api-dev] [CVS trunk] Simplify unit test code by making a better piece of
framework
David Holroyd
dave at badgers-in-foil.co.uk
Sun, 03 Jul 2005 20:11:52 +0000
<html>
<head>
<style><!--
body {background-color:#ffffff;}
.file {border:1px solid #eeeeee;margin-top:1em;margin-bottom:1em;}
.pathname {font-family:monospace; float:right;}
.fileheader {margin-bottom:.5em;}
.diff {margin:0;}
.tasklist {padding:4px;border:1px dashed #000000;margin-top:1em;}
.tasklist ul {margin-top:0;margin-bottom:0;}
tr.alt {background-color:#eeeeee}
#added {background-color:#ddffdd;}
#addedchars {background-color:#99ff99;font-weight:bolder;}
tr.alt #added {background-color:#ccf7cc;}
#removed {background-color:#ffdddd;}
#removedchars {background-color:#ff9999;font-weight:bolder;}
tr.alt #removed {background-color:#f7cccc;}
#info {color:#888888;}
#context {background-color:#eeeeee;}
td {padding-left:.3em;padding-right:.3em;}
tr.head {border-bottom-width:1px;border-bottom-style:solid;}
tr.head td {padding:0;padding-top:.2em;}
.task {background-color:#ffff00;}
.comment {padding:4px;border:1px dashed #000000;background-color:#ffffdd}
.error {color:red;}
hr {border-width:0px;height:2px;background:black;}
--></style>
</head>
<body>
<table cellspacing="0" cellpadding="0" border="0" rules="cols">
<tr class="head"><td colspan="4">Commit in <b><tt>trunk/as2api</tt></b><span id="info"> on MAIN</span></td></tr>
<tr><td><tt><a href="#file1">tc_actionscript_lexer.rb</a></tt></td><td align="right" id="added">+26</td><td align="right" id="removed">-60</td><td nowrap="nowrap" align="center"><a href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk/as2api/tc_actionscript_lexer.rb?rev=181&content-type=text/vnd.viewcvs-markup">181</a> <a href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk/as2api/tc_actionscript_lexer.rb.diff?r1=181&r2=182">-></a> <a href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk/as2api/tc_actionscript_lexer.rb?rev=182&content-type=text/vnd.viewcvs-markup">182</a></td></tr>
</table>
<pre class="comment">
Simplify unit test code by making a better piece of framework
</pre>
<hr /><a name="file1" /><div class="file">
<span class="pathname"><a
href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk">trunk</a>/<a
href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk/as2api">as2api</a></span><br />
<div class="fileheader"><big><b>tc_actionscript_lexer.rb</b></big> <small id="info"><a href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk/as2api/tc_actionscript_lexer.rb?rev=181&content-type=text/vnd.viewcvs-markup">181</a> <a href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk/as2api/tc_actionscript_lexer.rb.diff?r1=181&r2=182">-></a> <a href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk/as2api/tc_actionscript_lexer.rb?rev=182&content-type=text/vnd.viewcvs-markup">182</a></small></div>
<pre class="diff"><small id="info">--- trunk/as2api/tc_actionscript_lexer.rb 2005-07-03 19:20:42 UTC (rev 181)
+++ trunk/as2api/tc_actionscript_lexer.rb 2005-07-03 20:11:50 UTC (rev 182)
@@ -6,81 +6,36 @@
</small></pre><pre class="diff" id="context"> include ActionScript::Parse
def test_simple_string
</pre><pre class="diff" id="removed">- simple_lex("'test'") do |tok|
- assert_instance_of(StringToken, tok)
- assert_equal("test", tok.body)
- end
- simple_lex("\"test\"") do |tok|
- assert_instance_of(StringToken, tok)
- assert_equal("test", tok.body)
- end
- simple_lex("\"'\"") do |tok|
- assert_instance_of(StringToken, tok)
- assert_equal("'", tok.body)
- end
- simple_lex('"\\"\"') do |tok|
- assert_instance_of(StringToken, tok)
- assert_equal('"', tok.body)
- end
</pre><pre class="diff" id="added">+ assert_lex_to("'test'", StringToken.new("test", 1))
+ assert_lex_to("\"test\"", StringToken.new("test", 1))
+ assert_lex_to("\"'\"", StringToken.new("'", 1))
+ assert_lex_to('"\\"\"', StringToken.new('"', 1))
</pre><pre class="diff" id="context"> end
def test_identfier
</pre><pre class="diff" id="removed">- simple_lex("foo") do |tok|
- assert_instance_of(IdentifierToken, tok)
- assert_equal("foo", tok.body)
- end
</pre><pre class="diff" id="added">+ assert_lex_to("foo", IdentifierToken.new("foo", 1))
</pre><pre class="diff" id="context"> # check keyword at start of identifier doesn't confuse lexer
</pre><pre class="diff" id="removed">- simple_lex("getfoo") do |tok|
- assert_instance_of(IdentifierToken, tok)
- assert_equal("getfoo", tok.body)
- end
- simple_lex("BAR") do |tok|
- assert_instance_of(IdentifierToken, tok)
- assert_equal("BAR", tok.body)
- end
</pre><pre class="diff" id="added">+ assert_lex_to("getfoo", IdentifierToken.new("getfoo", 1))
+ assert_lex_to("BAR", IdentifierToken.new("BAR", 1))
</pre><pre class="diff" id="context"> # 'dollar' and underscore are allowed
</pre><pre class="diff" id="removed">- simple_lex("$foo") do |tok|
- assert_instance_of(IdentifierToken, tok)
- assert_equal("$foo", tok.body)
- end
- simple_lex("bar$") do |tok|
- assert_instance_of(IdentifierToken, tok)
- assert_equal("bar$", tok.body)
- end
- simple_lex("_x") do |tok|
- assert_instance_of(IdentifierToken, tok)
- assert_equal("_x", tok.body)
- end
- simple_lex("z_") do |tok|
- assert_instance_of(IdentifierToken, tok)
- assert_equal("z_", tok.body)
- end
</pre><pre class="diff" id="added">+ assert_lex_to("$foo", IdentifierToken.new("$foo", 1))
+ assert_lex_to("bar$", IdentifierToken.new("bar$", 1))
+ assert_lex_to("_x", IdentifierToken.new("_x", 1))
+ assert_lex_to("z_", IdentifierToken.new("z_", 1))
</pre><pre class="diff" id="context"> end
def test_number
</pre><pre class="diff" id="removed">- simple_lex("1") do |tok|
- assert_instance_of(NumberToken, tok)
- assert_equal("1", tok.body)
- end
</pre><pre class="diff" id="added">+ assert_lex_to("1", NumberToken.new("1", 1))
</pre><pre class="diff" id="context"> end
def test_single_line_comment
</pre><pre class="diff" id="removed">- simple_lex("// foo ") do |tok|
- assert_instance_of(SingleLineCommentToken, tok)
- assert_equal(" foo ", tok.body)
- end
</pre><pre class="diff" id="added">+ assert_lex_to("// foo ", SingleLineCommentToken.new(" foo ", 1))
</pre><pre class="diff" id="context"> # 'single line' comments shouldn't eat the whole body of a Mac-format file
</pre><pre class="diff" id="removed">- simple_lex("//foo\r") do |tok|
- assert_instance_of(SingleLineCommentToken, tok)
- assert_equal("foo", tok.body)
- end
</pre><pre class="diff" id="added">+ assert_lex_to("//foo\r", SingleLineCommentToken.new("foo", 1))
</pre><pre class="diff" id="context"> end
def test_multiline_comment
</pre><pre class="diff" id="removed">- simple_lex("/* hide!/* */") do |tok|
- assert_instance_of(MultiLineCommentToken, tok)
- assert_equal(" hide!/* ", tok.body)
- end
</pre><pre class="diff" id="added">+ assert_lex_to("/* hide!/* */", MultiLineCommentToken.new(" hide!/* ", 1))
</pre><pre class="diff" id="context"> end
def test_comma; assert_simple_token(",", CommaToken) end
</pre><pre class="diff"><small id="info">@@ -118,6 +73,17 @@
</small></pre><pre class="diff" id="context"> assert_nil(lex.get_next)
end
</pre><pre class="diff" id="added">+ def assert_lex_to(text, *tokens)
+ input = StrIO.new(text)
+ lex = ASLexer.new(input)
+ tokens.each do |expected|
+ tok = lex.get_next
+ assert_equal(expected.class, tok.class)
+ assert_equal(expected.body, tok.body)
+ end
+ assert_nil(lex.get_next)
+ end
+
</pre><pre class="diff" id="context"> def assert_simple_token(text, token)
simple_lex(text) do |tok|
assert_instance_of(token, tok)
</pre></div>
<center><small><a href="http://www.badgers-in-foil.co.uk/projects/cvsspam/" title="commit -> email">CVSspam</a> 0.2.11</small></center>
</body></html>