Skip to content

Instantly share code, notes, and snippets.

@gromnitsky
Created May 18, 2011 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gromnitsky/979233 to your computer and use it in GitHub Desktop.
Save gromnitsky/979233 to your computer and use it in GitHub Desktop.
Tests 'runner' generator for unity_fixture (not plain Unity) framework, http://embunity.sourceforge.net
#!/usr/bin/env ruby
# -*-ruby-*-
require 'erb'
class UnityFixture
def initialize
@tests = {}
end
# fills @tests with something like
#
# @tests = {
# 'dumb' => ['first', 'second', 'third']
# 'crumb' => ['foo', 'bar']
# }
#
# TODO: rewrite as a normal parser with citrus
def parse(input)
begin
src = File.read(input)
rescue
abort $!.to_s
end
# remove line comments
source_scrubbed = src.gsub(/\/\/.*$/, '')
# remove block comments
source_scrubbed = source_scrubbed.gsub(/\/\*.*?\*\//m, '')
lines = source_scrubbed.split(/(^\s*\#.*$) # Treat preprocessor directives as a logical line
| (;|\{|\}) /x) # Match ;, {, and } as end of lines
# fill @tests
lines.each {|line|
if line =~ /^\s*TEST\s*\((.+?)\)/
args = $1.split(',').map(&:strip)
if @tests.key?(args[0])
@tests[args[0]] << args[1]
else
@tests[args[0]] = [args[1]]
end
end
}
end
def to_hash
@tests
end
def generate(template)
ERB.new(template).result(binding)
end
# Templates helper
def tmpl_group_runner
r = ''
@tests.each {|k,v|
r << "\nTEST_GROUP_RUNNER(#{k})\n{\n"
v.each {|i|
r << " RUN_TEST_CASE(#{k}, #{i});\n"
}
r << "}\n"
}
r
end
private :tmpl_group_runner
TEMPLATE_SIMPLE = <<-END
/* AUTO-GENERATED--DO NOT EDIT */
#include "unity/unity_fixture.h"
<%= tmpl_group_runner %>
static
void run_all_tests()
{ <% @tests.keys.each {|i| %>
RUN_TEST_GROUP(<%= i %>); <% } %>
}
int main(int argc, char* argv[])
{
return UnityMain(argc, argv, run_all_tests);
}
END
end
# ---
abort("Usage: #{File.basename $0} file1.c [file2.c, ...]") if ARGV.size == 0
uf = UnityFixture.new
ARGV.each {|i| uf.parse i }
puts uf.generate(UnityFixture::TEMPLATE_SIMPLE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment