From 085bccfc5b13be2ba7ac8785d9b9460ae7e8eb15 Mon Sep 17 00:00:00 2001 From: guanguans Date: Wed, 20 Jan 2021 18:36:54 +0800 Subject: [PATCH] Update tests --- examples/main.go | 2 +- id_validator_test.go | 84 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 id_validator_test.go diff --git a/examples/main.go b/examples/main.go index e92cd4a..5890eee 100644 --- a/examples/main.go +++ b/examples/main.go @@ -16,7 +16,7 @@ func main() { ffmt.P(idvalidator.GetInfo("440308199901101512")) // 生成可通过校验的假身份证号 - ffmt.P(idvalidator.FakeId()) // 随机生成 + ffmt.P(idvalidator.FakeId()) // 随机生成 ffmt.P(idvalidator.FakeRequireId(true, "江苏省", "200001", 1)) // 生成出生于2000年1月江苏省的男性居民身份证 // 15位号码升级为18位 diff --git a/id_validator_test.go b/id_validator_test.go new file mode 100644 index 0000000..1ea7372 --- /dev/null +++ b/id_validator_test.go @@ -0,0 +1,84 @@ +package idvalidator + +import ( + "testing" +) + +// go test -v -cover -coverprofile=cover.out +// go tool cover -func=cover.out +// go tool cover -html=cover.out +func TestIsValid(t *testing.T) { + ids := [4]string{ + "440308199901101512", + "610104620927690", + "810000199408230021", + "830000199201300022", + } + for _, id := range ids { + if !IsValid(id) { + t.Errorf("%s must be true.", id) + } + } + + errIds := [6]string{ + "440308199901101513", + "4403081999011015133", + "510104621927691", + "61010462092769", + "810000199408230022", + "830000199201300023", + } + for _, id := range errIds { + if IsValid(id) { + t.Errorf("%s must be false.", id) + } + } +} + +func TestGetInfo(t *testing.T) { + _, err := GetInfo("440308199901101512") + if err != nil { + t.Errorf("Errors must be nil.") + } + _, e := GetInfo("440308199901101513") + if e == nil { + t.Errorf("Errors must not be nil.") + } +} + +func TestUpgradeId(t *testing.T) { + _, err := UpgradeId("610104620927690") + if err != nil { + t.Errorf("Errors must be nil.") + } + + _, e := UpgradeId("61010462092769") + if e == nil { + t.Errorf("Errors must not be nil.") + } +} + +func TestFakeId(t *testing.T) { + id := FakeId() + if len(id) != 18 { + t.Errorf("String length must be 18. : %s", id) + } + if !IsValid(id) { + t.Errorf("%s must be true.", id) + } +} + +func TestFakeRequireId(t *testing.T) { + id := FakeRequireId(false, "", "", 0) + if len(id) != 15 { + t.Errorf("String length must be 15. : %s", id) + } + if !IsValid(id) { + t.Errorf("%s must be true.", id) + } + + info, _ := GetInfo(id) + if info.Sex != 0 { + t.Errorf("%s must be 0.", "0") + } +}