-
Notifications
You must be signed in to change notification settings - Fork 16
Example generators
The example requests and response bodies stored in a pact file are static. The idea being that the pact file represents a contract that can always be fulfilled if the provider is in the correct state. However, this assumption is not always correct. In some cases, dates and times may need to be relative to the current date and time, and some things like tokens may have a very short life span.
An example of the date issue is a provider which only accepts a date value in the current financial year. As soon as we switch over to a new financial year (or any time period), that pact file can no longer be used.
Note: In Objective-C the Example Generators are prefixed with PFGenerator
(PF
- Pact Foundation).
PactSwift provides the following example generators:
Generates a random integer value between provided min
and max
values.
// DSL Swift:
{
"someInt": ExampleGenerator.RandomInt(min: 0, max: 100)
}
// DSL Objective-C:
@{@"someInt": [[PFGeneratorRandomInt alloc] min:@0, max:@100]};
Generates a random string value of the provided size characters.
// DSL Swift:
{
"someString": ExampleGenerator.RandomString(size: 16)
}
// DSL Objective-C:
@{@"someString": [[PFGeneratorRandomString alloc] size:@16]};
Generates a random string value from the provided regular expression.
Use a raw String
(eg: #"\\d{2}/\\d{2,4}"#
) to avoid interpreting special characters.
// DSL Swift:
{
"someString": ExampleGenerator.RandomString(regex: #"\d{2}[a-z]"#)
}
// DSL Objective-C:
@{@"someString": [[PFGeneratorRandomString alloc] regex:@"\\d{2}[a-z]"]};
Generates a random boolean value.
// DSL Swift:
{
"someBool": ExampleGenerator.RandomBool()
}
// DSL Objective-C:
@{@"someBool": [[PFGeneratorRandomBool alloc] init]};
Generates a random decimal value (BigDecimal) with the provided number of digits.
// DSL Swift
{
"someDecimal": ExampleGenerator.RandomDecimal(digits: 6)
}
// DSL Objective-C:
@{@"someDecimal": [[PFGeneratorRandomDecimal alloc] digits:@6]};
Generates a random hexadecimal value (String) with the provided number of digits.
// DSL Swift:
{
"someHex": ExampleGenerator.RandomHexadecimal(digits: 12)
}
// DSL Objective-C:
@{@"someHex": [[PFGeneratorRandomHexadecimal alloc] digits:@12]};
Generates a random UUID value compliant with RFC4122 specifically ITU-T Rec. X.667 (ISO/IEC 9834-8:2005 section 6.5.4).
// DSL Swift:
{
"someUUID": ExampleGenerator.RandomUUID()
}
// DSL Objective-C:
@{@"someUUID": [[PFGeneratorRandomUUID alloc] init]};
Note: generates a lowercased simple format UUID such as "385336e1d64744a88b1b3dbf4a073416". See https://github.com/surpher/PactSwift/issues/70.
Generates a Date value from the current date either in ISO format or using the provided format string.
// DSL Swift:
{
"someDate": ExampleGenerator.RandomDate(format: "dd/MM/yy")
}
// DSL Objective-C:
@{@"someDate": [[PFGeneratorRandomDate alloc] format:@"dd/MM/yy"]};
Generates a Time value from the current time either in ISO format or using the provided format string.
// DSL Swift:
{
"someTime": ExampleGenerator.RandomTime(format: "HH:mm")
}
// DSL Objective-C:
@{@"someTime": [[PFGeneratorRandomTime alloc] format:@"HH:mm"]};
Generates a Date and Time (timestamp) value from the current date and time either in ISO format or using the provided format string.
// DSL Swift:
{
"someDateTime": ExampleGenerator.RandomDateTime(format: "HH:mm - dd/MM")
}
// DSL Objective-C:
@{@"someDateTime": [[PFGeneratorRandomDateTime alloc] format:@"HH:mm - dd/MM"]};