Skip to content
Snippets Groups Projects
Commit 9a06f5a5 authored by Max Leuthäuser's avatar Max Leuthäuser
Browse files

- added task and tests

parent 0053d090
No related branches found
No related tags found
No related merge requests found
Showing
with 1160 additions and 111 deletions
Role-based Programming with SCROLL - Task 1
Role-based Programming with SCROLL - Task 2
===========================================
Task and test files for MOST SCROLL task 1.
Task and test files for MOST SCROLL task 2.
**Edit and develop:**
......
name := "MOSTSCROLLTask1"
name := "MOSTSCROLLTask2"
lazy val commonSettings = Seq(
organization := "tu.dresden.de",
......
import scroll.internal.Compartment
import solution.Behavior
import task.Robot
import solution.Behavior.ServiceRole
import solution.Navigation.NavigationRole
import solution.Sensor.ObservingRole
import solution.Actor.DrivableRole
import solution.{Transaction, Bank, Account, Person}
import task.Currency
object Main extends App {
new Compartment {
// constructing a Robot with its roles
val myRobot = Robot("Pete") play ServiceRole() play NavigationRole() play ObservingRole() play DrivableRole()
val stan = Person("Stan")
val brian = Person("Brian")
// merging all the role-playing-relations defined above
Behavior partOf this
val accForStan = new Account(Currency(10.0, "USD"))
val accForBrian = new Account(Currency(0, "USD"))
// calling the actual behavior
println(myRobot move())
new Bank {
stan play new Customer
brian play new Customer
accForStan play new CheckingsAccount
accForBrian play new SavingsAccount
+stan addAccount accForStan
+brian addAccount accForBrian
println("### Before transaction ###")
println("Balance for Stan: " + accForStan.balance)
println("Balance for Brian: " + accForBrian.balance)
val transaction = new Transaction(Currency(10.0, "USD"))
accForStan play new transaction.Source
accForBrian play new transaction.Target
// Defining a partOf relation between Transaction and Bank.
// The transaction needs full access to registered/bound Accounts like
// CheckingsAccount and SavingsAccount.
transaction partOf this
transaction play new TransactionRole execute()
println("### After transaction ###")
println("Balance for Stan: " + accForStan.balance)
println("Balance for Brian: " + accForBrian.balance)
println("Stan is playing the Customer role? " + (+stan).isPlaying[Customer])
println("Brian is playing the Customer role? " + (+brian).isPlaying[Customer])
println("Account for Stan is a CheckingsAccount? " + (+accForStan).isPlaying[CheckingsAccount])
println("Account for Brian is a SavingsAccount? " + (+accForBrian).isPlaying[SavingsAccount])
println(+stan listBalances())
println(+brian listBalances())
}
}
\ No newline at end of file
package solution
import task.Currency
import task.Increasable
import task.Decreasable
// TODO: implement the class Account here!
class Account /* ... */ {
/* ... */
}
\ No newline at end of file
package solution
import scroll.internal.Compartment
object Actor extends Compartment {
// TODO: Implement the role DrivableRole here!
}
package solution
import scroll.internal.support.DispatchQuery
import task.{Increasable, Decreasable, Accountable, Currency}
import scroll.internal.Compartment
import scroll.internal.support.DispatchQuery._
// TODO: implement the compartment Bank with its roles here!
class Bank /* ... */ {
implicit var dd: DispatchQuery = DispatchQuery.empty
/* ... */
}
package solution
import scroll.internal.Compartment
object Behavior extends Compartment {
// TODO: Implement the role ServiceRole here!
}
package solution
import scroll.internal.Compartment
object Navigation extends Compartment {
// TODO: Implement the role NavigationRole here!
}
package solution
// TODO: implement case class Person here!
\ No newline at end of file
package solution
import scroll.internal.Compartment
object Sensor extends Compartment {
// TODO: Implement the role ObservingRole here!
}
package solution
import scroll.internal.Compartment
import task.Currency
// TODO: implement the compartment Transaction with its roles here!
class Transaction /* ... */ {
/* ... */
}
\ No newline at end of file
package task
case class Robot(name: String)
trait Accountable
\ No newline at end of file
This diff is collapsed.
package task
trait Decreasable extends Accountable {
def decrease(amount: Currency)
}
\ No newline at end of file
package task
trait Increasable extends Accountable {
def increase(amount: Currency)
}
\ No newline at end of file
package task
case class Target(name: String)
case class Actor(name: String)
case class Result(
name: String,
target: Target,
sensorValue: Int,
actor: Actor
)
import org.scalatest.FeatureSpec
import org.scalatest.GivenWhenThen
import org.scalatest.Matchers
import solution.{Transaction, Bank, Person, Account}
import task.{Accountable, Currency}
class BankTests extends FeatureSpec with GivenWhenThen with Matchers {
info("Test spec for MOST SCROLL task 2.")
feature("Your implementation of Account") {
scenario("Initialization") {
Given("an new Account")
val a = new Account()
Then("its initial balance should be 0.")
a.balance shouldBe Currency(0, "USD")
}
scenario("Changing the balance") {
Given("an new Account")
val a = new Account()
Then("changing its balance should work.")
a.increase(Currency(10, "USD"))
a.balance shouldBe Currency(10, "USD")
a.decrease(Currency(10, "USD"))
a.balance shouldBe Currency(0, "USD")
}
}
feature("Your implementation of Person") {
scenario("Initialization") {
Given("an new Person")
val p = Person("Pete")
Then("its name should be set correctly.")
p.name shouldBe "Pete"
}
}
feature("Your implementation of Bank") {
scenario("Transferring money") {
Given("some Persons")
val stan = Person("Stan")
val brian = Person("Brian")
And("some Accounts for them")
val accForStan = new Account(Currency(10.0, "USD"))
val accForBrian = new Account(Currency(0, "USD"))
And("an new Bank")
val b = new Bank {
stan play new Customer
brian play new Customer
accForStan play new CheckingsAccount
accForBrian play new SavingsAccount
When("registering accounts to Customers")
+stan addAccount accForStan
+brian addAccount accForBrian
Then("they should be added correctly")
val acc1: List[Accountable] = (+stan).accounts
val acc2: List[Accountable] = (+brian).accounts
acc1 shouldBe List(accForStan)
acc2 shouldBe List(accForBrian)
And("their balances should be initialized correctly")
accForStan.balance shouldBe Currency(10, "USD")
accForBrian.balance shouldBe Currency(0, "USD")
When("defining a new transaction")
val transaction = new Transaction(Currency(10.0, "USD"))
accForStan play new transaction.Source
accForBrian play new transaction.Target
// Defining a partOf relation between Transaction and Bank.
// The transaction needs full access to registered/bound Accounts like
// CheckingsAccount and SavingsAccount.
transaction partOf this
And("executing it")
transaction play new TransactionRole execute()
Then("the corresponding balances should be correct")
accForStan.balance shouldBe Currency(0, "USD")
accForBrian.balance shouldBe Currency(9, "USD")
val c1: List[Currency] = +stan listBalances()
val c2: List[Currency] = +brian listBalances()
And("listed correctly as well.")
c1 shouldBe List(Currency(0, "USD"))
c2 shouldBe List(Currency(9, "USD"))
}
}
}
}
import org.scalatest.FeatureSpec
import org.scalatest.GivenWhenThen
import org.scalatest.Matchers
import scroll.internal.Compartment
import solution.Actor.DrivableRole
import solution.Behavior
import solution.Behavior.ServiceRole
import solution.Navigation.NavigationRole
import solution.Sensor.ObservingRole
import task.Robot
class RobotTests extends FeatureSpec with GivenWhenThen with Matchers {
info("Test spec for MOST SCROLL task 1.")
Given("Your solution for the Roles in the Compartments")
val r1 = ServiceRole()
val r2 = NavigationRole()
val r3 = ObservingRole()
val r4 = DrivableRole()
feature("Compartments and Roles") {
scenario("") {
Then("Calling their behavior should return the expected values.")
r2.getTarget shouldBe task.Target("kitchen")
r3.readSensor shouldBe 100
r4.getActor shouldBe task.Actor("wheels")
}
scenario("Merging all together") {
val name = "Pete"
Then("Calling move() should return the expected value.")
val c = new Compartment {
val myRobot = Robot(name) play r1 play r2 play r3 play r4
Behavior partOf this
val r: task.Result = myRobot move()
r shouldBe task.Result(
name,
task.Target("kitchen"),
100,
task.Actor("wheels"))
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment