Skip to content

Commit

Permalink
chore: match regex with pact-jvm for int/decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Aug 14, 2024
1 parent 75a36cd commit 3cd978d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/pact/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ def like_date date
Pact::Term.new(generate: date, matcher: /^\d{4}-[01]\d-[0-3]\d$/)
end

# regex matched with pact-jvm
# https://github.com/pact-foundation/pact-jvm/blob/00442e6df51e5be906ed470b19859246312e5c83/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatcherExecutor.kt#L56-L59
def like_integer int
Pact::Term.new(generate: int, matcher: /[0-9]/)
Pact::Term.new(generate: int, matcher: /^-?\d+$/)
end

def like_decimal float
Pact::Term.new(generate: float, matcher: /[-+]?([0-9]*\.[0-9]+|[0-9]+)/)
Pact::Term.new(generate: float, matcher: /^0|-?\d+\.\d*$/)
end

def like_datetime_rfc822 datetime
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/pact/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ module Pact
it "creates a Pact::Term with regex matcher for integers" do
expect(like_integer(integer)).to eq Pact::Term.new(
generate: integer,
matcher: /[0-9]/
matcher: /^-?\d+$/
)
end
end
Expand All @@ -156,7 +156,7 @@ module Pact
it "creates a Pact::Term with regex matcher for decimals" do
expect(like_decimal(10.2)).to eq Pact::Term.new(
generate: float,
matcher: /[-+]?([0-9]*\.[0-9]+|[0-9]+)/
matcher: /^0|-?\d+\.\d*$/
)
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/pact/term_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ module Pact
end

context "when the generate is a integer" do
let(:term) { Term.new(generate: 10, matcher: /[0-9]/)}
let(:term) { Term.new(generate: 10, matcher: /^-?\d+$/)}

it 'does not raise an exception' do
term
end
end

context "when the generate is a float" do
let(:term) { Term.new(generate: 50.51, matcher: /[-+]?([0-9]*\.[0-9]+|[0-9]+)/)}
let(:term) { Term.new(generate: 50.51, matcher: /^0|-?\d+\.\d*$/)}

it 'does not raise an exception' do
term
Expand Down

0 comments on commit 3cd978d

Please sign in to comment.