Commit 3f53b241 authored by Rick van der Wal's avatar Rick van der Wal
Browse files

Improved snake tests using whitelist

parent aeab5bbf
......@@ -20,44 +20,50 @@ set[Message] aSnake_model_separated(M3 model) = classDependsOn(model, "World", j
set[Message] aSnake_view_separated(M3 model)
{
// Check if no classes other than SnakeGame contain JavaFX shapes
set[loc] shapeClasses =
{ |java+class:///javafx/scene/shape/Circle|
, |java+class:///javafx/scene/shape/Rectangle|
, |java+class:///javafx/scene/shape/Polygon|
, |java+class:///javafx/scene/shape/Ellipse|
, |java+class:///javafx/scene/shape/Line|
, |java+class:///javafx/scene/shape/Arc|
};
set[loc] createdClasses =
{ findClass(model, "Main")
, findClass(model, "Segment")
, findClass(model, "Snake")
, findClass(model, "Food")
, findClass(model, "World")
};
return { error("<getName(model, shapeClass)>: only SnakeGame should use shapes", createdClass)
| createdClass <- createdClasses
, shapeClass <- shapeClasses
, shapeClass in model.extends[createdClass] || shapeClass in allTypeDependenciesLoc(model, createdClass)
};
try
{
set[loc] shapeClasses =
{ javafxSceneShapeCircle
, javafxSceneShapeRectangle
, javafxSceneShapePolygon
, javafxSceneShapeEllipse
, javafxSceneShapeLine
, javafxSceneShapeArc
};
set[loc] permittedClasses = { findClass(model, "SnakeGame") };
return { error("<getName(model, shapeClass)>: only SnakeGame should use shapes", class)
| class <- classes(model) - permittedClasses
, shapeClass <- shapeClasses
, shapeClass in model.extends[class] || shapeClass in allTypeDependenciesLoc(model, class)
};
}
catch e:error(_,_):
{
return { e };
}
}
set[Message] aSnake_controller_separated(M3 model)
{
// Check if InputHandler is the only class that uses JavaFX EventHandler
set[loc] eventClasses =
{ javafxSceneInputKeyEvent
, javafxSceneInputMouseEvent
};
set[loc] createdClasses =
{ findClass(model, "Segment")
, findClass(model, "Snake")
, findClass(model, "Food")
, findClass(model, "World")
};
return { error("<getName(model, eventClass)>: only InputHandler should handle input events", createdClass)
| createdClass <- createdClasses
, eventClass <- eventClasses
, eventClass in allTypeDependenciesLoc(model, createdClass)
};
try
{
set[loc] eventClasses =
{ javafxSceneInputKeyEvent
, javafxSceneInputMouseEvent
};
set[loc] permittedClasses =
{findClass(model, "Main")
, findClass(model, "InputHandler")
};
return { error("<getName(model, eventClass)>: only InputHandler should handle input events", class)
| class <- classes(model) - permittedClasses
, eventClass <- eventClasses
, eventClass in allTypeDependenciesLoc(model, class)
};
}
catch e:error(_,_):
{
return { e };
}
}
......@@ -37,3 +37,10 @@ public loc javafxAnimationTimeline = |java+class:///javafx/animation/Timeline|;
public loc javafxSceneInputKeyEvent = |java+class:///javafx/scene/input/KeyEvent|;
public loc javafxSceneInputMouseEvent = |java+class:///javafx/scene/input/MouseEvent|;
public loc javafxSceneShapeCircle = |java+class:///javafx/scene/shape/Circle|;
public loc javafxSceneShapeRectangle = |java+class:///javafx/scene/shape/Rectangle|;
public loc javafxSceneShapePolygon = |java+class:///javafx/scene/shape/Polygon|;
public loc javafxSceneShapeEllipse = |java+class:///javafx/scene/shape/Ellipse|;
public loc javafxSceneShapeLine = |java+class:///javafx/scene/shape/Line|;
public loc javafxSceneShapeArc = |java+class:///javafx/scene/shape/Arc|;
package snake;
import javafx.event.EventHandler;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
/**
* Handles controls of a snake game, where the 'a' and 'd' keys can be used to move and 's' (un)pauses the game
*/
public class InputHandler {
private final EventHandler<KeyEvent> keyHandler;
private final EventHandler<MouseEvent> mouseHandler;
public InputHandler(World world) {
Snake snake = world.getSnake();
keyHandler = keyEvent -> {
keyEvent.consume();
};
mouseHandler = mouseEvent -> {
mouseEvent.consume();
};
}
public EventHandler<KeyEvent> getKeyHandler() {
return keyHandler;
}
public EventHandler<MouseEvent> getMouseHandler() {
return mouseHandler;
}
}
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