oj 2.9.4 → 2.9.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of oj might be problematic. Click here for more details.

@@ -1,125 +1,91 @@
1
1
  #!/usr/bin/env ruby
2
2
  # encoding: UTF-8
3
3
 
4
- # Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
5
- # required. That can be set in the RUBYOPT environment variable.
6
- # export RUBYOPT=-w
4
+ $: << File.dirname(__FILE__)
7
5
 
8
- $VERBOSE = true
6
+ require 'helper'
9
7
 
10
- $: << File.join(File.dirname(__FILE__), "../lib")
11
- $: << File.join(File.dirname(__FILE__), "../ext")
8
+ class Juice < Minitest::Test
12
9
 
13
- require 'test/unit'
14
- require 'stringio'
15
- require 'date'
16
- require 'bigdecimal'
17
- require 'oj'
10
+ class Jam
11
+ attr_accessor :x, :y
18
12
 
19
- $ruby = RUBY_DESCRIPTION.split(' ')[0]
20
- $ruby = 'ree' if 'ruby' == $ruby && RUBY_DESCRIPTION.include?('Ruby Enterprise Edition')
21
-
22
- def hash_eql(h1, h2)
23
- return false if h1.size != h2.size
24
- h1.keys.each do |k|
25
- return false unless h1[k] == h2[k]
26
- end
27
- true
28
- end
29
-
30
- class Jam
31
- attr_accessor :x, :y
32
-
33
- def initialize(x, y)
34
- @x = x
35
- @y = y
36
- end
13
+ def initialize(x, y)
14
+ @x = x
15
+ @y = y
16
+ end
37
17
 
38
- def eql?(o)
39
- self.class == o.class && @x == o.x && @y == o.y
40
- end
41
- alias == eql?
18
+ def eql?(o)
19
+ self.class == o.class && @x == o.x && @y == o.y
20
+ end
21
+ alias == eql?
42
22
 
43
- end# Jam
23
+ end# Jam
44
24
 
45
- class Jeez < Jam
46
- def initialize(x, y)
47
- super
48
- end
25
+ class Jeez < Jam
26
+ def initialize(x, y)
27
+ super
28
+ end
49
29
 
50
- def to_json()
51
- %{{"json_class":"#{self.class}","x":#{@x},"y":#{@y}}}
52
- end
30
+ def to_json()
31
+ %{{"json_class":"#{self.class}","x":#{@x},"y":#{@y}}}
32
+ end
53
33
 
54
- def self.json_create(h)
55
- self.new(h['x'], h['y'])
56
- end
57
- end# Jeez
34
+ def self.json_create(h)
35
+ self.new(h['x'], h['y'])
36
+ end
37
+ end# Jeez
58
38
 
59
- # contributed by sauliusg to fix as_json
60
- class Orange < Jam
61
- def initialize(x, y)
62
- super
63
- end
39
+ # contributed by sauliusg to fix as_json
40
+ class Orange < Jam
41
+ def initialize(x, y)
42
+ super
43
+ end
64
44
 
65
- def as_json()
66
- { :json_class => self.class,
67
- :x => @x,
68
- :y => @y }
69
- end
45
+ def as_json()
46
+ { :json_class => self.class,
47
+ :x => @x,
48
+ :y => @y }
49
+ end
70
50
 
71
- def self.json_create(h)
72
- self.new(h['x'], h['y'])
51
+ def self.json_create(h)
52
+ self.new(h['x'], h['y'])
53
+ end
73
54
  end
74
- end
75
55
 
76
- class Melon < Jam
77
- def initialize(x, y)
78
- super
79
- end
56
+ class Melon < Jam
57
+ def initialize(x, y)
58
+ super
59
+ end
80
60
 
81
- def as_json()
82
- "#{x} #{y}"
83
- end
61
+ def as_json()
62
+ "#{x} #{y}"
63
+ end
84
64
 
85
- def self.json_create(h)
86
- self.new(h['x'], h['y'])
65
+ def self.json_create(h)
66
+ self.new(h['x'], h['y'])
67
+ end
87
68
  end
88
- end
89
69
 
90
- class Jazz < Jam
91
- def initialize(x, y)
92
- super
93
- end
94
- def to_hash()
95
- { 'json_class' => self.class.to_s, 'x' => @x, 'y' => @y }
96
- end
97
- def self.json_create(h)
98
- self.new(h['x'], h['y'])
99
- end
100
- end# Jazz
70
+ class Jazz < Jam
71
+ def initialize(x, y)
72
+ super
73
+ end
74
+ def to_hash()
75
+ { 'json_class' => self.class.to_s, 'x' => @x, 'y' => @y }
76
+ end
77
+ def self.json_create(h)
78
+ self.new(h['x'], h['y'])
79
+ end
80
+ end# Jazz
101
81
 
102
- class Range
103
- def to_hash()
104
- { 'begin' => self.begin, 'end' => self.end, 'exclude_end' => self.exclude_end? }
82
+ def setup
83
+ @default_options = Oj.default_options
105
84
  end
106
- end # Range
107
85
 
108
- # define the symbol
109
- class ActiveSupport
110
- end
111
-
112
- class RailsLike
113
- attr_accessor :x
114
- def initialize(x)
115
- @x = x
116
- end
117
- def to_json(options = nil)
118
- Oj.dump(self, :mode => :compat)
86
+ def teardown
87
+ Oj.default_options = @default_options
119
88
  end
120
- end # RailsLike
121
-
122
- class Juice < ::Test::Unit::TestCase
123
89
 
124
90
  def test0_get_options
125
91
  opts = Oj.default_options()
@@ -225,6 +191,7 @@ class Juice < ::Test::Unit::TestCase
225
191
  end
226
192
 
227
193
  def test_string_object
194
+ Oj.default_options = {:mode => :object}
228
195
  dump_and_load('abc', false)
229
196
  dump_and_load(':abc', false)
230
197
  end
@@ -252,6 +219,11 @@ class Juice < ::Test::Unit::TestCase
252
219
  assert_equal(json, json2)
253
220
  end
254
221
 
222
+ def test_null_char
223
+ assert_raises(Oj::ParseError) { Oj.load("\"\0\"") }
224
+ assert_raises(Oj::ParseError) { Oj.load("\"\\\0\"") }
225
+ end
226
+
255
227
  def test_array
256
228
  dump_and_load([], false)
257
229
  dump_and_load([true, false], false)
@@ -262,7 +234,7 @@ class Juice < ::Test::Unit::TestCase
262
234
  def test_array_not_closed
263
235
  begin
264
236
  Oj.load('[')
265
- rescue Exception => e
237
+ rescue Exception
266
238
  assert(true)
267
239
  return
268
240
  end
@@ -271,6 +243,7 @@ class Juice < ::Test::Unit::TestCase
271
243
 
272
244
  # rails encoding tests
273
245
  def test_does_not_escape_entities_by_default
246
+ Oj.default_options = { :escape_mode => :ascii } # set in mimic mode
274
247
  # use Oj to create the hash since some Rubies don't deal nicely with unicode.
275
248
  json = %{{"key":"I <3 this\\u2028space"}}
276
249
  hash = Oj.load(json)
@@ -543,7 +516,7 @@ class Juice < ::Test::Unit::TestCase
543
516
  def test_hash_not_closed
544
517
  begin
545
518
  Oj.load('{')
546
- rescue Exception => e
519
+ rescue Exception
547
520
  assert(true)
548
521
  return
549
522
  end
@@ -570,9 +543,9 @@ class Juice < ::Test::Unit::TestCase
570
543
  Oj.default_options = { :mode => :compat, :use_to_json => true }
571
544
  obj = Jeez.new(true, 58)
572
545
  json = Oj.dump(obj, :indent => 2)
573
- assert(%{{"json_class":"Jeez","x":true,"y":58}
546
+ assert(%{{"json_class":"Juice::Jeez","x":true,"y":58}
574
547
  } == json ||
575
- %{{"json_class":"Jeez","y":58,"x":true}
548
+ %{{"json_class":"Juice::Jeez","y":58,"x":true}
576
549
  } == json)
577
550
  dump_and_load(obj, false)
578
551
  Oj.default_options = { :mode => :compat, :use_to_json => false }
@@ -580,7 +553,7 @@ class Juice < ::Test::Unit::TestCase
580
553
  def test_json_object_create_id
581
554
  Oj.default_options = { :mode => :compat, :create_id => 'kson_class' }
582
555
  expected = Jeez.new(true, 58)
583
- json = %{{"kson_class":"Jeez","x":true,"y":58}}
556
+ json = %{{"kson_class":"Juice::Jeez","x":true,"y":58}}
584
557
  obj = Oj.load(json)
585
558
  assert_equal(expected, obj)
586
559
  Oj.default_options = { :create_id => 'json_class' }
@@ -589,13 +562,13 @@ class Juice < ::Test::Unit::TestCase
589
562
  obj = Jeez.new(true, 58)
590
563
  json = Oj.dump(obj, :mode => :object, :indent => 2)
591
564
  assert(%{{
592
- "^o":"Jeez",
565
+ "^o":"Juice::Jeez",
593
566
  "x":true,
594
567
  "y":58
595
568
  }
596
569
  } == json ||
597
570
  %{{
598
- "^o":"Jeez",
571
+ "^o":"Juice::Jeez",
599
572
  "y":58,
600
573
  "x":true
601
574
  }
@@ -630,13 +603,13 @@ class Juice < ::Test::Unit::TestCase
630
603
  obj = Jazz.new(true, 58)
631
604
  json = Oj.dump(obj, :mode => :object, :indent => 2)
632
605
  assert(%{{
633
- "^o":"Jazz",
606
+ "^o":"Juice::Jazz",
634
607
  "x":true,
635
608
  "y":58
636
609
  }
637
610
  } == json ||
638
611
  %{{
639
- "^o":"Jazz",
612
+ "^o":"Juice::Jazz",
640
613
  "y":58,
641
614
  "x":true
642
615
  }
@@ -682,13 +655,13 @@ class Juice < ::Test::Unit::TestCase
682
655
  obj = Orange.new(true, 58)
683
656
  json = Oj.dump(obj, :mode => :object, :indent => 2)
684
657
  assert(%{{
685
- "^o":"Orange",
658
+ "^o":"Juice::Orange",
686
659
  "x":true,
687
660
  "y":58
688
661
  }
689
662
  } == json ||
690
663
  %{{
691
- "^o":"Orange",
664
+ "^o":"Juice::Orange",
692
665
  "y":58,
693
666
  "x":true
694
667
  }
@@ -731,13 +704,13 @@ class Juice < ::Test::Unit::TestCase
731
704
  obj = Jam.new(true, 58)
732
705
  json = Oj.dump(obj, :mode => :object, :indent => 2)
733
706
  assert(%{{
734
- "^o":"Jam",
707
+ "^o":"Juice::Jam",
735
708
  "x":true,
736
709
  "y":58
737
710
  }
738
711
  } == json ||
739
712
  %{{
740
- "^o":"Jam",
713
+ "^o":"Juice::Jam",
741
714
  "y":58,
742
715
  "x":true
743
716
  }
@@ -750,13 +723,13 @@ class Juice < ::Test::Unit::TestCase
750
723
  obj = Jam.new(true, 58)
751
724
  json = Oj.dump(obj, :mode => :object, :indent => 2)
752
725
  assert(%{{
753
- "^o":"Jam",
726
+ "^o":"Juice::Jam",
754
727
  "x":true,
755
728
  "y":58
756
729
  }
757
730
  } == json ||
758
731
  %{{
759
- "^o":"Jam",
732
+ "^o":"Juice::Jam",
760
733
  "y":58,
761
734
  "x":true
762
735
  }
@@ -932,6 +905,7 @@ class Juice < ::Test::Unit::TestCase
932
905
  end
933
906
  end
934
907
  def test_date_object
908
+ Oj.default_options = {:mode => :object}
935
909
  dump_and_load(Date.new(2012, 6, 19), false)
936
910
  end
937
911
 
@@ -958,17 +932,18 @@ class Juice < ::Test::Unit::TestCase
958
932
  assert_equal(orig.to_s, x)
959
933
  end
960
934
  def test_datetime_object
935
+ Oj.default_options = {:mode => :object}
961
936
  dump_and_load(DateTime.new(2012, 6, 19), false)
962
937
  end
963
938
 
964
939
  # autodefine Oj::Bag
965
940
  def test_bag
966
941
  json = %{{
967
- "^o":"Jem",
942
+ "^o":"Juice::Jem",
968
943
  "x":true,
969
944
  "y":58 }}
970
945
  obj = Oj.load(json, :mode => :object, :auto_define => true)
971
- assert_equal('Jem', obj.class.name)
946
+ assert_equal('Juice::Jem', obj.class.name)
972
947
  assert_equal(true, obj.x)
973
948
  assert_equal(58, obj.y)
974
949
  end
@@ -979,14 +954,14 @@ class Juice < ::Test::Unit::TestCase
979
954
  obj.x = obj
980
955
  json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
981
956
  assert(%{{
982
- "^o":"Jam",
957
+ "^o":"Juice::Jam",
983
958
  "^i":1,
984
959
  "x":"^r1",
985
960
  "y":58
986
961
  }
987
962
  } == json ||
988
963
  %{{
989
- "^o":"Jam",
964
+ "^o":"Juice::Jam",
990
965
  "^i":1,
991
966
  "y":58,
992
967
  "x":"^r1"
@@ -1026,7 +1001,7 @@ class Juice < ::Test::Unit::TestCase
1026
1001
  obj.x['b'] = obj
1027
1002
  json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
1028
1003
  ha = Oj.load(json, :mode => :strict)
1029
- assert_equal({'^o' => 'Jam', '^i' => 1, 'x' => { '^i' => 2, 'a' => 7, 'b' => '^r1' }, 'y' => 58 }, ha)
1004
+ assert_equal({'^o' => 'Juice::Jam', '^i' => 1, 'x' => { '^i' => 2, 'a' => 7, 'b' => '^r1' }, 'y' => 58 }, ha)
1030
1005
  Oj.load(json, :mode => :object, :circular => true)
1031
1006
  assert_equal(obj.x.__id__, h.__id__)
1032
1007
  assert_equal(h['b'].__id__, obj.__id__)
@@ -1048,7 +1023,7 @@ class Juice < ::Test::Unit::TestCase
1048
1023
  a = []
1049
1024
  10000.times { a << [a] }
1050
1025
  Oj.dump(a)
1051
- rescue Exception => e
1026
+ rescue Exception
1052
1027
  assert(true)
1053
1028
  return
1054
1029
  end
@@ -1068,7 +1043,7 @@ class Juice < ::Test::Unit::TestCase
1068
1043
 
1069
1044
  def test_io_file
1070
1045
  src = { 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}
1071
- filename = 'open_file_test.json'
1046
+ filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
1072
1047
  File.open(filename, "w") { |f|
1073
1048
  Oj.to_stream(f, src)
1074
1049
  }
@@ -1129,7 +1104,7 @@ class Juice < ::Test::Unit::TestCase
1129
1104
  def test_nilnil_false
1130
1105
  begin
1131
1106
  Oj.load(nil)
1132
- rescue Exception => e
1107
+ rescue Exception
1133
1108
  assert(true)
1134
1109
  return
1135
1110
  end
@@ -1141,18 +1116,10 @@ class Juice < ::Test::Unit::TestCase
1141
1116
  assert_equal(nil, obj)
1142
1117
  end
1143
1118
 
1144
- # Rails re-call test. Active support recalls the json dumper when the to_json
1145
- # method is called. This mimics that and verifies Oj detects it.
1146
- def test_rails_like
1147
- obj = RailsLike.new(3)
1148
- json = Oj.dump(obj, :mode => :compat)
1149
- assert_equal('{"x":3}', json)
1150
- end
1151
-
1152
1119
  def dump_and_load(obj, trace=false)
1153
1120
  json = Oj.dump(obj, :indent => 2)
1154
1121
  puts json if trace
1155
- loaded = Oj.load(json);
1122
+ loaded = Oj.load(json)
1156
1123
  assert_equal(obj, loaded)
1157
1124
  loaded
1158
1125
  end
@@ -1,21 +1,19 @@
1
1
  #!/usr/bin/env ruby
2
2
  # encoding: UTF-8
3
3
 
4
- # Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
5
- # required. That can be set in the RUBYOPT environment variable.
6
- # export RUBYOPT=-w
4
+ $: << File.dirname(__FILE__)
7
5
 
8
- $VERBOSE = true
6
+ require 'helper'
9
7
 
10
- $: << File.join(File.dirname(__FILE__), "../lib")
11
- $: << File.join(File.dirname(__FILE__), "../ext")
8
+ class OjWriter < Minitest::Test
12
9
 
13
- require 'test/unit'
14
- require 'stringio'
15
- require 'date'
16
- require 'oj'
10
+ def setup
11
+ @default_options = Oj.default_options
12
+ end
17
13
 
18
- class OjWriter < ::Test::Unit::TestCase
14
+ def teardown
15
+ Oj.default_options = @default_options
16
+ end
19
17
 
20
18
  def test_string_writer_empty_array
21
19
  w = Oj::StringWriter.new(:indent => 0)
@@ -249,7 +247,7 @@ class OjWriter < ::Test::Unit::TestCase
249
247
  end
250
248
 
251
249
  def test_stream_writer_mixed_file
252
- filename = 'open_file_writer_test.json'
250
+ filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
253
251
  File.open(filename, "w") do |f|
254
252
  w = Oj::StreamWriter.new(f, :indent => 0)
255
253
  w.push_object()