Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

general test case assurance on TestGetValue #69

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions clog/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (b *builder) Err(err error) *builder {
return b
}

// Label adds all of the appended labels to the error.
// Label adds all of the appended labels to the log.
// Adding labels is a great way to categorize your logs into broad scale
// concepts like "configuration", "process kickoff", or "process conclusion".
// they're also a great way to set up various granularities of debugging
Expand Down Expand Up @@ -190,17 +190,19 @@ func (b *builder) SkipCaller(nSkips int) *builder {
return b
}

// getValue will return the value if not pointer, or the dereferenced
// getValue will return the value if not a pointer, or the dereferenced
// value if it is a pointer.
func getValue(v any) any {
rv := reflect.ValueOf(v)

if rv.Kind() == reflect.Ptr {
if rv.IsNil() {
return nil
}

return rv.Elem().Interface()
}

return v
}

Expand Down
182 changes: 112 additions & 70 deletions clog/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,9 @@ import (

"github.com/alcionai/clues"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type BuilderUnitSuite struct {
suite.Suite
}

func TestBuilderUnitSuite(t *testing.T) {
suite.Run(t, new(BuilderUnitSuite))
}

func (suite *BuilderUnitSuite) TestBuilder() {
func TestBuilder(t *testing.T) {
table := []struct {
name string
init func(ctx context.Context) context.Context
Expand All @@ -37,63 +28,66 @@ func (suite *BuilderUnitSuite) TestBuilder() {
}

for _, test := range table {
var (
t = suite.T()
ctx = test.init(context.Background())
bld = test.bldr(ctx)
)

// standard builder checks
bld.With("foo", "bar", "baz", 1)
assert.Contains(t, bld.with, "foo")
assert.Equal(t, bld.with["foo"].(string), "bar")
assert.Contains(t, bld.with, "baz")
assert.Equal(t, bld.with["baz"].(int), 1)

bld.Label("l1", "l2", "l1")
assert.Contains(t, bld.labels, "l1")
assert.Contains(t, bld.labels, "l2")

bld.Comment("a comment")
bld.Comment("another comment")
bld.Comment("a comment")
assert.Contains(t, bld.comments, "a comment")
assert.Contains(t, bld.comments, "another comment")

bld.SkipCaller(1)

// ensure no collision between separate builders
// using the same ctx.
err := clues.New("an error").
With("fnords", "i have seen them").
Label("errLabel")

other := CtxErr(ctx, err)
assert.Empty(t, other.with)
assert.Empty(t, other.labels)
assert.Empty(t, other.comments)
assert.ErrorIs(t, other.err, err, clues.ToCore(err))

other.With("foo", "smarf")
assert.Contains(t, other.with, "foo")
assert.Equal(t, bld.with["foo"].(string), "bar")

other.Label("l3")
assert.Contains(t, other.labels, "l3")
assert.NotContains(t, bld.labels, "l3")

other.Comment("comment a")
assert.Contains(t, other.comments, "comment a")
assert.NotContains(t, bld.comments, "comment a")

// ensure no panics when logging
suite.testDebugLogs(bld)
suite.testInfoLogs(bld)
suite.testErrorLogs(bld)
t.Run(test.name, func(t *testing.T) {
var (
ctx = test.init(context.Background())
bld = test.bldr(ctx)
)

// standard builder checks
bld.With("foo", "bar", "baz", 1)
assert.Contains(t, bld.with, "foo")
assert.Equal(t, bld.with["foo"].(string), "bar")
assert.Contains(t, bld.with, "baz")
assert.Equal(t, bld.with["baz"].(int), 1)

bld.Label("l1", "l2", "l1")
assert.Contains(t, bld.labels, "l1")
assert.Contains(t, bld.labels, "l2")

bld.Comment("a comment")
bld.Comment("another comment")
bld.Comment("a comment")
assert.Contains(t, bld.comments, "a comment")
assert.Contains(t, bld.comments, "another comment")

bld.SkipCaller(1)

// ensure no collision between separate builders
// using the same ctx.
err := clues.New("an error").
With("fnords", "i have seen them").
Label("errLabel")

other := CtxErr(ctx, err)
assert.Empty(t, other.with)
assert.Empty(t, other.labels)
assert.Empty(t, other.comments)
assert.ErrorIs(t, other.err, err, clues.ToCore(err))

other.With("foo", "smarf")
assert.Contains(t, other.with, "foo")
assert.Equal(t, bld.with["foo"].(string), "bar")

other.Label("l3")
assert.Contains(t, other.labels, "l3")
assert.NotContains(t, bld.labels, "l3")

other.Comment("comment a")
assert.Contains(t, other.comments, "comment a")
assert.NotContains(t, bld.comments, "comment a")

// ensure no panics when logging
runDebugLogs(bld)
runInfoLogs(bld)
runErrorLogs(bld)
})
}
}

func (suite *BuilderUnitSuite) testDebugLogs(bld *builder) {
func runDebugLogs(
bld *builder,
) {
bld.Debug("a", "log")
bld.Debugf("a %s", "log")
bld.Debugw("a log", "with key")
Expand All @@ -104,7 +98,9 @@ func (suite *BuilderUnitSuite) testDebugLogs(bld *builder) {
Debugw("a log", "with key", "and value")
}

func (suite *BuilderUnitSuite) testInfoLogs(bld *builder) {
func runInfoLogs(
bld *builder,
) {
bld.Info("a", "log")
bld.Infof("a %s", "log")
bld.Infow("a log", "with key")
Expand All @@ -115,7 +111,9 @@ func (suite *BuilderUnitSuite) testInfoLogs(bld *builder) {
Infow("a log", "with key", "and value")
}

func (suite *BuilderUnitSuite) testErrorLogs(bld *builder) {
func runErrorLogs(
bld *builder,
) {
bld.Error("a", "log")
bld.Errorf("a %s", "log")
bld.Errorw("a log", "with key")
Expand All @@ -126,7 +124,13 @@ func (suite *BuilderUnitSuite) testErrorLogs(bld *builder) {
Errorw("a log", "with key", "and value")
}

func (suite *BuilderUnitSuite) TestGetValue() {
func TestGetValue(t *testing.T) {
var (
p1 int = 1
ps string = "ptr"
pn any
)

table := []struct {
name string
value any
Expand All @@ -137,11 +141,39 @@ func (suite *BuilderUnitSuite) TestGetValue() {
value: 1,
expected: 1,
},
{
name: "integer value pointer",
value: &p1,
expected: p1,
},
{
name: "integer value",
value: p1,
expected: p1,
},
{
name: "pointer to integer",
value: func() *int {
i := 8
return &i
}(),
expected: 8,
},
{
name: "string",
value: "foo",
expected: "foo",
},
{
name: "string value pointer",
value: &ps,
expected: ps,
},
{
name: "string value",
value: ps,
expected: ps,
},
{
name: "pointer to string",
value: func() *string {
Expand All @@ -155,6 +187,16 @@ func (suite *BuilderUnitSuite) TestGetValue() {
value: nil,
expected: nil,
},
{
name: "nil value pointer",
value: &pn,
expected: nil,
},
{
name: "nil value",
value: pn,
expected: nil,
},
{
name: "nil pointer",
value: func() *string {
Expand All @@ -164,9 +206,9 @@ func (suite *BuilderUnitSuite) TestGetValue() {
},
}

for _, tt := range table {
suite.T().Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, getValue(tt.value))
for _, test := range table {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expected, getValue(test.value))
})
}
}
Loading