Commit 8c8b055d authored by Markus Klinik's avatar Markus Klinik
Browse files

move dedicated_main to CodingRules

parent 11b14427
......@@ -15,7 +15,6 @@ set[Message] allAssignment01Rules(M3 model)
messages
= a01_student(model)
+ a01_group(model)
+ a01_main(model)
+ a01_io(model)
;
return messages;
......@@ -25,26 +24,6 @@ set[Message] a01_student(M3 model) = assertExists(findClass, model, "Student");
set[Message] a01_group(M3 model) = assertExists(findClass, model, "Group");
// A main method is public static and called main. We don't check for arguments yet.
// Checking argument types is complicated, and probably this catches all cases anyway.
set[loc] getMainMethods(M3 model)
{
publicStatics = domain(invert(declaredMethods(model, checkModifiers = {\public(), \static()})));
mains = { func | func <- publicStatics, getName(model, func) == "main" };
return mains;
}
set[Message] a01_main(M3 model)
{
mains = getMainMethods(model);
if( size(mains) != 1 )
{
return { error("There should be exactly one main method", model.id) };
}
return {};
}
set[Message] a01_io(M3 model)
{
// we assume there is only one main method, which lets us identify the main class
......
......@@ -14,6 +14,7 @@ set[Message] allCodingRules(M3 model)
= ps_class_names_uppercase(model)
+ ps_attribute_names_lowercase(model)
+ ps_all_caps_constant_names(model)
+ ps_dedicated_main(model)
;
// Given an M3 model, returns all classes whose name does not start with an uppercase letter
......@@ -50,3 +51,15 @@ set[Message] ps_all_caps_constant_names(M3 model)
, ! (/^[A-Z_]+$/ := getName(model,constant))
};
}
set[Message] ps_dedicated_main(M3 model)
{
mains = getMainMethods(model);
if( size(mains) != 1 )
{
return { error("There should be one main method", model.id) };
}
return {};
}
\ No newline at end of file
......@@ -6,4 +6,13 @@ import Relation;
import Set;
// Assumption: the given identifier has exactly one name.
str getName(M3 model, loc identifier) = getOneFrom(invert(model.names)[identifier]);
\ No newline at end of file
str getName(M3 model, loc identifier) = getOneFrom(invert(model.names)[identifier]);
// A main method is public static and called main. We don't check for arguments yet.
// Checking argument types is complicated, and probably this catches all cases anyway.
set[loc] getMainMethods(M3 model)
{
publicStatics = domain(invert(declaredMethods(model, checkModifiers = {\public(), \static()})));
mains = { func | func <- publicStatics, getName(model, func) == "main" };
return mains;
}
\ No newline at end of file
......@@ -11,12 +11,6 @@ test bool allIsWell()
return isEmpty(errors);
}
test bool twoMains()
{
errors = a01_main(loadTestProject("assignment01-two-mains"));
return "There should be exactly one main method" in { message | error(message, _) <- errors };
}
test bool ioOnlyInMainClass()
{
errors = a01_io(loadTestProject("assignment01-io-in-group"));
......
......@@ -47,3 +47,9 @@ test bool projectWithImproperConstantName()
constants = {c.file | error(_, c) <- ps_all_caps_constant_names(loadTestProject("assignment01-wrong-constant-name"))};
return constants == {"FOO_bar_BAZ"};
}
test bool twoMains()
{
errors = ps_dedicated_main(loadTestProject("assignment01-two-mains"));
return "There should be one main method" in { message | error(message, _) <- errors };
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment