Skip to content

Commit

Permalink
Introduce Rapid library for property based tests. Initial cut of doma…
Browse files Browse the repository at this point in the history
…in generators for use with Rapid testing library.
  • Loading branch information
gmalouf committed Feb 16, 2023
1 parent 7d03ca3 commit afc46c7
Show file tree
Hide file tree
Showing 4 changed files with 1,548 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
golang.org/x/sys v0.1.0
golang.org/x/text v0.4.0
gopkg.in/sohlich/elogrus.v3 v3.0.0-20180410122755-1fa29e2f2009
pgregory.net/rapid v0.4.8
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,5 @@ gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
pgregory.net/rapid v0.4.8 h1:d+5SGZWUbJPbl3ss6tmPFqnNeQR6VDOFly+eTjwPiEw=
pgregory.net/rapid v0.4.8/go.mod h1:Z5PbWqjvWR1I3UGjvboUuan4fe4ZYEYNLNQLExzCoUs=
55 changes: 55 additions & 0 deletions test/rapidgen/rapidgenerators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package rapidgen

// See https://github.com/flyingmutant/rapid/pull/18

import (
"fmt"
"pgregory.net/rapid"
)

const (
domainMaxLength = 255
domainMaxElementLength = 63
)

// Domain generates an RFC 1035 compliant domain name.
func Domain() *rapid.Generator {
return DomainOf(255, 63)
}

// DomainOf generates an RFC 1035 compliant domain name,
// with a maximum overall length of maxLength
// and a maximum number of elements of maxElements.
func DomainOf(maxLength, maxElementLength int) *rapid.Generator {
assertf(4 <= maxLength, "maximum length (%v) should not be less than 4, to generate a two character domain and a one character subdomain", maxLength)
assertf(maxLength <= 255, "maximum length (%v) should not be greater than 255 to comply with RFC 1035", maxLength)
assertf(1 <= maxElementLength, "maximum element length (%v) should not be less than 1 to comply with RFC 1035", maxElementLength)
assertf(maxElementLength <= 63, "maximum element length (%v) should not be greater than 63 to comply with RFC 1035", maxElementLength)

return rapid.Custom(func(t *rapid.T) string {
domain := fmt.Sprint(tldGenerator.
Filter(func(s string) bool { return len(s)+2 <= domainMaxLength }).
Draw(t, "domain"))

expr := fmt.Sprintf(`[a-zA-Z]([a-zA-Z0-9\-]{0,%d}[a-zA-Z0-9])?`, domainMaxElementLength-2)

el := rapid.Int8Range(1, 126).Example().(int)
for i := 0; i < el; i++ {
subDomain := fmt.Sprint(rapid.StringMatching(expr).Draw(t, "subdomain"))
if len(domain)+len(subDomain) >= domainMaxLength {
break
}
domain = subDomain + "." + domain
}

return domain
})
}

var tldGenerator = rapid.SampledFrom(tlds)

func assertf(ok bool, format string, args ...interface{}) {
if !ok {
panic(fmt.Sprintf(format, args...))
}
}

0 comments on commit afc46c7

Please sign in to comment.