Skip to content

Commit 21141c7

Browse files
committed
Fix remaining 1.8.7 failures in pure ruby
1 parent 6b90b26 commit 21141c7

File tree

8 files changed

+91
-50
lines changed

8 files changed

+91
-50
lines changed

ruby/lib/bson.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class String
3535
# In versions prior to 1.9 we need to ignore the encoding requests.
3636
#
3737
# @since 2.0.0
38+
def chr; self; end
3839
def force_encoding(*); self; end
3940
def encode(*); self; end
4041
def encode!(*); self; end

ruby/lib/bson/document.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ module BSON
2121
# @since 2.0.0
2222
class Document < ::Hash
2323

24+
# Message for argument error when providing bad arguments to [].
25+
#
26+
# @since 2.0.0
27+
ARG_ERROR = "An even number of arguments must be passed to BSON::Document[]."
28+
2429
# Sets a value for the provided key.
2530
#
2631
# @example Set the value in the document.
@@ -298,7 +303,7 @@ def reject(&block)
298303
# @since 2.0.0
299304
def replace(other)
300305
super
301-
@order = other.order.dup
306+
@order = other.keys
302307
self
303308
end
304309

@@ -392,6 +397,21 @@ def values
392397

393398
class << self
394399

400+
# Create a new document given the provided arguments. The args can either
401+
# be empty in order to instantiate an empty document, or an array of
402+
# key/value pairs in the order that they should remain in.
403+
#
404+
# @example Create a new empty document.
405+
# BSON::Document[]
406+
#
407+
# @example Create a new document with the provided elements.
408+
# BSON::Document[1, 2, 3, 4]
409+
#
410+
# @param [ Array<Object> ] args The key/value pairs.
411+
#
412+
# @return [ BSON::Document ] The new document.
413+
#
414+
# @since 2.0.0
395415
def [](*args)
396416
document = new
397417

@@ -403,9 +423,7 @@ def [](*args)
403423
return document
404424
end
405425

406-
unless (args.size % 2 == 0)
407-
raise ArgumentError.new("odd number of arguments for Hash")
408-
end
426+
raise ArgumentError.new(ARG_ERROR) unless (args.size % 2 == 0)
409427

410428
args.each_with_index do |val, ind|
411429
next if (ind % 2 != 0)

ruby/lib/bson/object_id.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def from_time(time, options = {})
216216
#
217217
# @since 2.0.0
218218
def legal?(string)
219-
/\A\h{24}\Z/ === string.to_s
219+
string.to_s =~ /^[0-9a-f]{24}$/i ? true : false
220220
end
221221
end
222222

ruby/spec/bson/array_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
let(:type) { 4.chr }
99
let(:obj) {[ "one", "two" ]}
1010
let(:bson) do
11-
{ "0" => "one", "1" => "two" }.to_bson
11+
BSON::Document["0", "one", "1", "two"].to_bson
1212
end
1313

1414
it_behaves_like "a bson element"

ruby/spec/bson/document_spec.rb

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -524,18 +524,21 @@
524524
end
525525
end
526526

527-
context "when provided hashes" do
527+
if ordered_hash_support?
528528

529-
let(:alternate) do
530-
described_class[1 => 2, 3 => 4]
531-
end
529+
context "when provided hashes" do
532530

533-
it "sets the keys" do
534-
expect(alternate.keys).to eq([ 1, 3 ])
535-
end
531+
let(:alternate) do
532+
described_class[1 => 2, 3 => 4]
533+
end
536534

537-
it "sets the values" do
538-
expect(alternate.values).to eq([ 2, 4 ])
535+
it "sets the keys" do
536+
expect(alternate.keys).to eq([ 1, 3 ])
537+
end
538+
539+
it "sets the values" do
540+
expect(alternate.values).to eq([ 2, 4 ])
541+
end
539542
end
540543
end
541544
end
@@ -598,7 +601,7 @@
598601
context "when the hash is a single level" do
599602

600603
let(:obj) do
601-
described_class["key" => "value"]
604+
described_class["key","value"]
602605
end
603606

604607
let(:bson) do
@@ -613,7 +616,7 @@
613616
context "when the hash is embedded" do
614617

615618
let(:obj) do
616-
described_class["field" => { "key" => "value" }]
619+
described_class["field", BSON::Document["key", "value"]]
617620
end
618621

619622
let(:bson) do
@@ -632,7 +635,7 @@
632635
context "when the keys are utf-8" do
633636

634637
let(:document) do
635-
described_class["gültig" => "type"]
638+
described_class["gültig", "type"]
636639
end
637640

638641
it_behaves_like "a document able to handle utf-8"
@@ -641,7 +644,7 @@
641644
context "when the values are utf-8" do
642645

643646
let(:document) do
644-
described_class["type" => "gültig"]
647+
described_class["type", "gültig"]
645648
end
646649

647650
it_behaves_like "a document able to handle utf-8"
@@ -650,7 +653,7 @@
650653
context "when both the keys and values are utf-8" do
651654

652655
let(:document) do
653-
described_class["gültig" => "gültig"]
656+
described_class["gültig", "gültig"]
654657
end
655658

