Skip to content

Example generators

Marko Justinek edited this page Mar 5, 2022 · 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:

DateTimeExpression(expression: String, format: String)

Generates a date time example in the provided format based on provided expression.

// DSL
{
  "someDateExpression": ExampleGenerator.DateTimeExpression(expression: "last december + 2 weeks + 4 days", format: "yyyy-MM-dd")
}

// DSL Objective-C:
@{@"someDateExpression": [[PFGeneratorDateTimeExpression alloc] expression:@"last december + 2 weeks + 4 days", format:@"yyyy-MM-dd"]};

These expressions work relative to a base date + time, normally the current system clock. They provide a reliable way to work with relative dates in tests.

Given the base date-time of 2000-01-01T10:00Z, then the following will resolve to

Expression Resulting date-time
'' '2000-01-01T10:00Z'
'now' '2000-01-01T10:00Z'
'today' '2000-01-01T10:00Z'
'yesterday' '1999-12-31T10:00Z'
'tomorrow' '2000-01-02T10:00Z'
'+ 1 day' '2000-01-02T10:00Z'
'+ 1 week' '2000-01-08T10:00Z'
'- 2 weeks' '1999-12-18T10:00Z'
'+ 4 years' '2004-01-01T10:00Z'
'tomorrow+ 4 years' '2004-01-02T10:00Z'
'next week' '2000-01-08T10:00Z'
'last month' '1999-12-01T10:00Z'
'next fortnight' '2000-01-15T10:00Z'
'next monday' '2000-01-03T10:00Z'
'last wednesday' '1999-12-29T10:00Z'
'next mon' '2000-01-03T10:00Z'
'last december' '1999-12-01T10:00Z'
'next jan' '2001-01-01T10:00Z'
'next june + 2 weeks' '2000-06-15T10:00Z'
'last mon + 2 weeks' '2000-01-10T10:00Z'
'+ 1 day - 2 weeks' '1999-12-19T10:00Z'
'last december + 2 weeks + 4 days' '1999-12-19T10:00Z'
'@ now' '2000-01-01T10:00Z'
'@ midnight' '2000-01-01T00:00Z'
'@ noon' '2000-01-01T12:00Z'
'@ 2 o'clock' '2000-01-01T14:00Z'
'@ 12 o'clock am' '2000-01-01T12:00Z'
'@ 1 o'clock pm' '2000-01-01T13:00Z'
'@ + 1 hour' '2000-01-01T11:00Z'
'@ - 2 minutes' '2000-01-01T09:58Z'
'@ + 4 seconds' '2000-01-01T10:00:04Z'
'@ + 4 milliseconds' '2000-01-01T10:00:00.004Z'
'@ midnight+ 4 minutes' '2000-01-01T00:04Z'
'@ next hour' '2000-01-01T11:00Z'
'@ last minute' '2000-01-01T09:59Z'
'@ now + 2 hours - 4 minutes' '2000-01-01T11:56Z'
'@ + 2 hours - 4 minutes' '2000-01-01T11:56Z'
'today @ 1 o'clock' '2000-01-01T13:00Z'
'yesterday @ midnight' '1999-12-31T00:00Z'
'yesterday @ midnight - 1 hour' '1999-12-30T23:00Z'
'tomorrow @ now' '2000-01-02T10:00Z'
'+ 1 day @ noon' '2000-01-02T12:00Z'
'+ 1 week @ +1 hour' '2000-01-08T11:00Z'
'- 2 weeks @ now + 1 hour' '1999-12-18T11:00Z'
'+ 4 years @ midnight' '2004-01-01T00:00Z'
'tomorrow+ 4 years @ 3 o'clock + 40 milliseconds' '2004-01-02T15:00:00.040Z'
'next week @ next hour' '2000-01-08T11:00Z'
'last month @ last hour' '1999-12-01T09:00Z'

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 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.

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