(t *testing.T)
| 7 | ) |
| 8 | |
| 9 | func TestStack(t *testing.T) { |
| 10 | t.Run("test_push_adds_entry_and_creates_new_stack", func(t *testing.T) { |
| 11 | firstStack := Push(nil, "hello") |
| 12 | secondStack := Push(firstStack, "world") |
| 13 | require.NotEqual(t, Peek(firstStack), Peek(secondStack)) |
| 14 | require.Equal(t, 1, Len(firstStack)) |
| 15 | require.Equal(t, 2, Len(secondStack)) |
| 16 | }) |
| 17 | |
| 18 | t.Run("test_pop_does_not_affect_original", func(t *testing.T) { |
| 19 | firstStack := Push(nil, "hello") |
| 20 | val, secondStack := Pop(firstStack) |
| 21 | require.Equal(t, "hello", val) |
| 22 | |
| 23 | // the second stack should be Nil, since we .popped our only element |
| 24 | require.Nil(t, secondStack) |
| 25 | require.Equal(t, 0, Len(secondStack)) |
| 26 | |
| 27 | // But the first stack should not have been modified |
| 28 | require.Equal(t, "hello", Peek(firstStack)) |
| 29 | }) |
| 30 | |
| 31 | t.Run("test_push_then_pop", func(t *testing.T) { |
| 32 | firstStack := Push(nil, "hello") |
| 33 | secondStack := Push(firstStack, "world") |
| 34 | |
| 35 | // This should now have the same value as the first one |
| 36 | _, thirdStack := Pop(secondStack) |
| 37 | require.Equal(t, Peek(firstStack), Peek(thirdStack)) |
| 38 | |
| 39 | // This Pop removes the last value from this stack |
| 40 | _, fourth := Pop(thirdStack) |
| 41 | require.Nil(t, fourth) |
| 42 | |
| 43 | // But does not affect the other stack with the same value |
| 44 | require.NotNil(t, firstStack) |
| 45 | }) |
| 46 | |
| 47 | t.Run("test_pop_on_empty_stack", func(t *testing.T) { |
| 48 | defer func() { |
| 49 | if r := recover(); r == nil { |
| 50 | t.Errorf("expected test to panic and it did not") |
| 51 | } |
| 52 | }() |
| 53 | |
| 54 | firstStack := Push(nil, "hello") |
| 55 | _, secondStack := Pop(firstStack) |
| 56 | |
| 57 | require.Nil(t, secondStack) |
| 58 | Pop(secondStack) // this line should cause a panic |
| 59 | }) |
| 60 | |
| 61 | t.Run("test_string", func(t *testing.T) { |
| 62 | s := Push(nil, "hello") |
| 63 | s = Push(s, "world") |
| 64 | require.Equal(t, "worldhello", String(s)) |
| 65 | |
| 66 | _, s = Pop(s) |
nothing calls this directly
no test coverage detected
searching dependent graphs…