Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #77
- Loading branch information
Showing
11 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
module Enumerize | ||
class AttributeMap | ||
attr_reader :attributes | ||
|
||
def initialize | ||
@attributes = {} | ||
@dependants = [] | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'enumerize/integrations/rspec/matcher' | ||
|
||
module Enumerize | ||
module Integrations | ||
module RSpec | ||
def enumerize(attr) | ||
::Enumerize::Integrations::RSpec::Matcher.new(attr) | ||
end | ||
end | ||
end | ||
end | ||
|
||
module RSpec | ||
module Matchers | ||
include Enumerize::Integrations::RSpec | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
module Enumerize | ||
module Integrations | ||
module RSpec | ||
class Matcher | ||
attr_accessor :attr, :values, :subject, :default | ||
|
||
def initialize(attr) | ||
self.attr = attr | ||
end | ||
|
||
def in(*values) | ||
self.values = values.map(&:to_s).sort | ||
self | ||
end | ||
|
||
def with_default(default) | ||
self.default = default.to_s | ||
self | ||
end | ||
|
||
def failure_message | ||
message = " expected :#{attr} to allow value#{values.size == 1 ? nil : 's'}: #{quote_values(values)}," | ||
message += " but it allows #{quote_values(enumerized_values)} instead" | ||
|
||
if default | ||
message = " expected :#{attr} to have #{default.inspect} as default value," | ||
message += " but it sets #{enumerized_default.inspect} instead" | ||
end | ||
|
||
message | ||
end | ||
|
||
def description | ||
description = "enumerize :#{attr} in: #{quote_values(values)}" | ||
description += " with #{default.inspect} as default value" if default | ||
end | ||
|
||
def matches?(subject) | ||
self.subject = subject | ||
matches = true | ||
|
||
matches &= matches_attributes? | ||
matches &= matches_default_value? if default | ||
|
||
matches | ||
end | ||
|
||
private | ||
|
||
def matches_attributes? | ||
values == enumerized_values | ||
end | ||
|
||
def matches_default_value? | ||
default == enumerized_default | ||
end | ||
|
||
def enumerized_values | ||
@enumerized_values ||= attributes[attr.to_s].values.sort | ||
end | ||
|
||
def enumerized_default | ||
@enumerized_default ||= attributes[attr.to_s].default_value | ||
end | ||
|
||
def attributes | ||
subject.class.enumerized_attributes.attributes | ||
end | ||
|
||
def quote_values(values) | ||
values.map(&:inspect).join(', ') | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require 'test_helper' | ||
require 'enumerize/integrations/rspec' | ||
|
||
describe Enumerize::Integrations::RSpec do | ||
class Should | ||
include Enumerize::Integrations::RSpec | ||
end | ||
|
||
let(:klass) do | ||
Class.new do | ||
extend Enumerize | ||
end | ||
end | ||
|
||
let(:should) { Should.new } | ||
let(:object) { klass.new } | ||
|
||
describe '#matches?' do | ||
before do | ||
klass.enumerize(:sex, :in => [:male, :female]) | ||
end | ||
|
||
it 'returns true' do | ||
matcher = should.enumerize(:sex).in(:male, :female) | ||
matcher.matches?(object).must_equal true | ||
end | ||
|
||
it 'returns false' do | ||
matcher = should.enumerize(:sex).in(:bar) | ||
matcher.matches?(object).must_equal false | ||
end | ||
end | ||
|
||
describe '#failure_message' do | ||
before do | ||
klass.enumerize(:sex, :in => [:male, :female], :default => :male) | ||
end | ||
|
||
it 'returns failure message for invalid :in option' do | ||
matcher = should.enumerize(:sex).in(:bar) | ||
matcher.subject = object | ||
expected = ' expected :sex to allow value: "bar", but it allows "female", "male" instead' | ||
matcher.failure_message.must_equal expected | ||
end | ||
|
||
it 'returns failure message for invalid :with_default option' do | ||
matcher = should.enumerize(:sex).in(:male, :female).with_default(:foo) | ||
matcher.subject = object | ||
expected = ' expected :sex to have "foo" as default value, but it sets "male" instead' | ||
matcher.failure_message.must_equal expected | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'enumerize' | ||
require 'rspec' | ||
|
||
class RSpecUser | ||
extend Enumerize | ||
|
||
enumerize :sex, in: [:male, :female], default: :male | ||
end | ||
|
||
describe RSpecUser do | ||
it { should enumerize(:sex).in(:male, :female) } | ||
it { should enumerize(:sex).in(:male, :female).with_default(:male) } | ||
end |
I think it would be better to require this matcher in tests manually to spec_helper, because this line doesn't work with spork. I have to require it manually like this:
or just add this note to readme?