github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/vin-code/vin_validator_test.go (about) 1 package vincode 2 3 import ( 4 "fmt" 5 "testing" 6 "unicode" 7 ) 8 9 func TestCheckVINLength1(t *testing.T) { 10 bytes := make([]byte, 17, 17) 11 str := string(bytes) 12 expectedLen := vinLegalLength 13 actualLen := len(str) 14 if expectedLen != actualLen { 15 t.Errorf("expected length:%d,actual length:%d", expectedLen, actualLen) 16 } 17 if !checkVINLength(str) { 18 t.Errorf("str:%s length is not correct", str) 19 } 20 } 21 22 func TestCheckVINLength2(t *testing.T) { 23 bytes := make([]byte, 16, 16) 24 str := string(bytes) 25 if checkVINLength(str) { 26 t.Errorf("str:%s length is not correct", str) 27 } 28 } 29 30 func TestCheckVINCharacter1(t *testing.T) { 31 bytes := make([]byte, 17, 17) 32 for k := range bytes { 33 bytes[k] = 'A' 34 } 35 str := string(bytes) 36 if !checkVINCharacter(str) { 37 t.Errorf("str:%s has illegal character", str) 38 } 39 } 40 41 func TestCheckVINCharacter2(t *testing.T) { 42 bytes := make([]byte, 17, 17) 43 for k := range bytes { 44 bytes[k] = '2' 45 } 46 str := string(bytes) 47 if !checkVINCharacter(str) { 48 t.Errorf("str:%s has illegal character", str) 49 } 50 } 51 52 func TestCheckVINCharacter3(t *testing.T) { 53 str := "中文的VIN1234" 54 55 var tmp rune 56 tmp = rune(str[0]) 57 fmt.Println(unicode.IsLetter(tmp)) 58 if checkVINCharacter(str) { 59 t.Errorf("str:%s has illegal character", str) 60 } 61 } 62 63 func TestCheckVINCheckDigit1(t *testing.T) { 64 vin := "LFWADRJF011002346" 65 if !checkVINCheckDigit(vin) { 66 t.Errorf("vin:%s check digit is wrong", vin) 67 } 68 } 69 70 func TestCheckVINCheckDigit2(t *testing.T) { 71 vin := "LFWADRJF011002356" 72 if checkVINCheckDigit(vin) { 73 t.Errorf("vin:%s check digit is wrong", vin) 74 } 75 } 76 77 func TestVINValidator1(t *testing.T) { 78 //for right code 79 vinList := []string{"LSVHJ133022221761", "LFWADRJF011002346"} 80 for _, v := range vinList { 81 if err := VINValidator(v, true); err != nil { 82 t.Error(err.Error()) 83 } 84 } 85 } 86 87 func TestVINValidator2(t *testing.T) { 88 vin := "LFWADRJF01100346" 89 if err := VINValidator(vin, true); err != nil { 90 t.Log(err.Error()) 91 } 92 } 93 94 func TestVINValidator3(t *testing.T) { 95 vin := "JA32A3HU6HH123456" 96 if err := VINValidator(vin, true); err != nil { 97 t.Error(err.Error()) 98 } 99 }