Skip to content

Commit

Permalink
fix: cover to bool
Browse files Browse the repository at this point in the history
  • Loading branch information
miaolz123 committed Sep 9, 2020
1 parent cd5966e commit cc3009f
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 3 deletions.
8 changes: 5 additions & 3 deletions conver/conver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func String(val interface{}) (string, error) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return fmt.Sprintf("%d", result), nil
default:
return fmt.Sprintf("%+v", val), nil
return fmt.Sprintf("%#v", val), nil
}
}

Expand All @@ -39,8 +39,10 @@ func Bool(val interface{}) (bool, error) {
switch result := val.(type) {
case bool:
return result, nil
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
return result != 0, nil
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return Int64Must(val) != 0, nil
case float32, float64:
return Float64Must(val) != 0, nil
case []byte:
return stringToBool(string(result))
case string:
Expand Down
145 changes: 145 additions & 0 deletions conver/conver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package conver

import (
"errors"
"fmt"
"testing"
)

type intStr int

func (i intStr) String() string {
return fmt.Sprintf("intStr(%d)", i)
}

type myStruct struct {
Name string
}

func TestString(t *testing.T) {
type check struct {
val interface{}
want string
}

checks := []check{
{val: "str", want: "str"},
{val: []byte("bytes"), want: "bytes"},
{val: intStr(1), want: "intStr(1)"},
{val: errors.New("err"), want: "err"},
{val: 123, want: "123"},
{val: 3.14, want: "3.14"},
{val: myStruct{Name: "name"}, want: `conver.myStruct{Name:"name"}`},
{val: []int64{1, 2, 3}, want: "[]int64{1, 2, 3}"},
}

for _, c := range checks {
str, err := String(c.val)
if err != nil {
t.Fatal("TestString err:", err)
}
if str != c.want {
t.Fatalf("TestString(%v) want: %s, but: %s", c.val, c.want, str)
}
}
}

func TestBool(t *testing.T) {
type check struct {
val interface{}
want bool
}

checks := []check{
{val: true, want: true},
{val: false, want: false},
{val: 1, want: true},
{val: 0, want: false},
{val: 0.0, want: false},
{val: "1", want: true},
{val: "t", want: true},
{val: "T", want: true},
{val: "true", want: true},
{val: "TRUE", want: true},
{val: "True", want: true},
{val: "ok", want: true},
{val: "OK", want: true},
{val: "yes", want: true},
{val: "YES", want: true},
{val: "0", want: false},
{val: "f", want: false},
{val: "F", want: false},
{val: "false", want: false},
{val: "FALSE", want: false},
{val: "False", want: false},
{val: "", want: false},
}

for i, c := range checks {
bl, err := Bool(c.val)
if err != nil {
t.Fatal("TestBool err:", err)
}
if bl != c.want {
t.Fatalf("%d TestBool(%v) want: %v, but: %v", i, c.val, c.want, bl)
}
}
}

func TestFloat64(t *testing.T) {
type check struct {
val interface{}
want float64
}

checks := []check{
{val: true, want: 1},
{val: false, want: 0},
{val: 1, want: 1},
{val: -1, want: -1},
{val: "1", want: 1},
{val: "-1", want: -1},
{val: "3.1415", want: 3.1415},
{val: "-3.1415", want: -3.1415},
}

for i, c := range checks {
fl, err := Float64(c.val)
if err != nil {
t.Fatal("TestFloat64 err:", err)
}
if fl != c.want {
t.Fatalf("%d TestFloat64(%v) want: %v, but: %v", i, c.val, c.want, fl)
}
}
}

func TestInt64(t *testing.T) {
type check struct {
val interface{}
want int64
}

checks := []check{
{val: true, want: 1},
{val: false, want: 0},
{val: 1, want: 1},
{val: -1, want: -1},
{val: "1", want: 1},
{val: "-1", want: -1},
{val: "3.1415", want: 3},
{val: "-3.1415", want: -3},
{val: "3.91415", want: 3},
{val: "-3.91415", want: -3},
}

for i, c := range checks {
it, err := Int64(c.val)
if err != nil {
t.Fatal("TestInt64 err:", err)
}
if it != c.want {
t.Fatalf("%d TestInt64(%v) want: %v, but: %v", i, c.val, c.want, it)
}
}
}

0 comments on commit cc3009f

Please sign in to comment.