Skip to content
This repository has been archived by the owner on Feb 5, 2018. It is now read-only.

Commit

Permalink
added missing methods to State + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flori authored and mernen committed Aug 7, 2010
1 parent deeda14 commit 5f8ba2f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/json/pure/generator.rb
Expand Up @@ -163,6 +163,10 @@ def initialize(opts = {})
# the generated JSON, max_nesting = 0 if no maximum is checked.
attr_accessor :max_nesting

# This integer returns the current depth data structure nesting in the
# generated JSON.
attr_accessor :depth

def check_max_nesting(depth) # :nodoc:
return if @max_nesting.zero?
current_nesting = depth + 1
Expand Down Expand Up @@ -196,6 +200,7 @@ def configure(opts)
@array_nl = opts[:array_nl] if opts.key?(:array_nl)
@allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
@ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
@depth = opts[:depth] || 0
if !opts.key?(:max_nesting) # defaults to 19
@max_nesting = 19
elsif opts[:max_nesting]
Expand All @@ -210,7 +215,7 @@ def configure(opts)
# passed to the configure method.
def to_h
result = {}
for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting]
for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only depth]
result[iv.intern] = instance_variable_get("@#{iv}")
end
result
Expand Down
45 changes: 45 additions & 0 deletions tests/test_json_generate.rb
Expand Up @@ -102,6 +102,51 @@ def test_states
assert s[:check_circular?]
end

def test_pretty_state
state = PRETTY_STATE_PROTOTYPE.dup
assert_equal({
:allow_nan => false,
:array_nl => "\n",
:ascii_only => false,
:depth => 0,
:indent => " ",
:max_nesting => 19,
:object_nl => "\n",
:space => " ",
:space_before => "",
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
end

def test_safe_state
state = SAFE_STATE_PROTOTYPE.dup
assert_equal({
:allow_nan => false,
:array_nl => "",
:ascii_only => false,
:depth => 0,
:indent => "",
:max_nesting => 19,
:object_nl => "",
:space => "",
:space_before => "",
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
end

def test_fast_state
state = FAST_STATE_PROTOTYPE.dup
assert_equal({
:allow_nan => false,
:array_nl => "",
:ascii_only => false,
:depth => 0,
:indent => "",
:max_nesting => 0,
:object_nl => "",
:space => "",
:space_before => "",
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
end

def test_allow_nan
assert_raises(GeneratorError) { generate([JSON::NaN]) }
assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true)
Expand Down

0 comments on commit 5f8ba2f

Please sign in to comment.