bson 1.7.0-java
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of bson might be problematic. Click here for more details.
- data/LICENSE.txt +190 -0
- data/bin/b2json +65 -0
- data/bin/j2bson +72 -0
- data/ext/java/jar/jbson.jar +0 -0
- data/ext/java/jar/mongo-2.6.5.jar +0 -0
- data/lib/bson.rb +101 -0
- data/lib/bson/bson_c.rb +41 -0
- data/lib/bson/bson_java.rb +30 -0
- data/lib/bson/bson_ruby.rb +641 -0
- data/lib/bson/byte_buffer.rb +280 -0
- data/lib/bson/exceptions.rb +41 -0
- data/lib/bson/ordered_hash.rb +187 -0
- data/lib/bson/types/binary.rb +56 -0
- data/lib/bson/types/code.rb +59 -0
- data/lib/bson/types/dbref.rb +46 -0
- data/lib/bson/types/min_max_keys.rb +60 -0
- data/lib/bson/types/object_id.rb +230 -0
- data/lib/bson/types/timestamp.rb +76 -0
- data/lib/bson/version.rb +3 -0
- data/test/bson/binary_test.rb +13 -0
- data/test/bson/bson_test.rb +667 -0
- data/test/bson/byte_buffer_test.rb +208 -0
- data/test/bson/hash_with_indifferent_access_test.rb +38 -0
- data/test/bson/json_test.rb +17 -0
- data/test/bson/object_id_test.rb +148 -0
- data/test/bson/ordered_hash_test.rb +247 -0
- data/test/bson/test_helper.rb +30 -0
- data/test/bson/timestamp_test.rb +46 -0
- metadata +91 -0
@@ -0,0 +1,208 @@
|
|
1
|
+
# encoding: binary
|
2
|
+
require File.expand_path("../test_helper", __FILE__)
|
3
|
+
|
4
|
+
class ByteBufferTest < Test::Unit::TestCase
|
5
|
+
include BSON
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@buf = ByteBuffer.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_initial_state
|
12
|
+
assert_equal 0, @buf.position
|
13
|
+
assert_equal [], @buf.to_a
|
14
|
+
assert_equal "", @buf.to_s
|
15
|
+
assert_equal 0, @buf.length
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_nil_get_returns_one_byte
|
19
|
+
@buf.put_array([1, 2, 3, 4])
|
20
|
+
@buf.rewind
|
21
|
+
assert_equal 1, @buf.get
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_unpack
|
25
|
+
@buf.put_array([17, 2, 3, 4])
|
26
|
+
assert_equal [17, 2, 3, 4], @buf.to_a
|
27
|
+
assert_equal ["11020304"], @buf.unpack("H*")
|
28
|
+
assert_equal ["11020304"], @buf.to_a("H*")
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_one_get_returns_array_length_one
|
32
|
+
@buf.put_array([1, 2, 3, 4])
|
33
|
+
@buf.rewind
|
34
|
+
assert_equal [1], @buf.get(1)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_zero_get_returns_empty_array
|
38
|
+
@buf.put_array([1, 2, 3, 4])
|
39
|
+
@buf.rewind
|
40
|
+
assert_equal [], @buf.get(0)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_length
|
44
|
+
@buf.put_int 3
|
45
|
+
assert_equal 4, @buf.length
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_default_order
|
49
|
+
assert_equal :little_endian, @buf.order
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_long_length
|
53
|
+
@buf.put_long 1027
|
54
|
+
assert_equal 8, @buf.length
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_get_long
|
58
|
+
@buf.put_long 1027
|
59
|
+
@buf.rewind
|
60
|
+
assert_equal 1027, @buf.get_long
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_get_double
|
64
|
+
@buf.put_double 41.2
|
65
|
+
@buf.rewind
|
66
|
+
assert_equal 41.2, @buf.get_double
|
67
|
+
end
|
68
|
+
|
69
|
+
if defined?(Encoding)
|
70
|
+
def test_serialize_cstr_converts_encoding_to_utf8
|
71
|
+
theta = "hello \xC8".force_encoding("ISO-8859-7")
|
72
|
+
ByteBuffer.serialize_cstr(@buf, theta)
|
73
|
+
assert_equal "hello \xCE\x98\0", @buf.to_s
|
74
|
+
assert_equal Encoding.find('binary'), @buf.to_s.encoding
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_serialize_cstr_validates_data_as_utf8
|
78
|
+
assert_raises(Encoding::UndefinedConversionError) do
|
79
|
+
ByteBuffer.serialize_cstr(@buf, "hello \xFF")
|
80
|
+
end
|
81
|
+
end
|
82
|
+
else
|
83
|
+
def test_serialize_cstr_forces_encoding_to_utf8
|
84
|
+
# Unicode snowman (\u2603)
|
85
|
+
ByteBuffer.serialize_cstr(@buf, "hello \342\230\203")
|
86
|
+
assert_equal "hello \342\230\203\0", @buf.to_s
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_serialize_cstr_validates_data_as_utf8
|
90
|
+
assert_raises(BSON::InvalidStringEncoding) do
|
91
|
+
ByteBuffer.serialize_cstr(@buf, "hello \xFF")
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_put_negative_byte
|
97
|
+
@buf.put(-1)
|
98
|
+
@buf.rewind
|
99
|
+
assert_equal 255, @buf.get
|
100
|
+
assert_equal "\xFF", @buf.to_s
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_put_with_offset
|
104
|
+
@buf.put(1)
|
105
|
+
@buf.put(2, 0)
|
106
|
+
@buf.put(3, 3)
|
107
|
+
assert_equal "\x02\x00\x00\x03", @buf.to_s
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_put_array_with_offset
|
111
|
+
@buf.put(1)
|
112
|
+
@buf.put_array([2, 3], 0)
|
113
|
+
@buf.put_array([4, 5], 4)
|
114
|
+
assert_equal "\x02\x03\x00\x00\x04\x05", @buf.to_s
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_put_int_with_offset
|
118
|
+
@buf.put(1)
|
119
|
+
@buf.put_int(2, 0)
|
120
|
+
@buf.put_int(3, 5)
|
121
|
+
assert_equal "\x02\x00\x00\x00\x00\x03\x00\x00\x00", @buf.to_s
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_put_long_with_offset
|
125
|
+
@buf.put(1)
|
126
|
+
@buf.put_long(2, 0)
|
127
|
+
@buf.put_long(3, 9)
|
128
|
+
assert_equal(
|
129
|
+
"\x02\x00\x00\x00\x00\x00\x00\x00" +
|
130
|
+
"\x00" +
|
131
|
+
"\x03\x00\x00\x00\x00\x00\x00\x00",
|
132
|
+
@buf.to_s)
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_put_binary
|
136
|
+
@buf.put(1)
|
137
|
+
@buf.put_binary("\x02\x03", 0)
|
138
|
+
@buf.put_binary("\x04\x05", 4)
|
139
|
+
assert_equal "\x02\x03\x00\x00\x04\x05", @buf.to_s
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_rewrite
|
143
|
+
@buf.put_int(0)
|
144
|
+
@buf.rewind
|
145
|
+
@buf.put_int(1027)
|
146
|
+
assert_equal 4, @buf.length
|
147
|
+
@buf.rewind
|
148
|
+
assert_equal 1027, @buf.get_int
|
149
|
+
assert_equal 4, @buf.position
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_prepend_byte_buffer
|
153
|
+
@buf.put_int(4)
|
154
|
+
new_buf = ByteBuffer.new([5, 0, 0, 0])
|
155
|
+
@buf.prepend!(new_buf)
|
156
|
+
assert_equal [5, 0, 0, 0, 4, 0, 0, 0], @buf.to_a
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_append_byte_buffer
|
160
|
+
@buf.put_int(4)
|
161
|
+
new_buf = ByteBuffer.new([5, 0, 0, 0])
|
162
|
+
@buf.append!(new_buf)
|
163
|
+
assert_equal [4, 0, 0, 0, 5, 0, 0, 0], @buf.to_a
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_array_as_initial_input
|
167
|
+
@buf = ByteBuffer.new([5, 0, 0, 0])
|
168
|
+
assert_equal 4, @buf.size
|
169
|
+
assert_equal "\x05\x00\x00\x00", @buf.to_s
|
170
|
+
assert_equal [5, 0, 0, 0], @buf.to_a
|
171
|
+
@buf.put_int(32)
|
172
|
+
@buf.rewind
|
173
|
+
assert_equal 5, @buf.get_int
|
174
|
+
assert_equal 32, @buf.get_int
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_binary_string_as_initial_input
|
178
|
+
str = "abcd"
|
179
|
+
str.force_encoding('binary') if str.respond_to?(:force_encoding)
|
180
|
+
@buf = ByteBuffer.new(str)
|
181
|
+
assert_equal "abcd", @buf.to_s
|
182
|
+
assert_equal [97, 98, 99, 100], @buf.to_a
|
183
|
+
@buf.put_int(0)
|
184
|
+
assert_equal [97, 98, 99, 100, 0, 0, 0, 0], @buf.to_a
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_more
|
188
|
+
assert !@buf.more?
|
189
|
+
@buf.put_int(5)
|
190
|
+
assert !@buf.more?
|
191
|
+
@buf.rewind
|
192
|
+
assert @buf.more?
|
193
|
+
@buf.get_int
|
194
|
+
assert !@buf.more?
|
195
|
+
end
|
196
|
+
|
197
|
+
def test_equality
|
198
|
+
@buf = ByteBuffer.new("foo")
|
199
|
+
assert_equal @buf, @buf
|
200
|
+
assert_equal ByteBuffer.new(""), ByteBuffer.new("")
|
201
|
+
assert_equal ByteBuffer.new("123"), ByteBuffer.new("123")
|
202
|
+
assert_not_equal ByteBuffer.new("123"), ByteBuffer.new("1234")
|
203
|
+
assert_equal @buf, "foo"
|
204
|
+
assert_not_equal @buf, 123
|
205
|
+
assert_not_equal @buf, nil
|
206
|
+
end
|
207
|
+
|
208
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding:utf-8
|
2
|
+
require File.expand_path("../test_helper", __FILE__)
|
3
|
+
require './test/support/hash_with_indifferent_access'
|
4
|
+
|
5
|
+
class HashWithIndifferentAccessTest < Test::Unit::TestCase
|
6
|
+
include BSON
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@encoder = BSON::BSON_CODER
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_document
|
13
|
+
doc = HashWithIndifferentAccess.new
|
14
|
+
doc['foo'] = 1
|
15
|
+
doc['bar'] = 'baz'
|
16
|
+
|
17
|
+
bson = @encoder.serialize(doc)
|
18
|
+
assert_equal doc, @encoder.deserialize(bson.to_s)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_embedded_document
|
22
|
+
jimmy = HashWithIndifferentAccess.new
|
23
|
+
jimmy['name'] = 'Jimmy'
|
24
|
+
jimmy['species'] = 'Siberian Husky'
|
25
|
+
|
26
|
+
stats = HashWithIndifferentAccess.new
|
27
|
+
stats['eyes'] = 'blue'
|
28
|
+
|
29
|
+
person = HashWithIndifferentAccess.new
|
30
|
+
person['_id'] = BSON::ObjectId.new
|
31
|
+
person['name'] = 'Mr. Pet Lover'
|
32
|
+
person['pets'] = [jimmy, {'name' => 'Sasha'}]
|
33
|
+
person['stats'] = stats
|
34
|
+
|
35
|
+
bson = @encoder.serialize(person)
|
36
|
+
assert_equal person, @encoder.deserialize(bson.to_s)
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path("../test_helper", __FILE__)
|
2
|
+
require 'rubygems'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
class JSONTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
# This test passes when run by itself but fails
|
8
|
+
# when run as part of the whole test suite.
|
9
|
+
def test_object_id_as_json
|
10
|
+
#warn "Pending test object id as json"
|
11
|
+
#id = BSON::ObjectId.new
|
12
|
+
|
13
|
+
#obj = {'_id' => id}
|
14
|
+
#assert_equal "{\"_id\":#{id.to_json}}", obj.to_json
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
require File.expand_path("../test_helper", __FILE__)
|
2
|
+
require 'rubygems'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
class ObjectIdTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@o = ObjectId.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_hashcode
|
12
|
+
assert_equal @o.instance_variable_get(:@data).hash, @o.hash
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_array_uniq_for_equilavent_ids
|
16
|
+
a = ObjectId.new('123')
|
17
|
+
b = ObjectId.new('123')
|
18
|
+
assert_equal a, b
|
19
|
+
assert_equal 1, [a, b].uniq.size
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_create_pk_method
|
23
|
+
doc = {:name => 'Mongo'}
|
24
|
+
doc = ObjectId.create_pk(doc)
|
25
|
+
assert doc[:_id]
|
26
|
+
|
27
|
+
doc = {:name => 'Mongo', :_id => '12345'}
|
28
|
+
doc = ObjectId.create_pk(doc)
|
29
|
+
assert_equal '12345', doc[:_id]
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_different
|
33
|
+
a = ObjectId.new
|
34
|
+
b = ObjectId.new
|
35
|
+
assert_not_equal a.to_a, b.to_a
|
36
|
+
assert_not_equal a, b
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_eql?
|
40
|
+
o2 = ObjectId.new(@o.to_a)
|
41
|
+
assert_equal @o, o2
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_to_s
|
45
|
+
s = @o.to_s
|
46
|
+
assert_equal 24, s.length
|
47
|
+
s =~ /^([0-9a-f]+)$/
|
48
|
+
assert_equal 24, $1.length
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_to_s2
|
52
|
+
@o = ObjectId.new([76, 244, 52, 174, 44, 84, 121, 76, 88, 0, 0, 3])
|
53
|
+
s = '4cf434ae2c54794c58000003'
|
54
|
+
assert_equal @o.to_s, s
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_method
|
58
|
+
assert_equal ObjectId.from_string(@o.to_s), BSON::ObjectId(@o.to_s)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_inspect
|
62
|
+
assert_equal "BSON::ObjectId('#{@o.to_s}')", @o.inspect
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_from_string
|
66
|
+
hex_str = @o.to_s
|
67
|
+
o2 = ObjectId.from_string(hex_str)
|
68
|
+
assert_equal hex_str, o2.to_s
|
69
|
+
assert_equal @o, o2
|
70
|
+
assert_equal @o.to_s, o2.to_s
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_illegal_from_string
|
74
|
+
assert_raise InvalidObjectId do
|
75
|
+
ObjectId.from_string("")
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_legal
|
80
|
+
assert !ObjectId.legal?(nil)
|
81
|
+
assert !ObjectId.legal?("fred")
|
82
|
+
assert !ObjectId.legal?("0000")
|
83
|
+
assert !ObjectId.legal?('000102030405060708090A0')
|
84
|
+
assert ObjectId.legal?('000102030405060708090A0B')
|
85
|
+
assert ObjectId.legal?('abcdefABCDEF123456789012')
|
86
|
+
assert !ObjectId.legal?('abcdefABCDEF12345678901x')
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_from_string_leading_zeroes
|
90
|
+
hex_str = '000000000000000000000000'
|
91
|
+
o = ObjectId.from_string(hex_str)
|
92
|
+
assert_equal hex_str, o.to_s
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_byte_order
|
96
|
+
hex_str = '000102030405060708090A0B'
|
97
|
+
o = ObjectId.from_string(hex_str)
|
98
|
+
assert_equal [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b], o.to_a
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_generation_time
|
102
|
+
time = Time.now
|
103
|
+
id = ObjectId.new
|
104
|
+
generated_time = id.generation_time
|
105
|
+
|
106
|
+
assert_in_delta time.to_i, generated_time.to_i, 2
|
107
|
+
assert_equal "UTC", generated_time.zone
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_from_time
|
111
|
+
time = Time.now.utc
|
112
|
+
id = ObjectId.from_time(time)
|
113
|
+
|
114
|
+
assert id.to_a[4, 8].all? {|byte| byte == 0 }
|
115
|
+
assert_equal time.to_i, id.generation_time.to_i
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_from_time_unique
|
119
|
+
time = Time.now.utc
|
120
|
+
id = ObjectId.from_time(time, :unique => true)
|
121
|
+
|
122
|
+
assert_equal id.to_a[4, 3], ObjectId.machine_id.unpack("C3")
|
123
|
+
assert_equal time.to_i, id.generation_time.to_i
|
124
|
+
|
125
|
+
id2 = ObjectId.new(nil, time)
|
126
|
+
assert_equal time.to_i, id2.generation_time.to_i
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_json
|
130
|
+
id = ObjectId.new
|
131
|
+
assert_equal "{\"$oid\": \"#{id}\"}", id.to_json
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_as_json
|
135
|
+
id = ObjectId.new
|
136
|
+
assert_equal({"$oid" => id.to_s}, id.as_json)
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_object_id_array_flatten
|
140
|
+
id = ObjectId.new
|
141
|
+
assert_equal [ id ], [[ id ]].flatten
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_object_id_array_flatten_bang
|
145
|
+
id = ObjectId.new
|
146
|
+
assert_equal [ id ], [[ id ]].flatten!
|
147
|
+
end
|
148
|
+
end
|
@@ -0,0 +1,247 @@
|
|
1
|
+
require File.expand_path("../test_helper", __FILE__)
|
2
|
+
|
3
|
+
class OrderedHashTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@oh = BSON::OrderedHash.new
|
7
|
+
@oh['c'] = 1
|
8
|
+
@oh['a'] = 2
|
9
|
+
@oh['z'] = 3
|
10
|
+
@ordered_keys = %w(c a z)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_initialize
|
14
|
+
a = BSON::OrderedHash.new
|
15
|
+
a['x'] = 1
|
16
|
+
a['y'] = 2
|
17
|
+
|
18
|
+
b = BSON::OrderedHash['x', 1, 'y', 2]
|
19
|
+
assert_equal a, b
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_hash_code
|
23
|
+
o = BSON::OrderedHash.new
|
24
|
+
o['number'] = 50
|
25
|
+
assert o.hash
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_empty
|
29
|
+
assert_equal [], BSON::OrderedHash.new.keys
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_uniq
|
33
|
+
list = []
|
34
|
+
doc = BSON::OrderedHash.new
|
35
|
+
doc['_id'] = 'ab12'
|
36
|
+
doc['name'] = 'test'
|
37
|
+
|
38
|
+
same_doc = BSON::OrderedHash.new
|
39
|
+
same_doc['_id'] = 'ab12'
|
40
|
+
same_doc['name'] = 'test'
|
41
|
+
|
42
|
+
list << doc
|
43
|
+
list << same_doc
|
44
|
+
|
45
|
+
assert_equal 2, list.size
|
46
|
+
assert_equal 1, list.uniq.size
|
47
|
+
end
|
48
|
+
|
49
|
+
if !(RUBY_VERSION =~ /1.8.6/)
|
50
|
+
def test_compatibility_with_hash
|
51
|
+
list = []
|
52
|
+
doc = BSON::OrderedHash.new
|
53
|
+
doc['_id'] = 'ab12'
|
54
|
+
doc['name'] = 'test'
|
55
|
+
|
56
|
+
doc2 = {}
|
57
|
+
doc2['_id'] = 'ab12'
|
58
|
+
doc2['name'] = 'test'
|
59
|
+
list << doc
|
60
|
+
list << doc2
|
61
|
+
|
62
|
+
assert_equal 1, list.uniq.size
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_equality
|
67
|
+
a = BSON::OrderedHash.new
|
68
|
+
a['x'] = 1
|
69
|
+
a['y'] = 2
|
70
|
+
|
71
|
+
b = BSON::OrderedHash.new
|
72
|
+
b['y'] = 2
|
73
|
+
b['x'] = 1
|
74
|
+
|
75
|
+
c = BSON::OrderedHash.new
|
76
|
+
c['x'] = 1
|
77
|
+
c['y'] = 2
|
78
|
+
|
79
|
+
d = BSON::OrderedHash.new
|
80
|
+
d['x'] = 2
|
81
|
+
d['y'] = 3
|
82
|
+
|
83
|
+
e = BSON::OrderedHash.new
|
84
|
+
e['z'] = 1
|
85
|
+
e['y'] = 2
|
86
|
+
|
87
|
+
assert_equal a, c
|
88
|
+
assert_not_equal a, b
|
89
|
+
assert_not_equal a, d
|
90
|
+
assert_not_equal a, e
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_order_preserved
|
94
|
+
assert_equal @ordered_keys, @oh.keys
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_replace
|
98
|
+
h1 = BSON::OrderedHash.new
|
99
|
+
h1[:a] = 1
|
100
|
+
h1[:b] = 2
|
101
|
+
|
102
|
+
h2 = BSON::OrderedHash.new
|
103
|
+
h2[:c] = 3
|
104
|
+
h2[:d] = 4
|
105
|
+
h1.replace(h2)
|
106
|
+
|
107
|
+
assert_equal [:c, :d], h1.keys
|
108
|
+
assert_equal [3, 4], h1.values
|
109
|
+
assert h1.keys.object_id != h2.keys.object_id
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_to_a_order_preserved
|
113
|
+
assert_equal @ordered_keys, @oh.to_a.map {|m| m.first}
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_order_preserved_after_replace
|
117
|
+
@oh['a'] = 42
|
118
|
+
assert_equal @ordered_keys, @oh.keys
|
119
|
+
@oh['c'] = 'foobar'
|
120
|
+
assert_equal @ordered_keys, @oh.keys
|
121
|
+
@oh['z'] = /huh?/
|
122
|
+
assert_equal @ordered_keys, @oh.keys
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_each
|
126
|
+
keys = []
|
127
|
+
@oh.each { |k, v| keys << k }
|
128
|
+
assert_equal keys, @oh.keys
|
129
|
+
|
130
|
+
@oh['z'] = 42
|
131
|
+
assert_equal keys, @oh.keys
|
132
|
+
|
133
|
+
assert_equal @oh, @oh.each {|k,v|}
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_values
|
137
|
+
assert_equal [1, 2, 3], @oh.values
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_merge
|
141
|
+
other = BSON::OrderedHash.new
|
142
|
+
other['f'] = 'foo'
|
143
|
+
noob = @oh.merge(other)
|
144
|
+
assert_equal @ordered_keys + ['f'], noob.keys
|
145
|
+
assert_equal [1, 2, 3, 'foo'], noob.values
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_merge_bang
|
149
|
+
other = BSON::OrderedHash.new
|
150
|
+
other['f'] = 'foo'
|
151
|
+
@oh.merge!(other)
|
152
|
+
assert_equal @ordered_keys + ['f'], @oh.keys
|
153
|
+
assert_equal [1, 2, 3, 'foo'], @oh.values
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_merge_bang_with_overlap
|
157
|
+
other = BSON::OrderedHash.new
|
158
|
+
other['a'] = 'apple'
|
159
|
+
other['c'] = 'crab'
|
160
|
+
other['f'] = 'foo'
|
161
|
+
@oh.merge!(other)
|
162
|
+
assert_equal @ordered_keys + ['f'], @oh.keys
|
163
|
+
assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_merge_bang_with_hash_with_overlap
|
167
|
+
other = Hash.new
|
168
|
+
other['a'] = 'apple'
|
169
|
+
other['c'] = 'crab'
|
170
|
+
other['f'] = 'foo'
|
171
|
+
@oh.merge!(other)
|
172
|
+
assert_equal @ordered_keys + ['f'], @oh.keys
|
173
|
+
assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_equality_with_hash
|
177
|
+
o = BSON::OrderedHash.new
|
178
|
+
o[:a] = 1
|
179
|
+
o[:b] = 2
|
180
|
+
o[:c] = 3
|
181
|
+
r = {:a => 1, :b => 2, :c => 3}
|
182
|
+
assert r == o
|
183
|
+
assert o == r
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_update
|
187
|
+
other = BSON::OrderedHash.new
|
188
|
+
other['f'] = 'foo'
|
189
|
+
noob = @oh.update(other)
|
190
|
+
assert_equal @ordered_keys + ['f'], noob.keys
|
191
|
+
assert_equal [1, 2, 3, 'foo'], noob.values
|
192
|
+
end
|
193
|
+
|
194
|
+
if RUBY_VERSION < "1.9.2"
|
195
|
+
def test_inspect_retains_order
|
196
|
+
assert_equal "#<BSON::OrderedHash:0x#{@oh.object_id.to_s(16)} {\"c\"=>1, \"a\"=>2, \"z\"=>3}>", @oh.inspect
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_clear
|
201
|
+
@oh.clear
|
202
|
+
assert @oh.keys.empty?
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_delete
|
206
|
+
assert @oh.keys.include?('z')
|
207
|
+
@oh.delete('z')
|
208
|
+
assert !@oh.keys.include?('z')
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_delete_if
|
212
|
+
assert @oh.keys.include?('z')
|
213
|
+
@oh.delete_if { |k,v| k == 'z' }
|
214
|
+
assert !@oh.keys.include?('z')
|
215
|
+
@oh.delete_if { |k, v| v > 0 }
|
216
|
+
assert @oh.keys.empty?
|
217
|
+
end
|
218
|
+
|
219
|
+
def test_reject
|
220
|
+
new = @oh.reject { |k, v| k == 'foo' }
|
221
|
+
assert new.keys == @oh.keys
|
222
|
+
|
223
|
+
new = @oh.reject { |k, v| k == 'z' }
|
224
|
+
assert !new.keys.include?('z')
|
225
|
+
end
|
226
|
+
|
227
|
+
def test_reject_bang
|
228
|
+
@oh.reject! { |k, v| k == 'z' }
|
229
|
+
assert !@oh.keys.include?('z')
|
230
|
+
assert_nil @oh.reject! { |k, v| k == 'z' }
|
231
|
+
end
|
232
|
+
|
233
|
+
def test_clone
|
234
|
+
copy = @oh.clone
|
235
|
+
assert copy.keys == @oh.keys
|
236
|
+
|
237
|
+
copy[:foo] = 1
|
238
|
+
assert copy.keys != @oh.keys
|
239
|
+
end
|
240
|
+
|
241
|
+
def test_dup
|
242
|
+
oh2 = @oh.dup
|
243
|
+
oh2['f'] = 9
|
244
|
+
assert_nil @oh['f']
|
245
|
+
assert_equal ['c', 'a', 'z'], @oh.keys
|
246
|
+
end
|
247
|
+
end
|