caruby-core 1.5.5 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. data/Gemfile +9 -0
  2. data/History.md +5 -1
  3. data/lib/caruby.rb +3 -5
  4. data/lib/caruby/caruby-src.tar.gz +0 -0
  5. data/lib/caruby/database.rb +53 -69
  6. data/lib/caruby/database/application_service.rb +25 -0
  7. data/lib/caruby/database/cache.rb +60 -0
  8. data/lib/caruby/database/fetched_matcher.rb +52 -38
  9. data/lib/caruby/database/lazy_loader.rb +4 -4
  10. data/lib/caruby/database/operation.rb +34 -0
  11. data/lib/caruby/database/persistable.rb +171 -86
  12. data/lib/caruby/database/persistence_service.rb +32 -34
  13. data/lib/caruby/database/persistifier.rb +100 -43
  14. data/lib/caruby/database/reader.rb +107 -85
  15. data/lib/caruby/database/reader_template_builder.rb +60 -0
  16. data/lib/caruby/database/saved_matcher.rb +3 -3
  17. data/lib/caruby/database/sql_executor.rb +88 -17
  18. data/lib/caruby/database/writer.rb +213 -177
  19. data/lib/caruby/database/writer_template_builder.rb +334 -0
  20. data/lib/caruby/{util → helpers}/controlled_value.rb +0 -0
  21. data/lib/caruby/{util → helpers}/coordinate.rb +4 -4
  22. data/lib/caruby/{util → helpers}/person.rb +3 -3
  23. data/lib/caruby/{util → helpers}/properties.rb +7 -9
  24. data/lib/caruby/{util → helpers}/roman.rb +2 -2
  25. data/lib/caruby/{util → helpers}/version.rb +1 -1
  26. data/lib/caruby/json/deserializer.rb +2 -2
  27. data/lib/caruby/json/serializer.rb +49 -7
  28. data/lib/caruby/metadata.rb +30 -0
  29. data/lib/caruby/metadata/java_property.rb +21 -0
  30. data/lib/caruby/metadata/propertied.rb +191 -0
  31. data/lib/caruby/metadata/property.rb +22 -0
  32. data/lib/caruby/metadata/property_characteristics.rb +201 -0
  33. data/lib/caruby/migration/migratable.rb +11 -182
  34. data/lib/caruby/rdbi/driver/jdbc.rb +446 -0
  35. data/lib/caruby/resource.rb +20 -823
  36. data/lib/caruby/version.rb +1 -1
  37. data/test/lib/caruby/database/cache_test.rb +54 -0
  38. data/test/lib/caruby/{util → helpers}/controlled_value_test.rb +3 -5
  39. data/test/lib/caruby/{util → helpers}/person_test.rb +4 -6
  40. data/test/lib/caruby/helpers/properties_test.rb +34 -0
  41. data/test/lib/caruby/{util → helpers}/roman_test.rb +2 -3
  42. data/test/lib/caruby/{util → helpers}/version_test.rb +2 -3
  43. data/test/lib/helper.rb +7 -0
  44. metadata +161 -214
  45. data/lib/caruby/cli/application.rb +0 -36
  46. data/lib/caruby/cli/command.rb +0 -202
  47. data/lib/caruby/csv/csv_mapper.rb +0 -159
  48. data/lib/caruby/csv/csvio.rb +0 -203
  49. data/lib/caruby/database/search_template_builder.rb +0 -56
  50. data/lib/caruby/database/store_template_builder.rb +0 -278
  51. data/lib/caruby/domain.rb +0 -193
  52. data/lib/caruby/domain/attribute.rb +0 -584
  53. data/lib/caruby/domain/attributes.rb +0 -628
  54. data/lib/caruby/domain/dependency.rb +0 -225
  55. data/lib/caruby/domain/id_alias.rb +0 -22
  56. data/lib/caruby/domain/importer.rb +0 -183
  57. data/lib/caruby/domain/introspection.rb +0 -176
  58. data/lib/caruby/domain/inverse.rb +0 -172
  59. data/lib/caruby/domain/inversible.rb +0 -90
  60. data/lib/caruby/domain/java_attribute.rb +0 -173
  61. data/lib/caruby/domain/merge.rb +0 -185
  62. data/lib/caruby/domain/metadata.rb +0 -142
  63. data/lib/caruby/domain/mixin.rb +0 -35
  64. data/lib/caruby/domain/properties.rb +0 -95
  65. data/lib/caruby/domain/reference_visitor.rb +0 -428
  66. data/lib/caruby/domain/uniquify.rb +0 -50
  67. data/lib/caruby/import/java.rb +0 -387
  68. data/lib/caruby/migration/migrator.rb +0 -918
  69. data/lib/caruby/migration/resource_module.rb +0 -9
  70. data/lib/caruby/migration/uniquify.rb +0 -17
  71. data/lib/caruby/util/attribute_path.rb +0 -44
  72. data/lib/caruby/util/cache.rb +0 -56
  73. data/lib/caruby/util/class.rb +0 -149
  74. data/lib/caruby/util/collection.rb +0 -1152
  75. data/lib/caruby/util/domain_extent.rb +0 -46
  76. data/lib/caruby/util/file_separator.rb +0 -65
  77. data/lib/caruby/util/inflector.rb +0 -27
  78. data/lib/caruby/util/log.rb +0 -95
  79. data/lib/caruby/util/math.rb +0 -12
  80. data/lib/caruby/util/merge.rb +0 -59
  81. data/lib/caruby/util/module.rb +0 -18
  82. data/lib/caruby/util/options.rb +0 -97
  83. data/lib/caruby/util/partial_order.rb +0 -35
  84. data/lib/caruby/util/pretty_print.rb +0 -204
  85. data/lib/caruby/util/stopwatch.rb +0 -74
  86. data/lib/caruby/util/topological_sync_enumerator.rb +0 -62
  87. data/lib/caruby/util/transitive_closure.rb +0 -55
  88. data/lib/caruby/util/tree.rb +0 -48
  89. data/lib/caruby/util/trie.rb +0 -37
  90. data/lib/caruby/util/uniquifier.rb +0 -30
  91. data/lib/caruby/util/validation.rb +0 -20
  92. data/lib/caruby/util/visitor.rb +0 -365
  93. data/lib/caruby/util/weak_hash.rb +0 -36
  94. data/test/lib/caruby/csv/csv_mapper_test.rb +0 -40
  95. data/test/lib/caruby/csv/csvio_test.rb +0 -69
  96. data/test/lib/caruby/database/persistable_test.rb +0 -92
  97. data/test/lib/caruby/domain/domain_test.rb +0 -112
  98. data/test/lib/caruby/domain/inversible_test.rb +0 -99
  99. data/test/lib/caruby/domain/reference_visitor_test.rb +0 -130
  100. data/test/lib/caruby/import/java_test.rb +0 -80
  101. data/test/lib/caruby/import/mixed_case_test.rb +0 -14
  102. data/test/lib/caruby/migration/test_case.rb +0 -102
  103. data/test/lib/caruby/test_case.rb +0 -230
  104. data/test/lib/caruby/util/cache_test.rb +0 -23
  105. data/test/lib/caruby/util/class_test.rb +0 -61
  106. data/test/lib/caruby/util/collection_test.rb +0 -398
  107. data/test/lib/caruby/util/command_test.rb +0 -55
  108. data/test/lib/caruby/util/domain_extent_test.rb +0 -60
  109. data/test/lib/caruby/util/file_separator_test.rb +0 -30
  110. data/test/lib/caruby/util/inflector_test.rb +0 -12
  111. data/test/lib/caruby/util/lazy_hash_test.rb +0 -34
  112. data/test/lib/caruby/util/merge_test.rb +0 -83
  113. data/test/lib/caruby/util/module_test.rb +0 -25
  114. data/test/lib/caruby/util/options_test.rb +0 -59
  115. data/test/lib/caruby/util/partial_order_test.rb +0 -42
  116. data/test/lib/caruby/util/pretty_print_test.rb +0 -85
  117. data/test/lib/caruby/util/properties_test.rb +0 -50
  118. data/test/lib/caruby/util/stopwatch_test.rb +0 -18
  119. data/test/lib/caruby/util/topological_sync_enumerator_test.rb +0 -69
  120. data/test/lib/caruby/util/transitive_closure_test.rb +0 -67
  121. data/test/lib/caruby/util/tree_test.rb +0 -23
  122. data/test/lib/caruby/util/trie_test.rb +0 -14
  123. data/test/lib/caruby/util/visitor_test.rb +0 -278
  124. data/test/lib/caruby/util/weak_hash_test.rb +0 -45
  125. data/test/lib/examples/clinical_trials/migration/migration_test.rb +0 -58
  126. data/test/lib/examples/clinical_trials/migration/test_case.rb +0 -38
@@ -1,23 +0,0 @@
1
- $:.unshift 'lib'
2
-
3
- require 'caruby/util/cache'
4
- require "test/unit"
5
-
6
- class CacheTest < Test::Unit::TestCase
7
-
8
- def test_cache
9
- cache = CaRuby::Cache.new { |n| n % 2 }
10
- assert_nil(cache[1], "Value mistakenly cached")
11
- cache.add(1)
12
- assert_equal(1, cache[1], "Cached value not found")
13
- assert_nil(cache[2], "Uncached value found")
14
- assert_equal(1, cache[3], "Cached equivalent not found")
15
- end
16
-
17
- def test_cache_factory
18
- cache = CaRuby::Cache.new(Proc.new { |n| n * 4 }) { |n| n % 2 }
19
- assert_equal(4, cache[1], "Cached factory value not found")
20
- assert_equal(8, cache[2], "Cached factory value found")
21
- assert_equal(4, cache[3], "Cached factory equivalent not found")
22
- end
23
- end
@@ -1,61 +0,0 @@
1
- $:.unshift 'lib'
2
-
3
- require "test/unit"
4
- require 'caruby/util/class'
5
-
6
- class ClassTest < Test::Unit::TestCase
7
- def ssn
8
- '555-55-555'
9
- end
10
-
11
- def self.redefine_ssn
12
- redefine_method(:ssn) { |old_method| lambda { send(old_method).delete('-').to_i } }
13
- end
14
-
15
- def test_redefine_method
16
- self.class.redefine_ssn
17
- assert_equal(55555555, ssn, "Method not redefined correctly")
18
- end
19
-
20
- def test_class_hierarchy
21
- assert_equal([Array, Object], Array.class_hierarchy.to_a, "Class ancestors incorrect")
22
- end
23
-
24
- class Person
25
- attr_reader :social_security_number
26
- attr_accessor :postal_code
27
- alias_attribute(:ssn, :social_security_number)
28
- alias_attribute(:zip_code, :postal_code)
29
- end
30
-
31
- def test_attribute_alias
32
- assert(Person.method_defined?(:ssn), "Reader alias not defined")
33
- assert(!Person.method_defined?(:ssn=), "Writer alias incorrectly defined")
34
- assert(Person.method_defined?(:zip_code), "Reader alias not defined")
35
- assert(Person.method_defined?(:zip_code=), "Writer alias not defined")
36
- end
37
-
38
- class A; end
39
- class B < A; end
40
-
41
- def test_range
42
- assert_equal([B, A, Object], (B..Object).to_a, "Class range incorrect")
43
- end
44
-
45
- class OneBased
46
- attr_accessor :index
47
- offset_attr_accessor :zero_based_index => :index
48
- offset_attr_accessor({:two_based_index => :index}, 1)
49
- end
50
-
51
- def test_offset_attr_accessor
52
- x = OneBased.new
53
- x.index = 1
54
- assert_equal(0, x.zero_based_index, "Offset reader incorrect")
55
- x.zero_based_index = 1
56
- assert_equal(2, x.index, "Offset writer incorrect")
57
- assert_equal(3, x.two_based_index, "Offset reader incorrect")
58
- x.two_based_index = 1
59
- assert_equal(0, x.index, "Offset writer incorrect")
60
- end
61
- end
@@ -1,398 +0,0 @@
1
- $:.unshift 'lib'
2
-
3
- require "test/unit"
4
- require 'caruby/util/collection'
5
-
6
- class CollectionTest < Test::Unit::TestCase
7
- def test_collection_classifier
8
- assert([].collection?, "array is not a collecton")
9
- assert(!nil.collection?, "nil is a collecton")
10
- assert(!'a'.collection?, "String is a collecton")
11
- end
12
-
13
- def test_hashify
14
- actual = [1, 2, 3].hashify { |key| key + 1 unless key == 2 }
15
- expected = {1 => 2, 2 => nil, 3 => 4}
16
- assert_equal(expected, actual, 'Hashify result incorrect')
17
- end
18
-
19
- def test_to_compact_hash
20
- actual = [1, 2, 3].to_compact_hash { |key| key + 1 unless key == 2 }
21
- expected = {1 => 2, 3 => 4}
22
- assert_equal(expected, actual, 'Compact hash incorrect')
23
- end
24
-
25
- def test_lazy_hash
26
- hash = LazyHash.new { |n| n * 2 }
27
- assert_equal(2, hash[1], "Lazy hash value incorrect")
28
- hash.merge!(2 => 3)
29
- assert_equal({1 => 2, 2 => 3}, hash, "Lazy hash merge incorrect")
30
- end
31
-
32
- def test_array_operator_set_argument
33
- array = [1, 2, 3]
34
- set = [3, 4].to_set
35
- assert_equal([3], array & set, "Array | Set result incorrect")
36
- assert_equal([1, 2, 3, 4], array | set, "Array | Set result incorrect")
37
- assert_equal([1, 2], array - set, "Array - Set result incorrect")
38
- end
39
-
40
- def test_empty_hash
41
- assert_raises(NotImplementedError, "Assigment to empty hash succeeds") { Hash::EMPTY_HASH[:a] = 2 }
42
- end
43
-
44
- def test_set_first
45
- assert_equal(1, [1, 2].to_set.first, "first of set incorrect")
46
- assert_nil(Set.new.first, "first of empty set incorrect")
47
- end
48
-
49
- def test_assoc_values_single
50
- expected = {:a => [1, 3], :b => [2, nil], :c => [nil, 4]}
51
- actual = {:a => 1, :b => 2}.assoc_values({:a => 3, :c => 4})
52
- assert_equal(expected, actual, "Association hash incorrect")
53
- end
54
-
55
- def test_assoc_values_multiple
56
- expected = {:a => [1, 3, 4], :b => [2, nil, 5]}
57
- actual = {:a => 1, :b => 2}.assoc_values({:a => 3}, { :a => 4, :b => 5 })
58
- assert_equal(expected, actual, "Multiple association hash incorrect")
59
- end
60
-
61
- def test_detect_value
62
- assert_equal(4, [1, 2, 3].detect_value { |item| item * 2 if item > 1 }, "Detect value incorrect")
63
- assert_nil([1, 2, 3].detect_value { |item| item * 2 if item > 3 }, "Value incorrectly detected")
64
- end
65
-
66
- def test_detect_with_value
67
- assert_equal([2, 1], [1, 2].detect_with_value { |item| item / 2 if item % 2 == 0 }, "Detect with value incorrect")
68
- end
69
-
70
- def test_array_filter
71
- base = [1, 2, 3]
72
- filter = base.filter { |n| n != 2 }
73
- assert_equal([1, 3], filter.to_a, 'Filter incorrect')
74
- base << 4
75
- assert_equal([1, 3, 4], filter.to_a, 'Filter does not reflect operand modification')
76
- filter << 5
77
- assert_equal([1, 2, 3, 4, 5], base.to_a, 'Filter does not modify the base')
78
- end
79
-
80
- def test_enum_join
81
- assert_equal("1", [1].filter { true }.join, "Enumerable singleton join incorrect")
82
- assert_equal("1,2", [1, 2].filter { true }.join(','), "Enumerable join incorrect")
83
- end
84
-
85
- def test_array_filter_without_block
86
- assert_equal([1, 3], [1, nil, 3, false].filter.to_a, 'Filter incorrect')
87
- end
88
-
89
- def test_set_filter_include
90
- assert([1, 2, 3].to_set.filter { |n| n > 1 }.include?(2), 'Set filter include? incorrect')
91
- assert(false == [1, 2, 3].to_set.filter { |n| n > 1 }.include?(1), 'Set filter include? incorrect')
92
- end
93
-
94
- def test_union
95
- base = [1, 2]
96
- sum = base.union([4])
97
- assert_equal([1, 2, 4], sum.to_a, 'Enumerator union incorrect')
98
- assert(sum.include?(2), "Enumerator union missing first array element")
99
- assert(sum.include?(4), "Enumerator union missing second array element")
100
- base << 3
101
- assert_equal([1, 2, 3, 4], sum.to_a, 'Enumerator union does not reflect operand modification')
102
- end
103
-
104
- def test_intersection
105
- base = [1, 2, 3, 4]
106
- other = [3]
107
- intersection = base.intersect(other)
108
- assert_equal([3], intersection.to_a, 'Enumerator intersection incorrect')
109
- other << 4 << 5
110
- assert_equal([3, 4], intersection.to_a, 'Enumerator intersection does not reflect operand modification')
111
- end
112
-
113
- def test_difference
114
- base = [1, 2, 3]
115
- diff = base.difference([3])
116
- assert_equal([1, 2], diff.to_a, 'Enumerator subtraction incorrect')
117
- base << 4
118
- assert_equal([1, 2, 4], diff.to_a, 'Enumerator subtraction does not reflect operand modification')
119
- end
120
-
121
- def test_wrap
122
- assert_equal([2, 4, 6], [1, 2, 3].wrap { |n| n * 2 }.to_a, 'Wrap incorrect')
123
- end
124
-
125
- def test_enum_addition
126
- a = [1, 2].filter { true }
127
- b = [3, 4].filter { true }
128
- ab = a + b
129
- assert_equal([1, 2, 3, 4], ab.to_a, "Composite array incorrect")
130
- a << 3
131
- assert_equal([1, 2, 3, 3, 4], ab.to_a, "Addition does not reflect change to first enumerable")
132
- b << 5
133
- assert_equal([1, 2, 3, 3, 4, 5], ab.to_a, "Addition does not reflect change to second enumerable")
134
- end
135
-
136
- def test_partial_sort
137
- sorted = [Array, Object, Numeric, Enumerable, Set].partial_sort
138
- assert(sorted.index(Array) < sorted.index(Enumerable), "Partial sort order incorrect")
139
- assert(sorted.index(Set) < sorted.index(Enumerable), "Partial sort order incorrect")
140
- end
141
-
142
- def test_hash_union
143
- a = {:a => 1, :b => 2}
144
- b = {:b => 3, :c => 4}
145
- ab = a + b
146
- assert_equal({:a => 1, :b => 2, :c => 4}, ab.keys.to_compact_hash { |key| ab[key] }, "Hash union incorrect")
147
- assert_equal([1, 2, 4], ab.values.sort, "Hash union values incorrect")
148
- a.delete(:b)
149
- assert_equal({:a => 1, :b => 3, :c => 4}, ab.keys.to_compact_hash { |key| ab[key] }, "Hash union does not reflect underlying change")
150
- end
151
-
152
- def test_hash_compose
153
- x = {:a => :c, :b => :d}
154
- y = {:c => 1}
155
- xy = x.compose(y)
156
- assert_equal({:a => {:c => 1}}, xy.keys.to_compact_hash { |key| xy[key] }, "Composed hash incorrect")
157
- y[:d] = 2
158
- assert_equal({:a => {:c => 1}, :b => {:d => 2}}, xy.keys.to_compact_hash { |key| xy[key] }, "Composed hash does not reflect underlying change")
159
- end
160
-
161
- def test_hash_join
162
- x = {:a => :c, :b => :d}
163
- y = {:c => 1}
164
- xy = x.join(y)
165
- assert_equal({:a => 1}, xy.keys.to_compact_hash { |key| xy[key] }, "Joined hash incorrect")
166
- y[:d] = 2
167
- assert_equal({:a => 1, :b => 2}, xy.keys.to_compact_hash { |key| xy[key] }, "Joined hash does not reflect underlying change")
168
- end
169
-
170
- def test_hash_diff
171
- x = {:a => 1, :b => 2, :c => 3}
172
- y = {:b => 2, :c => 4, :d => 5}
173
- assert_equal({:a => [1,nil], :c => [3,4], :d => [nil,5]}, x.diff(y), "Hash diff incorrect")
174
- end
175
-
176
- def test_to_assoc_hash
177
- actual = [[:a, 1], [:b, 2, 3], [:c], []].to_assoc_hash
178
- expected = {:a => 1, :b => [2,3], :c => nil}
179
- assert_equal(expected, actual, 'Association hash incorrect')
180
- end
181
-
182
- def test_hashable_equal
183
- assert_equal({:a => 1}, {:a => 1}.filter, "Hash equal incorrect")
184
- end
185
-
186
- def test_hash_enum_keys
187
- hash = { 1 => :a, 2 => :b }
188
- ek = hash.enum_keys
189
- assert_equal([1, 2], ek.sort, "Hash key enumerator incorrect")
190
- hash[3] = :c
191
- assert_equal([1, 2, 3], ek.sort, "Hash key enumerator does not reflect hash change")
192
- end
193
-
194
- def test_hash_enum_keys_with_value
195
- assert_equal([:b, :c], {:a => 1, :b => 2, :c => 2}.enum_keys_with_value(2).to_a, "Hash filtered value keys incorrect")
196
- end
197
-
198
- def test_hash_enum_keys_with_value_block
199
- assert_equal([:b, :c], {:a => 1, :b => 2, :c => 3}.enum_keys_with_value { |value| value > 1 }.to_a, "Hash filtered value block keys incorrect")
200
- end
201
-
202
- def test_hash_enum_values
203
- hash = { :a => 1, :b => 2 }
204
- ev = hash.enum_values
205
- assert_equal([1, 2], ev.sort, "Hash value enumerator incorrect")
206
- hash[:c] = 3
207
- assert_equal([1, 2, 3], ev.sort, "Hash value enumerator does not reflect hash change")
208
- end
209
-
210
- def test_hash_flatten
211
- assert_equal([:a, :b, :c, :d, :e, :f, :g], {:a => {:b => :c}, :d => :e, :f => [:g]}.flatten, "Hash flatten incorrect")
212
- end
213
-
214
- def test_hash_first
215
- assert_equal([:a, 1], {:a => 1, :b => 2}.first, "Hash first incorrect")
216
- end
217
-
218
- def test_hash_filter
219
- assert_equal({:a => 1, :c => 3}, {:a => 1, :b => 2, :c => 3}.filter { |k, v| k != :b }, "Hash filter incorrect")
220
- end
221
-
222
- def test_hash_sort
223
- assert_equal([['a', 1], ['b', 2]], {'a'=>1, 'b'=>2}.sort.to_a, "Hash sort incorrect")
224
- end
225
-
226
- def test_hash_sort_with_comparator
227
- assert_equal([[:a, 1], [:b, 2]], {:a => 1, :b => 2}.sort { |k1, k2| k1.to_s <=> k2.to_s }, "Hash sort with comparator incorrect")
228
- end
229
-
230
- def test_hash_default_filter
231
- assert_equal({:a => 1, :c => 3}, {:a => 1, :b => nil, :c => 3}.filter, "Hash default filter incorrect")
232
- end
233
-
234
- def test_hash_partition
235
- assert_equal([{:a => 1, :c => 3}, {:b => 2}], {:a => 1, :b => 2, :c => 3}.split { |k, v| k == :a or v == 3 }, "Hash partition incorrect")
236
- end
237
-
238
- def test_hash_filter_on_key
239
- filtered = {:a => 1, :b => 2, :c => 3}.filter_on_key { |k| k != :b }
240
- assert_equal({:a => 1, :c => 3}, filtered.to_hash, "Hash on key filter incorrect")
241
- assert_equal(1, filtered[:a], "Access on key filter inclusion incorrect")
242
- assert_nil(filtered[:b], "Access on key filter exclusion incorrect")
243
- end
244
-
245
- def test_hash_filter_on_value
246
- filtered = {:a => 1, :b => 2, :c => 3}.filter_on_value { |v| v != 2 }
247
- assert_equal({:a => 1, :c => 3}, filtered.to_hash, "Hash on value filter incorrect")
248
- end
249
-
250
- def test_hash_compact
251
- assert_equal({:a => 1, :c => 3}, {:a => 1, :b => nil, :c => 3}.compact.to_hash, "Compact hash incorrect")
252
- end
253
-
254
- def test_set_flatten
255
- inner = Set.new << :a
256
- actual = [inner, 'b'].flatten
257
- expected = [:a, 'b']
258
- assert_equal(expected, actual, 'Inner set not flattened')
259
- end
260
-
261
- def test_to_compact_hash
262
- assert_equal({1 => 2, 2 => 3}, [1, 2].to_compact_hash { |item| item + 1 }, 'to_compact_hash result incorrect')
263
- end
264
-
265
- def test_to_compact_hash_with_index
266
- assert_equal({:a => 1, :b => 2}, [:a, :b].to_compact_hash_with_index { |item, index| index + 1 }, 'to_compact_hash_with_index result incorrect')
267
- end
268
-
269
- def test_to_compact_hash_reject_missing
270
- assert_equal({1 => 2, 2 => 3}, [1, 2, 3].to_compact_hash { |item| item + 1 unless item > 2 }, 'to_compact_hash maps a key with a nil value')
271
- end
272
-
273
- def test_series
274
- actual = [1, 2, 3].to_series
275
- assert_equal('1, 2 and 3', actual, 'Print string incorrect')
276
- end
277
-
278
- def test_empty_series
279
- actual = [].to_series
280
- assert_equal('', actual, 'Print string incorrect')
281
- end
282
-
283
- def test_singleton_series
284
- actual = [1].to_series
285
- assert_equal('1', actual, 'Print string incorrect')
286
- end
287
-
288
- def test_copy_recursive
289
- hash = {1 => { 2 => 3 }, 4 => 5 }
290
- copy = hash.copy_recursive
291
- assert_equal(hash, copy, 'Copy not equal')
292
- hash[1][2] = 6
293
- assert_equal(3, copy[1][2], 'Copy reflects change to original')
294
- hash[4] = 7
295
- assert_equal(5, copy[4], 'Copy reflects change to original')
296
- end
297
-
298
- def test_case_insensitive_hash
299
- hash = CaseInsensitiveHash.new
300
- hash[:UP] = :down
301
- assert_equal(:down, hash['up'], "Case-insensitive hash look-up incorrect")
302
- end
303
-
304
- def test_transformed_hash
305
- hash = {:a => 1, :b => 2}
306
- xfm = hash.transform { |value| value * 2 }
307
- assert_equal(2, xfm[:a], 'Transformed hash accessor incorrect')
308
- assert_equal([2, 4], xfm.values.sort, 'Transformed hash values incorrect')
309
- assert(xfm.has_value?(4), 'Transformed hash value query incorrect')
310
- assert(!xfm.has_value?(1), 'Transformed hash value query incorrect')
311
- # base hash should be reflected in transformed hash
312
- hash[:b] = 3; hash[:c] = 4
313
- assert_equal(6, xfm[:b], 'Transformed hash does not reflect base hash change')
314
- assert_equal(8, xfm[:c], 'Transformed hash does not reflect base hash change')
315
- end
316
-
317
- def test_key_transformer_hash
318
- hash = KeyTransformerHash.new { |key| key % 2 }
319
- hash[1] = :a
320
- assert_equal(:a, hash[1], 'Key transformer hash entered value not found')
321
- assert_nil(hash[2], 'Transformed hash unentered value found')
322
- assert_equal(:a, hash[3], 'Key transformer hash equivalent value not found')
323
- end
324
-
325
- def test_hashinator
326
- base = {:a => 1, :b => 2}.to_a
327
- hash = Hashinator.new(base)
328
- assert_equal(base.to_set, hash.to_set, "Hashinator enumeration invalid")
329
- assert_equal(1, hash[:a], "Hashinator a value invalid")
330
- assert_equal(2, hash[:b], "Hashinator b value invalid")
331
- assert_nil(hash[:c], "Hashinator has association not in the base")
332
- base.first[1] = 3
333
- assert_equal(3, hash[:a], "Hashinator does not reflect change to underlying Enumerator")
334
- assert_equal(base, hash.to_hash.to_a, "Hashable to_hash incorrect")
335
- end
336
-
337
- def test_collector
338
- assert_equal([2, [3, 4]], Collector.on([1, [2, 3]]) { |n| n + 1 }, "Collector on nested array incorrect")
339
- assert_nil(Collector.on(nil) { |n| n + 1 }, "Collector on nil incorrect")
340
- assert_equal(2, Collector.on(1) { |n| n + 1 }, "Collector on non-collection incorrect")
341
- end
342
-
343
- def test_enumerate
344
- counter = 0
345
- nil.enumerate { |item| counter += item }
346
- assert_equal(0, counter, "Enumerate on nil incorrect")
347
- [1, 2, 3].enumerate { |item| counter += item }
348
- assert_equal(6, counter, "Enumerate on array incorrect")
349
- [1, [2, 3]].enumerate { |item| counter += 1 }
350
- assert_equal(8, counter, "Enumerate on nested array incorrect")
351
- 2.enumerate { |item| counter += item }
352
- assert_equal(10, counter, "Enumerate on non-collection incorrect")
353
- end
354
-
355
- def test_to_enum
356
- assert_equal([], nil.to_enum.to_a, "to_enum on nil incorrect")
357
- assert_equal([1], 1.to_enum.to_a, "to_enum on non-collection incorrect")
358
- array = [1, 2]
359
- assert_same(array, array.to_enum, "to_enum on array incorrect")
360
- s = 'a'
361
- assert_same(s, s.to_enum, "to_enum on String incorrect")
362
- end
363
-
364
- def test_flattener
365
- assert_equal([1, 2, 3], Flattener.new([1, [2, 3]]).to_a, "Flattener on nested array incorrect")
366
- assert_equal([], Flattener.new(nil).to_a, "Flattener on nil incorrect")
367
- assert_equal([1], Flattener.new(1).to_a, "Flattener on non-collection incorrect")
368
- assert(Flattener.new(nil).all?, "Flattener all? on nil incorrect")
369
- assert_equal([:b, :c, :e], {:a => {:b => :c}, :d => [:e]}.enum_values.flatten.to_a, "Enumerable flatten incorrect")
370
- end
371
-
372
- def test_hash_flattener
373
- assert_equal([:a, :b, :c], {:a => {:b => :c}}.flatten.to_a, "Hash flatten incorrect")
374
- assert_equal([:b, :c, :e], {:a => {:b => :c}, :d => [:e]}.enum_values.flatten.to_a, "Enumerable flatten incorrect")
375
- end
376
-
377
-
378
- def test_conditional_enumerator
379
- assert_equal([1, 2], ConditionalEnumerator.new([1, 2, 3]) { |i| i < 3 }.to_a, "ConditionalEnumerator filter not applied")
380
- end
381
-
382
- def test_enumerable_size
383
- assert_equal(2, {:a => 1, :b => 2}.enum_keys.size, "Enumerable size incorrect")
384
- end
385
-
386
- def test_set_merge
387
- set = [1, 2].to_set
388
- merged = set.merge!([3])
389
- assert_equal([1, 2, 3].to_set, merged, "Merged set incorrect")
390
- assert_same(set, merged, "Set merge! did not return same set")
391
- end
392
-
393
- def test_set_add_all
394
- actual = [1, 2].add_all([3])
395
- expected = [1, 2, 3]
396
- assert_equal(expected, actual, 'Set content not added')
397
- end
398
- end