summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2010-08-07 01:18:56 +0200
committerFlorian Frank <flori@ping.de>2010-08-07 01:20:52 +0200
commit310c96b4134f8c0f5d63822a25ccdbc83c30e86e (patch)
tree8f8f34719090dc0d272ce564135f3f203ad5e0cb
parentf8fcc04c9f1d5de3c035a71a507af5023a0406b0 (diff)
downloadjson-310c96b4134f8c0f5d63822a25ccdbc83c30e86e.tar.gz
added missing methods to State + tests
-rw-r--r--lib/json/pure/generator.rb7
-rwxr-xr-xtests/test_json_generate.rb45
2 files changed, 51 insertions, 1 deletions
diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb
index 0584108..12959bf 100644
--- a/lib/json/pure/generator.rb
+++ b/lib/json/pure/generator.rb
@@ -163,6 +163,10 @@ module JSON
# 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
@@ -196,6 +200,7 @@ module JSON
@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]
@@ -210,7 +215,7 @@ module JSON
# 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
diff --git a/tests/test_json_generate.rb b/tests/test_json_generate.rb
index 26ca1ad..8dc804b 100755
--- a/tests/test_json_generate.rb
+++ b/tests/test_json_generate.rb
@@ -102,6 +102,51 @@ EOT
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)