Skip to content

Commit

Permalink
Rewrite tests, drop all dependencies
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Oct 20, 2023
1 parent 28ae508 commit c97c8bd
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 107 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Dig

A go package that provides a Ruby-like `Hash.dig` mechanism for `map[string]any`, which in YAML is refered to as "Mapping".
A simple zero-dependency go package that provides a Ruby-like `Hash.dig` mechanism for `map[string]any`, which in YAML is refered to as "Mapping".

## Usage

Expand Down
40 changes: 0 additions & 40 deletions examples/readme.go

This file was deleted.

11 changes: 0 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
module github.com/k0sproject/dig

go 1.21

require (
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 0 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +0,0 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
117 changes: 74 additions & 43 deletions mapping_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
package dig_test

import (
"encoding/json"
"fmt"
"testing"

"github.com/k0sproject/dig"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)

func mustEqualString(t *testing.T, expected, actual string) {
if expected != actual {
t.Errorf("Expected %v, got %v", expected, actual)
}
}

func mustBeNil(t *testing.T, actual any) {
if actual != nil {
t.Errorf("Expected nil, got %v", actual)
}
}

func mustEqual(t *testing.T, expected, actual any) {
if expected != actual {
t.Errorf("Expected %v, got %v", expected, actual)
}
}

func TestDig(t *testing.T) {
m := dig.Mapping{
"foo": dig.Mapping{
"bar": "foobar",
},
}

assert.Equal(t, "foobar", m.Dig("foo", "bar"))
t.Run("fetch nested value", func(t *testing.T) {
mustEqualString(t, "foobar", m.Dig("foo", "bar").(string))
})

assert.Nil(t, m.Dig("foo", "non-existing", "key"))
t.Run("non-existing key should return nil", func(t *testing.T) {
mustBeNil(t, m.Dig("foo", "non-existing"))
})
}

func TestDigString(t *testing.T) {
Expand All @@ -29,9 +49,14 @@ func TestDigString(t *testing.T) {
},
}

assert.Equal(t, "foobar", m.DigString("foo", "bar"))
assert.Equal(t, "", m.DigString("foo", "nonexisting"))
assert.Equal(t, "", m.DigString("nonexisting", "nonexisting"))
t.Run("fetch nested string", func(t *testing.T) {
mustEqualString(t, "foobar", m.DigString("foo", "bar"))
})

t.Run("non-existing key should return an empty string", func(t *testing.T) {
mustEqualString(t, "", m.DigString("foo", "non-existing"))
mustEqualString(t, "", m.DigString("non-existing", "non-existing"))
})
}

func TestDigMapping(t *testing.T) {
Expand All @@ -41,16 +66,23 @@ func TestDigMapping(t *testing.T) {
},
}

assert.Equal(t, "foobar", m.DigMapping("foo")["bar"])
t.Run("fetch nested mapping", func(t *testing.T) {
mustEqualString(t, "foobar", m.DigMapping("foo")["bar"].(string))
})

t.Run("set a nested value", func(t *testing.T) {
m.DigMapping("foo", "baz")["dog"] = 1
mustEqual(t, 1, m.Dig("foo", "baz", "dog"))

m.DigMapping("foo", "baz")["dog"] = 1
assert.Equal(t, 1, m.Dig("foo", "baz", "dog"))
// Make sure foo.bar was left intact
assert.Equal(t, "foobar", m.Dig("foo", "bar"))
// Make sure foo.bar was left intact
mustEqualString(t, "foobar", m.DigString("foo", "bar"))
})

// Overwrite foo.bar with a new mapping
m.DigMapping("foo", "bar")["baz"] = "hello"
assert.Equal(t, "hello", m.Dig("foo", "bar", "baz"))
t.Run("overwrite mapping", func(t *testing.T) {
m.DigMapping("foo", "bar")["baz"] = "hello"
mustEqualString(t, "hello", m.DigString("foo", "bar", "baz"))
mustBeNil(t, m.Dig("foo", "bar", "dog"))
})
}

func TestDup(t *testing.T) {
Expand All @@ -69,37 +101,36 @@ func TestDup(t *testing.T) {

dup := m.Dup()

m.DigMapping("foo")["bar"] = "barbar"
arr := m.Dig("array").([]string)
arr = append(arr, "world")
m["array"] = arr

ma := m["mappingarray"].([]dig.Mapping)
maa := ma[0]
maa["bar"] = "barbar"

assert.Equal(t, "barbar", m.Dig("foo", "bar"))
assert.Equal(t, "foobar", dup.Dig("foo", "bar"))

a := m.Dig("array").([]string)
b := dup.Dig("array").([]string)

assert.Len(t, a, 2)
assert.Len(t, b, 1)

am := m.Dig("mappingarray").([]dig.Mapping)
bm := dup.Dig("mappingarray").([]dig.Mapping)

assert.Equal(t, "barbar", am[0]["bar"])
assert.Equal(t, "foobar", bm[0]["bar"])
t.Run("modifying clone's values should not modify original", func(t *testing.T) {
m.DigMapping("foo")["bar"] = "barbar"
mustEqualString(t, "foobar", dup.DigString("foo", "bar"))
mustEqualString(t, "barbar", m.DigString("foo", "bar"))
})

t.Run("modifying a cloned slice should not modify original", func(t *testing.T) {
arr := m.Dig("array").([]string)
arr = append(arr, "world")
m["array"] = arr

ma := m["mappingarray"].([]dig.Mapping)
maa := ma[0]
maa["bar"] = "barbar"

mustEqual(t, 1, len(dup.Dig("array").([]string)))
mustEqual(t, 2, len(m.Dig("array").([]string)))

am := m.Dig("mappingarray").([]dig.Mapping)
bm := dup.Dig("mappingarray").([]dig.Mapping)
mustEqualString(t, "barbar", am[0]["bar"].(string))
mustEqualString(t, "foobar", bm[0]["bar"].(string))
})
}

func TestUnmarshalYamlWithNil(t *testing.T) {
data := `foo: null`
data := []byte(`{"foo": null}`)
var m dig.Mapping
err := yaml.Unmarshal([]byte(data), &m)
assert.NoError(t, err)
assert.Nil(t, m.Dig("foo"))
mustBeNil(t, json.Unmarshal(data, &m))
mustBeNil(t, m.Dig("foo"))
}

func ExampleMapping_Dig() {
Expand Down

0 comments on commit c97c8bd

Please sign in to comment.