From 2d1cf72461f4957dfcfbc51569f8ca00fd5765a4 Mon Sep 17 00:00:00 2001 From: David Cuddeback Date: Sun, 13 Nov 2011 19:07:05 -0800 Subject: [PATCH] add HasCheckbox matcher --- lib/rspec/tag_matchers.rb | 1 + lib/rspec/tag_matchers/has_checkbox.rb | 22 +++++++++ .../rspec/tag_matchers/has_checkbox_spec.rb | 45 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 lib/rspec/tag_matchers/has_checkbox.rb create mode 100644 spec/lib/rspec/tag_matchers/has_checkbox_spec.rb diff --git a/lib/rspec/tag_matchers.rb b/lib/rspec/tag_matchers.rb index 89e4f85..6c6a3fd 100644 --- a/lib/rspec/tag_matchers.rb +++ b/lib/rspec/tag_matchers.rb @@ -3,3 +3,4 @@ require 'rspec/tag_matchers/has_tag' require 'rspec/tag_matchers/has_form' require 'rspec/tag_matchers/has_input' +require 'rspec/tag_matchers/has_checkbox' diff --git a/lib/rspec/tag_matchers/has_checkbox.rb b/lib/rspec/tag_matchers/has_checkbox.rb new file mode 100644 index 0000000..5cd2331 --- /dev/null +++ b/lib/rspec/tag_matchers/has_checkbox.rb @@ -0,0 +1,22 @@ +module RSpec::TagMatchers + def have_checkbox + HasCheckbox.new + end + + class HasCheckbox < HasInput + def initialize + super + with_attribute(:type => :checkbox) + end + + def checked + with_attribute(:checked => true) + self + end + + def not_checked + with_attribute(:checked => false) + self + end + end +end diff --git a/spec/lib/rspec/tag_matchers/has_checkbox_spec.rb b/spec/lib/rspec/tag_matchers/has_checkbox_spec.rb new file mode 100644 index 0000000..0b75627 --- /dev/null +++ b/spec/lib/rspec/tag_matchers/has_checkbox_spec.rb @@ -0,0 +1,45 @@ +require 'spec_helper' + +describe RSpec::TagMatchers::HasCheckbox do + include RSpec::TagMatchers + + describe "matching checkbox tags" do + context "have_checkbox" do + subject { have_checkbox } + it { should_not match("") } + it { should match("") } + it { should match("") } + it { should_not match("") } + it { should_not match("") } + it { should_not match("input checkbox") } + end + end + + describe "matching checkbox state" do + context "have_checkbox.checked" do + subject { have_checkbox.checked } + it { should_not match("") } + it { should match("") } + it { should match("") } + it { should match("") } + end + + context "have_checkbox.not_checked" do + subject { have_checkbox.not_checked } + it { should match("") } + it { should_not match("") } + it { should_not match("") } + it { should_not match("") } + end + end + + describe "matching input names" do + context "have_checkbox.for(:user => :admin)" do + subject { have_checkbox.for(:user => :admin) } + it { should_not match("") } + it { should_not match("") } + it { should match("") } + it { should_not match("") } + end + end +end