-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstack_test.go
More file actions
79 lines (68 loc) · 1.55 KB
/
stack_test.go
File metadata and controls
79 lines (68 loc) · 1.55 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package json_markd
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
// Components from https://flaviocopes.com/golang-data-structure-stack/
func initStack() *itemStack {
var s itemStack
if s.items == nil {
s = itemStack{}
s.new()
}
return &s
}
func TestSize(t *testing.T) {
t.Run("when popping from empty stack", func(t *testing.T) {
s := initStack()
t.Run("it should return error", func(t *testing.T) {
s.push(1)
s.push(2)
s.push(3)
assert.Equal(t, 3, s.size())
})
})
}
func TestPush(t *testing.T) {
t.Run("when popping from empty stack", func(t *testing.T) {
s := initStack()
t.Run("it should return error", func(t *testing.T) {
s.push(1)
assert.Equal(t, 1, s.size())
s.push(2)
assert.Equal(t, 2, s.size())
s.push(3)
assert.Equal(t, 3, s.size())
})
})
}
func TestTop(t *testing.T) {
t.Run("when popping from empty stack", func(t *testing.T) {
s := initStack()
t.Run("it should return error", func(t *testing.T) {
s.push(1)
s.push(2)
item := (*s.top()).(int)
assert.Equal(t, 2, item)
})
})
}
func TestPop(t *testing.T) {
t.Run("when popping from empty stack", func(t *testing.T) {
s := initStack()
t.Run("it should return error", func(t *testing.T) {
_, err := s.pop()
assert.Equal(t, errors.New(".errors.stack_empty"), err)
})
})
t.Run("when popping from a stack", func(t *testing.T) {
s := initStack()
t.Run("it should remove one element", func(t *testing.T) {
s.push(1)
s.push(2)
item, _ := s.pop()
assert.Equal(t, 2, (*item).(int))
})
})
}