-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_test.go
More file actions
52 lines (43 loc) · 1.29 KB
/
stack_test.go
File metadata and controls
52 lines (43 loc) · 1.29 KB
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
package stack_test
import (
"testing"
"github.com/tiny-go/stack"
)
func TestStack(t *testing.T) {
t.Run("Given an empty stack", func(t *testing.T) {
stack := new(stack.Stack[int])
t.Run("test if 'nil' is returned when Top() is called on empty stack", func(t *testing.T) {
if _, ok := stack.Top(); ok {
t.Error("stack was expected to be empty")
}
})
stack.Push(42)
t.Run("test if stack length is changed when element is pushed", func(t *testing.T) {
if stack.Len() != 1 {
t.Error("stack length was expected to be changed")
}
})
top, ok := stack.Top()
t.Run("test if Top() points to the correct element in the stack", func(t *testing.T) {
if !ok || top != 42 {
t.Errorf("unexpected value was returned: %v", top)
}
})
pop, ok := stack.Pop()
t.Run("test if Pop() returns expected value", func(t *testing.T) {
if !ok || pop != 42 {
t.Errorf("unexpected value was returned: %v", pop)
}
})
t.Run("test if length is changed when the element is popped", func(t *testing.T) {
if stack.Len() != 0 {
t.Error("stack length was expected to be changed")
}
})
t.Run("test if 'nil' is returned when Pop() is called on empty stack", func(t *testing.T) {
if _, ok := stack.Pop(); ok {
t.Error("stack was expected to be empty")
}
})
})
}