Skip to content

Instantly share code, notes, and snippets.

@sneakin
Created September 25, 2011 05:05
Show Gist options
  • Save sneakin/1240261 to your computer and use it in GitHub Desktop.
Save sneakin/1240261 to your computer and use it in GitHub Desktop.
ERB and C++
#include "console_parser.hpp"
class ConsoleParserTest: public CxxTest::TestSuite
{
public:
class Callback: public ConsoleParser::CommandCallback
{
public:
bool called;
int argc;
const Argument *argv;
Callback() : called(false), argc(0), argv(NULL) { }
void operator()(uint8_t ac, const Argument av[])
{
called = true;
argc = ac;
argv = av;
}
};
Callback *cb;
ConsoleParser *cp;
void setUp()
{
cb = new Callback();
cp = new ConsoleParser(*cb);
}
void tearDown()
{
delete cp;
delete cb;
}
<% line = __LINE__ + 2 %>
<% {
'line_feed_changes_nothing' => [ "\n", nil ],
'carriage_return_changes_nothing' => [ "\r", nil ],
'single_letter' => [ 'a', nil ],
'single_letter_plus_linefeed_calls_callback' => [ "a\r", 1, [ "a" ]],
'single_letter_plus_newline_calls_callback' => [ "a\n", 1, [ 'a' ]],
'symbol_without_linefeed_does_not_call_callback' => [ "ab", nil ],
'symbol_with_linefeed_calls_callback' => [ "ab\n", 1, [ 'ab' ] ],
'number_without_linefeed_does_not_callback' => [ "12", nil ],
'number_with_linefeed_does_callback' => [ "-12\r", 1, [ '-12' ] ],
'calls_back_with_multiple_tokens' => [ "aoeu 123 -456\n", 3, [ 'aoeu', '123', '-456' ] ],
'calls_back_with_multiple_tokens_spaced_with_tabs' => [ "aoeu\t 123\t-456\n", 3, [ 'aoeu', '123', '-456' ] ],
'calls_back_with_four_tokens' => [ "a b c d\n", 4, [ 'a', 'b', 'c', 'd' ] ],
'no_call_back_with_five_tokens' => [ "a b c d e\n", nil ],
'no_call_back_with_to_many_tokens' => [ "a b c d e f g\n", nil ],
'calls_back_on_line_following_to_many_tokens' => [ "a b c d e f g h i\nhello\n", 1, [ "hello" ] ],
'calls_back_with_leading_newlines' => [ "\r\n\r\n\r\n\r\nabcd -123 123\n", 3, [ "abcd", "-123", "123" ] ],
'calls_back_with_leading_spaces' => [ " abcd -123 123\n", 3, [ "abcd", "-123", "123" ] ],
'calls_back_with_trailing_spaces' => [ "abcd -123 123 \n", 3, [ "abcd", "-123", "123" ] ],
'calls_back_with_spaces' => [ " abcd -123 123 \n", 3, [ "abcd", "-123", "123" ] ],
'does_not_call_back_with_large_tokens' => [ "abcdefghij 123456789\n", nil ],
'calls_back_after_line_of_garbage' => [ "----------!!!@#(*#*(@(*&@()#)*$&)(*@&(@)@*(&#(#()*&!!!@(*&\n123 hello\n", 2, [ "123", "hello" ] ]
}.each_with_index do |(desc, (data, argc, argv)), index| %>
void test_<%= desc %>()
{
<% data.bytes.each do |b| %>
cp->push(<%= b %>);
<% end %>
TS_ASSERT_EQUALS(cb->called, <%= argc != nil %>)
<% if argc %>
TS_ASSERT_EQUALS(cb->argc, <%= argc %>);
<% argv.each_with_index do |arg, i| %>
TS_ASSERT_SAME_DATA(cb->argv[<%= i %>], <%= arg.inspect %>, <%= arg.length %>);
TS_ASSERT_EQUALS(strlen(cb->argv[<%= i %>]), <%= arg.length %>);
<% end %>
<% else %>
TS_ASSERT_EQUALS(cb->argc, 0);
TS_ASSERT_EQUALS(cb->argv, (Callback::Argument *)NULL);
<% end %>
}
<% end %>
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment