-
Notifications
You must be signed in to change notification settings - Fork 25
/
strings_external_test.go
100 lines (83 loc) · 2.42 KB
/
strings_external_test.go
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
// Copyright 2019 Gregory Petrosyan <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rapid_test
import (
"strconv"
"testing"
"unicode"
"unicode/utf8"
. "pgregory.net/rapid"
)
func TestStringExamples(t *testing.T) {
g := StringN(10, -1, -1)
for i := 0; i < 100; i++ {
s := g.Example()
t.Log(len(s), s)
}
}
func TestRegexpExamples(t *testing.T) {
g := StringMatching("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
for i := 0; i < 100; i++ {
s := g.Example()
t.Log(len(s), s)
}
}
func TestStringOfRunesIsUTF8(t *testing.T) {
t.Parallel()
gens := []*Generator[string]{
String(),
StringN(2, 10, -1),
StringOf(Rune()),
StringOfN(Rune(), 2, 10, -1),
StringOf(RuneFrom(nil, unicode.Cyrillic)),
StringOf(RuneFrom([]rune{'a', 'b', 'c'})),
}
for _, g := range gens {
t.Run(g.String(), MakeCheck(func(t *T) {
s := g.Draw(t, "s")
if !utf8.ValidString(s) {
t.Fatalf("invalid UTF-8 string: %q", s)
}
}))
}
}
func TestStringRuneCountLimits(t *testing.T) {
t.Parallel()
genFuncs := []func(i, j int) *Generator[string]{
func(i, j int) *Generator[string] { return StringN(i, j, -1) },
func(i, j int) *Generator[string] { return StringOfN(Rune(), i, j, -1) },
}
for i, gf := range genFuncs {
t.Run(strconv.Itoa(i), MakeCheck(func(t *T) {
minRunes := IntRange(0, 256).Draw(t, "minRunes")
maxRunes := IntMin(minRunes).Draw(t, "maxRunes")
s := gf(minRunes, maxRunes).Draw(t, "s")
n := utf8.RuneCountInString(s)
if n < minRunes {
t.Fatalf("got string with %v runes with lower limit %v", n, minRunes)
}
if n > maxRunes {
t.Fatalf("got string with %v runes with upper limit %v", n, maxRunes)
}
}))
}
}
func TestStringNMaxLen(t *testing.T) {
t.Parallel()
genFuncs := []func(int) *Generator[string]{
func(i int) *Generator[string] { return StringN(-1, -1, i) },
func(i int) *Generator[string] { return StringOfN(Rune(), -1, -1, i) },
}
for i, gf := range genFuncs {
t.Run(strconv.Itoa(i), MakeCheck(func(t *T) {
maxLen := IntMin(0).Draw(t, "maxLen")
s := gf(maxLen).Draw(t, "s")
if len(s) > maxLen {
t.Fatalf("got string of length %v with maxLen %v", len(s), maxLen)
}
}))
}
}