oj 3.13.23 → 3.16.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +81 -0
  3. data/README.md +2 -2
  4. data/ext/oj/buf.h +7 -6
  5. data/ext/oj/cache.c +29 -26
  6. data/ext/oj/cache.h +3 -2
  7. data/ext/oj/cache8.c +10 -9
  8. data/ext/oj/circarray.c +7 -5
  9. data/ext/oj/circarray.h +2 -2
  10. data/ext/oj/code.c +5 -12
  11. data/ext/oj/code.h +2 -2
  12. data/ext/oj/compat.c +20 -60
  13. data/ext/oj/custom.c +26 -59
  14. data/ext/oj/debug.c +3 -9
  15. data/ext/oj/dump.c +103 -53
  16. data/ext/oj/dump.h +1 -4
  17. data/ext/oj/dump_compat.c +557 -592
  18. data/ext/oj/dump_leaf.c +3 -5
  19. data/ext/oj/dump_object.c +42 -48
  20. data/ext/oj/dump_strict.c +10 -22
  21. data/ext/oj/encoder.c +1 -1
  22. data/ext/oj/err.c +2 -13
  23. data/ext/oj/err.h +9 -12
  24. data/ext/oj/extconf.rb +16 -7
  25. data/ext/oj/fast.c +60 -92
  26. data/ext/oj/intern.c +62 -47
  27. data/ext/oj/intern.h +3 -7
  28. data/ext/oj/mem.c +318 -0
  29. data/ext/oj/mem.h +53 -0
  30. data/ext/oj/mimic_json.c +51 -32
  31. data/ext/oj/object.c +33 -43
  32. data/ext/oj/odd.c +8 -6
  33. data/ext/oj/odd.h +4 -4
  34. data/ext/oj/oj.c +243 -212
  35. data/ext/oj/oj.h +83 -81
  36. data/ext/oj/parse.c +94 -148
  37. data/ext/oj/parse.h +21 -24
  38. data/ext/oj/parser.c +80 -67
  39. data/ext/oj/parser.h +7 -8
  40. data/ext/oj/rails.c +70 -92
  41. data/ext/oj/reader.c +9 -14
  42. data/ext/oj/reader.h +4 -2
  43. data/ext/oj/resolve.c +3 -4
  44. data/ext/oj/rxclass.c +6 -5
  45. data/ext/oj/rxclass.h +1 -1
  46. data/ext/oj/saj.c +10 -9
  47. data/ext/oj/saj2.c +37 -49
  48. data/ext/oj/saj2.h +1 -1
  49. data/ext/oj/scp.c +3 -14
  50. data/ext/oj/sparse.c +22 -70
  51. data/ext/oj/stream_writer.c +45 -41
  52. data/ext/oj/strict.c +20 -52
  53. data/ext/oj/string_writer.c +64 -38
  54. data/ext/oj/trace.h +31 -4
  55. data/ext/oj/usual.c +125 -114
  56. data/ext/oj/usual.h +7 -6
  57. data/ext/oj/util.h +1 -1
  58. data/ext/oj/val_stack.c +13 -2
  59. data/ext/oj/val_stack.h +8 -7
  60. data/ext/oj/wab.c +25 -57
  61. data/lib/oj/active_support_helper.rb +1 -3
  62. data/lib/oj/bag.rb +7 -1
  63. data/lib/oj/easy_hash.rb +4 -5
  64. data/lib/oj/error.rb +0 -1
  65. data/lib/oj/json.rb +162 -150
  66. data/lib/oj/mimic.rb +7 -7
  67. data/lib/oj/schandler.rb +5 -4
  68. data/lib/oj/state.rb +8 -5
  69. data/lib/oj/version.rb +1 -2
  70. data/lib/oj.rb +2 -0
  71. data/pages/InstallOptions.md +20 -0
  72. data/pages/Options.md +4 -0
  73. data/test/_test_active.rb +8 -9
  74. data/test/_test_active_mimic.rb +7 -8
  75. data/test/_test_mimic_rails.rb +17 -20
  76. data/test/activerecord/result_test.rb +5 -6
  77. data/test/activesupport6/encoding_test.rb +63 -28
  78. data/test/activesupport7/abstract_unit.rb +4 -1
  79. data/test/activesupport7/encoding_test.rb +72 -22
  80. data/test/files.rb +15 -15
  81. data/test/foo.rb +18 -69
  82. data/test/helper.rb +5 -8
  83. data/test/isolated/shared.rb +3 -2
  84. data/test/json_gem/json_addition_test.rb +2 -2
  85. data/test/json_gem/json_common_interface_test.rb +8 -6
  86. data/test/json_gem/json_encoding_test.rb +0 -0
  87. data/test/json_gem/json_ext_parser_test.rb +1 -0
  88. data/test/json_gem/json_fixtures_test.rb +3 -2
  89. data/test/json_gem/json_generator_test.rb +50 -33
  90. data/test/json_gem/json_generic_object_test.rb +11 -11
  91. data/test/json_gem/json_parser_test.rb +46 -46
  92. data/test/json_gem/json_string_matching_test.rb +9 -9
  93. data/test/mem.rb +13 -12
  94. data/test/perf.rb +21 -26
  95. data/test/perf_compat.rb +31 -33
  96. data/test/perf_dump.rb +28 -28
  97. data/test/perf_fast.rb +80 -82
  98. data/test/perf_file.rb +27 -29
  99. data/test/perf_object.rb +65 -69
  100. data/test/perf_once.rb +12 -11
  101. data/test/perf_parser.rb +42 -48
  102. data/test/perf_saj.rb +46 -54
  103. data/test/perf_scp.rb +57 -69
  104. data/test/perf_simple.rb +41 -39
  105. data/test/perf_strict.rb +68 -70
  106. data/test/perf_wab.rb +67 -69
  107. data/test/prec.rb +5 -5
  108. data/test/sample/change.rb +0 -1
  109. data/test/sample/dir.rb +0 -1
  110. data/test/sample/doc.rb +0 -1
  111. data/test/sample/file.rb +0 -1
  112. data/test/sample/group.rb +0 -1
  113. data/test/sample/hasprops.rb +0 -1
  114. data/test/sample/layer.rb +0 -1
  115. data/test/sample/rect.rb +0 -1
  116. data/test/sample/shape.rb +0 -1
  117. data/test/sample/text.rb +0 -1
  118. data/test/sample.rb +16 -16
  119. data/test/sample_json.rb +8 -8
  120. data/test/test_compat.rb +81 -54
  121. data/test/test_custom.rb +63 -52
  122. data/test/test_debian.rb +7 -10
  123. data/test/test_fast.rb +86 -90
  124. data/test/test_file.rb +24 -29
  125. data/test/test_gc.rb +5 -5
  126. data/test/test_generate.rb +5 -5
  127. data/test/test_hash.rb +4 -4
  128. data/test/test_integer_range.rb +9 -9
  129. data/test/test_null.rb +20 -20
  130. data/test/test_object.rb +92 -87
  131. data/test/test_parser.rb +4 -4
  132. data/test/test_parser_debug.rb +5 -5
  133. data/test/test_parser_saj.rb +27 -25
  134. data/test/test_parser_usual.rb +44 -6
  135. data/test/test_rails.rb +2 -2
  136. data/test/test_saj.rb +10 -8
  137. data/test/test_scp.rb +35 -35
  138. data/test/test_strict.rb +38 -32
  139. data/test/test_various.rb +146 -97
  140. data/test/test_wab.rb +46 -44
  141. data/test/test_writer.rb +63 -47
  142. data/test/tests.rb +7 -7
  143. data/test/tests_mimic.rb +6 -6
  144. data/test/tests_mimic_addition.rb +6 -6
  145. metadata +46 -26
  146. data/test/activesupport4/decoding_test.rb +0 -108
  147. data/test/activesupport4/encoding_test.rb +0 -531
  148. data/test/activesupport4/test_helper.rb +0 -41
  149. data/test/activesupport5/abstract_unit.rb +0 -45
  150. data/test/activesupport5/decoding_test.rb +0 -133
  151. data/test/activesupport5/encoding_test.rb +0 -500
  152. data/test/activesupport5/encoding_test_cases.rb +0 -98
  153. data/test/activesupport5/test_helper.rb +0 -72
  154. data/test/activesupport5/time_zone_test_helpers.rb +0 -39
  155. data/test/bar.rb +0 -11
  156. data/test/baz.rb +0 -16
  157. data/test/bug.rb +0 -16
  158. data/test/zoo.rb +0 -13
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << File.dirname(__FILE__)
4
+ $LOAD_PATH << __dir__
5
5
 
