summaryrefslogtreecommitdiff
path: root/test/json/json_ext_parser_test.rb
blob: 9db8ae772f7170f3a1a794aca9f6a7aa326cecaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true
require_relative 'test_helper'

class JSONExtParserTest < Test::Unit::TestCase
  if defined?(JSON::Ext::Parser)
    include JSON

    def test_allocate
      parser = JSON::Ext::Parser.new("{}")
      assert_raise(TypeError, '[ruby-core:35079]') do
        parser.__send__(:initialize, "{}")
      end
      parser = JSON::Ext::Parser.allocate
      assert_raise(TypeError, '[ruby-core:35079]') { parser.source }
    end

    def test_error_messages
      ex = assert_raise(ParserError) { parse('Infinity') }
      assert_equal "unexpected token at 'Infinity'", ex.message

      unless RUBY_PLATFORM =~ /java/
        ex = assert_raise(ParserError) { parse('-Infinity') }
        assert_equal "unexpected token at '-Infinity'", ex.message
      end

      ex = assert_raise(ParserError) { parse('NaN') }
      assert_equal "unexpected token at 'NaN'", ex.message
    end

    if GC.respond_to?(:stress=)
      def test_gc_stress_parser_new
        payload = JSON.dump([{ foo: 1, bar: 2, baz: 3, egg: { spam: 4 } }] * 10)

        previous_stress = GC.stress
        JSON::Parser.new(payload).parse
      ensure
        GC.stress = previous_stress
      end

      def test_gc_stress
        payload = JSON.dump([{ foo: 1, bar: 2, baz: 3, egg: { spam: 4 } }] * 10)

        previous_stress = GC.stress
        JSON.parse(payload)
      ensure
        GC.stress = previous_stress
      end
    end

    def parse(json)
      JSON::Ext::Parser.new(json).parse
    end
  end
end