Created
October 19, 2015 21:48
-
-
Save jnettome/cff9f5334a9aacf73aa3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'spec_helper' | |
| shared_examples_for 'bitmask_options' do | |
| # http://stackoverflow.com/questions/16525222/how-to-test-a-concern-in-rails#answer-20010923 | |
| # http://stackoverflow.com/questions/25562639/rspec-testing-ar-model-concern-within-described-class-or-dummyclass | |
| let(:model) { described_class } # the class that includes the concern | |
| has_attributes :bitmask_options | |
| it 'has at least one option' do | |
| expect(model.all_options).to have_at_least(1).item | |
| end | |
| context 'batch (array) setters' do | |
| it 'can set each option (true)' do | |
| model.all_options.each.with_index do |option, index| | |
| subject.bitmask_options += [option] | |
| expect(subject.bitmask_options).to include(option) | |
| end | |
| end | |
| it 'can unset each option (to false)' do | |
| model.all_options.each.with_index do |option, index| | |
| subject.bitmask_options -= [option] | |
| expect(subject.bitmask_options).to_not include(option) | |
| end | |
| end | |
| end | |
| context 'boolean setters' do | |
| it 'can set each option (true)' do | |
| model.all_options.each.with_index do |option, index| | |
| subject.send("#{option}=", true) | |
| expect(subject.send("#{option}")).to be true | |
| end | |
| end | |
| it 'can unset each option (false)' do | |
| model.all_options.each.with_index do |option, index| | |
| subject.send("#{option}=", false) | |
| expect(subject.send("#{option}")).to be false | |
| end | |
| end | |
| it 'does not modify other options when setting one (true)' do | |
| subject.bitmask = 0 | |
| model.all_options.shuffle.each do |option| | |
| bitmask_before = subject.bitmask | |
| subject.send("#{option}=", true) | |
| bitmask_after = subject.bitmask | |
| expect(bitmask_before ^ bitmask_after).to be (1 << model.all_options.index(option)) | |
| end | |
| end | |
| it 'does not modify other options when unsetting one (false)' do | |
| subject.bitmask = (1 << model.all_options.count) - 1 | |
| model.all_options.shuffle.each do |option| | |
| bitmask_before = subject.bitmask | |
| subject.send("#{option}=", false) | |
| bitmask_after = subject.bitmask | |
| expect(bitmask_before ^ bitmask_after).to be (1 << model.all_options.index(option)) | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment