diff --git a/go.mod b/go.mod index 8a9efce..bc793c8 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,3 @@ module git.wisehodl.dev/jay/go-mana-component go 1.25.0 - -require github.com/stretchr/testify v1.11.1 - -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 -) diff --git a/go.sum b/go.sum index c4c1710..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +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.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= -github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= -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.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main_test.go b/main_test.go index 11bd2cd..01da8c8 100644 --- a/main_test.go +++ b/main_test.go @@ -2,29 +2,51 @@ package component import ( "context" - "github.com/stretchr/testify/assert" "log/slog" + "slices" "testing" ) +func assertPanics(t *testing.T, f func()) { + t.Helper() + defer func() { + if recover() == nil { + t.Error("expected panic, but none occurred") + } + }() + f() +} + func TestNew(t *testing.T) { t.Run("creates a new component", func(t *testing.T) { ctx, err := New(context.Background(), "mymodule", "mycomponent") - assert.NoError(t, err) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } c, ok := Get(ctx) - assert.True(t, ok) - assert.Equal(t, "mymodule", c.Module()) - assert.Equal(t, []string{"mycomponent"}, c.Path()) - assert.Equal(t, "mycomponent", c.PathString()) + if !ok { + t.Fatal("expected component in context") + } + if c.Module() != "mymodule" { + t.Errorf("expected module %q, got %q", "mymodule", c.Module()) + } + if !slices.Equal(c.Path(), []string{"mycomponent"}) { + t.Errorf("expected path %v, got %v", []string{"mycomponent"}, c.Path()) + } + if c.PathString() != "mycomponent" { + t.Errorf("expected path string %q, got %q", "mycomponent", c.PathString()) + } }) t.Run("should return error", func(t *testing.T) { _, err := New(nil, "mymodule", "mycomponent") - assert.Error(t, err) + if err == nil { + t.Fatal("expected error, got nil") + } }) t.Run("must variant should panic", func(t *testing.T) { - assert.Panics(t, func() { + assertPanics(t, func() { MustNew(nil, "mymodule", "mycomponent") }) }) @@ -34,21 +56,33 @@ func TestExtend(t *testing.T) { t.Run("extends existing component", func(t *testing.T) { ctx := MustNew(context.Background(), "mymodule", "mycomponent") ctx, err := Extend(ctx, "subcomponent") - assert.NoError(t, err) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } c, ok := Get(ctx) - assert.True(t, ok) - assert.Equal(t, "mymodule", c.Module()) - assert.Equal(t, []string{"mycomponent", "subcomponent"}, c.Path()) - assert.Equal(t, "mycomponent.subcomponent", c.PathString()) + if !ok { + t.Fatal("expected component in context") + } + if c.Module() != "mymodule" { + t.Errorf("expected module %q, got %q", "mymodule", c.Module()) + } + if !slices.Equal(c.Path(), []string{"mycomponent", "subcomponent"}) { + t.Errorf("expected path %v, got %v", []string{"mycomponent", "subcomponent"}, c.Path()) + } + if c.PathString() != "mycomponent.subcomponent" { + t.Errorf("expected path string %q, got %q", "mycomponent.subcomponent", c.PathString()) + } }) t.Run("should return error", func(t *testing.T) { _, err := Extend(context.Background(), "subcomponent") - assert.Error(t, err) + if err == nil { + t.Fatal("expected error, got nil") + } }) t.Run("must variant should panic", func(t *testing.T) { - assert.Panics(t, func() { + assertPanics(t, func() { MustExtend(context.Background(), "subcomponent") }) }) @@ -56,28 +90,52 @@ func TestExtend(t *testing.T) { func TestGet(t *testing.T) { _, ok := Get(context.Background()) - assert.False(t, ok) + if ok { + t.Fatal("expected no component on bare context") + } ctx := MustNew(context.Background(), "mymodule", "mycomponent") _, ok = Get(ctx) - assert.True(t, ok) + if !ok { + t.Fatal("expected component in context") + } } func TestGetFields(t *testing.T) { _, ok := GetFields(context.Background()) - assert.False(t, ok) + if ok { + t.Fatal("expected no fields on bare context") + } ctx := MustNew(context.Background(), "mymodule", "mycomponent") fields, ok := GetFields(ctx) - assert.True(t, ok) - assert.Equal(t, "mymodule", fields["module"]) - assert.Equal(t, "mycomponent", fields["path"]) + if !ok { + t.Fatal("expected fields in context") + } + if fields["module"] != "mymodule" { + t.Errorf("expected module %q, got %q", "mymodule", fields["module"]) + } + if fields["path"] != "mycomponent" { + t.Errorf("expected path %q, got %q", "mycomponent", fields["path"]) + } +} + +func assertAttr(t *testing.T, attr slog.Attr, key string, value string) { + t.Helper() + if attr.Key != key { + t.Errorf("expected attr key %q, got %q", key, attr.Key) + } + if attr.Value.String() != value { + t.Errorf("expected attr value %q, got %q", value, attr.Value.String()) + } } func TestAttrs(t *testing.T) { ctx := MustNew(context.Background(), "mymodule", "mycomponent") attrs, ok := Attrs(ctx) - assert.True(t, ok) - assert.Equal(t, slog.String("module", "mymodule"), attrs[0]) - assert.Equal(t, slog.String("path", "mycomponent"), attrs[1]) + if !ok { + t.Fatal("expected attrs in context") + } + assertAttr(t, attrs[0], "module", "mymodule") + assertAttr(t, attrs[1], "path", "mycomponent") }