configurations 2.0.0.pre → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -7
  2. data/.travis.yml +4 -2
  3. data/License.txt +1 -1
  4. data/README.md +23 -7
  5. data/Rakefile +1 -1
  6. data/configurations.gemspec +7 -3
  7. data/lib/configurations.rb +8 -4
  8. data/lib/configurations/arbitrary.rb +124 -0
  9. data/lib/configurations/blank_object.rb +60 -0
  10. data/lib/configurations/configurable.rb +153 -42
  11. data/lib/configurations/configuration.rb +96 -213
  12. data/lib/configurations/strict.rb +170 -0
  13. data/test/configurations/arbitrary/test.rb +23 -0
  14. data/test/configurations/arbitrary/test_defaults.rb +5 -0
  15. data/test/configurations/arbitrary/test_hash_methods.rb +11 -0
  16. data/test/configurations/arbitrary/test_methods.rb +5 -0
  17. data/test/configurations/arbitrary/test_not_configured.rb +5 -0
  18. data/test/configurations/arbitrary/test_not_configured_default.rb +5 -0
  19. data/test/configurations/shared/defaults.rb +43 -0
  20. data/test/configurations/shared/hash_methods.rb +40 -0
  21. data/test/configurations/shared/kernel_methods.rb +9 -0
  22. data/test/configurations/shared/methods.rb +31 -0
  23. data/test/configurations/shared/not_configured_callbacks.rb +42 -0
  24. data/test/configurations/shared/not_configured_default_callback.rb +42 -0
  25. data/test/configurations/shared/properties.rb +46 -0
  26. data/test/configurations/shared/properties_outside_block.rb +40 -0
  27. data/test/configurations/shared/strict_hash_methods.rb +43 -0
  28. data/test/configurations/strict/test.rb +20 -0
  29. data/test/configurations/strict/test_defaults.rb +6 -0
  30. data/test/configurations/strict/test_hash_methods.rb +11 -0
  31. data/test/configurations/strict/test_methods.rb +6 -0
  32. data/test/configurations/strict/test_not_configured.rb +6 -0
  33. data/test/configurations/strict/test_not_configured_default.rb +6 -0
  34. data/test/configurations/strict_types/test.rb +18 -0
  35. data/test/configurations/strict_types/test_defaults.rb +6 -0
  36. data/test/configurations/strict_types/test_hash_methods.rb +11 -0
  37. data/test/configurations/strict_types/test_methods.rb +6 -0
  38. data/test/configurations/strict_types/test_not_configured.rb +6 -0
  39. data/test/configurations/strict_types/test_not_configured_default.rb +6 -0
  40. data/test/configurations/strict_types_with_blocks/test.rb +22 -0
  41. data/test/configurations/strict_types_with_blocks/test_defaults.rb +6 -0
  42. data/test/configurations/strict_types_with_blocks/test_hash_methods.rb +14 -0
  43. data/test/configurations/strict_types_with_blocks/test_methods.rb +6 -0
  44. data/test/configurations/strict_types_with_blocks/test_not_configured.rb +6 -0
  45. data/test/configurations/strict_types_with_blocks/test_not_configured_default.rb +6 -0
  46. data/test/configurations/strict_with_blocks/test.rb +16 -0
  47. data/test/configurations/strict_with_blocks/test_defaults.rb +6 -0
  48. data/test/configurations/strict_with_blocks/test_hash_methods.rb +14 -0
  49. data/test/configurations/strict_with_blocks/test_methods.rb +6 -0
  50. data/test/configurations/strict_with_blocks/test_not_configured.rb +6 -0
  51. data/test/configurations/strict_with_blocks/test_not_configured_default.rb +6 -0
  52. data/test/support/setup.rb +173 -0
  53. data/test/support/shared.rb +11 -0
  54. data/test/test_helper.rb +6 -5
  55. metadata +148 -65
  56. data/test/configurations/test_configurable_with_blocks_configuration.rb +0 -54
  57. data/test/configurations/test_configuration.rb +0 -182
  58. data/test/configurations/test_configuration_methods.rb +0 -85
  59. data/test/configurations/test_stricter_configuration.rb +0 -173
  60. data/test/support/testmodules.rb +0 -10
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+
3
+ class TestArbitrary < ConfigurationsTest
4
+ shares_tests :properties, :kernel_methods
5
+
6
+ def test_respond_to_on_writer_while_writeable
7
+ TestModule.configure do |c|
8
+ assert_respond_to :p1=, c
9
+ end
10
+ end
11
+
12
+ def test_respond_to_on_writer_when_not_writeable
13
+ refute_respond_to @configuration, :p1=
14
+ end
15
+
16
+ def test_undefined_property
17
+ assert_nil @configuration.p15
18
+ end
19
+
20
+ def test_respond_to_undefined_property
21
+ assert_respond_to @configuration, :p15
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ require 'test_helper'
2
+
3
+ class TestArbitraryWithDefaults < ConfigurationsTest
4
+ shares_tests :defaults
5
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class TestHashMethods < ConfigurationsTest
4
+ shares_tests :hash_methods
5
+
6
+ def test_from_h_raises_when_not_writeable
7
+ assert_raises ArgumentError do
8
+ @configuration.from_h({})
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ require 'test_helper'
2
+
3
+ class TestMethodsOnArbitrary < ConfigurationsTest
4
+ shares_tests :methods
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'test_helper'
2
+
3
+ class TestArbitraryNotConfigured < ConfigurationsTest
4
+ shares_tests :not_configured_callbacks
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'test_helper'
2
+
3
+ class TestArbitraryNotConfiguredDefault < ConfigurationsTest
4
+ shares_tests :not_configured_default_callback
5
+ end
@@ -0,0 +1,43 @@
1
+ module Tests
2
+ module Shared
3
+ module Defaults
4
+ def self.included(base)
5
+ base.setup_with :defaults do |c|
6
+ c.p2 = 23
7
+
8
+ c.p3.p4 = 'CONFIGURED P3P4'
9
+
10
+ c.module = -> { 'MODULE' }
11
+ end
12
+ end
13
+
14
+ def test_property
15
+ assert_equal 23, @configuration.p2
16
+ end
17
+
18
+ def test_property_with_default
19
+ assert_equal 'P1', @configuration.p1
20
+ end
21
+
22
+ def test_overwritten_property
23
+ assert_equal 'MODULE', @configuration.module.call
24
+ end
25
+
26
+ def test_overwritten_nested_property
27
+ assert_equal 'CONFIGURED P3P4', @configuration.p3.p4
28
+ end
29
+
30
+ def test_nested_property_with_default
31
+ assert_equal({ hash: :hash }, @configuration.p3.p5.p7)
32
+ end
33
+
34
+ def test_keywords_defaults
35
+ assert_equal :class, @configuration.class
36
+ end
37
+
38
+ def test_no_default_no_write
39
+ assert_nil @configuration.puts
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,40 @@
1
+ module Tests
2
+ module Shared
3
+ module HashMethods
4
+ def self.included(base)
5
+ base.setup_with do |c|
6
+ c.p1 = 'CONFIGURED P1'
7
+ c.p2 = 2
8
+ c.p3.p4 = 'CONFIGURED P3P4'
9
+ c.p3.p5.p6 = %w(P3 P5 P6)
10
+ c.p3.p5.p7 = { config: 'hash' }
11
+ c.class = :class
12
+ c.puts = Class
13
+ end
14
+ end
15
+
16
+ def test_to_h
17
+ assert_equal({
18
+ p1: 'CONFIGURED P1',
19
+ p2: 2,
20
+ p3: {
21
+ p4: 'CONFIGURED P3P4',
22
+ p5: {
23
+ p6: %w(P3 P5 P6),
24
+ p7: {
25
+ config: 'hash'
26
+ }
27
+ }
28
+ },
29
+ class: :class,
30
+ puts: Class
31
+ }, @configuration.to_h)
32
+ end
33
+
34
+ def test_from_h
35
+ old_to_h = @configuration.to_h.dup
36
+ assert_equal(old_to_h, @module.configure { |c| c.from_h(old_to_h) }.to_h)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,9 @@
1
+ module Tests
2
+ module Shared
3
+ module KernelMethods
4
+ def test_respond_to_kernel_method
5
+ assert_respond_to @configuration, :fail
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,31 @@
1
+ module Tests
2
+ module Shared
3
+ module Methods
4
+ def self.included(base)
5
+ base.setup_with :methods
6
+ end
7
+
8
+ def test_method
9
+ assert_equal ['CONFIGURED P1', 2], @configuration.method1.props
10
+ end
11
+
12
+ def test_method_with_context
13
+ assert_equal 'CONTEXTCONFIGURED P1', @configuration.method2
14
+ end
15
+
16
+ def test_method_with_arguments
17
+ assert_equal 'ARGCONFIGURED P1', @configuration.method3('ARG')
18
+ end
19
+
20
+ def test_nested_method
21
+ assert_equal({ a: :b, config: 'hash' }, @configuration.p3.p5.combination)
22
+ end
23
+
24
+ def test_kernel_methods
25
+ assert_raises NotImplementedError do
26
+ @configuration.kernel_raise
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,42 @@
1
+ module Tests
2
+ module Shared
3
+ module NotConfiguredCallbacks
4
+ def self.included(base)
5
+ base.setup_with :defaults, :not_configured_callbacks do |c|
6
+ c.p2 = 23
7
+ c.p3.p4 = 'CONFIGURED P3P4'
8
+
9
+ c.module = -> { 'MODULE' }
10
+ end
11
+ end
12
+
13
+ def test_configured_with_default
14
+ assert_equal 'P1', @configuration.p1
15
+ end
16
+
17
+ def test_configured_with_overwritten_default
18
+ assert_equal 23, @configuration.p2
19
+ end
20
+
21
+ def test_nested_configured_with_default
22
+ assert_equal({ hash: :hash }, @configuration.p3.p5.p7)
23
+ end
24
+
25
+ def test_nested_configured_with_overwritten_default
26
+ assert_equal 'CONFIGURED P3P4', @configuration.p3.p4
27
+ end
28
+
29
+ def test_not_configured
30
+ assert_raises NotImplementedError do
31
+ @configuration.puts
32
+ end
33
+ end
34
+
35
+ def test_nested_not_configured
36
+ assert_raises NotImplementedError do
37
+ @configuration.p3.p5.p6
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,42 @@
1
+ module Tests
2
+ module Shared
3
+ module NotConfiguredDefaultCallback
4
+ def self.included(base)
5
+ base.setup_with :defaults, :not_configured_default_callback do |c|
6
+ c.p2 = 23
7
+ c.p3.p4 = 'CONFIGURED P3P4'
8
+
9
+ c.module = -> { 'MODULE' }
10
+ end
11
+ end
12
+
13
+ def test_configured_with_default
14
+ assert_equal 'P1', @configuration.p1
15
+ end
16
+
17
+ def test_configured_with_overwritten_default
18
+ assert_equal 23, @configuration.p2
19
+ end
20
+
21
+ def test_nested_configured_with_default
22
+ assert_equal({ hash: :hash }, @configuration.p3.p5.p7)
23
+ end
24
+
25
+ def test_nested_configured_with_overwritten_default
26
+ assert_equal 'CONFIGURED P3P4', @configuration.p3.p4
27
+ end
28
+
29
+ def test_not_configured
30
+ assert_raises ArgumentError do
31
+ @configuration.puts
32
+ end
33
+ end
34
+
35
+ def test_nested_not_configured
36
+ assert_raises ArgumentError do
37
+ @configuration.p3.p5.p6
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,46 @@
1
+ module Tests
2
+ module Shared
3
+ module Properties
4
+ def self.included(base)
5
+ base.setup_with do |c|
6
+ c.p1 = 'CONFIGURED P1'
7
+ c.p2 = 2
8
+ c.p3.p4 = 'CONFIGURED P3P4'
9
+ c.p3.p5.p6 = %w(P3 P5 P6)
10
+ c.p3.p5.p7 = { config: 'hash' }
11
+ c.class = :class
12
+ c.module = ->(a) { a }
13
+ c.puts = Class
14
+ end
15
+ end
16
+
17
+ def test_property
18
+ assert_equal 'CONFIGURED P1', @configuration.p1
19
+ end
20
+
21
+ def test_nested_property
22
+ assert_equal 'CONFIGURED P3P4', @configuration.p3.p4
23
+ end
24
+
25
+ def test_nested_hash_property
26
+ assert_equal({ config: 'hash' }, @configuration.p3.p5.p7)
27
+ end
28
+
29
+ def test_keyword_property
30
+ assert_equal :class, @configuration.class
31
+ end
32
+
33
+ def test_kernel_method_property
34
+ assert_equal Class, @configuration.puts
35
+ end
36
+
37
+ def test_respond_to_property
38
+ assert_respond_to @configuration, :p1
39
+ end
40
+
41
+ def test_respond_to_nested_property
42
+ assert_respond_to @configuration.p3, :p4
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,40 @@
1
+ module Tests
2
+ module Shared
3
+ module PropertiesOutsideBlock
4
+ def self.included(base)
5
+ base.setup_with do |c|
6
+ c.p1 = 'CONFIGURED P1'
7
+ c.p2 = 2
8
+ c.p3.p4 = 'CONFIGURED P3P4'
9
+ c.p3.p5.p6 = %w(P3 P5 P6)
10
+ c.p3.p5.p7 = { config: 'hash' }
11
+ c.class = :class
12
+ c.module = ->(a) { a }
13
+ c.puts = Class
14
+ end
15
+ end
16
+
17
+ def test_respond_to_writer
18
+ assert_respond_to @configuration, :p1=
19
+ end
20
+
21
+ def test_respond_to_nested_writer
22
+ assert_respond_to @configuration.p3, :p4=
23
+ end
24
+
25
+ def test_not_respond_to_nested_configuration_writer
26
+ refute_respond_to @configuration, :p3=
27
+ end
28
+
29
+ def test_set_property_outside_block
30
+ @configuration.p1 = 'OUTSIDE BLOCK P1'
31
+ assert_equal 'OUTSIDE BLOCK P1', @configuration.p1
32
+ end
33
+
34
+ def test_set_nested_property_outside_block
35
+ @configuration.p3.p5.p6 = %w(OUTSIDE BLOCK P3 P5 P6)
36
+ assert_equal %w(OUTSIDE BLOCK P3 P5 P6), @configuration.p3.p5.p6
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,43 @@
1
+ module Tests
2
+ module Shared
3
+ module StrictHashMethods
4
+ def test_from_h
5
+ expected, input = expection_and_input
6
+ assert_equal(expected, @module.configure { |c| c.from_h(input) }.to_h)
7
+ end
8
+
9
+ def test_from_h_outside_block
10
+ expected, input = expection_and_input
11
+ assert_equal(expected, @configuration.from_h(input).to_h)
12
+ end
13
+
14
+ private
15
+
16
+ def expection_and_input
17
+ old_to_h = @configuration.to_h.dup
18
+ expected = deep_dup(old_to_h)
19
+
20
+ old_to_h[:p3][:p5][:p6].reverse!
21
+ expected[:puts] = Class
22
+
23
+ [expected, old_to_h]
24
+ end
25
+
26
+ def deep_dup(h)
27
+ h.reduce({}) do |hash, (k, v)|
28
+ hash[k] = if v.is_a?(Hash)
29
+ deep_dup(v)
30
+ else
31
+ begin
32
+ v.dup
33
+ rescue TypeError
34
+ v
35
+ end
36
+ end
37
+
38
+ hash
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class TestStrict < ConfigurationsTest
4
+ setup_with :strict
5
+ shares_tests :properties, :properties_outside_block, :kernel_methods
6
+
7
+ def test_respond_to_writer
8
+ assert_respond_to @configuration, :p1=
9
+ end
10
+
11
+ def test_undefined_property
12
+ assert_raises NoMethodError do
13
+ @configuration.p15
14
+ end
15
+ end
16
+
17
+ def test_respond_to_undefined_property
18
+ refute_respond_to @configuration, :p15
19
+ end
20
+ end
@@ -0,0 +1,6 @@
1
+ require 'test_helper'
2
+
3
+ class TestStrictWithDefaults < ConfigurationsTest
4
+ setup_with :strict
5
+ shares_tests :defaults
6
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class TestHashMethodsOnStrict < ConfigurationsTest
4
+ setup_with :strict
5
+ shares_tests :hash_methods
6
+
7
+ def test_from_h_outside_block
8
+ old_to_h = @configuration.to_h.dup
9
+ assert_equal(old_to_h, @configuration.from_h(old_to_h).to_h)
10
+ end
11
+ end