6
6
  require 'helper'
7
- #Oj.mimic_JSON
7
+ # Oj.mimic_JSON
8
8
  require 'rails/all'
9
9
 
10
10
  require 'active_model'
@@ -36,14 +36,12 @@ end
36
36
  class MimicRails < Minitest::Test
37
37
 
38
38
  def test_mimic_exception
39
- begin
40
- ActiveSupport::JSON.decode("{")
41
- puts "Failed"
42
- rescue ActiveSupport::JSON.parse_error
43
- assert(true)
44
- rescue Exception
45
- assert(false, 'Expected a JSON::ParserError')
46
- end
39
+ ActiveSupport::JSON.decode('{')
40
+ puts 'Failed'
41
+ rescue ActiveSupport::JSON.parse_error
42
+ assert(true)
43
+ rescue Exception
44
+ assert(false, 'Expected a JSON::ParserError')
47
45
  end
48
46
 
49
47
  def test_dump_string
@@ -84,18 +82,17 @@ class MimicRails < Minitest::Test
84
82
  category = Category.new(1, 'test')
85
83
  serializer = CategorySerializer.new(category)
86
84
 
87
- json = serializer.to_json()
85
+ serializer.to_json()
88
86
  puts "*** serializer.to_json() #{serializer.to_json()}"
