Skip to content

Instantly share code, notes, and snippets.

@brunodles
Last active June 7, 2016 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunodles/54b23e9c1814f0732a35751fde7163bd to your computer and use it in GitHub Desktop.
Save brunodles/54b23e9c1814f0732a35751fde7163bd to your computer and use it in GitHub Desktop.
import org.jetbrains.spek.api.Spek
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue
/**
* Created by bruno on 10/03/16.
*/
class StackSpeks : Spek() { init {
given("a Stack of Integers") {
on("the begining") {
val stack = Stack<Int>()
it("should be empty") {
assertTrue(stack.isEmpty)
}
it("size should be 0") {
assertEquals(0, stack.size())
}
}
on("enqueue a new Item") {
val stack = Stack<Int>()
stack.enqueue(5)
it("should not be empty") {
assertFalse(stack.isEmpty)
}
it("size should be equals to 1") {
assertEquals(1, stack.size())
}
}
on("dequeue a Item and Stack size is equals to 1") {
val stack = Stack<Int>();
stack.enqueue(3)
val item = stack.dequeue()
it("should return the enqueued item") {
assertEquals(3, item)
}
it("should be empty again") {
assertTrue(stack.isEmpty)
}
it("should have size equals to 0") {
assertEquals(0, stack.size())
}
}
on("dequeue a Item and Stack size is equals to 0") {
val stack = Stack<Int>();
val item = stack.dequeue()
it("should return null") {
assertNull(item)
}
it("should continue empty") {
assertTrue(stack.isEmpty)
}
it("should have size equals to 0") {
assertEquals(0, stack.size())
}
}
}
}
}
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Created by bruno on 09/03/16.
*/
@RunWith(Enclosed.class)
public class StackTest_junit4_nestedClasses {
static Stack<Integer> stack;
public static class WhenItWasCreated {
@Before
public void setup() {
stack = new Stack<>();
}
@Test
public void shouldBeEmpty() {
assertTrue("it should be empty", stack.isEmpty());
}
@Test
public void shouldHaveSizeEqualsToZero() {
assertEquals("size should be 0", 0, stack.size());
}
}
public static class WhenEnqueueOneObject {
@Before
public void setup() {
stack = new Stack<>();
stack.enqueue(1);
}
@Test
public void shouldNotBeEmpty() {
assertFalse("it should not be empty", stack.isEmpty());
}
@Test
public void shouldHaveSizeEqualsToOne() {
assertEquals("size should be 1", 1, stack.size());
}
}
@RunWith(Enclosed.class)
public static class WhenDequeue {
static Integer item;
public static class AndSizeIsEqualsToOne {
@Before
public void setup() {
stack = new Stack<>();
stack.enqueue(2);
item = stack.dequeue();
}
@Test
public void shouldReturnTheEnqueuedItem() {
assertEquals("should return the enqueued item", new Integer(2), item);
}
@Test
public void shouldBeEmpty() {
assertTrue("it .hould be empty", stack.isEmpty());
}
@Test
public void shouldHaveSizeEqualsToZero() {
assertEquals("size should be 0", 0, stack.size());
}
}
public static class AndSizeIsEqualsToZero {
@Before
public void setup() {
stack = new Stack<>();
item = stack.dequeue();
}
@Test
public void shouldReturnNull() {
assertNull("shouuld return null", item);
}
@Test
public void shouldContinueEmpty() {
assertTrue("should continue empty", stack.isEmpty());
}
@Test
public void shouldHaveSizeEqualsToZero() {
assertEquals("should have size equals to zero", 0, stack.size());
}
}
}
}
import com.mscharhag.oleaster.matcher.Matchers.expect
import com.mscharhag.oleaster.runner.OleasterRunner
import com.mscharhag.oleaster.runner.StaticRunnerSupport.*
import org.junit.runner.RunWith
/**
* Created by bruno on 08/03/16.
*/
@RunWith(OleasterRunner::class)
class StackTest_oleaster_kotlin {
internal var stack: Stack<Integer> = Stack()
internal var item: Integer? = null
init {
beforeEach { stack = Stack<Integer>() }
describe("When it was created") {
it("should be empty") {
expect(stack.isEmpty).toBeTrue()
}
}
describe("When a item is enqueued") {
beforeEach { stack.enqueue(Integer(5)) }
it("should not be empty") { expect(stack.isEmpty).toBeFalse() }
it("size should be 1") { expect(stack.size()).toEqual(1) }
}
describe("When a item is dequeued") {
describe("and the size is equals to 1") {
beforeEach {
stack.enqueue(Integer(3))
item = stack.dequeue()
}
it("should return the enqueued item") { expect(item).toEqual(3) }
it("should be empty again") {
expect(stack.isEmpty).toBeTrue()
expect(stack.size()).toEqual(0L)
}
}
describe("and the size is equals to 0") {
it("should return null") { expect(stack.dequeue()).toBeNull() }
it("should continue empty") {
expect(stack.isEmpty).toBeTrue()
expect(stack.size()).toEqual(0L)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment