modelish 0.3.0 → 1.0.0.pre.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.rspec +3 -1
- data/.ruby-version +1 -1
- data/.travis.yml +14 -0
- data/Appraisals +11 -0
- data/CHANGELOG.md +11 -0
- data/Gemfile +5 -1
- data/README.md +5 -0
- data/gemfiles/hashie_1.gemfile +7 -0
- data/gemfiles/hashie_2.gemfile +7 -0
- data/gemfiles/hashie_3.gemfile +7 -0
- data/lib/modelish/base.rb +108 -72
- data/lib/modelish/configuration.rb +9 -8
- data/lib/modelish/property_translations.rb +23 -11
- data/lib/modelish/property_types.rb +67 -50
- data/lib/modelish/validations.rb +70 -51
- data/lib/modelish/version.rb +3 -1
- data/lib/modelish.rb +3 -1
- data/modelish.gemspec +20 -15
- data/spec/modelish/base_spec.rb +203 -182
- data/spec/modelish/configuration_spec.rb +26 -19
- data/spec/modelish/property_translations_spec.rb +47 -39
- data/spec/modelish/property_types_spec.rb +53 -45
- data/spec/modelish/validations_spec.rb +296 -256
- data/spec/modelish_spec.rb +5 -3
- data/spec/spec_helper.rb +97 -2
- data/spec/support/property_examples.rb +15 -12
- data/spec/support/typed_property_examples.rb +39 -35
- data/spec/support/unknown_property_examples.rb +6 -4
- data/spec/support/validation_examples.rb +29 -23
- metadata +136 -103
- data/.ruby-gemset +0 -1
data/spec/modelish_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,102 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'simplecov'
|
4
|
+
SimpleCov.start 'test_frameworks'
|
5
|
+
|
6
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
7
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
8
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
9
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
10
|
+
# files.
|
11
|
+
#
|
12
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
13
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
14
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
15
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
16
|
+
# a separate helper file that requires the additional dependencies and performs
|
17
|
+
# the additional setup, and require it from the spec files that actually need
|
18
|
+
# it.
|
19
|
+
|
20
|
+
require 'rspec/its'
|
2
21
|
|
3
22
|
Dir[File.join(File.dirname(__FILE__), 'support', '*.rb')].each { |f| require f }
|
4
23
|
|
24
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
5
25
|
RSpec.configure do |config|
|
6
|
-
# config goes here
|
26
|
+
# rspec-expectations config goes here. You can use an alternate
|
27
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
28
|
+
# assertions if you prefer.
|
29
|
+
config.expect_with :rspec do |expectations|
|
30
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
31
|
+
# and `failure_message` of custom matchers include text for helper methods
|
32
|
+
# defined using `chain`, e.g.:
|
33
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
34
|
+
# # => "be bigger than 2 and smaller than 4"
|
35
|
+
# ...rather than:
|
36
|
+
# # => "be bigger than 2"
|
37
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
38
|
+
end
|
39
|
+
|
40
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
41
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
42
|
+
config.mock_with :rspec do |mocks|
|
43
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
44
|
+
# a real object. This is generally recommended, and will default to
|
45
|
+
# `true` in RSpec 4.
|
46
|
+
mocks.verify_partial_doubles = true
|
47
|
+
end
|
48
|
+
|
49
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
50
|
+
# have no way to turn it off -- the option exists only for backwards
|
51
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
52
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
53
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
54
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
55
|
+
|
56
|
+
# This allows you to limit a spec run to individual examples or groups
|
57
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
58
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
59
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
60
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
61
|
+
config.filter_run_when_matching :focus
|
62
|
+
|
63
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
64
|
+
# recommended. For more details, see:
|
65
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
66
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
67
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
68
|
+
config.disable_monkey_patching!
|
69
|
+
|
70
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
71
|
+
# be too noisy due to issues in dependencies.
|
72
|
+
config.warnings = true
|
73
|
+
|
74
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
75
|
+
# file, and it's useful to allow more verbose output when running an
|
76
|
+
# individual spec file.
|
77
|
+
if config.files_to_run.one?
|
78
|
+
# Use the documentation formatter for detailed output,
|
79
|
+
# unless a formatter has already been configured
|
80
|
+
# (e.g. via a command-line flag).
|
81
|
+
config.default_formatter = 'doc'
|
82
|
+
end
|
83
|
+
|
84
|
+
# Print the 10 slowest examples and example groups at the
|
85
|
+
# end of the spec run, to help surface which specs are running
|
86
|
+
# particularly slow.
|
87
|
+
# config.profile_examples = 10
|
88
|
+
|
89
|
+
# Run specs in random order to surface order dependencies. If you find an
|
90
|
+
# order dependency and want to debug it, you can fix the order by providing
|
91
|
+
# the seed, which is printed after each run.
|
92
|
+
# --seed 1234
|
93
|
+
config.order = :random
|
94
|
+
|
95
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
96
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
97
|
+
# test failures related to randomization by passing the same `--seed` value
|
98
|
+
# as the one that triggered the failure.
|
99
|
+
Kernel.srand config.seed
|
7
100
|
end
|
101
|
+
|
102
|
+
require 'modelish'
|
@@ -1,38 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Assumes the following have been defined
|
2
4
|
# let(:model) -- with a model initializer that calls init_options
|
3
5
|
# let(:property_name)
|
4
6
|
# let(:property_value)
|
5
7
|
# let(:default_value)
|
6
|
-
shared_examples_for 'a modelish property' do
|
8
|
+
RSpec.shared_examples_for 'a modelish property' do
|
7
9
|
subject { model }
|
8
10
|
|
9
|
-
let(:init_options) { {property_name => property_value} }
|
11
|
+
let(:init_options) { { property_name => property_value } }
|
10
12
|
|
11
|
-
it {
|
13
|
+
it { is_expected.to respond_to(property_name) }
|
12
14
|
|
13
|
-
describe
|
15
|
+
describe 'getter' do
|
14
16
|
subject { model.send(property_name) }
|
15
17
|
|
16
|
-
context
|
18
|
+
context 'without init options' do
|
17
19
|
let(:init_options) { nil }
|
18
20
|
|
19
|
-
it {
|
21
|
+
it { is_expected.to eq(default_value) }
|
20
22
|
end
|
21
23
|
|
22
|
-
context
|
23
|
-
it {
|
24
|
+
context 'with init options' do
|
25
|
+
it { is_expected.to eq(property_value) }
|
24
26
|
end
|
25
27
|
end
|
26
28
|
|
27
|
-
it {
|
29
|
+
it { is_expected.to respond_to("#{property_name}=".to_sym) }
|
28
30
|
|
29
|
-
describe
|
31
|
+
describe 'setter' do
|
30
32
|
subject { model.send("#{property_name}=", new_property_value) }
|
31
33
|
|
32
34
|
let(:new_property_value) { 'a new value' }
|
33
35
|
|
34
|
-
it
|
35
|
-
expect { subject }.to change{model.send(property_name)}
|
36
|
+
it 'changes the property value' do
|
37
|
+
expect { subject }.to change { model.send(property_name) }
|
38
|
+
.from(property_value).to(new_property_value)
|
36
39
|
end
|
37
40
|
end
|
38
41
|
end
|
@@ -1,10 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Assume the following are defined:
|
2
4
|
# model - the model on which the property is defined
|
3
5
|
# valid_string - a string with a valid value for the property
|
4
|
-
# valid_typed_value -
|
6
|
+
# valid_typed_value - instance of property_type that is valid for the property
|
5
7
|
# invalid_value - an object that cannot be translated to the property type
|
6
8
|
# default_value - the default value for the property
|
7
|
-
|
9
|
+
# error_type - defaults to ArgumentError
|
10
|
+
RSpec.shared_examples_for 'a typed property' do |prop_name, prop_type|
|
8
11
|
accessor = prop_name.to_sym
|
9
12
|
raw_accessor = "raw_#{prop_name}".to_sym
|
10
13
|
bang_accessor = "#{prop_name}!".to_sym
|
@@ -12,81 +15,82 @@ shared_examples_for "a typed property" do |prop_name, prop_type|
|
|
12
15
|
|
13
16
|
subject { model }
|
14
17
|
|
15
|
-
it {
|
16
|
-
its(prop_name) {
|
18
|
+
it { is_expected.to respond_to(accessor) }
|
19
|
+
its(prop_name) { is_expected.to eq(default_value) }
|
17
20
|
|
18
|
-
it {
|
19
|
-
its(raw_accessor) {
|
21
|
+
it { is_expected.to respond_to(raw_accessor) }
|
22
|
+
its(raw_accessor) { is_expected.to eq(default_value) }
|
20
23
|
|
21
|
-
it {
|
22
|
-
its(bang_accessor) {
|
24
|
+
it { is_expected.to respond_to(bang_accessor) }
|
25
|
+
its(bang_accessor) { is_expected.to eq(default_value) }
|
23
26
|
|
24
|
-
it {
|
27
|
+
it { is_expected.to respond_to(mutator) }
|
25
28
|
|
26
|
-
describe
|
29
|
+
describe 'property_type mapping' do
|
27
30
|
subject { model.class.property_types }
|
28
31
|
|
29
|
-
it {
|
30
|
-
it {
|
32
|
+
it { is_expected.to be }
|
33
|
+
it { is_expected.to have_key(prop_name) }
|
31
34
|
|
32
35
|
if prop_type
|
33
|
-
its([prop_name]) {
|
36
|
+
its([prop_name]) { is_expected.to eq(prop_type) }
|
34
37
|
else
|
35
|
-
its([prop_name]) {
|
38
|
+
its([prop_name]) { is_expected.to be_a Proc }
|
36
39
|
end
|
37
40
|
end
|
38
41
|
|
39
|
-
describe
|
42
|
+
describe 'assignment' do
|
40
43
|
before { model.send(mutator, property_value) }
|
41
44
|
|
42
|
-
context
|
45
|
+
context 'with nil' do
|
43
46
|
let(:property_value) { nil }
|
44
47
|
|
45
|
-
its(accessor) {
|
46
|
-
its(raw_accessor) {
|
48
|
+
its(accessor) { is_expected.to be_nil }
|
49
|
+
its(raw_accessor) { is_expected.to be_nil }
|
47
50
|
|
48
|
-
it
|
51
|
+
it 'does not raise an error when the bang accessor is invoked' do
|
49
52
|
expect { subject.send(bang_accessor) }.to_not raise_error
|
50
53
|
end
|
51
54
|
|
52
|
-
its(bang_accessor) {
|
55
|
+
its(bang_accessor) { is_expected.to be_nil }
|
53
56
|
end
|
54
57
|
|
55
|
-
context
|
58
|
+
context 'with valid string' do
|
56
59
|
let(:property_value) { valid_string }
|
57
60
|
|
58
|
-
its(accessor) {
|
59
|
-
its(raw_accessor) {
|
61
|
+
its(accessor) { is_expected.to eq(valid_typed_value) }
|
62
|
+
its(raw_accessor) { is_expected.to eq(property_value) }
|
60
63
|
|
61
|
-
it
|
64
|
+
it 'does not raise an error when the bang accessor is invoked' do
|
62
65
|
expect { subject.send(bang_accessor) }.to_not raise_error
|
63
66
|
end
|
64
67
|
|
65
|
-
its(bang_accessor) {
|
68
|
+
its(bang_accessor) { is_expected.to eq(valid_typed_value) }
|
66
69
|
end
|
67
70
|
|
68
|
-
context
|
71
|
+
context 'with valid typed value' do
|
69
72
|
let(:property_value) { valid_typed_value }
|
70
73
|
|
71
|
-
its(accessor) {
|
72
|
-
its(raw_accessor) {
|
74
|
+
its(accessor) { is_expected.to eq(property_value) }
|
75
|
+
its(raw_accessor) { is_expected.to eq(property_value) }
|
73
76
|
|
74
|
-
it
|
77
|
+
it 'does not raise an error when the bang accessor is invoked' do
|
75
78
|
expect { subject.send(bang_accessor) }.to_not raise_error
|
76
79
|
end
|
77
80
|
|
78
|
-
its(bang_accessor) {
|
81
|
+
its(bang_accessor) { is_expected.to eq(valid_typed_value) }
|
79
82
|
end
|
80
83
|
|
81
84
|
unless [String, Array, Symbol].include?(prop_type)
|
82
|
-
context
|
85
|
+
context 'with a value that cannot be converted' do
|
83
86
|
let(:property_value) { invalid_value }
|
84
87
|
|
85
|
-
its(accessor) {
|
86
|
-
its(raw_accessor) {
|
88
|
+
its(accessor) { is_expected.to eq(property_value) }
|
89
|
+
its(raw_accessor) { is_expected.to eq(property_value) }
|
87
90
|
|
88
|
-
it
|
89
|
-
|
91
|
+
it 'raises an error when the bang accessor is invoked' do
|
92
|
+
error_class = respond_to?(:error_type) ? error_type : ArgumentError
|
93
|
+
expect { subject.send(bang_accessor) }.to raise_error(error_class)
|
90
94
|
end
|
91
95
|
end
|
92
96
|
end
|
@@ -1,8 +1,10 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.shared_examples_for 'an unknown property handler' do
|
2
4
|
context 'when ignore_unknown_properties is set to false' do
|
3
5
|
let(:ignore_unknown_props) { false }
|
4
6
|
|
5
|
-
it '
|
7
|
+
it 'raises an error' do
|
6
8
|
expect { subject }.to raise_error(NoMethodError)
|
7
9
|
end
|
8
10
|
end
|
@@ -10,10 +12,10 @@ shared_examples_for 'an unknown property handler' do
|
|
10
12
|
context 'when ignore_unknown_properties is set to true' do
|
11
13
|
let(:ignore_unknown_props) { true }
|
12
14
|
|
13
|
-
it '
|
15
|
+
it 'does not raise an error' do
|
14
16
|
expect { subject }.to_not raise_error
|
15
17
|
end
|
16
18
|
|
17
|
-
it {
|
19
|
+
it { is_expected.to_not respond_to(unknown_prop) }
|
18
20
|
end
|
19
21
|
end
|
@@ -1,23 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Assumes that let(:model) has been defined
|
2
|
-
shared_examples_for 'a valid model' do
|
4
|
+
RSpec.shared_examples_for 'a valid model' do
|
3
5
|
subject { model }
|
4
6
|
|
5
|
-
it {
|
7
|
+
it { is_expected.to be_valid }
|
6
8
|
|
7
|
-
it {
|
9
|
+
it { is_expected.to respond_to(:validate) }
|
8
10
|
|
9
|
-
describe
|
11
|
+
describe '#validate' do
|
10
12
|
subject { model.validate }
|
11
13
|
|
12
|
-
it {
|
14
|
+
it { is_expected.to be_empty }
|
13
15
|
end
|
14
16
|
|
15
|
-
it {
|
17
|
+
it { is_expected.to respond_to(:validate!) }
|
16
18
|
|
17
|
-
describe
|
19
|
+
describe '#validate!' do
|
18
20
|
subject { model.validate! }
|
19
21
|
|
20
|
-
it
|
22
|
+
it 'does not raise any errors' do
|
21
23
|
expect { subject }.to_not raise_error
|
22
24
|
end
|
23
25
|
end
|
@@ -27,44 +29,48 @@ end
|
|
27
29
|
# model - the model on which the property has been defined
|
28
30
|
# property_name - the name of the property that is invalid
|
29
31
|
# error_count - the number of expected errors on the property
|
30
|
-
shared_examples_for 'a model with an invalid property' do
|
32
|
+
RSpec.shared_examples_for 'a model with an invalid property' do
|
31
33
|
subject { model }
|
32
34
|
|
33
|
-
it {
|
35
|
+
it { is_expected.to_not be_valid }
|
34
36
|
|
35
|
-
it {
|
37
|
+
it { is_expected.to respond_to(:validate) }
|
36
38
|
|
37
|
-
describe
|
39
|
+
describe '#validate' do
|
38
40
|
subject { errors }
|
39
41
|
let(:errors) { model.validate }
|
40
42
|
|
41
|
-
it {
|
43
|
+
it { is_expected.to have_key(property_name) }
|
42
44
|
|
43
|
-
describe
|
45
|
+
describe '[property_name]' do
|
44
46
|
subject { prop_errors }
|
45
47
|
let(:prop_errors) { errors[property_name] }
|
46
48
|
|
47
|
-
|
49
|
+
its(:size) { is_expected.to eq(error_count) }
|
48
50
|
|
49
|
-
it
|
50
|
-
prop_errors.each { |p| p.
|
51
|
+
it 'is a collection of ArgumentErrors' do
|
52
|
+
prop_errors.each { |p| expect(p).to be_an ArgumentError }
|
51
53
|
end
|
52
54
|
|
53
|
-
it
|
54
|
-
prop_errors.each
|
55
|
+
it 'references the property name in the error message(s)' do
|
56
|
+
prop_errors.each do |p|
|
57
|
+
expect(p.message).to match(/#{property_name}/i)
|
58
|
+
end
|
55
59
|
end
|
56
60
|
end
|
57
61
|
end
|
58
62
|
|
59
|
-
describe
|
63
|
+
describe '#validate!' do
|
60
64
|
subject { model.validate! }
|
61
65
|
|
62
|
-
it
|
66
|
+
it 'raise an ArgumentError' do
|
63
67
|
expect { subject }.to raise_error(ArgumentError)
|
64
68
|
end
|
65
69
|
|
66
|
-
it
|
67
|
-
expect { subject }.to raise_error
|
70
|
+
it 'references the property name in the error message' do
|
71
|
+
expect { subject }.to raise_error do |e|
|
72
|
+
expect(e.message).to match(/#{property_name}/i)
|
73
|
+
end
|
68
74
|
end
|
69
75
|
end
|
70
76
|
end
|