Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
class NonEmptyFixedStackTest extends GroovyTestCase {
    private stack

    void setUp() {
        stack = new FixedStack()
        [ "a", "b", "c" ].each{ x -> stack.push x }
    }

    void testPreConditions() {
        assert !stack.isEmpty()
    }

    void testShouldAddToTheTopWhenSentPush() {
        stack.push "d"
        assert stack.peek() == "d"
    }

    void testShouldBeUnchangedWhenSentPushThenPop() {
        stack.push "anything"
        stack.pop()
        assert stack.peek() == "c"
    }

    void testShouldReturnTheTopItemWhenSentPeek() {
        assert stack.peek() == "c"
    }

    void testShouldNotRemoveTheTopItemWhenSentPeek() {
        assert stack.peek() == "c"
        assert stack.peek() == "c"
    }

    void testShouldReturnTheTopItemWhenSentPop() {
        assert stack.pop() == "c"
    }

    void testShouldRemoveTheTopItemWhenSentPop() {
        assert stack.pop() == "c"
        assert stack.pop() == "b"
    }

}

class EmptyFixedStackTest extends GroovyTestCase {
    private stack = new FixedStack()

    void testPreConditions() {
        assert stack.isEmpty()
    }

    void testShouldNoLongerBeEmptyAfterPush() {
        stack.push "anything"
        assert !stack.isEmpty()
    }

    void testShouldComplainWhenSentPeek() {
        shouldFail(StackUnderflowException) {
            stack.peek()
        }
    }

    void testShouldComplainWhenSentPop() {
        shouldFail(StackUnderflowException) {
            stack.pop()
        }
    }
}

class FullFixedStackTest extends GroovyTestCase {
    private stack

    void setUp() {
        stack = new FixedStack()
        (1..FixedStack.MAXSIZE).each{ x -> stack.push x }
    }

    void testPreConditions() {
        assert stack.isFull()
    }

    void testShouldRemainFullAfterPeek() {
        stack.peek()
        assert stack.isFull()
    }

    void testShouldNoLongerBeFullAfterPop() {
        stack.pop()
        assert !stack.isFull()
    }

    void testShouldComplainOnPush() {
        shouldFail(StackOverflowException) {
            stack.push "anything"
        }
    }
}

class AlmostFullFixedStackTest extends GroovyTestCase {
    private stack

    void setUp() {
        stack = new FixedStack()
        (1..<FixedStack.MAXSIZE).each{ x -> stack.push x }
    }

    void testPreConditions() {
        assert !stack.isFull()
    }

    void testShouldBecomeFullAfterPush() {
        stack.push "anything"
        assert stack.isFull()
    }
}

class AlmostEmptyFixedStackTest extends GroovyTestCase {
    private stack

    void setUp() {
        stack = new FixedStack()
        stack.push "anything"
    }

    void testPreConditions() {
        assert !stack.isEmpty()
    }

    void testShouldRemainNotEmptyAfterPeek() {
        stack.peek()
        testPreConditions()
    }

    void testShouldBecomeEmptyAfterPop() {
        stack.pop()
        assert stack.isEmpty()
    }
}

...