89
- json = serializer.as_json()
87
+ serializer.as_json()
90
88
  puts "*** serializer.as_json() #{serializer.as_json()}"
91
- json = JSON.dump(serializer)
89
+ JSON.dump(serializer)
92
90
  puts "*** JSON.dump(serializer) #{JSON.dump(serializer)}"
93
91
 
94
92
  puts "*** category.to_json() #{category.to_json()}"
95
93
  puts "*** category.as_json() #{category.as_json()}"
96
94
  puts "*** JSON.dump(serializer) #{JSON.dump(category)}"
97
95
  puts "*** Oj.dump(serializer) #{Oj.dump(category)}"
98
-
99
96
  end
100
97
 
101
98
  def test_dump_object_array
@@ -104,7 +101,7 @@ class MimicRails < Minitest::Test
104
101
  cat2 = Category.new(2, 'test')
105
102
  a = Array.wrap([cat1, cat2])
106
103
 
107
- #serializer = CategorySerializer.new(a)
104
+ # serializer = CategorySerializer.new(a)
108
105
 
109
106
  puts "*** a.to_json() #{a.to_json()}"
110
107
  puts "*** a.as_json() #{a.as_json()}"
@@ -114,13 +111,13 @@ class MimicRails < Minitest::Test
114
111
 
115
112
  def test_dump_time
116
113
  Oj.default_options= {:indent => 2}
117
- now = ActiveSupport::TimeZone['America/Chicago'].parse("2014-11-01 13:20:47")
114
+ now = ActiveSupport::TimeZone['America/Chicago'].parse('2014-11-01 13:20:47')
118
115
  json = Oj.dump(now, mode: :object, time_format: :xmlschema)
119
- #puts "*** json: #{json}"
116
+ # puts "*** json: #{json}"
120
117
 
121
118
  oj_dump = Oj.load(json, mode: :object, time_format: :xmlschema)
122
- #puts "Now: #{now}\n Oj: #{oj_dump}"
123
- assert_equal("2014-11-01T13:20:47-05:00", oj_dump.xmlschema)
119
+ # puts "Now: #{now}\n Oj: #{oj_dump}"
120
+ assert_equal('2014-11-01T13:20:47-05:00', oj_dump.xmlschema)
124
121
  end
125
122
 
126
123
  end # MimicRails
@@ -13,13 +13,12 @@ Oj.default_options = { mode: :rails }
13
13
 
14
14
  class ActiveRecordResultTest < Minitest::Test
15
15
  def test_hash_rows
16
-
17
16
  result = ActiveRecord::Result.new(["one", "two"],
18
- [
19
- ["row 1 col 1", "row 1 col 2"],
20
- ["row 2 col 1", "row 2 col 2"],
21
- ["row 3 col 1", "row 3 col 2"],
22
- ])
17
+ [
18
+ ["row 1 col 1", "row 1 col 2"],
19
+ ["row 2 col 1", "row 2 col 2"],
20
+ ["row 3 col 1", "row 3 col 2"],
21
+ ])
23
22
  #puts "*** result: #{Oj.dump(result, indent: 2)}"
24
23
  json_result = if ActiveRecord.version >= Gem::Version.new("6")
25
24
  result.to_a
@@ -74,42 +74,77 @@ class TestJSONEncoding < ActiveSupport::TestCase
74
74
  ActiveSupport.escape_html_entities_in_json = false
75
75
  end
76
76
 
77
- def test_utf8_string_encoded_properly
78
- # The original test seems to expect that
79
- # ActiveSupport.escape_html_entities_in_json reverts to true even after
80
- # being set to false. I haven't been able to figure that out so the value is
81
- # set to true, the default, before running the test. This might be wrong but
82
- # for now it will have to do.
83
- ActiveSupport.escape_html_entities_in_json = true
84
- result = ActiveSupport::JSON.encode("€2.99")
85
- assert_equal '"€2.99"', result
86
- assert_equal(Encoding::UTF_8, result.encoding)
87
-
88
- result = ActiveSupport::JSON.encode("✎☺")
89
- assert_equal '"✎☺"', result
90
- assert_equal(Encoding::UTF_8, result.encoding)
77
+ def test_hash_keys_encoding_without_escaping
78
+ assert_equal "{\"<>\":\"<>\"}", ActiveSupport::JSON.encode("<>" => "<>")
91
79
  end
92
80
 
93
- def test_non_utf8_string_transcodes
94
- s = "二".encode("Shift_JIS")
95
- result = ActiveSupport::JSON.encode(s)
96
- assert_equal '""', result
97
- assert_equal Encoding::UTF_8, result.encoding
81
+ module UnicodeTests
82
+ def test_utf8_string_encoded_properly
83
+ result = ActiveSupport::JSON.encode("€2.99")
84
+ assert_equal '"€2.99"', result
85
+ assert_equal(Encoding::UTF_8, result.encoding)
86
+
87
+ result = ActiveSupport::JSON.encode("✎☺")
88
+ assert_equal '"✎☺"', result
89
+ assert_equal(Encoding::UTF_8, result.encoding)
90
+ end
91
+
92
+ def test_non_utf8_string_transcodes
93
+ s = "二".encode("Shift_JIS")
94
+ result = ActiveSupport::JSON.encode(s)
95
+ assert_equal '"二"', result
96
+ assert_equal Encoding::UTF_8, result.encoding
97
+ end
98
+
99
+ def test_wide_utf8_chars
100
+ w = "𠜎"
101
+ result = ActiveSupport::JSON.encode(w)
102
+ assert_equal '"𠜎"', result
103
+ end
104
+
105
+ def test_wide_utf8_roundtrip
106
+ hash = { string: "𐒑" }
107
+ json = ActiveSupport::JSON.encode(hash)
108
+ decoded_hash = ActiveSupport::JSON.decode(json)
109
+ assert_equal "𐒑", decoded_hash["string"]
110
+ end
111
+
112
+ def test_invalid_encoding_raises
113
+ s = "\xAE\xFF\x9F"
114
+ refute s.valid_encoding?
115
+
116
+ # n.b. this raises EncodingError, because we didn't call Oj.mimic_JSON in the test setup; but,
117
+ # if you do that (even indirectly through Oj.optimize_rails), then this raises a
118
+ # JSON::GeneratorError instead of an EncodingError.
119
+ assert_raises(EncodingError) do
120
+ ActiveSupport::JSON.encode([s])
121
+ end
122
+ end
98
123
  end
99
124
 
100
- def test_wide_utf8_chars
101
- w = "𠜎"
102
- result = ActiveSupport::JSON.encode(w)
103
- assert_equal '"𠜎"', result
125
+ module UnicodeTestsWithEscapingOn
126
+ def setup
127
+ ActiveSupport.escape_html_entities_in_json = true
128
+ end
129
+
130
+ def teardown
131
+ ActiveSupport.escape_html_entities_in_json = false
132
+ end
133
+
134
+ include UnicodeTests
104
135
  end
105
136
 
106
- def test_wide_utf8_roundtrip
107
- hash = { string: "𐒑" }
108
- json = ActiveSupport::JSON.encode(hash)
109
- decoded_hash = ActiveSupport::JSON.decode(json)
110
- assert_equal "𐒑", decoded_hash["string"]
137
+ module UnicodeTestsWithEscapingOff
138
+ def setup
139
+ ActiveSupport.escape_html_entities_in_json = false
140
+ end
141
+
142
+ include UnicodeTests
111
143
  end
112
144
 
145
+ include UnicodeTestsWithEscapingOn
146
+ include UnicodeTestsWithEscapingOff
147
+
113
148
  def test_hash_key_identifiers_are_always_quoted
