inquery 1.1.0 → 1.1.1

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.
@@ -14,18 +14,88 @@ module Inquery
14
14
  assert_equal '{}', MethodAccessibleHash.new.to_s
15
15
  end
16
16
 
17
+ def test_is_not_a_hash
18
+ # MethodAccessibleHash must NOT subclass Hash, otherwise method access
19
+ # collides with Hash/Enumerable methods (see
20
+ # `test_does_not_collide_with_hash_methods`).
21
+ refute_kind_of ::Hash, MethodAccessibleHash.new
22
+ end
23
+
24
+ def test_does_not_collide_with_hash_methods
25
+ # Keys whose names match Hash/Enumerable/Object methods must be returned
26
+ # as values, not invoke the inherited method. Regression test: previously
27
+ # `osparams.group_by` returned an Enumerator instead of the stored value.
28
+ hash = MethodAccessibleHash.new(
29
+ group_by: 'alphabetical',
30
+ count: 5,
31
+ zip: '8001',
32
+ select: 'everything',
33
+ map: 'world',
34
+ first: 'one',
35
+ merge: 'combine'
36
+ )
37
+
38
+ assert_equal 'alphabetical', hash.group_by
39
+ assert_equal 5, hash.count
40
+ assert_equal '8001', hash.zip
41
+ assert_equal 'everything', hash.select
42
+ assert_equal 'world', hash.map
43
+ assert_equal 'one', hash.first
44
+
45
+ # Bracket access works for the same keys too.
46
+ assert_equal 'alphabetical', hash[:group_by]
47
+ assert_equal '8001', hash[:zip]
48
+ end
49
+
50
+ def test_new_with_nil
51
+ assert_equal '{}', MethodAccessibleHash.new(nil).to_s
52
+ end
53
+
54
+ def test_does_not_fake_conversion_methods
55
+ # The object must not pretend to respond to Ruby's implicit type-coercion
56
+ # protocol, otherwise `**hash`, `Hash(obj)` etc. detect the method, call
57
+ # it, receive nil and raise a TypeError. Regression test for splatting.
58
+ hash = MethodAccessibleHash.new(foo: :bar)
59
+
60
+ refute_respond_to hash, :to_hash
61
+ refute_respond_to hash, :to_ary
62
+ refute_respond_to hash, :to_str
63
+
64
+ assert_raises(NoMethodError) { hash.to_hash }
65
+ assert_equal({ foo: :bar }, { **hash.to_h })
66
+ end
67
+
68
+ def test_conversion_method_name_as_key_still_returns_value
69
+ hash = MethodAccessibleHash.new(to_ary: 'value')
70
+
71
+ assert_respond_to hash, :to_ary
72
+ assert_equal 'value', hash.to_ary
73
+ end
74
+
17
75
  def test_to_h
18
- assert MethodAccessibleHash.new.merge(foo: :bar).to_h.instance_of?(::Hash)
76
+ hash = MethodAccessibleHash.new(foo: :bar)
77
+
78
+ assert_instance_of ::Hash, hash.to_h
79
+ assert_equal({ foo: :bar }, hash.to_h)
80
+
81
+ # `to_h` returns a copy; mutating it must not affect the original.
82
+ copy = hash.to_h
83
+ copy[:foo] = :changed
84
+ assert_equal :bar, hash.foo
19
85
  end
20
86
 
21
87
  def test_getter
22
- hash = MethodAccessibleHash.new.merge(foo: :bar, bar: :baz)
88
+ hash = MethodAccessibleHash.new(foo: :bar, bar: :baz)
23
89
  assert_equal :bar, hash.foo
24
90
  assert_equal :baz, hash.bar
25
91
  end
26
92
 
93
+ def test_getter_unknown_key_returns_nil
94
+ assert_nil MethodAccessibleHash.new.foo
95
+ end
96
+
27
97
  def test_setter
28
- hash = MethodAccessibleHash.new.merge(foo: :bar)
98
+ hash = MethodAccessibleHash.new(foo: :bar)
29
99
  assert_equal :bar, hash.foo
