-
Notifications
You must be signed in to change notification settings - Fork 1
/
text_test.go
107 lines (91 loc) · 2.34 KB
/
text_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
101
102
103
104
105
106
107
// Copyright 2019 by David A. Golden. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package jfdi
import (
"regexp"
"testing"
)
func TestWords(t *testing.T) {
t.Parallel()
re := regexp.MustCompile(`^\S+$`)
w := Word()(nil).(string)
if !re.MatchString(w) {
t.Errorf("failed: `%s` doesn't match `%s`", w, re)
}
for i := 0; i <= 10; i++ {
f := Words(i)
s := f(nil).([]string)
if len(s) != i {
t.Errorf("Words(%d) produces slice of length %d", i, len(s))
}
for _, v := range s {
if !re.MatchString(v) {
t.Errorf("failed: `%s` doesn't match `%s`", v, re)
}
}
}
xs := Words(Int(3, 5))(nil).([]string)
if len(xs) < 3 || len(xs) > 5 {
t.Errorf("Words(Int(x,y)) returned incorrect length slice")
}
for _, v := range xs {
if !re.MatchString(v) {
t.Errorf("failed: `%s` doesn't match `%s`", v, re)
}
}
}
func TestSentences(t *testing.T) {
t.Parallel()
re := regexp.MustCompile(`^[A-Z]\S*(\s*\S+)*[.!?]$`)
s := Sentence()(nil).(string)
if !re.MatchString(s) {
t.Errorf("failed: `%s` doesn't match `%s`", s, re)
}
for i := 0; i <= 10; i++ {
f := Sentences(i)
s := f(nil).([]string)
if len(s) != i {
t.Errorf("Sentences(%d) produces slice of length %d", i, len(s))
}
for _, v := range s {
if !re.MatchString(v) {
t.Errorf("failed: `%s` doesn't match `%s`", v, re)
}
}
}
xs := Sentences(Int(3, 5))(nil).([]string)
if len(xs) < 3 || len(xs) > 5 {
t.Errorf("Sentences(Int(x,y)) returned incorrect length slice")
}
for _, v := range xs {
if !re.MatchString(v) {
t.Errorf("failed: `%s` doesn't match `%s`", v, re)
}
}
}
func TestJoin(t *testing.T) {
t.Parallel()
cases := []struct {
input interface{}
sep interface{}
match string
}{
{[]string{"a", "b", "c"}, " ", `^a b c$`},
{[]string{"a", "b", "c"}, Pick(" ", "-"), `^a[- ]b[- ]c$`},
{Words(3), " ", `^\S+ \S+ \S+$`},
{Words(3), "", `^\S+$`},
{Words(3), Pick(" ", "-"), `^\S+[- ]\S+[- ]\S+$`},
{Words(0), "", `^$`},
}
for _, c := range cases {
f := Join(c.input, c.sep)
s := f(nil).(string)
re := regexp.MustCompile(c.match)
if !re.MatchString(s) {
t.Errorf("failed: `%s` doesn't match `%s`", s, c.match)
}
}
}