114
149
  values = { 0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B" }
115
150
  assert_equal %w( "$" "A" "A0" "A0B" "_" "a" "0" "1" ).sort, object_keys(ActiveSupport::JSON.encode(values))
@@ -19,7 +19,10 @@ require "active_support"
19
19
  Thread.abort_on_exception = true
20
20
 
21
21
  # Show backtraces for deprecated behavior for quicker cleanup.
22
- ActiveSupport::Deprecation.debug = true
22
+ if ActiveSupport::Deprecation.respond_to?(:debug)
23
+ # Rails 7.2 does not have ActiveSupport::Deprecation.debug
24
+ ActiveSupport::Deprecation.debug = true
25
+ end
23
26
 
24
27
  # Default to old to_time behavior but allow running tests with new behavior
25
28
  ActiveSupport.to_time_preserves_timezone = ENV["PRESERVE_TIMEZONES"] == "1"
@@ -8,9 +8,18 @@ require "active_support/time"
8
8
  require_relative "time_zone_test_helpers"
9
9
  require_relative "encoding_test_cases"
10
10
 
11
+ require 'oj'
12
+ # Sets the ActiveSupport encoder to be Oj and also wraps the setting of globals.
13
+ Oj::Rails.set_encoder()
14
+ Oj::Rails.optimize()
15
+
11
16
  class TestJSONEncoding < ActiveSupport::TestCase
12
17
  include TimeZoneTestHelpers
13
18
 
19
+ def test_is_actually_oj
20
+ assert_equal Oj::Rails::Encoder, ActiveSupport.json_encoder
21
+ end
22
+
14
23
  def sorted_json(json)
15
24
  if json.start_with?("{") && json.end_with?("}")
16
25
  "{" + json[1..-2].split(",").sort.join(",") + "}"
@@ -61,36 +70,77 @@ class TestJSONEncoding < ActiveSupport::TestCase
61
70
  ActiveSupport.escape_html_entities_in_json = false
62
71
  end
63
72
 
64
- def test_utf8_string_encoded_properly
65
- result = ActiveSupport::JSON.encode("€2.99")
66
- assert_equal '"€2.99"', result
67
- assert_equal(Encoding::UTF_8, result.encoding)
68
-
69
- result = ActiveSupport::JSON.encode("✎☺")
70
- assert_equal '"✎☺"', result
71
- assert_equal(Encoding::UTF_8, result.encoding)
73
+ def test_hash_keys_encoding_without_escaping
74
+ assert_equal "{\"<>\":\"<>\"}", ActiveSupport::JSON.encode("<>" => "<>")
72
75
  end
73
76
 
74
- def test_non_utf8_string_transcodes
75
- s = "二".encode("Shift_JIS")
76
- result = ActiveSupport::JSON.encode(s)
77
- assert_equal '""', result
78
- assert_equal Encoding::UTF_8, result.encoding
77
+ module UnicodeTests
78
+ def test_utf8_string_encoded_properly
79
+ result = ActiveSupport::JSON.encode("€2.99")
80
+ assert_equal '"€2.99"', result
81
+ assert_equal(Encoding::UTF_8, result.encoding)
82
+
83
+ result = ActiveSupport::JSON.encode("✎☺")
84
+ assert_equal '"✎☺"', result
85
+ assert_equal(Encoding::UTF_8, result.encoding)
86
+ end
87
+
88
+ def test_non_utf8_string_transcodes
89
+ s = "二".encode("Shift_JIS")
90
+ result = ActiveSupport::JSON.encode(s)
91
+ assert_equal '"二"', result
92
+ assert_equal Encoding::UTF_8, result.encoding
93
+ end
94
+
95
+ def test_wide_utf8_chars
96
+ w = "𠜎"
97
+ result = ActiveSupport::JSON.encode(w)
98
+ assert_equal '"𠜎"', result
99
+ end
100
+
101
+ def test_wide_utf8_roundtrip
102
+ hash = { string: "𐒑" }
103
+ json = ActiveSupport::JSON.encode(hash)
104
+ decoded_hash = ActiveSupport::JSON.decode(json)
105
+ assert_equal "𐒑", decoded_hash["string"]
106
+ end
107
+
108
+ def test_invalid_encoding_raises
109
+ s = "\xAE\xFF\x9F"
110
+ refute s.valid_encoding?
111
+
112
+ # n.b. this raises EncodingError, because we didn't call Oj.mimic_JSON in the test setup; but,
113
+ # if you do that (even indirectly through Oj.optimize_rails), then this raises a
114
+ # JSON::GeneratorError instead of an EncodingError.
115
+ assert_raises(EncodingError) do
116
+ ActiveSupport::JSON.encode([s])
117
+ end
118
+ end
79
119
  end
80
120
 
81
- def test_wide_utf8_chars
82
- w = "𠜎"
83
- result = ActiveSupport::JSON.encode(w)
84
- assert_equal '"𠜎"', result
121
+ module UnicodeTestsWithEscapingOn
122
+ def setup
123
+ ActiveSupport.escape_html_entities_in_json = true
124
+ end
125
+
126
+ def teardown
127
+ ActiveSupport.escape_html_entities_in_json = false
128
+ end
129
+
130
+ include UnicodeTests
85
131
  end
86
132
 
87
- def test_wide_utf8_roundtrip
88
- hash = { string: "𐒑" }
89
- json = ActiveSupport::JSON.encode(hash)
90
- decoded_hash = ActiveSupport::JSON.decode(json)
91
- assert_equal "𐒑", decoded_hash["string"]
133
+ module UnicodeTestsWithEscapingOff
134
+ def setup
135
+ ActiveSupport.escape_html_entities_in_json = false
136
+ end
137
+
138
+ include UnicodeTests
92
139
  end
93
140
 
141
+ include UnicodeTestsWithEscapingOn
142
+ include UnicodeTestsWithEscapingOff
143
+
94
144
  def test_hash_key_identifiers_are_always_quoted
95
145
  values = { 0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B" }
96
146
  assert_equal %w( "$" "A" "A0" "A0B" "_" "a" "0" "1" ).sort, object_keys(ActiveSupport::JSON.encode(values))
data/test/files.rb CHANGED
@@ -1,29 +1,29 @@
1
1
  #!/usr/bin/env ruby -wW2
2
+ # frozen_string_literal: true
2
3
 
3
- if $0 == __FILE__
4
- $: << '.'
5
- $: << '..'
6
- $: << '../lib'
7
- $: << '../ext'
4
+ if $PROGRAM_NAME == __FILE__
5
+ $LOAD_PATH << '.'
6
+ $LOAD_PATH << '..'
7
+ $LOAD_PATH << '../lib'
8
+ $LOAD_PATH << '../ext'
8
9
  end
9
10
 
10
- require 'pp'
11
11
  require 'sample/file'
12
12
  require 'sample/dir'
13
13
 
14
14
  def files(dir)
15
- d = ::Sample::Dir.new(dir)
15
+ d = Sample::Dir.new(dir)
16
16
  Dir.new(dir).each do |fn|
17
17
  next if fn.start_with?('.')
18
+
18
19
  filename = File.join(dir, fn)
19
- #filename = '.' == dir ? fn : File.join(dir, fn)
20
- if File.directory?(filename)
21
- d << files(filename)
22
- else
23
- d << ::Sample::File.new(filename)
24
- end
20
+ # filename = '.' == dir ? fn : File.join(dir, fn)
21
+ d << if File.directory?(filename)
22
+ files(filename)
23
+ else
24
+ Sample::File.new(filename)
25
+ end
25
26
  end
26
- #pp d
27
+ # pp d
27
28
  d
28
29
  end
29
-
data/test/foo.rb CHANGED
@@ -1,77 +1,26 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- $: << '.'
4
- $: << File.join(File.dirname(__FILE__), "../lib")
5
- $: << File.join(File.dirname(__FILE__), "../ext")
4
+ $LOAD_PATH << '.'
5
+ $LOAD_PATH << File.join(__dir__, '../lib')
6
+ $LOAD_PATH << File.join(__dir__, '../ext')
6
7
 
7
- require "oj"
8
- require "socket"
9
- require 'io/nonblock'
8
+ require 'oj'
10
9
 
11
- #pid = spawn("nc -d 0.1 -l 5000", out: "/dev/null")
12
- pid = spawn("nc -i 1 -l 7777", out: "/dev/null")
13
- at_exit { Process.kill 9, pid }
14
- sleep 0.2
15
- s = Socket.tcp("localhost", 7777)
16
- s.nonblock = false
17
- 1_000_000.times do |x|
18
- Oj.to_stream(s, { x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]})
19
- end
10
+ reader, writer = IO.pipe
20
11
 
21
- =begin
22
- IO.pipe do |r, w|
23
- if fork
24
- r.close
25
- #w.nonblock = false
26
- 1_000_000.times do |i|
27
- begin
28
- Oj.to_stream(w, { x: i})
29
- rescue IOError => e
30
- puts "*** #{i} raised #{e.class}: #{e}"
31
- IO.select(nil, [w])
32
- retry
33
- end
34
- w.puts
12
+ thread =
13
+ Thread.new do
14
+ 5.times do |id|
15
+ Oj.to_stream(writer, { "id" => id })
16
+ sleep(1)
35
17
  end
36
- else
37
- w.close
38
- sleep(0.1)
39
- r.each_line { |b|
40
- #print b
41
- }
42
- r.close
43
- Process.exit(0)
44
- end
45
- end
46
- =end
47
18
 
48
- =begin
49
- IO.pipe do |r, w|
50
- if fork
51
- r.close
52
- #w.nonblock = false
53
- a = []
54
- 10_000.times do |i|
55
- a << i
56
- end
57
- begin
58
- Oj.to_stream(w, a, indent: 2)
59
- rescue IOError => e
60
- puts "*** raised #{e.class}: #{e}"
61
- puts "*** fileno: #{w.fileno}"
62
- puts "*** is an IO?: #{w.kind_of?(IO)}"
63
- IO.select(nil, [w])
64
- retry
65
- end
66
- w.puts
67
- else
68
- w.close
69
- sleep(0.5)
70
- r.each_line { |b|
71
- #print b
72
- }
73
- r.close
74
- Process.exit(0)
19
+ writer.close
75
20
  end
76
- end
77
- =end
21
+
22
+ p = Oj::Parser.new(:usual)
23
+ p.load(reader) { |data| puts "#{Time.now} -- ID: #{data["id"]}" }
24
+
25
+ reader.close
26
+ thread.join
data/test/helper.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- #
2
+
3
3
  # Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
4
4
  # required. That can be set in the RUBYOPT environment variable.
5
5
  # export RUBYOPT=-w
@@ -15,15 +15,13 @@ require 'minitest/autorun'
15
15
  require 'stringio'
16
16
  require 'date'
17
17
  require 'bigdecimal'
18
- require 'pp'
19
18
  require 'oj'
20
19
 
21
-
22
20
  def verify_gc_compaction
23
21
  # This method was added in Ruby 3.0.0. Calling it this way asks the GC to
24
22
  # move objects around, helping to find object movement bugs.
25
- if defined?(GC.verify_compaction_references) == 'method' && !(RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/)
26
- if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.2.0")
23
+ if defined?(GC.verify_compaction_references) == 'method' && RbConfig::CONFIG['host_os'] !~ /(mingw|mswin)/
24
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.2.0')
27
25
  GC.verify_compaction_references(expand_heap: true, toward: :empty)
28
26
  else
29
27
  GC.verify_compaction_references(double_heap: true, toward: :empty)
@@ -31,12 +29,11 @@ def verify_gc_compaction
31
29
  end
32
30
  end
33
31
 
34
-
35
32
  $ruby = RUBY_DESCRIPTION.split(' ')[0]
36
33
  $ruby = 'ree' if 'ruby' == $ruby && RUBY_DESCRIPTION.include?('Ruby Enterprise Edition')
37
34
 
38
35
  class Range
39
- def to_hash()
40
- { 'begin' => self.begin, 'end' => self.end, 'exclude_end' => self.exclude_end? }
36
+ def to_hash
37
+ { 'begin' => self.begin, 'end' => self.end, 'exclude_end' => exclude_end? }
41
38
  end
42
39
  end
@@ -21,7 +21,7 @@ class SharedMimicTest < Minitest::Test
21
21
  alias == eql?
22
22
 
23
23
  def as_json()
24
- {"json_class" => self.class.to_s,"x" => @x,"y" => @y}
24
+ {"json_class" => self.class.to_s, "x" => @x, "y" => @y}
25
25
  end
26
26
 
27
27
  def self.json_create(h)
@@ -207,7 +207,6 @@ class SharedMimicTest < Minitest::Test
207
207
  --],#
208
208
  --"a"~:*1#
209
209
  }} == json)
210
-
211
210
  end
212
211
 
213
212
  # fast_generate
@@ -261,6 +260,8 @@ class SharedMimicTest < Minitest::Test
261
260
  obj = JSON.parse(json, :create_additions => true)
262
261
  JSON.create_id = 'json_class'
263
262
  assert_equal(jam, obj)
263
+
264
+ assert_nothing_raised { JSON.create_id = nil }
264
265
  end
265
266
  def test_parse_bang
266
267
  json = %{{"a":1,"b":[true,false]}}
@@ -191,9 +191,9 @@ class JSONAdditionTest < Test::Unit::TestCase
191
191
  assert_equal d, JSON.parse(d.to_json, :create_additions => true)
192
192
  d = DateTime.parse(now.utc.to_s) # of = 0
193
193
  assert_equal d, JSON.parse(d.to_json, :create_additions => true)
194
- d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(1,24))
194
+ d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(1, 24))
195
195
  assert_equal d, JSON.parse(d.to_json, :create_additions => true)
196
- d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12,24))
196
+ d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12, 24))
197
197
  assert_equal d, JSON.parse(d.to_json, :create_additions => true)
198
198
  end
199
199
 
@@ -29,7 +29,7 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase
29
29
  #@json = '{"a":2,"b":5.23683071,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\