656659
it_behaves_like "a document able to handle utf-8"
@@ -659,7 +662,7 @@
659662
context "when the regexps are utf-8" do
660663

661664
let(:document) do
662-
described_class["type" => /^gültig/]
665+
described_class["type", /^gültig/]
663666
end
664667

665668
it_behaves_like "a document able to handle utf-8"
@@ -668,7 +671,7 @@
668671
context "when the symbols are utf-8" do
669672

670673
let(:document) do
671-
described_class["type" => "gültig".to_sym]
674+
described_class["type", "gültig".to_sym]
672675
end
673676

674677
it_behaves_like "a document able to handle utf-8"
@@ -677,7 +680,7 @@
677680
context "when utf-8 string values are in an array" do
678681

679682
let(:document) do
680-
described_class["type" => ["gültig"]]
683+
described_class["type", ["gültig"]]
681684
end
682685

683686
it_behaves_like "a document able to handle utf-8"
@@ -686,7 +689,7 @@
686689
context "when utf-8 code values are present" do
687690

688691
let(:document) do
689-
described_class["code" => BSON::Code.new("// gültig")]
692+
described_class["code", BSON::Code.new("// gültig")]
690693
end
691694

692695
it_behaves_like "a document able to handle utf-8"
@@ -695,7 +698,7 @@
695698
pending "when utf-8 code with scope values are present" do
696699

697700
let(:document) do
698-
described_class["code" => BSON::CodeWithScope.new("// gültig", {})]
701+
described_class["code", BSON::CodeWithScope.new("// gültig", {})]
699702
end
700703

701704
it_behaves_like "a document able to handle utf-8"
@@ -705,7 +708,7 @@
705708

706709
let(:string) { "gültig" }
707710
let(:document) do
708-
described_class["type" => string.encode("iso-8859-1")]
711+
described_class["type", string.encode("iso-8859-1")]
709712
end
710713

711714
it "encodes and decodes the document properly" do
@@ -719,7 +722,7 @@
719722

720723
let(:string) { "europäischen" }
721724
let(:document) do
722-
described_class["type" => string.encode("binary", "binary")]
725+
described_class["type", string.encode("binary", "binary")]
723726
end
724727

725728
it "encodes and decodes the document properly" do

ruby/spec/bson/object_id_spec.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@
276276
end
277277

278278
it "does not include process or sequence information" do
279-
expect(object_id.to_s =~ /\A\h{8}0{16}\Z/).to be_true
279+
expect(object_id.to_s =~ /\A[0-9a-f]{8}[0]{16}\Z/).to be_true
280280
end
281281
end
282282

@@ -380,8 +380,11 @@
380380
expect(object_id.to_s).to eq(expected)
381381
end
382382

383-
it "returns the string in UTF-8" do
384-
expect(object_id.to_s.encoding).to eq(Encoding.find(BSON::UTF8))
383+
if ordered_hash_support?
384+
385+
it "returns the string in UTF-8" do
386+
expect(object_id.to_s.encoding).to eq(Encoding.find(BSON::UTF8))
387+
end
385388
end
386389

387390
it "converts to a readable yaml string" do

ruby/spec/bson/registry_spec.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,20 @@
2222

2323
context "when the type has no corresponding class" do
2424

25-
it "raises an error" do
26-
expect {
27-
described_class.get("test")
28-
}.to raise_error(KeyError)
25+
if ordered_hash_support?
26+
27+
it "raises an error" do
28+
expect {
29+
described_class.get("test")
30+
}.to raise_error(KeyError)
31+
end
32+
else
33+
34+
it "raises an error" do
35+
expect {
36+
described_class.get("test")
37+
}.to raise_error(IndexError)
38+
end
2939
end
3040
end
3141
end

ruby/spec/bson/string_spec.rb

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,19 @@
8888
it_behaves_like "a binary encoded string"
8989
end
9090

91-
context "when the string contains non utf-8 characters" do
91+
if ordered_hash_support?
9292

93-
let(:string) do
94-
255.chr
95-
end
93+
context "when the string contains non utf-8 characters" do
9694

97-
it "raises an error" do
98-
expect {
99-
string.to_bson_cstring
100-
}.to raise_error(EncodingError)
95+
let(:string) do
96+
255.chr
97+
end
98+
99+
it "raises an error" do
100+
expect {
101+
string.to_bson_cstring
102+
}.to raise_error(EncodingError)
103+
end
101104
end
102105
end
103106
end
@@ -180,16 +183,19 @@
180183
it_behaves_like "a binary encoded string"
181184
end
182185

183-
context "when the string contains non utf-8 characters" do
186+
if ordered_hash_support?
184187

185-
let(:string) do
186-
255.chr
187-
end
188+
context "when the string contains non utf-8 characters" do
188189

189-
it "raises an error" do
190-
expect {
191-
string.to_bson_string
192-
}.to raise_error(EncodingError)
190+
let(:string) do
191+
255.chr
192+
end
193+
194+
it "raises an error" do
195+
expect {
196+
string.to_bson_string
197+
}.to raise_error(EncodingError)
198+
end
193199
end
194200
end
195201
end

0 commit comments

Comments
 (0)