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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
// Tideland Go Library - Audit - Unit Tests
//
// Copyright (C) 2013-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the NewGenerator BSD license.
package audit_test
//--------------------
// IMPORTS
//--------------------
import (
"fmt"
"strings"
"testing"
"time"
"github.com/tideland/golib/audit"
)
//--------------------
// TESTS
//--------------------
// TestBuildDate tests the generation of dates.
func TestBuildDate(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
layouts := []string{
time.ANSIC,
time.UnixDate,
time.RubyDate,
time.RFC822,
time.RFC822Z,
time.RFC850,
time.RFC1123,
time.RFC1123Z,
time.RFC3339,
time.RFC3339Nano,
time.Kitchen,
time.Stamp,
time.StampMilli,
time.StampMicro,
time.StampNano,
}
for _, layout := range layouts {
ts, t := audit.BuildTime(layout, 0)
tsp, err := time.Parse(layout, ts)
assert.Nil(err)
assert.Equal(t, tsp)
ts, t = audit.BuildTime(layout, -30*time.Minute)
tsp, err = time.Parse(layout, ts)
assert.Nil(err)
assert.Equal(t, tsp)
ts, t = audit.BuildTime(layout, time.Hour)
tsp, err = time.Parse(layout, ts)
assert.Nil(err)
assert.Equal(t, tsp)
}
}
// TestInts tests the generation of ints.
func TestInts(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
gen := audit.NewGenerator(audit.FixedRand())
// Test individual ints.
for i := 0; i < 10000; i++ {
lo := gen.Int(-100, 100)
hi := gen.Int(-100, 100)
n := gen.Int(lo, hi)
if hi < lo {
lo, hi = hi, lo
}
assert.True(lo <= n && n <= hi)
}
// Test int slices.
ns := gen.Ints(0, 500, 10000)
assert.Length(ns, 10000)
for _, n := range ns {
assert.True(n >= 0 && n <= 500)
}
// Test the generation of percent.
for i := 0; i < 10000; i++ {
p := gen.Percent()
assert.True(p >= 0 && p <= 100)
}
// Test the flipping of coins.
ct := 0
cf := 0
for i := 0; i < 10000; i++ {
c := gen.FlipCoin(50)
if c {
ct++
} else {
cf++
}
}
assert.About(float64(ct), float64(cf), 500)
}
// TestOneOf tests the generation of selections.
func TestOneOf(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
gen := audit.NewGenerator(audit.FixedRand())
for i := 0; i < 10000; i++ {
b := gen.OneByteOf(1, 2, 3, 4, 5)
assert.True(b >= 1 && b <= 5)
r := gen.OneRuneOf("abcdef")
assert.True(r >= 'a' && r <= 'f')
n := gen.OneIntOf(1, 2, 3, 4, 5)
assert.True(n >= 1 && n <= 5)
s := gen.OneStringOf("one", "two", "three", "four", "five")
assert.Substring(s, "one/two/three/four/five")
d := gen.OneDurationOf(1*time.Second, 2*time.Second, 3*time.Second)
assert.True(d >= 1*time.Second && d <= 3*time.Second)
}
}
// TestWords tests the generation of words.
func TestWords(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
gen := audit.NewGenerator(audit.FixedRand())
// Test single words.
for i := 0; i < 10000; i++ {
w := gen.Word()
for _, r := range w {
assert.True(r >= 'a' && r <= 'z')
}
}
// Test limited words.
for i := 0; i < 10000; i++ {
lo := gen.Int(audit.MinWordLen, audit.MaxWordLen)
hi := gen.Int(audit.MinWordLen, audit.MaxWordLen)
w := gen.LimitedWord(lo, hi)
wl := len(w)
if hi < lo {
lo, hi = hi, lo
}
assert.True(lo <= wl && wl <= hi, info("WL %d LO %d HI %d", wl, lo, hi))
}
}
// TestPattern tests the generation based on patterns.
func TestPattern(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
gen := audit.NewGenerator(audit.FixedRand())
assertPattern := func(pattern, runes string) {
set := make(map[rune]bool)
for _, r := range runes {
set[r] = true
}
for i := 0; i < 10; i++ {
result := gen.Pattern(pattern)
for _, r := range result {
assert.True(set[r], pattern, result, runes)
}
}
}
assertPattern("^^", "^")
assertPattern("^0^0^0^0^0", "0123456789")
assertPattern("^1^1^1^1^1", "123456789")
assertPattern("^o^o^o^o^o", "01234567")
assertPattern("^h^h^h^h^h", "0123456789abcdef")
assertPattern("^H^H^H^H^H", "0123456789ABCDEF")
assertPattern("^a^a^a^a^a", "abcdefghijklmnopqrstuvwxyz")
assertPattern("^A^A^A^A^A", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
assertPattern("^c^c^c^c^c", "bcdfghjklmnpqrstvwxyz")
assertPattern("^C^C^C^C^C", "BCDFGHJKLMNPQRSTVWXYZ")
assertPattern("^v^v^v^v^v", "aeiou")
assertPattern("^V^V^V^V^V", "AEIOU")
assertPattern("^z^z^z^z^z", "abcdefghijklmnopqrstuvwxyz0123456789")
assertPattern("^Z^Z^Z^Z^Z", "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
assertPattern("^1^0.^0^0^0,^0^0 €", "0123456789 .,€")
}
// TestText tests the generation of text.
func TestText(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
gen := audit.NewGenerator(audit.FixedRand())
for i := 0; i < 10000; i++ {
s := gen.Sentence()
ws := strings.Split(s, " ")
lws := len(ws)
assert.True(2 <= lws && lws <= 15, info("SL: %d", lws))
assert.True('A' <= s[0] && s[0] <= 'Z', info("SUC: %v", s[0]))
}
for i := 0; i < 10000; i++ {
p := gen.Paragraph()
ss := strings.Split(p, ". ")
lss := len(ss)
assert.True(2 <= lss && lss <= 10, info("PL: %d", lss))
for _, s := range ss {
ws := strings.Split(s, " ")
lws := len(ws)
assert.True(2 <= lws && lws <= 15, info("PSL: %d", lws))
assert.True('A' <= s[0] && s[0] <= 'Z', info("PSUC: %v", s[0]))
}
}
}
// TestName tests the generation of names.
func TestName(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
gen := audit.NewGenerator(audit.FixedRand())
assert.Equal(audit.ToUpperFirst("yadda"), "Yadda")
for i := 0; i < 10000; i++ {
first, middle, last := gen.Name()
assert.Match(first, `[A-Z][a-z]+(-[A-Z][a-z]+)?`)
assert.Match(middle, `[A-Z][a-z]+(-[A-Z][a-z]+)?`)
assert.Match(last, `[A-Z]['a-zA-Z]+`)
first, middle, last = gen.MaleName()
assert.Match(first, `[A-Z][a-z]+(-[A-Z][a-z]+)?`)
assert.Match(middle, `[A-Z][a-z]+(-[A-Z][a-z]+)?`)
assert.Match(last, `[A-Z]['a-zA-Z]+`)
first, middle, last = gen.FemaleName()
assert.Match(first, `[A-Z][a-z]+(-[A-Z][a-z]+)?`)
assert.Match(middle, `[A-Z][a-z]+(-[A-Z][a-z]+)?`)
assert.Match(last, `[A-Z]['a-zA-Z]+`)
}
}
// TestDomain tests the generation of domains.
func TestDomain(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
gen := audit.NewGenerator(audit.FixedRand())
for i := 0; i < 00100; i++ {
domain := gen.Domain()
assert.Match(domain, `^[a-z0-9.-]+\.[a-z]{2,4}$`)
}
}
// TestURL tests the generation of URLs.
func TestURL(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
gen := audit.NewGenerator(audit.FixedRand())
for i := 0; i < 10000; i++ {
url := gen.URL()
assert.Match(url, `(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?`)
}
}
// TestEMail tests the generation of e-mail addresses.
func TestEMail(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
gen := audit.NewGenerator(audit.FixedRand())
for i := 0; i < 10000; i++ {
addr := gen.EMail()
assert.Match(addr, `^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$`)
}
}
// TestTimes tests the generation of durations and times.
func TestTimes(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
gen := audit.NewGenerator(audit.FixedRand())
for i := 0; i < 10000; i++ {
// Test durations.
lo := gen.Duration(time.Second, time.Minute)
hi := gen.Duration(time.Second, time.Minute)
d := gen.Duration(lo, hi)
if hi < lo {
lo, hi = hi, lo
}
assert.True(lo <= d && d <= hi, "High / Low")
// Test times.
loc := time.Local
now := time.Now()
dur := gen.Duration(24*time.Hour, 30*24*time.Hour)
t := gen.Time(loc, now, dur)
assert.True(t.Equal(now) || t.After(now), "Equal or after now")
assert.True(t.Before(now.Add(dur)) || t.Equal(now.Add(dur)), "Before or equal now plus duration")
}
sleeps := map[int]time.Duration{
1: 1 * time.Millisecond,
2: 2 * time.Millisecond,
3: 3 * time.Millisecond,
4: 4 * time.Millisecond,
5: 5 * time.Millisecond,
}
for i := 0; i < 1000; i++ {
sleep := gen.SleepOneOf(sleeps[1], sleeps[2], sleeps[3], sleeps[4], sleeps[5])
s := int(sleep) / 1000000
_, ok := sleeps[s]
assert.True(ok, "Chosen duration is one the arguments")
}
}
//--------------------
// HELPER
//--------------------
var info = fmt.Sprintf
// EOF
|