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