github.com/leanovate/gopter@v0.2.9/gen/strings_test.go (about) 1 package gen_test 2 3 import ( 4 "testing" 5 "unicode" 6 "unicode/utf8" 7 8 "github.com/leanovate/gopter" 9 "github.com/leanovate/gopter/gen" 10 ) 11 12 func TestRune(t *testing.T) { 13 commonGeneratorTest(t, "rune", gen.Rune(), func(value interface{}) bool { 14 v, ok := value.(rune) 15 return ok && utf8.ValidRune(v) 16 }) 17 } 18 19 func TestNumChar(t *testing.T) { 20 commonGeneratorTest(t, "num char", gen.NumChar(), func(value interface{}) bool { 21 v, ok := value.(rune) 22 return ok && unicode.IsNumber(v) 23 }) 24 } 25 26 func TestAlphaUpper(t *testing.T) { 27 commonGeneratorTest(t, "alpha upper char", gen.AlphaUpperChar(), func(value interface{}) bool { 28 v, ok := value.(rune) 29 return ok && unicode.IsUpper(v) && unicode.IsLetter(v) 30 }) 31 } 32 33 func TestAlphaLower(t *testing.T) { 34 commonGeneratorTest(t, "alpha lower char", gen.AlphaLowerChar(), func(value interface{}) bool { 35 v, ok := value.(rune) 36 return ok && unicode.IsLower(v) && unicode.IsLetter(v) 37 }) 38 } 39 40 func TestAlphaChar(t *testing.T) { 41 commonGeneratorTest(t, "alpha char", gen.AlphaChar(), func(value interface{}) bool { 42 v, ok := value.(rune) 43 return ok && unicode.IsLetter(v) 44 }) 45 } 46 47 func TestAnyString(t *testing.T) { 48 commonGeneratorTest(t, "any string", gen.AnyString(), func(value interface{}) bool { 49 str, ok := value.(string) 50 51 if !ok { 52 return false 53 } 54 for _, ch := range str { 55 if !utf8.ValidRune(ch) { 56 return false 57 } 58 } 59 return true 60 }) 61 } 62 63 func TestAlphaString(t *testing.T) { 64 alphaString := gen.AlphaString() 65 commonGeneratorTest(t, "alpha string", alphaString, func(value interface{}) bool { 66 str, ok := value.(string) 67 68 if !ok { 69 return false 70 } 71 for _, ch := range str { 72 if !utf8.ValidRune(ch) || !unicode.IsLetter(ch) { 73 return false 74 } 75 } 76 return true 77 }) 78 sieve := alphaString(gopter.DefaultGenParameters()).Sieve 79 if sieve == nil { 80 t.Error("No sieve") 81 } 82 if !sieve("abcdABCD") || sieve("abc12") { 83 t.Error("Invalid sieve") 84 } 85 } 86 87 func TestNumString(t *testing.T) { 88 numString := gen.NumString() 89 commonGeneratorTest(t, "num string", numString, func(value interface{}) bool { 90 str, ok := value.(string) 91 92 if !ok { 93 return false 94 } 95 for _, ch := range str { 96 if !utf8.ValidRune(ch) || !unicode.IsDigit(ch) { 97 return false 98 } 99 } 100 return true 101 }) 102 sieve := numString(gopter.DefaultGenParameters()).Sieve 103 if sieve == nil { 104 t.Error("No sieve") 105 } 106 if !sieve("123456789") || sieve("123abcd") { 107 t.Error("Invalid sieve") 108 } 109 } 110 111 func TestIdentifier(t *testing.T) { 112 identifiers := gen.Identifier() 113 commonGeneratorTest(t, "identifiers", identifiers, func(value interface{}) bool { 114 str, ok := value.(string) 115 116 if !ok { 117 return false 118 } 119 if len(str) == 0 || !unicode.IsLetter([]rune(str)[0]) { 120 return false 121 } 122 for _, ch := range str { 123 if !utf8.ValidRune(ch) || (!unicode.IsDigit(ch) && !unicode.IsLetter(ch)) { 124 return false 125 } 126 } 127 return true 128 }) 129 sieve := identifiers(gopter.DefaultGenParameters()).Sieve 130 if sieve == nil { 131 t.Error("No sieve") 132 } 133 if !sieve("abc123") || sieve("123abc") || sieve("abcd123-") { 134 t.Error("Invalid sieve") 135 } 136 } 137 138 func TestUnicodeString(t *testing.T) { 139 fail := gen.UnicodeChar(nil) 140 value, ok := fail.Sample() 141 if value != nil || ok { 142 t.Fail() 143 } 144 145 for _, table := range unicode.Scripts { 146 unicodeString := gen.UnicodeString(table) 147 commonGeneratorTest(t, "unicodeString", unicodeString, func(value interface{}) bool { 148 str, ok := value.(string) 149 150 if !ok { 151 return false 152 } 153 for _, ch := range str { 154 if !utf8.ValidRune(ch) || !unicode.Is(table, ch) { 155 return false 156 } 157 } 158 return true 159 }) 160 } 161 }