Commit 6196c114 authored by Markus Klinik's avatar Markus Klinik
Browse files

refactor Assignment03Rules

parent b153f49b
......@@ -8,80 +8,74 @@ import lang::java::m3::AST;
import Message;
import ImplementsAbstractMethod;
import ClassHierarchy;
import Util;
// Runs all checkers for assignment 3 on the given model
set[Message] allAssignment03Rules(M3 model)
{
errors = a03_geometric_is_comparable(model);
// a03_shapes_implement_comparable can only be called if a03_geometric_is_comparable does not return any errors
return (isEmpty(errors) ? a03_shapes_implement_comparable(model) : errors);
}
= a03_geometric_is_comparable(model)
+ a03_shapes_implement_comparable(model)
;
set[Message] a03_geometric_is_comparable(M3 model)
{
// There should be an interface called "Geometric"
geometrics = { i | i <- interfaces(model), /^Geometric$/ := i.file };
if(size(geometrics) != 1)
set[Message] result = {};
try
{
return { error("There should be an interface called Geometric", model.id) };
geometric = findInterface(model, "Geometric");
if( ! ("Comparable" in { super.file | super <- model.extends[geometric] }) )
{
result += { error("Geometric should extend Comparable", geometric) };
}
}
// Geometric should extend Comparable<Geometric>
geometric = getOneFrom(geometrics);
if( ! ("Comparable" in { super.file | super <- model.extends[geometric] }) )
catch error:
{
return { error("Geometric should extend Comparable", geometric) };
return { error };
}
return {};
}
// Assumption: the model passes a03_geometric_is_comparable,
// which means there is exactly one interface called Geometric
loc findGeometric(M3 model)
{
geometrics = { i | i <- interfaces(model), /^Geometric$/ := i.file };
return getOneFrom(geometrics);
return result;
}
set[Message] a03_shapes_implement_comparable(M3 model)
{
geometric = findGeometric(model);
// There should be exactly two subclasses of Geometric
shapes = invert(model.implements)[geometric];
if( {"Rectangle", "Circle"} != { shape.file | shape <- shapes } )
set[Message] result = {};
try
{
return { error("There should be two implementations of Geometric: Rectangle and Circle", geometric) };
}
geometric = findInterface(model, "Geometric");
// There should be exactly two subclasses of Geometric
shapes = invert(model.implements)[geometric];
if( {"Rectangle", "Circle"} != { getName(model, shape) | shape <- shapes } )
{
return { error("There should be two implementations of Geometric: Rectangle and Circle", geometric) };
}
// Rectangle and Circle should only implement Geometric and nothing else
errors = union(
{ model.implements[shape] == { geometric }
? {}
: { error("<shape.file> should implement Geometric and nothing else", shape) }
| shape <- shapes
});
if( ! isEmpty(errors) )
{
return errors;
}
// Rectangle and Circle should only implement Geometric and nothing else
result += union(
{ model.implements[shape] == { geometric }
? {}
: { error("<getName(model,shape)> should implement Geometric and nothing else", shape) }
| shape <- shapes
});
// Rectangle and Circle should implement Comparable.compareTo
loc compareTo = |java+method:///java/lang/Comparable/compareTo(T)|;
// Rectangle and Circle should implement Comparable.compareTo
loc compareTo = |java+method:///java/lang/Comparable/compareTo(T)|;
// for every shape: one of the methods has to override compareTo
for( shape <- shapes )
{
if( !implementsAbstractMethod(model,shape,compareTo) )
// for every shape: one of the methods has to override compareTo
for( shape <- shapes )
{
errors += error("<shape.file> should override Comparable.compareTo", shape);
if( !implementsAbstractMethod(model,shape,compareTo) )
{
result += error("<shape.file> should override Comparable.compareTo", shape);
}
}
}
catch error:
{
return { error };
}
return errors;
return result;
}
......@@ -19,7 +19,7 @@ test bool projectWithGeometricInterface()
test bool projectWithoutGeometricInterface()
{
errors = a03_geometric_is_comparable(loadTestProject("assignment01-good"));
return "There should be an interface called Geometric" in messages(errors);
return "There should be one interface called Geometric" in messages(errors);
}
test bool geometricShouldExtendComparable()
......
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