mirror of
https://github.com/jefferyjob/go-easy-utils.git
synced 2025-10-05 23:16:50 +08:00
version 1.0.1 (#8)
* anyutil test * fix:delete debug * unit test utils * unit test * strUtil unit test * fix;4, 6, 9, 11 Month determination error * fix:The last digit of ID number is x, and the weighted sum comparison condition is wrong * test * test * docs * fix:docs * idcard test * test unit * no message * docs update * 单元测试覆盖 --------- Co-authored-by: libin <libinjob@163.com> Co-authored-by: 李斌 <libin1-hj@huafang.com>
This commit is contained in:
@@ -7,3 +7,5 @@
|
||||
- 优化了 `JsonToStruct` 方法中指针类型的参数判断
|
||||
- 优化了 `anyUtil` 包中关于指针类型的判断
|
||||
- 优化了 `anyUtil` 和 `jsonUtil` 文档
|
||||
- Fix:删除了 `AnyToInt` 方法中的debug代码 #5
|
||||
- Fix:修复了身份证号验证存在的问题 #7
|
@@ -7,3 +7,5 @@
|
||||
- Optimized the parameter judgment of pointer type in `JsonToStruct` method
|
||||
- Optimized the judgment of pointer type in `anyUti`l package
|
||||
- Optimized the documentation of `anyUtil` and `jsonUtil`
|
||||
- Fix: Removed debug code from the `AnyToInt` method #5
|
||||
- Fix: Problems in ID number verification #7
|
@@ -1,8 +1,12 @@
|
||||
package anyUtil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestToBool(t *testing.T) {
|
||||
iPtr := 90
|
||||
|
||||
var tests = []struct {
|
||||
input interface{}
|
||||
want bool
|
||||
@@ -44,6 +48,8 @@ func TestToBool(t *testing.T) {
|
||||
{complex128(1 + 1i), true},
|
||||
{complex128(0 + 0i), false},
|
||||
{(*int)(nil), false},
|
||||
{&iPtr, true},
|
||||
{[]int{}, false},
|
||||
}
|
||||
for _, test := range tests {
|
||||
if got := AnyToBool(test.input); got != test.want {
|
||||
|
@@ -31,6 +31,7 @@ func TestAnyToFloat32(t *testing.T) {
|
||||
{uint64(64), 64, nil},
|
||||
{"3.14", 3.14, nil},
|
||||
{math.MaxFloat64, 0, go_easy_utils.ErrValOut},
|
||||
{make(chan int), 0, go_easy_utils.ErrType},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
@@ -51,6 +52,8 @@ func TestAnyToFloat32(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAnyToFloat64(t *testing.T) {
|
||||
iPtr := 90
|
||||
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
want float64
|
||||
@@ -70,6 +73,13 @@ func TestAnyToFloat64(t *testing.T) {
|
||||
{uint32(32), 32, nil},
|
||||
{uint64(64), 64, nil},
|
||||
{"3.14", 3.14, nil},
|
||||
{true, 1, nil},
|
||||
{false, 0, nil},
|
||||
{complex64(1 + 2i), 1, nil},
|
||||
{complex128(1 + 2i), 1, nil},
|
||||
{&iPtr, 90, nil},
|
||||
{(*int)(nil), 0, nil},
|
||||
{"abc", 0, go_easy_utils.ErrSyntax},
|
||||
{[]int{}, 0, go_easy_utils.ErrType},
|
||||
}
|
||||
|
||||
|
@@ -3,9 +3,14 @@ package anyUtil
|
||||
import (
|
||||
"errors"
|
||||
"github.com/jefferyjob/go-easy-utils"
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//func TestAnyToIntDemo(t *testing.T) {
|
||||
// fmt.Println(AnyToInt(math.MinInt32))
|
||||
//}
|
||||
|
||||
func TestAnyToInt(t *testing.T) {
|
||||
type testCase struct {
|
||||
input interface{}
|
||||
@@ -154,6 +159,7 @@ func TestAnyToInt16(t *testing.T) {
|
||||
{float32(123.45), 123, nil},
|
||||
{float64(123.45), 123, nil},
|
||||
{"12345", 12345, nil},
|
||||
{math.MinInt16 - 1, 0, go_easy_utils.ErrValOut},
|
||||
{"not a number", 0, go_easy_utils.ErrSyntax},
|
||||
}
|
||||
|
||||
@@ -199,6 +205,8 @@ func TestAnyToInt32(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAnyToInt64(t *testing.T) {
|
||||
iPtr := 90
|
||||
|
||||
testCases := []struct {
|
||||
input interface{}
|
||||
expected int64
|
||||
@@ -225,6 +233,16 @@ func TestAnyToInt64(t *testing.T) {
|
||||
{"42.42", int64(0), true}, // invalid syntax
|
||||
// unsupported type
|
||||
{struct{}{}, int64(0), true},
|
||||
// complex
|
||||
{complex64(1 + 2i), 1, false},
|
||||
{complex128(1 + 2i), 1, false},
|
||||
// point
|
||||
{(*int)(nil), 0, false},
|
||||
{nil, 0, false},
|
||||
{&iPtr, 90, false},
|
||||
// bool
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
|
@@ -3,6 +3,7 @@ package anyUtil
|
||||
import "testing"
|
||||
|
||||
func TestAnyToStr(t *testing.T) {
|
||||
iPtr := 90
|
||||
testCases := []struct {
|
||||
input interface{}
|
||||
expected string
|
||||
@@ -20,8 +21,13 @@ func TestAnyToStr(t *testing.T) {
|
||||
{uint64(640), "640"},
|
||||
{float32(3.14159), "3.14159"},
|
||||
{float64(2.71828), "2.71828"},
|
||||
{complex64(1 + 2i), "(1+2i)"},
|
||||
{complex128(3 + 4i), "(3+4i)"},
|
||||
{(*int)(nil), ""},
|
||||
{&iPtr, "90"},
|
||||
{true, "true"},
|
||||
{nil, ""},
|
||||
{make(chan int), ""},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
|
@@ -17,6 +17,7 @@ func TestAnyToUint(t *testing.T) {
|
||||
{-5, 0, go_easy_utils.ErrUnsignedInt},
|
||||
{"20", 20, nil},
|
||||
{1.5, 1, nil},
|
||||
{make(chan int), 0, go_easy_utils.ErrType},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
@@ -40,6 +41,7 @@ func TestAnyToUint8(t *testing.T) {
|
||||
{300, 0, go_easy_utils.ErrValOut},
|
||||
{"20", 20, nil},
|
||||
{1.5, 1, nil},
|
||||
{make(chan int), 0, go_easy_utils.ErrType},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
@@ -63,6 +65,7 @@ func TestAnyToUint16(t *testing.T) {
|
||||
{70000, 0, go_easy_utils.ErrValOut},
|
||||
{"20", 20, nil},
|
||||
{1.5, 1, nil},
|
||||
{make(chan int), 0, go_easy_utils.ErrType},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
@@ -86,6 +89,7 @@ func TestAnyToUint32(t *testing.T) {
|
||||
{5000000000, 0, go_easy_utils.ErrValOut},
|
||||
{"20", 20, nil},
|
||||
{1.5, 1, nil},
|
||||
{make(chan int), 0, go_easy_utils.ErrType},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
@@ -99,6 +103,8 @@ func TestAnyToUint32(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAnyToUint64(t *testing.T) {
|
||||
iPtr := 90
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input interface{}
|
||||
@@ -231,6 +237,21 @@ func TestAnyToUint64(t *testing.T) {
|
||||
input: false,
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "Test int point",
|
||||
input: &iPtr,
|
||||
want: 90,
|
||||
},
|
||||
{
|
||||
name: "Test nil",
|
||||
input: nil,
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "test -complex",
|
||||
input: complex(-1, -1),
|
||||
wantError: go_easy_utils.ErrUnsignedInt,
|
||||
},
|
||||
{
|
||||
name: "Test invalid type",
|
||||
input: make(chan int),
|
||||
|
@@ -45,6 +45,7 @@ func TestToFloat64(t *testing.T) {
|
||||
{false, 0, nil},
|
||||
{(*bool)(nil), 0, nil},
|
||||
{make(chan int), 0, go_easy_utils.ErrType},
|
||||
{"abc", 0, go_easy_utils.ErrSyntax},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
|
@@ -112,6 +112,11 @@ func TestToUint64(t *testing.T) {
|
||||
input: complex128(complex(42, 0)),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "test -complex",
|
||||
input: complex(-1, -1),
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "Test string",
|
||||
input: "42",
|
||||
|
@@ -5,41 +5,127 @@ import (
|
||||
)
|
||||
|
||||
func TestStrToInt(t *testing.T) {
|
||||
expected := 123
|
||||
result := StrToInt("123")
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt test failed, expected %d but got %d", expected, result)
|
||||
// Test cases
|
||||
testCases := []struct {
|
||||
input string
|
||||
expected int
|
||||
}{
|
||||
{"0", 0},
|
||||
{"1", 1},
|
||||
{"-1", -1},
|
||||
{"invalid", 0},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
for _, tc := range testCases {
|
||||
// Call the function
|
||||
got := StrToInt(tc.input)
|
||||
|
||||
// Check the result
|
||||
if got != tc.expected {
|
||||
t.Errorf("Unexpected result: StrToInt(%v) = %v, expected %v", tc.input, got, tc.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt8(t *testing.T) {
|
||||
expected := int8(123)
|
||||
result := StrToInt8("123")
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt8 test failed, expected %d but got %d", expected, result)
|
||||
// Test cases
|
||||
testCases := []struct {
|
||||
input string
|
||||
expected int8
|
||||
}{
|
||||
{"0", 0},
|
||||
{"1", 1},
|
||||
{"-1", -1},
|
||||
{"invalid", 0},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
for _, tc := range testCases {
|
||||
// Call the function
|
||||
got := StrToInt8(tc.input)
|
||||
|
||||
// Check the result
|
||||
if got != tc.expected {
|
||||
t.Errorf("Unexpected result: StrToInt8(%v) = %v, expected %v", tc.input, got, tc.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt16(t *testing.T) {
|
||||
expected := int16(123)
|
||||
result := StrToInt16("123")
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt16 test failed, expected %d but got %d", expected, result)
|
||||
// Test cases
|
||||
testCases := []struct {
|
||||
input string
|
||||
expected int16
|
||||
}{
|
||||
{"0", 0},
|
||||
{"1", 1},
|
||||
{"-1", -1},
|
||||
{"invalid", 0},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
for _, tc := range testCases {
|
||||
// Call the function
|
||||
got := StrToInt16(tc.input)
|
||||
|
||||
// Check the result
|
||||
if got != tc.expected {
|
||||
t.Errorf("Unexpected result: StrToInt16(%v) = %v, expected %v", tc.input, got, tc.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt32(t *testing.T) {
|
||||
expected := int32(123)
|
||||
result := StrToInt32("123")
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt32 test failed, expected %d but got %d", expected, result)
|
||||
// Test cases
|
||||
testCases := []struct {
|
||||
input string
|
||||
expected int32
|
||||
}{
|
||||
{"0", 0},
|
||||
{"1", 1},
|
||||
{"-1", -1},
|
||||
{"invalid", 0},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
for _, tc := range testCases {
|
||||
// Call the function
|
||||
got := StrToInt32(tc.input)
|
||||
|
||||
// Check the result
|
||||
if got != tc.expected {
|
||||
t.Errorf("Unexpected result: StrToInt32(%v) = %v, expected %v", tc.input, got, tc.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt64(t *testing.T) {
|
||||
expected := int64(123)
|
||||
result := StrToInt64("123")
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt64 test failed, expected %d but got %d", expected, result)
|
||||
// Test cases
|
||||
testCases := []struct {
|
||||
input string
|
||||
expected int64
|
||||
}{
|
||||
{"0", 0},
|
||||
{"1", 1},
|
||||
{"-1", -1},
|
||||
{"9223372036854775807", 9223372036854775807},
|
||||
{"-9223372036854775808", -9223372036854775808},
|
||||
{"2147483647", 2147483647},
|
||||
{"-2147483648", -2147483648},
|
||||
{"invalid", 0},
|
||||
{"9223372036854775808", 0},
|
||||
{"-9223372036854775809", 0},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
for _, tc := range testCases {
|
||||
// Call the function
|
||||
got := StrToInt64(tc.input)
|
||||
|
||||
// Check the result
|
||||
if got != tc.expected {
|
||||
t.Errorf("Unexpected result: StrToInt64(%v) = %v, expected %v", tc.input, got, tc.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -30,7 +30,7 @@ func isValidMonth(month, year int) bool {
|
||||
case 1, 3, 5, 7, 8, 10, 12:
|
||||
return true
|
||||
case 4, 6, 9, 11:
|
||||
return false
|
||||
return true
|
||||
case 2:
|
||||
if year%4 == 0 && year%100 != 0 || year%400 == 0 {
|
||||
return true
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package validUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -22,6 +23,10 @@ func TestIsTime(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsDateDemo(t *testing.T) {
|
||||
fmt.Println(IsDate("2022-04-01"))
|
||||
}
|
||||
|
||||
func TestIsDate(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
@@ -29,9 +34,11 @@ func TestIsDate(t *testing.T) {
|
||||
}{
|
||||
{"2021-01-01", true},
|
||||
{"2021/01/01", false},
|
||||
{"2021-13-01", false},
|
||||
{"2021-20-01", false},
|
||||
{"2021-02-29", false},
|
||||
{"2020-02-29", true},
|
||||
{"2020-02-25", true},
|
||||
{"2023-04-30", true},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
|
@@ -108,18 +108,5 @@ func IsIDCard15(idCard string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// 验证校验位
|
||||
factors := []int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}
|
||||
checkCodes := []string{"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"}
|
||||
sum := 0
|
||||
for i := 0; i < 17; i++ {
|
||||
num, _ := strconv.Atoi(string(idCard[i]))
|
||||
sum += num * factors[i]
|
||||
}
|
||||
checkCode := checkCodes[sum%11]
|
||||
if string(idCard[14]) != checkCode {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
@@ -1,16 +1,32 @@
|
||||
package validUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsIDCard18Demo(t *testing.T) {
|
||||
fmt.Println(IsIDCard18("120103199001015953"))
|
||||
fmt.Println(IsIDCard18("44080319861221348X"))
|
||||
//func TestIsIDCard18Demo(t *testing.T) {
|
||||
// fmt.Println(IsIDCard18("120103199001015953"))
|
||||
// fmt.Println(IsIDCard18("44080319861221348X"))
|
||||
//}
|
||||
|
||||
func TestIsIDCard(t *testing.T) {
|
||||
cases := []struct {
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{"", false},
|
||||
{"110102197809193026", true},
|
||||
{"142629680611101", true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got := IsIDCard(c.input)
|
||||
if got != c.want {
|
||||
t.Errorf("IsIDCard(%q) == %v, want %v", c.input, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsIDCard1(t *testing.T) {
|
||||
func TestIsIDCard18(t *testing.T) {
|
||||
// 18位身份证号码测试
|
||||
cases18 := []struct {
|
||||
input string
|
||||
@@ -30,27 +46,31 @@ func TestIsIDCard1(t *testing.T) {
|
||||
{"44080319861221348X", true},
|
||||
}
|
||||
for _, c := range cases18 {
|
||||
got := IsIDCard(c.input)
|
||||
got := IsIDCard18(c.input)
|
||||
if got != c.want {
|
||||
t.Errorf("IsIDCard(%q) == %v, want %v", c.input, got, c.want)
|
||||
t.Errorf("IsIDCard18(%q) == %v, want %v", c.input, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsIDCard2(t *testing.T) {
|
||||
func TestIsIDCard15(t *testing.T) {
|
||||
// 15位身份证号码测试
|
||||
cases15 := []struct {
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{"", false},
|
||||
{"12345678901234", false},
|
||||
{"142629680611101", true},
|
||||
{"610104620927690", true},
|
||||
{"142629601611101", false}, // 年份非法
|
||||
{"01345678901234", false},
|
||||
{"1234567890123X", false},
|
||||
{"9994567890123X", false},
|
||||
}
|
||||
for _, c := range cases15 {
|
||||
got := IsIDCard(c.input)
|
||||
got := IsIDCard15(c.input)
|
||||
if got != c.want {
|
||||
t.Errorf("IsIDCard(%q) == %v, want %v", c.input, got, c.want)
|
||||
t.Errorf("IsIDCard15(%q) == %v, want %v", c.input, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user