Skip to content

Commit 4ecccaf

Browse files
nhAnikgavv
andauthored
Add Environment.Delete (#243)
Co-authored-by: Victor Gaydov <victor@enise.org>
1 parent 11b1b16 commit 4ecccaf

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

environment.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ func (e *Environment) Put(key string, value interface{}) {
6161
e.data[key] = value
6262
}
6363

64+
// Delete removes the value with key from the environment.
65+
//
66+
// Example:
67+
//
68+
// env := NewEnvironment(t)
69+
// env.Put("key1", "str")
70+
// env.Delete("key1")
71+
func (e *Environment) Delete(key string) {
72+
opChain := e.chain.enter("Delete(%q)", key)
73+
defer opChain.leave()
74+
75+
delete(e.data, key)
76+
}
77+
6478
// Has returns true if value exists in the environment.
6579
//
6680
// Example:

environment_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ func TestEnvironment_Generic(t *testing.T) {
5353
env.chain.assertFailed(t)
5454
}
5555

56+
func TestEnvironment_Delete(t *testing.T) {
57+
env := newEnvironment(newMockChain(t))
58+
59+
env.Put("good_key", 123)
60+
env.chain.assertNotFailed(t)
61+
62+
assert.True(t, env.Has("good_key"))
63+
assert.NotNil(t, env.Get("good_key"))
64+
assert.Equal(t, 123, env.Get("good_key").(int))
65+
env.chain.assertNotFailed(t)
66+
67+
env.Delete("good_key")
68+
env.chain.assertNotFailed(t)
69+
70+
assert.False(t, env.Has("good_key"))
71+
assert.Nil(t, env.Get("good_key"))
72+
env.chain.assertFailed(t)
73+
env.chain.clearFailed()
74+
}
75+
5676
func TestEnvironment_NotFound(t *testing.T) {
5777
env := newEnvironment(newMockChain(t))
5878

0 commit comments

Comments
 (0)