Skip to content

Commit 75a36cd

Browse files
committed
feat: add like_integer / like_decimal helpers
1 parent f2c448c commit 75a36cd

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

lib/pact/helpers.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ def like_date date
4747
Pact::Term.new(generate: date, matcher: /^\d{4}-[01]\d-[0-3]\d$/)
4848
end
4949

50+
def like_integer int
51+
Pact::Term.new(generate: int, matcher: /[0-9]/)
52+
end
53+
54+
def like_decimal float
55+
Pact::Term.new(generate: float, matcher: /[-+]?([0-9]*\.[0-9]+|[0-9]+)/)
56+
end
57+
5058
def like_datetime_rfc822 datetime
5159
Pact::Term.new(
5260
generate: datetime,

spec/lib/pact/helpers_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,26 @@ module Pact
139139

140140
end
141141
end
142+
143+
describe "#like_integer" do
144+
let(:integer) { 1 }
145+
146+
it "creates a Pact::Term with regex matcher for integers" do
147+
expect(like_integer(integer)).to eq Pact::Term.new(
148+
generate: integer,
149+
matcher: /[0-9]/
150+
)
151+
end
152+
end
153+
describe "#like_decimal" do
154+
let(:float) { 10.2 }
155+
156+
it "creates a Pact::Term with regex matcher for decimals" do
157+
expect(like_decimal(10.2)).to eq Pact::Term.new(
158+
generate: float,
159+
matcher: /[-+]?([0-9]*\.[0-9]+|[0-9]+)/
160+
)
161+
end
162+
end
142163
end
143164
end

spec/lib/pact/term_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module Pact
2323
end
2424

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

2828
it 'does not raise an exception' do
2929
term

0 commit comments

Comments
 (0)