-
Notifications
You must be signed in to change notification settings - Fork 0
/
CharGeneratorsSpec.scala
224 lines (196 loc) · 6.72 KB
/
CharGeneratorsSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package com.lightbend.hedgehog.generators
import com.lightbend.hedgehog.generators.CharGenerators._
import com.lightbend.hedgehog.implicits.ArbitraryImplicits._
import com.lightbend.hedgehog.implicits.TreeImplicits._
import com.lightbend.hedgehog.scalamock.{GeneratorSpec, TestRunnerMock}
import com.lightbend.hedgehog.testkit.{GeneratorTests, Probabilities}
import hedgehog._
import hedgehog.core.Seed
import hedgehog.runner.Test
import org.scalactic.TripleEquals._
import scala.Ordering._
object CharGeneratorsSpec extends TestRunnerMock with GeneratorSpec with Probabilities {
override def tests: List[Test] =
genCharTests ++
genLatinLetterLowerCaseTests ++
genSpaceTests ++
genWhitespaceCharTests ++
genCharFrequenciesTests ++
genBlankCharTests ++
genNonBlankCharTests ++
genNonLatinDigitTests ++
genWordCharTests
private def genCharTests =
test("genChar", genChar).addConstantRangeTests('\u0000', '\uFFFF').tests ++
test("genControlC0", genControlC0)
.addConstantRangeTests('\u0000', '\u001F')
.addGenProp(_ + s" generates only C0 control characters", propControlC0Chars)
.tests
private def genLatinLetterLowerCaseTests =
test("genLatinLetterLowerCase", genLatinLetterLowerCase)
.addGenProp(_ + s" generates only latin lower case letters", propLatinLetterLowerCaseChars)
.addConstantRangeTests('a', 'z')
.tests
private def genSpaceTests =
test("genSpace", genSpace).addSingletonTests(' ').tests
private def genWhitespaceCharTests =
charChoicesTests(
"genWhitespaceChar",
genWhitespaceChar,
"whitespace",
propWhitespaceChars,
'\u0000' -> '\u001F',
' ' -> ' '
).addGenProbabilities(_ + " generates characters with probabilities", propWhitespaceCharProbabilities).tests
private def genCharFrequenciesTests =
charFrequenciesTests(
"genNonWhitespaceChar",
genNonWhitespaceChar,
"non-whitespace",
propNonWhitespaceChars,
'!' -> '\uD7FF',
'\uE000' -> '\uFFFD'
).tests
private def genBlankCharTests =
charChoicesTests(
"genBlankChar",
genBlankChar,
"blank",
propBlankChars,
'\u0000' -> '\u001F',
' ' -> ' '
).addGenProbabilities(_ + " generates characters with probabilities", propWhitespaceCharProbabilities).tests
private def genNonBlankCharTests =
charFrequenciesTests(
"genNonBlankChar",
genNonBlankChar,
"non-blank",
propNonBlankChars,
'!' -> '\uD7FF',
'\uE000' -> '\uFFFD'
).tests
private def genNonLatinDigitTests =
charFrequenciesTests(
"genNonLatinDigit",
genNonLatinDigit,
"non-latin-digit",
propNonLatinDigit,
'\u0000' -> '\u002F',
'\u003A' -> '\uD7FF',
'\uE000' -> '\uFFFD'
).tests
private def genWordCharTests =
charFrequenciesTests(
"genWordChar",
genWordChar,
"non-word-char",
propWordChar,
'A' -> 'Z',
'a' -> 'z',
'_' -> '_',
'0' -> '9'
).tests
private def charChoicesTests(
name: String,
gen: Gen[Char],
description: String,
property: Gen[Char] => Property,
ranges: (Char, Char)*
): GeneratorTests[Char] = {
def nextAlternative(alternative: Long, from: Char, to: Char) = {
{ val _ = from }
{ val _ = to }
alternative + 1
}
charAlternativesTests(name, gen, description, property, 0, nextAlternative, ranges: _*)
}
private def charFrequenciesTests(
name: String,
gen: Gen[Char],
description: String,
property: Gen[Char] => Property,
ranges: (Char, Char)*
): GeneratorTests[Char] = {
def nextAlternative(alternative: Long, from: Char, to: Char) =
alternative + (to - from) + 1
charAlternativesTests(name, gen, description, property, 1, nextAlternative, ranges: _*)
}
private def charAlternativesTests(
name: String,
gen: Gen[Char],
description: String,
property: Gen[Char] => Property,
zero: Long,
nextAlternative: (Long, Char, Char) => Long,
ranges: (Char, Char)*
): GeneratorTests[Char] = {
val tests = test(name, gen)
.addGenProp(_ + s" generates only $description characters", property)
val (result, _) = ranges.foldLeft((tests, zero)) {
case ((tests, alternative), (from, to)) =>
val nextTests = addCharRangeExamples(tests, alternative, from, to)
(nextTests, nextAlternative(alternative, from, to))
}
result
}
private def addCharRangeExamples(
tests: GeneratorTests[Char],
alternative: Long,
from: Char,
to: Char
): GeneratorTests[Char] =
tests
.addGenExample(
_ + s" generates '${from.toString}' as a minimum",
testAlternative(_, MinSize, alternative, from, to, from)
)
.addGenExample(
_ + s" generates '${to.toString}' as a maximum",
testAlternative(_, MaxSize, alternative, from, to, to)
)
private def validUnicodeChar(c: Char): Result =
c.inside('\u0000', '\uD7FF').or(c.inside('\uE000', '\uFFFD'))
private def propControlC0Chars(gen: Gen[Char]): Property =
gen.map(_.inside('\u0000', '\u001F')).forAll
private def propLatinLetterLowerCaseChars(gen: Gen[Char]): Property =
gen.map(_.inside('a', 'z')).forAll
private def propWhitespaceChars(gen: Gen[Char]): Property =
gen.map(_.inside('\u0000', ' ')).forAll
private def propBlankChars(gen: Gen[Char]): Property =
gen.map(_.inside('\u0000', ' ')).forAll
private def propNonWhitespaceChars(gen: Gen[Char]): Property =
gen.map { c =>
validUnicodeChar(c).and(c.without('\u0000', '\u0020'))
}.forAll
private def propNonBlankChars(gen: Gen[Char]): Property =
gen.map { c =>
validUnicodeChar(c).and(c.without('\u0000', '\u0020'))
}.forAll
private def propNonLatinDigit(gen: Gen[Char]): Property =
gen.map { c =>
validUnicodeChar(c).and(c.without('0', '9'))
}.forAll
private def propWordChar(gen: Gen[Char]): Property =
gen.map { c =>
Result.assert(c.toString.matches("\\w"))
}.forAll
private def testAlternative(
gen: Gen[Char],
size: Size,
alternative: Long,
from: Char,
to: Char,
value: Char
): Result = {
val seed = stub[Seed]
// choices go from 0, frequencies go from 1.
(seed.chooseLong _).when(*, *).returns((seed, alternative)).noMoreThanOnce()
(seed.chooseLong _).when(from.toLong, to.toLong).returns((seed, value.toLong))
gen.run(size, seed).generated(_ ==== value)
}
private def propWhitespaceCharProbabilities(gen: Gen[Char]): Property =
gen.forAll
.cover(OneToOne, "spaces", _ === ' ')
.cover(OneToOne, "not spaces", _ !== ' ')
.map(_ => Result.success)
}