-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathTestStack.java
75 lines (66 loc) · 1.85 KB
/
TestStack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*******************************************************************************
* Companion code for the book "Introduction to Software Design with Java"
* by Martin P. Robillard.
*
* Copyright (C) 2019 by Martin P. Robillard
*
* This code is licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* See http://creativecommons.org/licenses/by-nc-nd/4.0/
*******************************************************************************/
package chapter5;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.EmptyStackException;
import java.util.Stack;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
public class TestStack
{
private final Stack<String> aStack = new Stack<>();
@Test
void testPush_Empty()
{
String foo = "foo";
assertSame(foo, aStack.push(foo));
assertSame(foo, aStack.peek());
}
@Test
void testPush_NonEmpty()
{
aStack.push("A");
String foo = "foo";
assertSame(foo, aStack.push(foo));
assertSame(foo, aStack.peek());
}
@Test
void testPush_Null()
{ // Stack (i.e., Vector) allows null references as elements
assertNull(aStack.push(null));
assertNull(aStack.peek());
}
@Test
void testPop_NonEmpty()
{
String foo = "foo";
aStack.push(foo);
assertSame(foo, aStack.pop());
assertTrue(aStack.isEmpty());
}
@Test
void testPop_Empty()
{
// Lambda expressions will be covered in Chapter 9
assertThrows(EmptyStackException.class, new Executable()
{
@Override
public void execute() throws Throwable
{
aStack.pop();
}
});
}
}