import $ivy.`org.scalatest::scalatest:3.0.5`
import $ivy.$
trait Fruit {
def color: String
???
}
defined trait Fruit
Next, we have the Companion Object for trait Fruit
that we created in PART I. Implement method create
, that creates a Fruit
, using the argument _color
, and setting the price
to "3.0".
TIP: Remember that a trait has no constructors, so you'll have to use anonymous class syntax to create an instance. (new Fruit {...}
)
object Fruit {
def create(_color: String): Fruit = ???
}
defined object Fruit
Make class Banana
extend from trait Fruit
, setting the method color
to "yellow" and receiving the price
in the constructor.
class Banana {
def color: String = ???
}
defined class Banana
Create a fruit with any color
using Fruit.create
and a banana of any price
using Banana class' constructor.
import org.scalatest._
class Test(fruit:Fruit, banana:Banana) extends FunSpec with Matchers {
describe("The fruit") {
it("should cost 3.0") {
// Uncomment next line once the exercise is done
// fruit.price shouldBe 3.0
}
}
describe("The banana") {
ignore("should be \"yellow\"") {
banana.color shouldBe "yellow"
}
}
}
val fruit: Fruit = ???
val banana: Banana = ???
run(new Test(fruit, banana))