Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Commit

Permalink
Fix StateImpl.equals, don't mock RootElement in tests but use a real one
Browse files Browse the repository at this point in the history
  • Loading branch information
tdauth committed Nov 5, 2018
1 parent d729a51 commit 77aab45
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object GuiStateMachineImpl extends GuiStateMachine {
if (states.contains(descriptors)) {
states(descriptors)
} else {
val s = new StateImpl(descriptors, neverExploredActions.to)
val s = new StateImpl(descriptors, neverExploredActions)
states += (descriptors -> s)
s
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class StateImpl(val descriptors: Descriptors, var neverExploredActions: Set[Acti
if (!transitions.contains(a)) {
transitions = transitions + (a -> HashSet(to))
// TODO #4 This is not done in the legacy code:
neverExploredActions -= a
neverExploredActions = neverExploredActions - a
} else {
transitions = transitions + (a -> (transitions(a) + to))
}
Expand All @@ -32,7 +32,7 @@ class StateImpl(val descriptors: Descriptors, var neverExploredActions: Set[Acti
override def equals(obj: Any): Boolean = {
if (obj.isInstanceOf[StateImpl]) {
val other = obj.asInstanceOf[StateImpl]
this.descriptors eq other.descriptors
this.descriptors == other.descriptors
} else {
super.equals(obj)
}
Expand Down
39 changes: 17 additions & 22 deletions src/test/scala/de/retest/guistatemachine/api/AbstractApiSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,34 @@ package de.retest.guistatemachine.api

import java.util.Collections

import de.retest.ui.Path
import de.retest.ui.descriptors._
import de.retest.ui.image.Screenshot
import org.scalamock.scalatest.MockFactory
import org.scalatest.{Matchers, WordSpec}
import java.util.Arrays


abstract class AbstractApiSpec extends WordSpec with Matchers with MockFactory {

/**
* The standard constructor of RootElement leads to an exception.
* Hence, we have to use the constructor with arguments.
*/
class MockableRootElement
extends RootElement(
"retestId",
new IdentifyingAttributes(Collections.emptyList()),
new Attributes(),
new Screenshot("prefix", Array(1, 2, 3), Screenshot.ImageType.PNG),
Collections.emptyList(),
"screen0",
0,
"My Window"
)
def getIdentifyingAttributes(id: String): IdentifyingAttributes =
new IdentifyingAttributes(Arrays.asList(new StringAttribute("a", id), new StringAttribute("b", id), new StringAttribute("c", id)))

/**
* The identifying attributes and the contained components specify the equality but we mock everything for our tests.
* The identifying attributes and the contained components specify the equality.
* @param id If the ID is equal the returned root element will be equal.
* @return A new root element which is equal to itself but not to any other root element.
*/
def getRootElement(): RootElement = {
val r = mock[MockableRootElement]
(r.equals _).expects().returning(false)
(r.equals _).expects(r).returning(true)
r
}
def getRootElement(id: String): RootElement = new RootElement(
"retestId",
getIdentifyingAttributes(id),
new Attributes(),
new Screenshot("prefix", Array(1, 2, 3), Screenshot.ImageType.PNG),
Collections.emptyList(),
"screen0",
0,
"My Window"
)

def getAction(): org.openqa.selenium.interactions.Action = mock[org.openqa.selenium.interactions.Action]
}
27 changes: 20 additions & 7 deletions src/test/scala/de/retest/guistatemachine/api/DescriptorsSpec.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
package de.retest.guistatemachine.api
import scala.collection.immutable.HashSet

class DescriptorsSpec extends AbstractApiSpec {

val identifyingAttributesA = getIdentifyingAttributes("a")
val identifyingAttributesB = getIdentifyingAttributes("b")

val rootElementA = getRootElement("a")
val rootElementB = getRootElement("b")
val descriptorsA = Descriptors(Set(rootElementA))
val descriptorsB = Descriptors(Set(rootElementB))

"Descriptor" should {
"make sure that identifying attributes with different IDs are not equal" in {
identifyingAttributesA shouldNot equal(identifyingAttributesB)
identifyingAttributesA.hashCode() shouldNot equal(identifyingAttributesB.hashCode())
rootElementA shouldNot equal(rootElementB)
rootElementA.hashCode() shouldNot equal(rootElementB.hashCode())
}

"not equal" in {
val d0 = Descriptors(HashSet(getRootElement()))
val d1 = Descriptors(HashSet(getRootElement()))
d0.equals(d1) shouldEqual false
descriptorsA.rootElements.size shouldEqual 1
descriptorsB.rootElements.size shouldEqual 1
descriptorsA shouldNot equal(descriptorsB)
}

"equal" in {
val d0 = Descriptors(HashSet(getRootElement()))
val d1 = Descriptors(HashSet(getRootElement()))
d0.equals(d1) shouldEqual true
descriptorsA.equals(descriptorsA) shouldEqual true
descriptorsB.equals(descriptorsB) shouldEqual true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import de.retest.guistatemachine.api.{AbstractApiSpec, Action, Descriptors}

class GuiStateMachineImplSpec extends AbstractApiSpec {

val rootElement0Mock = getRootElement()
val rootElement1Mock = getRootElement()
val rootElementA = getRootElement("a")
val rootElementB = getRootElement("b")
val action0Mock = getAction()
val action1Mock = getAction()

Expand All @@ -16,16 +16,20 @@ class GuiStateMachineImplSpec extends AbstractApiSpec {
initial.getTransitions.size shouldEqual 0
}

"add a transition" in {
"add a transition to a new state" in {
val initialDescriptors = getDescriptors
val initial = GuiStateMachineImpl.getState(getDescriptors, getNeverExploredActions)
val s = GuiStateMachineImpl.executeAction(initial, Action(action0Mock), Descriptors(Set(rootElement0Mock)), getNeverExploredActions)
val sDescriptors = Descriptors(Set(rootElementA))
// It should be a new state and not the same again.
initialDescriptors.equals(sDescriptors) shouldEqual false
val s = GuiStateMachineImpl.executeAction(initial, Action(action0Mock), sDescriptors, getNeverExploredActions)
initial.getNeverExploredActions.size shouldEqual 1
initial.getTransitions.size shouldEqual 1
s.getNeverExploredActions.size shouldEqual 2
s.getTransitions.size shouldEqual 0
}
}

def getDescriptors: Descriptors = Descriptors(Set(rootElement0Mock, rootElement1Mock))
def getDescriptors: Descriptors = Descriptors(Set(rootElementA, rootElementB))
def getNeverExploredActions: Set[Action] = Set(Action(action0Mock), Action(action1Mock))
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import scala.collection.immutable.HashSet

class StateImplSpec extends AbstractApiSpec {

val descriptorsA = Descriptors(HashSet(getRootElement("a")))
val descriptorsB = Descriptors(HashSet(getRootElement("b")))
val action0Mock = getAction()
val action1Mock = getAction()

"StateImpl" should {
"not equal" in {
val s0 = new StateImpl(Descriptors(HashSet(getRootElement())), HashSet(Action(action0Mock)))
val s1 = new StateImpl(Descriptors(HashSet(getRootElement())), HashSet(Action(action1Mock)))
val s0 = new StateImpl(descriptorsA, HashSet(Action(action0Mock)))
val s1 = new StateImpl(descriptorsB, HashSet(Action(action1Mock)))
s0.equals(s1) shouldEqual false
}

"equal" in {
val s0 = new StateImpl(Descriptors(HashSet(getRootElement())), HashSet(Action(action0Mock)))
val s1 = new StateImpl(Descriptors(HashSet(getRootElement())), HashSet(Action(action1Mock)))
val s0 = new StateImpl(descriptorsA, HashSet(Action(action0Mock)))
val s1 = new StateImpl(descriptorsA, HashSet(Action(action1Mock)))
s0.equals(s1) shouldEqual true
}
}
Expand Down

0 comments on commit 77aab45

Please sign in to comment.