Browse Source

Update tests

tags/v1.0.0
guanguans 3 years ago
parent
commit
085bccfc5b
2 changed files with 85 additions and 1 deletions
  1. +1
    -1
      examples/main.go
  2. +84
    -0
      id_validator_test.go

+ 1
- 1
examples/main.go View File

@@ -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位


+ 84
- 0
id_validator_test.go View File

@@ -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")
}
}

Loading…
Cancel
Save