30
30
  #'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
31
31
  @json = '{"a":2,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\
32
- '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
32
+ '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
33
33
  end
34
34
 
35
35
  def test_index
@@ -43,15 +43,15 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase
43
43
  # functionality and perform better.
44
44
 
45
45
  def test_parser
46
- assert_match /::Parser\z/, JSON.parser.name
46
+ assert_match(/::Parser\z/, JSON.parser.name)
47
47
  end
48
48
 
49
49
  def test_generator
50
- assert_match /::Generator\z/, JSON.generator.name
50
+ assert_match(/::Generator\z/, JSON.generator.name)
51
51
  end
52
52
 
53
53
  def test_state
54
- assert_match /::Generator::State\z/, JSON.state.name
54
+ assert_match(/::Generator::State\z/, JSON.state.name)
55
55
  end
56
56
 
57
57
  # This doesn't have anything to do with JSON parsing or generation. It seems
@@ -128,8 +128,10 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase
128
128
  too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
129
129
  assert_equal too_deep, JSON.dump(eval(too_deep))
130
130
  assert_kind_of String, Marshal.dump(eval(too_deep))
131
- assert_raise(ArgumentError) { JSON.dump(eval(too_deep), 100) }
132
- assert_raise(ArgumentError) { Marshal.dump(eval(too_deep), 100) }
131
+ if RUBY_ENGINE != 'truffleruby'
132
+ assert_raise(ArgumentError) { JSON.dump(eval(too_deep), 100) }
133
+ assert_raise(ArgumentError) { Marshal.dump(eval(too_deep), 100) }
134
+ end
133
135
  assert_equal too_deep, JSON.dump(eval(too_deep), 101)
