gorillib 0.4.1pre → 0.4.2pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. data/.gitignore +13 -10
  2. data/.rspec +1 -1
  3. data/.yardopts +1 -0
  4. data/CHANGELOG.md +47 -0
  5. data/Gemfile +22 -19
  6. data/Guardfile +23 -9
  7. data/README.md +12 -12
  8. data/Rakefile +29 -40
  9. data/VERSION +1 -1
  10. data/examples/benchmark/factories_benchmark.rb +87 -0
  11. data/examples/builder/ironfan.rb +1 -19
  12. data/examples/hash/slicing_methods.rb +101 -0
  13. data/gorillib.gemspec +36 -35
  14. data/lib/gorillib/array/deep_compact.rb +4 -3
  15. data/lib/gorillib/array/simple_statistics.rb +76 -0
  16. data/lib/gorillib/base.rb +0 -1
  17. data/lib/gorillib/builder.rb +15 -30
  18. data/lib/gorillib/collection.rb +159 -57
  19. data/lib/gorillib/collection/model_collection.rb +136 -43
  20. data/lib/gorillib/datetime/parse.rb +4 -2
  21. data/lib/gorillib/{array → deprecated/array}/average.rb +0 -0
  22. data/lib/gorillib/{array → deprecated/array}/random.rb +2 -1
  23. data/lib/gorillib/{array → deprecated/array}/sorted_median.rb +0 -0
  24. data/lib/gorillib/{array → deprecated/array}/sorted_percentile.rb +0 -0
  25. data/lib/gorillib/deprecated/array/sorted_sample.rb +13 -0
  26. data/lib/gorillib/{metaprogramming → deprecated/metaprogramming}/aliasing.rb +0 -0
  27. data/lib/gorillib/enumerable/sum.rb +3 -3
  28. data/lib/gorillib/exception/raisers.rb +92 -22
  29. data/lib/gorillib/factories.rb +550 -0
  30. data/lib/gorillib/hash/mash.rb +15 -58
  31. data/lib/gorillib/hashlike/deep_compact.rb +2 -2
  32. data/lib/gorillib/hashlike/slice.rb +55 -40
  33. data/lib/gorillib/model.rb +5 -3
  34. data/lib/gorillib/model/base.rb +33 -119
  35. data/lib/gorillib/model/defaults.rb +58 -14
  36. data/lib/gorillib/model/errors.rb +10 -0
  37. data/lib/gorillib/model/factories.rb +1 -367
  38. data/lib/gorillib/model/field.rb +40 -18
  39. data/lib/gorillib/model/fixup.rb +16 -0
  40. data/lib/gorillib/model/positional_fields.rb +35 -0
  41. data/lib/gorillib/model/schema_magic.rb +162 -0
  42. data/lib/gorillib/model/serialization.rb +1 -2
  43. data/lib/gorillib/model/serialization/csv.rb +59 -0
  44. data/lib/gorillib/pathname.rb +19 -8
  45. data/lib/gorillib/some.rb +2 -0
  46. data/lib/gorillib/string/constantize.rb +17 -10
  47. data/lib/gorillib/string/inflector.rb +11 -7
  48. data/lib/gorillib/type/boolean.rb +40 -0
  49. data/lib/gorillib/type/extended.rb +76 -40
  50. data/lib/gorillib/type/url.rb +6 -4
  51. data/lib/gorillib/utils/console.rb +1 -18
  52. data/lib/gorillib/utils/edge_cases.rb +18 -0
  53. data/spec/examples/builder/ironfan_spec.rb +5 -10
  54. data/spec/gorillib/array/compact_blank_spec.rb +36 -21
  55. data/spec/gorillib/array/simple_statistics_spec.rb +143 -0
  56. data/spec/gorillib/builder_spec.rb +16 -20
  57. data/spec/gorillib/collection_spec.rb +131 -35
  58. data/spec/gorillib/exception/raisers_spec.rb +39 -0
  59. data/spec/gorillib/hash/deep_compact_spec.rb +3 -3
  60. data/spec/gorillib/model/{record/defaults_spec.rb → defaults_spec.rb} +5 -1
  61. data/spec/gorillib/model/factories_spec.rb +335 -0
  62. data/spec/gorillib/model/{record/overlay_spec.rb → overlay_spec.rb} +0 -0
  63. data/spec/gorillib/model/serialization_spec.rb +2 -2
  64. data/spec/gorillib/model_spec.rb +19 -18
  65. data/spec/gorillib/pathname_spec.rb +7 -7
  66. data/spec/gorillib/string/truncate_spec.rb +3 -13
  67. data/spec/gorillib/type/extended_spec.rb +50 -2
  68. data/spec/gorillib/utils/capture_output_spec.rb +1 -1
  69. data/spec/spec_helper.rb +10 -7
  70. data/spec/support/factory_test_helpers.rb +76 -0
  71. data/spec/support/gorillib_test_helpers.rb +36 -24
  72. data/spec/support/model_test_helpers.rb +39 -2
  73. metadata +86 -51
  74. data/lib/alt/kernel/call_stack.rb +0 -56
  75. data/lib/gorillib/array/sorted_sample.rb +0 -12
  76. data/lib/gorillib/builder/field.rb +0 -5
  77. data/lib/gorillib/collection/has_collection.rb +0 -31
  78. data/lib/gorillib/collection/list_collection.rb +0 -58
  79. data/lib/gorillib/exception/confidence.rb +0 -17
  80. data/lib/gorillib/io/system_helpers.rb +0 -30
  81. data/lib/gorillib/model/record_schema.rb +0 -9
  82. data/lib/gorillib/utils/stub_module.rb +0 -33
  83. data/spec/array/average_spec.rb +0 -24
  84. data/spec/array/sorted_median_spec.rb +0 -18
  85. data/spec/array/sorted_percentile_spec.rb +0 -24
  86. data/spec/array/sorted_sample_spec.rb +0 -28
  87. data/spec/gorillib/metaprogramming/aliasing_spec.rb +0 -180
  88. data/spec/gorillib/model/record/factories_spec.rb +0 -335
  89. data/spec/support/kcode_test_helper.rb +0 -16