30
100
 
31
101
  hash.foo = :x
@@ -40,11 +110,57 @@ module Inquery
40
110
  hash.foo = 42
41
111
  assert_equal 42, hash[:foo]
42
112
  assert_equal 42, hash.foo
113
+
114
+ hash[:bar] = 43
115
+ assert_equal 43, hash[:bar]
116
+ assert_equal 43, hash.bar
117
+ end
118
+
119
+ def test_string_keys_are_symbolized
120
+ hash = MethodAccessibleHash.new('foo' => :bar)
121
+ assert_equal :bar, hash.foo
122
+ assert_equal :bar, hash[:foo]
123
+ assert_equal :bar, hash['foo']
124
+ end
125
+
126
+ def test_merge
127
+ hash = MethodAccessibleHash.new(foo: :bar).merge(bar: :baz)
128
+
129
+ assert_instance_of MethodAccessibleHash, hash
130
+ assert_equal :bar, hash.foo
131
+ assert_equal :baz, hash.bar
132
+ assert_instance_of ::Hash, hash.to_h
133
+ end
134
+
135
+ def test_merge_with_string_keys
136
+ hash = MethodAccessibleHash.new(foo: :bar).merge('bar' => :baz)
137
+
138
+ assert_equal :bar, hash.foo
139
+ assert_equal :baz, hash.bar
140
+ end
141
+
142
+ def test_merge_with_method_accessible_hash
143
+ other = MethodAccessibleHash.new(bar: :baz)
144
+ hash = MethodAccessibleHash.new(foo: :bar).merge(other)
145
+
146
+ assert_instance_of MethodAccessibleHash, hash
147
+ assert_equal :bar, hash.foo
148
+ assert_equal :baz, hash.bar
43
149
  end
44
150
 
45
151
  def test_comparison
46
- assert_equal({ foo: :bar }, MethodAccessibleHash.new(foo: :bar))
47
- refute_equal({ foo: :bar, bar: :baz }, MethodAccessibleHash.new(foo: :bar))
152
+ assert_equal MethodAccessibleHash.new(foo: :bar), MethodAccessibleHash.new(foo: :bar)
153
+ refute_equal MethodAccessibleHash.new(foo: :bar, bar: :baz), MethodAccessibleHash.new(foo: :bar)
154
+
155
+ # Equal to a plain hash with the same (symbolized) contents.
156
+ assert_equal MethodAccessibleHash.new(foo: :bar), { foo: :bar }
157
+ assert_equal MethodAccessibleHash.new(foo: :bar), { 'foo' => :bar }
158
+ refute_equal MethodAccessibleHash.new(foo: :bar), 'foo'
159
+ end
160
+
161
+ def test_respond_to
162
+ assert_respond_to MethodAccessibleHash.new(foo: :bar), :foo
163
+ assert_respond_to MethodAccessibleHash.new, :anything
48
164
  end
49
165
 
50
166
  def test_frozen
@@ -66,6 +182,13 @@ module Inquery
66
182
  assert clone.frozen?
67
183
  assert_equal 70, clone.age
68
184
 
185
+ # A frozen clone must be deeply frozen, i.e. its internal data must not
186
+ # be mutable through any path.
187
+ assert clone.instance_variable_get(:@table).frozen?
188
+ assert_raises FrozenError do
189
+ clone.instance_variable_get(:@table)[:age] = 42
190
+ end
191
+
69
192
  assert_raises RuntimeError do
70
193
  clone.age = 42
71
194
  end
@@ -80,6 +203,11 @@ module Inquery
80
203
  assert_equal 70, duplicate.age
81
204
  assert_equal 300, duplicate.pension
82
205
  assert_equal 'John Smith', duplicate.name
206
+
207
+ # The duplicate is independent and mutable.
208
+ duplicate.age = 42
209
+ assert_equal 42, duplicate.age
210
+ assert_equal 70, hash.age
83
211
  end
84
212
  end
85
213
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inquery
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sitrox
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-01-05 00:00:00.000000000 Z
10
+ date: 2026-06-15 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activesupport