134
136
  assert_kind_of String, Marshal.dump(eval(too_deep), 101)
135
137
  output = StringIO.new
File without changes
@@ -2,6 +2,7 @@
2
2
  # encoding: UTF-8
3
3
 
4
4
  #frozen_string_literal: false
5
+
5
6
  require 'json_gem/test_helper'
6
7
 
7
8
  class JSONExtParserTest < Test::Unit::TestCase
@@ -2,6 +2,7 @@
2
2
  # encoding: UTF-8
3
3
 
4
4
  #frozen_string_literal: false
5
+
5
6
  require 'json_gem/test_helper'
6
7
 
7
8
  class JSONFixturesTest < Test::Unit::TestCase
@@ -16,7 +17,7 @@ class JSONFixturesTest < Test::Unit::TestCase
16
17
  for name, source in @passed
17
18
  begin
18
19
  assert JSON.parse(source),
19
- "Did not pass for fixture '#{name}': #{source.inspect}"
20
+ "Did not pass for fixture '#{name}': #{source.inspect}"
20
21
  rescue => e
21
22
  warn "\nCaught #{e.class}(#{e}) for fixture '#{name}': #{source.inspect}\n#{e.backtrace * "\n"}"
22
23
  raise e
@@ -27,7 +28,7 @@ class JSONFixturesTest < Test::Unit::TestCase
27
28
  def test_failing
28
29
  for name, source in @failed
29
30
  assert_raise(JSON::ParserError, JSON::NestingError,
30
- "Did not fail for fixture '#{name}': #{source.inspect}") do
31
+ "Did not fail for fixture '#{name}': #{source.inspect}") do
31
32
  JSON.parse(source)
32
33
  end
33
34
  end