Skip to content

Instantly share code, notes, and snippets.

@lucabrunox
Last active August 29, 2015 13:56
Show Gist options
  • Save lucabrunox/8852201 to your computer and use it in GitHub Desktop.
Save lucabrunox/8852201 to your computer and use it in GitHub Desktop.
[Test (name = "/foo")]
public void foo () {
message ("foo");
}
[Test (name = "/bar")]
public void bar () {
message ("bar");
}
int main (string[] args) {
Test.init (ref args);
// code will be auto generated here
return Test.run ();
}
public void foo () {
message ("foo");
}
public void bar () {
message ("bar");
}
int main (string[] args) {
Test.init (ref args);
Test.add_func ("/foo", foo);
Test.add_func ("/bar", bar);
return Test.run ();
}
using Vala;
class TestTransformer : CodeTransformer {
Statement main_return_statement;
public override void visit_method (Method m) {
base.visit_method (m);
if (m.get_attribute ("Test") != null && context.entry_point != null) {
if (!m.has_attribute_argument ("Test", "name")) {
m.error = true;
Report.error (m.source_reference, "Test method without name");
return;
}
var test_name = m.get_attribute_string ("Test", "name");
// find the return statement in the main() method
if (main_return_statement == null) {
foreach (var stmt in context.entry_point.body) {
if (stmt is ReturnStatement) {
main_return_statement = stmt;
break;
}
}
}
// insert an empty statement as placeholder for generating the code in main() before the return
var empty_stmt = new EmptyStatement (m.source_reference);
context.entry_point.body.insert_before (main_return_statement, empty_stmt);
begin_replace_statement (empty_stmt);
statements (@"Test.add_func (\"$test_name\", $(m.get_full_name()));");
end_replace_statement ();
}
}
}
public void vala_plugin_register (CodeContext context) {
context.register_transformer (new TestTransformer ());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment