Skip to content

Commit

Permalink
Merge pull request #9 from fullpipe/test-coverage
Browse files Browse the repository at this point in the history
Test coverage
  • Loading branch information
fullpipe authored Sep 10, 2024
2 parents d3249cd + 29920c8 commit d90b30e
Show file tree
Hide file tree
Showing 5 changed files with 547 additions and 12 deletions.
5 changes: 5 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
comment: false
coverage:
range: 50..75
round: down
precision: 2
38 changes: 27 additions & 11 deletions message/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,35 @@ func (c Context) Int64(key string) (int64, error) {
switch i := v.(type) {
case int:
return int64(i), nil
case int8:
return int64(i), nil
case int16:
return int64(i), nil
case int32:
return int64(i), nil
case int64:
return int64(i), nil
case uint:
if i > math.MaxInt64 {
return 0, fmt.Errorf("unable to convert uint %v to int64 from arg %s", v, key)
}

return int64(i), nil //nolint:gosec
case float64:
case uint8:
return int64(i), nil
case float32:
case uint16:
return int64(i), nil
case int64:
return int64(i), nil
case int32:
case uint32:
return int64(i), nil
case uint64:
if i > math.MaxInt64 {
return 0, fmt.Errorf("unable to convert uint64 %v to int64 from arg %s", v, key)
}

return int64(i), nil //nolint:gosec
case uint32:
case float32:
return int64(i), nil
case float64:
return int64(i), nil
case string:
fl, err := strconv.ParseFloat(i, 64)
Expand All @@ -74,20 +82,28 @@ func (c Context) Float64(key string) (float64, error) {
switch i := v.(type) {
case int:
return float64(i), nil
case uint:
case int8:
return float64(i), nil
case float64:
case int16:
return float64(i), nil
case float32:
case int32:
return float64(i), nil
case int64:
return float64(i), nil
case int32:
case uint:
return float64(i), nil
case uint64:
case uint8:
return float64(i), nil
case uint16:
return float64(i), nil
case uint32:
return float64(i), nil
case uint64:
return float64(i), nil
case float64:
return float64(i), nil
case float32:
return float64(i), nil
case string:
fl, err := strconv.ParseFloat(i, 64)
if err != nil {
Expand Down
303 changes: 302 additions & 1 deletion message/context_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package message

import "testing"
import (
"math"
"testing"
)

func Test_context_AsString(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -69,3 +72,301 @@ func Test_context_AsString(t *testing.T) {
})
}
}

func TestContext_Int64(t *testing.T) {
tests := []struct {
name string
c Context
key string
want int64
wantErr bool
}{
{
"int",
Context{"foo": int(42)},
"foo",
42,
false,
},
{
"int8",
Context{"foo": int8(42)},
"foo",
42,
false,
},
{
"int16",
Context{"foo": int16(42)},
"foo",
42,
false,
},
{
"int32",
Context{"foo": int32(42)},
"foo",
42,
false,
},
{
"int64",
Context{"foo": int64(42)},
"foo",
42,
false,
},
{
"uint",
Context{"foo": uint(42)},
"foo",
42,
false,
},
{
"uint8",
Context{"foo": uint8(42)},
"foo",
42,
false,
},
{
"uint16",
Context{"foo": uint16(42)},
"foo",
42,
false,
},
{
"uint32",
Context{"foo": uint32(42)},
"foo",
42,
false,
},
{
"max uint32",
Context{"foo": uint32(math.MaxUint32)},
"foo",
math.MaxUint32,
false,
},
{
"uint64",
Context{"foo": uint64(42)},
"foo",
42,
false,
},
{
"max uint64",
Context{"foo": uint64(math.MaxUint64)},
"foo",
0,
true,
},
{
"float32",
Context{"foo": float32(42.42)},
"foo",
42,
false,
},
{
"float64",
Context{"foo": float64(42.42)},
"foo",
42,
false,
},
{
"string float",
Context{"foo": "42.42"},
"foo",
42,
false,
},
{
"string int",
Context{"foo": "42"},
"foo",
42,
false,
},
{
"invalid string",
Context{"foo": "bar"},
"foo",
0,
true,
},
{
"unknown type",
Context{"foo": []byte("bar")},
"foo",
0,
true,
},
{
"unknown name",
Context{"foo": 42},
"bar",
0,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.c.Int64(tt.key)
if (err != nil) != tt.wantErr {
t.Errorf("Context.Int64() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Context.Int64() = %v, want %v", got, tt.want)
}
})
}
}

func TestContext_Float64(t *testing.T) {
tests := []struct {
name string
c Context
key string
want float64
wantErr bool
}{
{
"int",
Context{"foo": int(42)},
"foo",
42,
false,
},
{
"int8",
Context{"foo": int8(42)},
"foo",
42,
false,
},
{
"int16",
Context{"foo": int16(42)},
"foo",
42,
false,
},
{
"int32",
Context{"foo": int32(42)},
"foo",
42,
false,
},
{
"int64",
Context{"foo": int64(42)},
"foo",
42,
false,
},
{
"uint",
Context{"foo": uint(42)},
"foo",
42,
false,
},
{
"uint8",
Context{"foo": uint8(42)},
"foo",
42,
false,
},
{
"uint16",
Context{"foo": uint16(42)},
"foo",
42,
false,
},
{
"uint32",
Context{"foo": uint32(42)},
"foo",
42,
false,
},
{
"uint64",
Context{"foo": uint64(42)},
"foo",
42,
false,
},
{
"float32",
Context{"foo": float32(42.42)},
"foo",
float64(float32(42.42)),
false,
},
{
"float64",
Context{"foo": float64(42.42)},
"foo",
42.42,
false,
},
{
"string float",
Context{"foo": "42.42"},
"foo",
42.42,
false,
},
{
"string int",
Context{"foo": "42"},
"foo",
42,
false,
},
{
"invalid string",
Context{"foo": "bar"},
"foo",
0,
true,
},
{
"unknown type",
Context{"foo": []byte("bar")},
"foo",
0,
true,
},
{
"unknown name",
Context{"foo": 42},
"bar",
0,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.c.Float64(tt.key)
if (err != nil) != tt.wantErr {
t.Errorf("Context.Float64() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Context.Float64() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit d90b30e

Please sign in to comment.