Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to match integers and floats #38

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/pact/term.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ def self.unpack_regexps source

def initialize(attributes = {})
@generate = attributes[:generate]
raise "Please specify a value to generate for the Term" unless @generate != nil

@generate = @generate.to_s
@matcher = attributes[:matcher]
raise "Please specify a matcher for the Term" unless @matcher != nil
raise "Please specify a value to generate for the Term" unless @generate != nil
raise "Value to generate \"#{@generate}\" does not match regular expression #{@matcher.inspect}" unless @generate =~ @matcher
end

Expand Down
16 changes: 16 additions & 0 deletions spec/lib/pact/term_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ module Pact
end
end

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

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: /\d(\.\d{1,2})/)}

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

context "when the matcher does not match the generated value" do
let(:generate) { 'banana' }
it 'raises an exception' do
Expand Down