@@ -1,17 +0,0 @@
1
- module Kernel
2
- def assert(value, message="Assertion failed", error=StandardError)
3
- raise error, message, caller unless value
4
- end
5
- end
6
-
7
- class NullObject
8
- def method_missing(*args, &block)
9
- self
10
- end
11
-
12
- def nil?; true; end
13
- end
14
-
15
- def Maybe(value)
16
- value.nil? ? NullObject.new : value
17
- end
@@ -1,30 +0,0 @@
1
- module Gorillib::CheckedPopen
2
- module_function
3
-
4
- def checked_popen(command, mode, fail_action, io_class=IO)
5
- check_child_exit_status do
6
- io_class.popen(command, mode) do |process|
7
- yield(process)
8
- end
9
- end
10
- rescue Errno::EPIPE
11
- fail_action.call
12
- end
13
-
14
- # @private
15
- NO_EXIT_STATUS = OpenStruct.new(:exitstatus => 0)
16
-
17
- def check_child_exit_status
18
- result = yield
19
- status = $? || NO_EXIT_STATUS
20
- unless [0, 172].include?(status.exitstatus)
21
- raise ArgumentError, "Command exited with status '#{status.exitstatus}'"
22
- end
23
- result
24
- end
25
-
26
- end
27
-
28
- ::IO.class_eval do
29
- include Gorillib::CheckedPopen
30
- end
@@ -1,9 +0,0 @@
1
- module Gorillib
2
- module Model
3
- class RecordSchema
4
- field :name, String
5
- field :doc, String
6
- field :type, Factory
7
- end
8
- end
9
- end
@@ -1,33 +0,0 @@
1
- # spec/spec_helper_lite.rb
2
-
3
- # Conditionally creates empty or "stub" modules only if
4
- # a) they are not already defined; and
5
- # b) they are not auto-loadable.
6
- #
7
- # From http://objectsonrails.com/#sec-7-1
8
- #
9
- # @example Faking out ActiveModel
10
- # # ...
11
- # require_relative '../spec_helper_lite'
12
- # stub_module 'ActiveModel::Conversion'
13
- # stub_module 'ActiveModel::Naming'
14
- # require_relative '../../app/models/post'
15
- # # ...
16
- #
17
- def stub_module(full_name)
18
- # Uses #const_get to attempt to reference the given module. If the module is
19
- # defined, or if calling #const_get causes it to be auto-loaded, the method
20
- # does nothing more. But if #const_get fails to turn up the module, it defines
21
- # an anonymous empty module to act as a placeholder.
22
- full_name.to_s.split(/::/).inject(Object) do |context, name|
23
- begin
24
- context.const_get(name)
25
- rescue NameError
26
- context.const_set(name, Module.new)
27
- end
28
- end
29
- end
30
-
31
-
32
- # refute
33
- # does_not_allow
@@ -1,24 +0,0 @@
1
- require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
- require 'gorillib/array/average'
3
-
4
- describe Array do
5
- describe '#average' do
6
- context 'on non-float array element' do
7
- it 'raises error' do
8
- expect { [0.0, :b, 1.0].average }.should raise_error(ArgumentError)
9
- end
10
- end
11
-
12
- context 'with empty' do
13
- it 'returns nil' do
14
- [].average.should be_nil
15
- end
16
- end
17
-
18
- context 'given a numerical array' do
19
- it 'returns the average of the elements' do
20
- (1..10).to_a.average.should == 5.5
21
- end
22
- end
23
- end
24
- end
@@ -1,18 +0,0 @@
1
- require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
- require 'gorillib/array/sorted_median'
3
-
4
- describe Array do
5
- describe '#sorted_median' do
6
- context 'with empty' do
7
- it 'returns nil' do
8
- [].sorted_median.should be_nil
9
- end
10
- end
11
-
12
- context 'given any array' do
13
- it 'returns the middle element of odd-sized arrays' do
14
- ("a".."y").to_a.sorted_median.should == "m"
15
- end
16
- end
17
- end
18
- end
@@ -1,24 +0,0 @@
1
- require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
- require 'gorillib/array/sorted_percentile'
3
-
4
- describe Array do
5
- describe '#sorted_percentile' do
6
- context 'with empty' do
7
- it 'returns nil' do
8
- [].sorted_percentile(0.0).should be_nil
9
- end
10
- end
11
-
12
- context 'given any array' do
13
- it 'returns the element closest to the given percentile' do
14
- ("a".."y").to_a.sorted_percentile( 0.0).should == "a"
15
- ("a".."y").to_a.sorted_percentile( 50.0).should == "m"
16
- ("a".."y").to_a.sorted_percentile(100.0).should == "y"
17
- end
18
- end
19
-
20
- # (Please do not define behavior for two elements equally close to
21
- # a given percentile.)
22
-
23
- end
24
- end
@@ -1,28 +0,0 @@
1
- require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
- require 'gorillib/array/sorted_sample'
3
-
4
- describe Array do
5
- describe '#sorted_median' do
6
- context 'with empty' do
7
- it 'returns an empty array' do
8
- [].sorted_sample(1).should be_empty
9
- end
10
- end
11
-
12
- context 'given an undersized array' do
13
- it 'does not return the same element more than once' do
14
- ("a".."z").to_a.sorted_sample(27).should == ("a".."z").to_a
15
- end
16
- end
17
-
18
- context 'given any array' do
19
- it ('returns a sample of the given size as close to evenly ' \
20
- 'distributed over the array as possible') do
21
- sample = (1..100).to_a.sorted_sample(26)
22
- deltas = sample[0..-2].zip(sample[1..-1]).map{|a,b| b-a}
23
- deltas.max.should <= 4
24
- deltas.min.should >= 3
25
- end
26
- end
27
- end
28
- end
@@ -1,180 +0,0 @@
1
- require 'spec_helper'
2
- require 'gorillib/metaprogramming/aliasing'
3
-
4
- module BarMethodAliaser
5
- def self.included(foo_class)
6
- foo_class.class_eval do
7
- include BarMethods
8
- alias_method_chain :bar, :baz
9
- end
10
- end
11
- end
12
-
13
- module BarMethods
14
- def bar_with_baz
15
- bar_without_baz << '_with_baz'
16
- end
17
-
18
- def quux_with_baz!
19
- quux_without_baz! << '_with_baz'
20
- end
21
-
22
- def quux_with_baz?
23
- false
24
- end
25
-
26
- def quux_with_baz=(v)
27
- send(:quux_without_baz=, v) << '_with_baz'
28
- end
29
-
30
- def duck_with_orange
31
- duck_without_orange << '_with_orange'
32
- end
33
- end
34
-
35
- describe 'metaprogramming', :metaprogramming_spec => true do
36
- before do
37
- Object.const_set :FooClassWithBarMethod, Class.new{ def bar() 'bar' end }
38
- @instance = FooClassWithBarMethod.new
39
- end
40
-
41
- after do
42
- Object.instance_eval { remove_const :FooClassWithBarMethod }
43
- end
44
-
45
- describe 'alias_method_chain' do
46
-
47
- it 'does not have an effect if already provided by another library.' unless ENV['QUIET_RSPEC']
48
-
49
- it 'creates a with_ and without_ method that chain' do
50
- @instance.should respond_to(:bar)
51
- feature_aliases = [:bar_with_baz, :bar_without_baz]
52
-
53
- feature_aliases.each do |method|
54
- @instance.should_not respond_to(method)
55
- end
56
- @instance.bar.should == 'bar'
57
-
58
- FooClassWithBarMethod.class_eval{ include BarMethodAliaser }
59
- feature_aliases.each do |method|
60
- @instance.should respond_to(method)
61
- end
62
- @instance.bar.should == 'bar_with_baz'
63
- @instance.bar_without_baz.should == 'bar'
64
- end
65
-
66
- it 'with bang' do
67
- FooClassWithBarMethod.class_eval do
68
- def quux!; 'quux' end
69
- end
70
-
71
- @instance.should_not respond_to(:quux_with_baz!)
72
- FooClassWithBarMethod.class_eval do
73
- include BarMethodAliaser
74
- alias_method_chain :quux!, :baz
75
- end
76
- @instance.should respond_to(:quux_with_baz!)
77
-
78
- @instance.quux!.should == 'quux_with_baz'
79
- @instance.quux_without_baz!.should == 'quux'
80
- end
81
-
82
- it 'with same names between predicates and bang methods' do
83
- FooClassWithBarMethod.class_eval do
84
- def quux!; 'quux!' end
85
- def quux?; true end
86
- def quux=(v); 'quux=' end
87
- end
88
-
89
- @instance.should_not respond_to(:quux_with_baz!)
90
- @instance.should_not respond_to(:quux_with_baz?)
91
- @instance.should_not respond_to(:quux_with_baz=)
92
-
93
- FooClassWithBarMethod.class_eval { include BarMethodAliaser }
94
- @instance.should respond_to(:quux_with_baz!)
95
- @instance.should respond_to(:quux_with_baz?)
96
- @instance.should respond_to(:quux_with_baz=)
97
-
98
- FooClassWithBarMethod.alias_method_chain :quux!, :baz
99
- @instance.quux!.should == 'quux!_with_baz'
100
- @instance.quux_without_baz!.should == 'quux!'
101
-
102
- FooClassWithBarMethod.alias_method_chain :quux?, :baz
103
- @instance.quux?.should == false
104
- @instance.quux_without_baz?.should == true
105
-
106
- FooClassWithBarMethod.alias_method_chain :quux=, :baz
107
- @instance.send(:quux=, 1234).should == 'quux=_with_baz'
108
- @instance.send(:quux_without_baz=, 1234).should == 'quux='
109
- end
110
-
111
- it 'with_feature_punctuation' do
112
- FooClassWithBarMethod.class_eval do
113
- def quux; 'quux' end
114
- def quux?; 'quux?' end
115
- include BarMethodAliaser
116
- alias_method_chain :quux, :baz!
117
- end
118
-
119
- @instance.quux_with_baz!.should == 'quux_with_baz'
120
-
121
- lambda{ FooClassWithBarMethod.alias_method_chain :quux?, :baz! }.should raise_error(NameError)
122
- end
123
-
124
- it 'yields target and punctuation' do
125
- args = nil
126
- FooClassWithBarMethod.class_eval do
127
- def quux?; end
128
- include BarMethods
129
-
130
- FooClassWithBarMethod.alias_method_chain :quux?, :baz do |target, punctuation|
131
- args = [target, punctuation]
132
- end
133
- end
134
-
135
- args.should_not be_nil
136
- args[0].should == 'quux'
137
- args[1].should == '?'
138
- end
139
-
140
- it 'preserves private method status' do
141
- FooClassWithBarMethod.class_eval do
142
- def duck; 'duck' end
143
- include BarMethodAliaser
144
- private :duck
145
- alias_method_chain :duck, :orange
146
- end
147
-
148
- lambda{ @instance.duck }.should raise_error(NoMethodError)
149
-
150
- @instance.instance_eval{ duck }.should == 'duck_with_orange'
151
- FooClassWithBarMethod.should be_private_method_defined(:duck)
152
- end
153
-
154
- it 'preserves protected method status' do
155
- FooClassWithBarMethod.class_eval do
156
- def duck; 'duck' end
157
- include BarMethodAliaser
158
- protected :duck
159
- alias_method_chain :duck, :orange
160
- end
161
-
162
- lambda{ @instance.duck }.should raise_error(NoMethodError)
163
-
164
- @instance.instance_eval{ duck }.should == 'duck_with_orange'
165
- FooClassWithBarMethod.should be_protected_method_defined(:duck)
166
- end
167
-
168
- it 'preserves public method status' do
169
- FooClassWithBarMethod.class_eval do
170
- def duck; 'duck' end
171
- include BarMethodAliaser
172
- public :duck
173
- alias_method_chain :duck, :orange
174
- end
175
-
176
- @instance.duck.should == 'duck_with_orange'
177
- FooClassWithBarMethod.should be_public_method_defined(:duck)
178
- end
179
- end
180
- end
@@ -1,335 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'gorillib/object/blank'
4
- require 'gorillib/object/try_dup'
5
- require 'gorillib/hash/slice'
6
- require 'gorillib/metaprogramming/class_attribute'
7
- require 'gorillib/string/inflector'
8
- #
9
- require 'gorillib/collection'
10
- require 'gorillib/model/factories'
11
-
12
- describe '', :model_spec => true do
13
- let(:mock_factory){ Gorillib::Factory::BaseFactory.new }
14
-
15
- def test_factory(*args, &block)
16
- described_class.new(*args, &block)
17
- end
18
-
19
- describe Gorillib::Factory do
20
- describe 'Factory()' do
21
- it 'turns a proc into an ApplyProcFactory' do
22
- ff = Gorillib::Factory( ->(obj){ "bob says #{obj}" } )
23
- ff.receive(3).should == "bob says 3"
24
- end
25
- it 'returns anything that responds to #receive directly' do
26
- ff = Object.new ; ff.define_singleton_method(:receive){}
27
- Gorillib::Factory(ff).should equal(ff)
28
- end
29
- it 'returns a factory directly' do
30
- ff = Gorillib::Factory::SymbolFactory.new
31
- Gorillib::Factory(ff).should equal(ff)
32
- end
33
- it 'does not look up factory **classes**' do
34
- ->{ Gorillib::Factory(Gorillib::Factory::SymbolFactory) }.should raise_error(ArgumentError, /Don\'t know which factory makes/)
35
- end
36
- it 'looks up factories by typename' do
37
- Gorillib::Factory(:symbol ).should be_a(Gorillib::Factory::SymbolFactory)
38
- Gorillib::Factory(:identical).should == (::Whatever)
39
- end
40
- it 'looks up factories by class' do
41
- Gorillib::Factory(Symbol).should be_a(Gorillib::Factory::SymbolFactory)
42
- Gorillib::Factory(String).should be_a(Gorillib::Factory::StringFactory)
43
- end
44
- it 'calls Gorillib::Factory.receive' do
45
- x = mock
46
- Gorillib::Factory.should_receive(:receive).with(x)
47
- Gorillib::Factory(x)
48
- end
49
- end
50
- end
51
-
52
- shared_examples_for :it_converts do |conversion_mapping|
53
- non_native_ok = conversion_mapping.delete(:non_native_ok)
54
- conversion_mapping.each do |obj, expected_result|
55
- it "#{obj.inspect} to #{expected_result.inspect}" do
56
- subject.native?( obj).should be_false
57
- subject.blankish?(obj).should be_false
58
- actual_result = subject.receive(obj)
59
- actual_result.should eql(expected_result)
60
- unless non_native_ok then subject.native?(actual_result).should be_true ; end
61
- end
62
- end
63
- end
64
-
65
- shared_examples_for :it_considers_native do |*native_objs|
66
- it native_objs.inspect do
67
- native_objs.each do |obj|
68
- subject.native?( obj).should be_true
69
- actual_result = subject.receive(obj)
70
- actual_result.should equal(obj)
71
- end
72
- end
73
- end
74
-
75
- shared_examples_for :it_considers_blankish do |*blankish_objs|
76
- it blankish_objs.inspect do
77
- blankish_objs.each do |obj|
78
- subject.blankish?(obj).should be_true
79
- subject.receive(obj).should be_nil
80
- end
81
- end
82
- end
83
-
84
- shared_examples_for :it_is_a_mismatch_for do |*mismatched_objs|
85
- it mismatched_objs.inspect do
86
- mismatched_objs.each do |obj|
87
- ->{ subject.receive(obj) }.should raise_error(Gorillib::Factory::FactoryMismatchError)
88
- end
89
- end
90
- end
91
-
92
- shared_examples_for :it_is_registered_as do |*keys|
93
- it "the factory for #{keys}" do
94
- keys.each do |key|
95
- Gorillib::Factory(key).should be_a(described_class)
96
- end
97
- its_factory = Gorillib::Factory(keys.first)
98
- Gorillib::Factory.send(:factories).to_hash.select{|key,val| val.equal?(its_factory) }.keys.should == keys
99
- end
100
- end
101
-
102
- # __________________________________________________________________________
103
- #
104
- #
105
- #
106
- describe Gorillib::Factory::StringFactory do
107
- it_behaves_like :it_considers_native, 'foo', ''
108
- it_behaves_like :it_converts, :foo => 'foo', 3 => '3', false => "false", [] => "[]"
109
- it_behaves_like :it_considers_blankish, nil
110
- it_behaves_like :it_is_registered_as, :string, String
111
- its(:typename){ should == :string }
112
- end
113
-
114
- describe Gorillib::Factory::SymbolFactory do
115
- it_behaves_like :it_considers_native, :foo, :"symbol :with weird chars"
116
- it_behaves_like :it_converts, 'foo' => :foo
117
- it_behaves_like :it_considers_blankish, nil, ""
118
- it_behaves_like :it_is_a_mismatch_for, 3, false, []
119
- it_behaves_like :it_is_registered_as, :symbol, Symbol
120
- its(:typename){ should == :symbol }
121
- end
122
-
123
- describe Gorillib::Factory::RegexpFactory do
124
- it_behaves_like :it_considers_native, /foo/, //
125
- it_behaves_like :it_converts, 'foo' => /foo/, ".*" => /.*/
126
- it_behaves_like :it_considers_blankish, nil, ""
127
- it_behaves_like :it_is_a_mismatch_for, :foo, 3, false, []
128
- it_behaves_like :it_is_registered_as, :regexp, Regexp
129
- its(:typename){ should == :regexp }
130
- end
131
-
132
- describe Gorillib::Factory::IntegerFactory do
133
- it_behaves_like :it_considers_native, 1, -1
134
- it_behaves_like :it_converts, 'one' => 0, '3blindmice' => 3, "0x10" => 0
135
- it_behaves_like :it_converts, '1.0' => 1, '1' => 1, "0" => 0, "0L" => 0, "1_234_567" => 1234567, "1_234_567.0" => 1234567
136
- it_behaves_like :it_converts, 1.0 => 1, 1.234567e6 => 1234567, Complex(1,0) => 1
137
- it_behaves_like :it_considers_blankish, nil, ""
138
- it_behaves_like :it_is_a_mismatch_for, :foo, false, [], Complex(1,3)
139
- it_behaves_like :it_is_registered_as, :int, :integer, Integer
140
- its(:typename){ should == :integer }
141
- end
142
-
143
- describe Gorillib::Factory::FloatFactory do
144
- it_behaves_like :it_considers_native, 1.0, 1.234567e6
145
- it_behaves_like :it_converts, 'one' => 0.0, '3blindmice' => 3.0, "0x10" => 0.0
146
- it_behaves_like :it_converts, '1.0' => 1.0, '1' => 1.0, "0" => 0.0, "0L" => 0.0, "1_234_567" => 1234567.0, "1_234_567.0" => 1234567.0
147
- it_behaves_like :it_converts, 1 => 1.0, -1 => -1.0, Complex(1,0) => 1.0
148
- it_behaves_like :it_considers_blankish, nil, ""
149
- it_behaves_like :it_is_a_mismatch_for, :foo, false, []
150
- it_behaves_like :it_is_registered_as, :float, Float
151
- its(:typename){ should == :float }
152
- end
153
-
154
- describe Gorillib::Factory::ComplexFactory do
155
- cplx0 = Complex(0) ; cplx1 = Complex(1) ; cplx1f = Complex(1.0) ;
156
- it_behaves_like :it_considers_native, Complex(1,3), Complex(1,0)
157
- it_behaves_like :it_converts, 'one' => cplx0, '3blindmice' => Complex(3), "0x10" => cplx0
158
- it_behaves_like :it_converts, '1.0' => cplx1f, '1' => cplx1, '0' => cplx0, '0L' => cplx0, '1_234_567' => Complex(1234567), '1_234_567.0' => Complex(1234567.0)
159
- it_behaves_like :it_converts, 1.0 => cplx1f, 1 => cplx1, -1 => Complex(-1), Rational(3,2) => Complex(Rational(3,2),0)
160
- it_behaves_like :it_considers_blankish, nil, ""
161
- it_behaves_like :it_is_a_mismatch_for, :foo, false, []
162
- it_behaves_like :it_is_registered_as, :complex, Complex
163
- its(:typename){ should == :complex }
164
- end
165
-
166
- describe Gorillib::Factory::RationalFactory do
167
- rat_0 = Rational(0) ; rat1 = Rational(1) ; rat3_2 = Rational(3, 2) ;
168
- it_behaves_like :it_considers_native, Rational(1, 3), Rational(1, 7)
169
- it_behaves_like :it_converts, 'one' => rat_0, '3blindmice' => Rational(3), "0x10" => rat_0
170
- it_behaves_like :it_converts, '1.5' => rat3_2, '1' => rat1, '0' => rat_0, '0L' => rat_0, '1_234_567' => Rational(1234567), '1_234_567.0' => Rational(1234567)
171
- it_behaves_like :it_converts, 1.5 => rat3_2, 1 => rat1, -1 => Rational(-1), Complex(1.5) => rat3_2
172
- it_behaves_like :it_considers_blankish, nil, ""
173
- it_behaves_like :it_is_a_mismatch_for, :foo, false, [], Complex(1.5,3)
174
- it_behaves_like :it_is_registered_as, :rational, Rational
175
- its(:typename){ should == :rational }
176
- end
177
-
178
- describe Gorillib::Factory::BooleanFactory do
179
- it_behaves_like :it_considers_native, true, false
180
- it_behaves_like :it_considers_blankish, nil
181
- it_behaves_like :it_converts, "false" => false, :false => false
182
- it_behaves_like :it_converts, "true" => true, :true => true, [] => true, :foo => true, [] => true, Complex(1.5,3) => true, Object.new => true
183
- it_behaves_like :it_is_registered_as, :boolean
184
- its(:typename){ should == :boolean }
185
- end
186
-
187
- describe ::Whatever do
188
- it_behaves_like :it_considers_native, true, false, nil, 3, '', 'a string', :a_symbol, [], {}, ->(){ 'a proc' }, Module.new, Complex(1,3), Object.new
189
- it "it is itself the factory for :identical and :whatever" do
190
- keys = [Whatever, :identical, :whatever]
191
- keys.each do |key|
192
- Gorillib::Factory(key).should equal(described_class)
193
- end
194
- its_factory = ::Whatever
195
- Gorillib::Factory.send(:factories).to_hash.select{|key,val| val.equal?(its_factory) }.keys.should == keys
196
- end
197
- end
198
- describe Gorillib::Factory::IdenticalFactory do
199
- it{ described_class.should equal(Whatever) }
200
- end
201
-
202
- describe Gorillib::Factory::ModuleFactory do
203
- it_behaves_like :it_considers_blankish, nil
204
- it_behaves_like :it_considers_native, Module, Module.new, Class, Class.new, Object, String, BasicObject
205
- it_behaves_like :it_is_a_mismatch_for, true, false, 3, '', 'a string', :a_symbol, [], {}, ->(){ 'a proc' }, Complex(1,3), Object.new
206
- it_behaves_like :it_is_registered_as, :module, Module
207
- its(:typename){ should == :module }
208
- end
209
-
210
- describe Gorillib::Factory::ClassFactory do
211
- it_behaves_like :it_considers_blankish, nil
212
- it_behaves_like :it_considers_native, Module, Class, Class.new, Object, String, BasicObject
213
- it_behaves_like :it_is_a_mismatch_for, true, false, 3, '', 'a string', :a_symbol, [], {}, ->(){ 'a proc' }, Module.new, Complex(1,3), Object.new
214
- it_behaves_like :it_is_registered_as, :class, Class
215
- its(:typename){ should == :class }
216
- end
217
-
218
- describe Gorillib::Factory::NilFactory do
219
- it_behaves_like :it_considers_native, nil
220
- it_behaves_like :it_is_a_mismatch_for, true, false, 3, '', 'a string', :a_symbol, [], {}, ->(){ 'a proc' }, Module.new, Complex(1,3), Object.new
221
- it_behaves_like :it_is_registered_as, :nil, NilClass
222
- its(:typename){ should == :nil_class }
223
- end
224
-
225
- describe Gorillib::Factory::TrueFactory do
226
- it_behaves_like :it_considers_native, true
227
- it_behaves_like :it_is_a_mismatch_for, false, 3, '', 'a string', :a_symbol, [], {}, ->(){ 'a proc' }, Module.new, Complex(1,3), Object.new
228
- it_behaves_like :it_is_registered_as, :true, TrueClass
229
- end
230
-
231
- describe Gorillib::Factory::FalseFactory do
232
- it_behaves_like :it_considers_native, false
233
- it_behaves_like :it_is_a_mismatch_for, true, 3, '', 'a string', :a_symbol, [], {}, ->(){ 'a proc' }, Module.new, Complex(1,3), Object.new
234
- it_behaves_like :it_is_registered_as, :false, FalseClass
235
- end
236
-
237
- describe Gorillib::Factory::RangeFactory do
238
- it_behaves_like :it_considers_blankish, nil, []
239
- it_behaves_like :it_considers_native, (1..2), ('a'..'z')
240
- it_behaves_like :it_is_a_mismatch_for, true, 3, '', 'a string', :a_symbol, [1,2], {}, ->(){ 'a proc' }, Module.new, Complex(1,3), Object.new
241
- it_behaves_like :it_is_registered_as, :range, Range
242
- its(:typename){ should == :range }
243
- end
244
-
245
- # __________________________________________________________________________
246
-
247
- # hand it a collection with entries 1, 2, 3 please
248
- shared_examples_for :an_enumerable_factory do
249
- it "accepts a factory for its items" do
250
- mock_factory.should_receive(:receive).with(1)
251
- mock_factory.should_receive(:receive).with(2)
252
- mock_factory.should_receive(:receive).with(3)
253
- factory = test_factory(:items => mock_factory)
254
- factory.receive( collection_123 )
255
- end
256
- it "can generate an empty collection" do
257
- subject.empty_product.should == empty_collection
258
- end
259
- it "lets you override the empty collection" do
260
- ep = mock; ep.should_receive(:try_dup).and_return 'hey'
261
- subject = test_factory(:empty_product => ep)
262
- subject.empty_product.should == 'hey'
263
- subject = test_factory(:empty_product => ->{ 'yo' })
264
- subject.empty_product.should == 'yo'
265
- end
266
- end
267
-
268
- describe Gorillib::Factory::HashFactory do
269
- let(:collection_123){ { 'a' => 1, :b => 2, 'c' => 3 } }
270
- let(:empty_collection){ {} }
271
-
272
- it_behaves_like :it_considers_blankish, nil
273
- it_behaves_like :it_converts, { {} => {}, { "a" => 2 } => { 'a' => 2 }, { :a => 2 } => { :a => 2 }, :non_native_ok => true }
274
- it_behaves_like :it_is_a_mismatch_for, [1,2,3]
275
- it_behaves_like :an_enumerable_factory
276
-
277
- it 'follows examples' do
278
- test_factory.receive(collection_123).should == { 'a' => 1, :b => 2, 'c' => 3}
279
- test_factory(:keys => :symbol).receive({'a' => 'x', :b => 'y', 'c' => :z}).should == {:a => 'x', :b => 'y', :c => :z}
280
- test_factory(:items => :symbol).receive({'a' => 'x', :b => 'y', 'c' => :z}).should == {'a' => :x, :b => :y, 'c' => :z}
281
- autov_factory = test_factory(:empty_product => Hash.new{|h,k| h[k] = {} })
282
- result = autov_factory.receive({:a => :b}) ; result.should == { :a => :b }
283
- result[:flanger][:modacity] = 11 ; result.should == { :a => :b, :flanger => { :modacity => 11 }}
284
- result2 = autov_factory.receive({:x => :y}) ; result2.should == { :x => :y } ; result.should == { :a => :b, :flanger => { :modacity => 11 }}
285
- end
286
-
287
- it "accepts a factory for the keys" do
288
- mock_factory.should_receive(:receive).with(3).and_return("converted!")
289
- factory = test_factory(:keys => mock_factory)
290
- factory.receive( { 3 => 4 } ).should == { 'converted!' => 4 }
291
- end
292
- it_behaves_like :it_is_registered_as, :hash, Hash
293
- its(:typename){ should == :hash }
294
- end
295
-
296
- describe Gorillib::Factory::ArrayFactory do
297
- let(:collection_123){ [1,2,3] }
298
- let(:empty_collection){ [] }
299
-
300
- it 'follows examples' do
301
- test_factory.receive([1,2,3]).should == [1,2,3]
302
- test_factory(:items => :symbol).receive(['a', 'b', :c]).should == [:a, :b, :c]
303
- test_factory(:empty_product => [1,2,3]).receive([:a, :b, :c]).should == [1, 2, 3, :a, :b, :c]
304
- end
305
-
306
- it_behaves_like :it_considers_blankish, nil
307
- it_behaves_like :it_converts, { [] => [], {} => [], [1,2,3] => [1,2,3], {:a => :b} => [[:a, :b]], [:a] => [:a], [[]] => [[]], :non_native_ok => true }
308
- it_behaves_like :an_enumerable_factory
309
- it_behaves_like :it_is_registered_as, :array, Array
310
- its(:typename){ should == :array }
311
- end
312
-
313
- describe Gorillib::Factory::SetFactory do
314
- let(:collection_123){ Set.new([1,2,3]) }
315
- let(:empty_collection){ Set.new }
316
-
317
- it 'follows examples' do
318
- test_factory.receive([1,2,3]).should == collection_123
319
- test_factory(:items => :symbol).receive(['a', 'b', :c]).should == [:a, :b, :c].to_set
320
- test_factory(:empty_product => [1,2,3].to_set).receive([:a, :b, :c]).should == [1, 2, 3, :a, :b, :c].to_set
321
-
322
- has_an_empty_array = test_factory.receive( [[]] )
323
- has_an_empty_array.should == Set.new( [[]] )
324
- has_an_empty_array.first.should == []
325
- has_an_empty_array.size.should == 1
326
- end
327
-
328
- it_behaves_like :it_considers_blankish, nil
329
- it_behaves_like :it_converts, { [] => Set.new, {} => Set.new, [1,2,3] => [1,2,3].to_set, {:a => :b} => Set.new({:a => :b}), [:a] => [:a].to_set, :non_native_ok => true }
330
- it_behaves_like :an_enumerable_factory
331
- it_behaves_like :it_is_registered_as, :set, Set
332
- its(:typename){ should == :set }
333
- end
334
-
335
- end