DiUS / pact-consumer-js-dsl

*DEPRECATED* A Javascript DSL for creating pacts, superceded by Pact JS
https://github.com/pact-foundation/pact-js
Other
56 stars 26 forks source link

matchers for number #52

Closed ichyr closed 8 years ago

ichyr commented 8 years ago

Hi!

Is there a way to specify that the response will have any number (maybe even separately integer and double kind) as some property ( e.g. age : { .., 'age': 12, ... } )

Now this can be achieved using Pact.Matcher as:

{ .., 'age': Pact.Match.term({**matcher**: "\\d{1,20}", generate: "1111222233334444"}), ... }`

Thus, what are other possible values of matcher in this Pact.Match.term?

ichyr commented 8 years ago

(https://github.com/DiUS/pact-consumer-js-dsl/blob/master/src/match.js) simply puts the matcher text into json:

this.term = function(term) {
        if (!term ||
            typeof term.generate === 'undefined' ||
            typeof term.matcher === 'undefined') {
            throw new Error('Error creating a Pact Term. Please provide an object containing \'generate\' and \'matcher\' properties');
        }

        return {
            'json_class': 'Pact::Term',
            'data': {
                'generate': term.generate,
                'matcher': {
                    'json_class': 'Regexp',
                    'o': 0,
                    's': term.matcher
                }
            }
        };
    };
mefellows commented 8 years ago

Hi @ichyr, it is marshalled into JSON as the DSL itself does not perform the Pact verification - this is passed to the Pact Provider Proxy.

As per the README, the types of matchers support in this JavaScript version mirror those specified here: https://github.com/realestate-com-au/pact/wiki/Regular-expressions-and-type-matching-with-Pact.

If you wanted to match on integers, separately to decimals, you could simply provide different regular expressions for your need, e.g. for integers:

{ .., 'age': Pact.Match.term({matcher: "\\d{1,20}", generate: "1111222233334444"}), ... }`

for floating points with 2 or more decimal places:

{ .., 'age': Pact.Match.term({matcher: "\\d+(\\.\\d{2})?", generate: "3.142"}), ... }`

Does that answer your question?

ichyr commented 8 years ago

@mefellows Thanks!

My API will return numbers, so I use regular expressions for this.

But you answered it. In the link you've provided there is reference for Pact::SomethingLike.

 Pact.like(
        name: "Mary",
        age: 73
      )

There is this matcher defined in js-dsl. Thus my test will look like

{ ..,
   'age': Pact.Match.somethingLike(1111222233334444),
... }
mefellows commented 8 years ago

Beautiful - glad to hear it's all sorted :+1: