Skip to content

Commit

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

def like_integer int
Pact::Term.new(generate: int, matcher: /[0-9]/)
end

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

def like_datetime_rfc822 datetime
Pact::Term.new(
generate: datetime,
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/pact/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,26 @@ module Pact

end
end

describe "#like_integer" do
let(:integer) { 1 }

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]/
)
end
end
describe "#like_decimal" do
let(:float) { 10.2 }

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]+)/
)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/pact/term_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Pact
end

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

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

0 comments on commit 75a36cd

Please sign in to comment.