Skip to content

Example generators

Marko Justinek edited this page Oct 26, 2020 · 11 revisions

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:

RandomInt(min: Int, max: Int)

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]};

RandomString(size: Int)

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]};

RandomString(regex: String)

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]"]};

RandomBool()

Generates a random boolean value.

// DSL Swift:
{
  "someBool": ExampleGenerator.RandomBool()
}

// DSL Objective-C:
@{@"someBool": [[PFGeneratorRandomBool alloc] init]};

RandomDecimal(digits: Int)

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]};

RandomHexadecimal(digits: UInt8)

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]};

RandomUUID()

Generates a random UUID value.

// DSL Swift:
{
  "someUUID": ExampleGenerator.RandomUUID()
}

// DSL Objective-C:
@{@"someUUID": [[PFGeneratorRandomUUID alloc] init]};

RandomDate(format: String?)

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"]};

RandomTime(format: String?)

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"]};

RandomDateTime(format: String?)

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"]};
Clone this wiki locally