-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Rapid library for property based tests. Initial cut of doma…
…in generators for use with Rapid testing library.
- Loading branch information
Showing
4 changed files
with
1,548 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...)) | ||
} | ||
} |
Oops, something went wrong.