summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/psych/test_coder.rb15
-rw-r--r--test/psych/test_json_tree.rb18
-rw-r--r--test/psych/test_nil.rb18
-rw-r--r--test/psych/test_parser.rb60
-rw-r--r--test/psych/visitors/test_depth_first.rb23
5 files changed, 127 insertions, 7 deletions
diff --git a/test/psych/test_coder.rb b/test/psych/test_coder.rb
index 0fa01ca..4809b13 100644
--- a/test/psych/test_coder.rb
+++ b/test/psych/test_coder.rb
@@ -89,6 +89,21 @@ module Psych
end
end
+ class RepresentWithObject
+ def encode_with coder
+ coder.represent_object self.class.name, 20
+ end
+ end
+
+ def test_represent_with_object
+ thing = Psych.load(Psych.dump(RepresentWithObject.new))
+ assert_equal 20, thing
+ end
+
+ def test_json_dump_exclude_tag
+ refute_match('TestCoder::InitApi', Psych.to_json(InitApi.new))
+ end
+
def test_map_takes_block
coder = Psych::Coder.new 'foo'
tag = coder.tag
diff --git a/test/psych/test_json_tree.rb b/test/psych/test_json_tree.rb
index 84bd36c..5fcb0d6 100644
--- a/test/psych/test_json_tree.rb
+++ b/test/psych/test_json_tree.rb
@@ -3,11 +3,11 @@ require_relative 'helper'
module Psych
class TestJSONTree < TestCase
def test_string
- assert_match(/(['"])foo\1/, Psych.to_json("foo"))
+ assert_match(/"foo"/, Psych.to_json("foo"))
end
def test_symbol
- assert_match(/(['"])foo\1/, Psych.to_json(:foo))
+ assert_match(/"foo"/, Psych.to_json(:foo))
end
def test_nil
@@ -36,8 +36,18 @@ module Psych
json = Psych.to_json(list)
assert_match(/]$/, json)
assert_match(/^\[/, json)
- assert_match(/['"]one['"]/, json)
- assert_match(/['"]two['"]/, json)
+ assert_match(/"one"/, json)
+ assert_match(/"two"/, json)
+ end
+
+ def test_time
+ time = Time.new(2010, 10, 10).utc
+ assert_equal "{\"a\": \"2010-10-10 07:00:00.000000000Z\"}\n", Psych.to_json({'a' => time })
+ end
+
+ def test_datetime
+ time = Time.new(2010, 10, 10).to_datetime
+ assert_equal "{\"a\": \"#{time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")}\"}\n", Psych.to_json({'a' => time })
end
end
end
diff --git a/test/psych/test_nil.rb b/test/psych/test_nil.rb
new file mode 100644
index 0000000..e63c6c7
--- /dev/null
+++ b/test/psych/test_nil.rb
@@ -0,0 +1,18 @@
+require_relative 'helper'
+
+module Psych
+ class TestNil < TestCase
+ def test_nil
+ yml = Psych.dump nil
+ assert_equal "--- \n...\n", yml
+ assert_equal nil, Psych.load(yml)
+ end
+
+ def test_array_nil
+ yml = Psych.dump [nil]
+ assert_equal "---\n- \n", yml
+ assert_equal [nil], Psych.load(yml)
+ end
+
+ end
+end
diff --git a/test/psych/test_parser.rb b/test/psych/test_parser.rb
index 0b1e92e..cd22914 100644
--- a/test/psych/test_parser.rb
+++ b/test/psych/test_parser.rb
@@ -5,9 +5,12 @@ require_relative 'helper'
module Psych
class TestParser < TestCase
class EventCatcher < Handler
- attr_reader :calls
+ attr_accessor :parser
+ attr_reader :calls, :marks
def initialize
- @calls = []
+ @parser = nil
+ @calls = []
+ @marks = []
end
(Handler.instance_methods(true) -
@@ -15,6 +18,7 @@ module Psych
class_eval %{
def #{m} *args
super
+ @marks << @parser.mark if @parser
@calls << [:#{m}, args]
end
}
@@ -23,7 +27,57 @@ module Psych
def setup
super
- @parser = Psych::Parser.new EventCatcher.new
+ @handler = EventCatcher.new
+ @parser = Psych::Parser.new @handler
+ @handler.parser = @parser
+ end
+
+ def test_line_numbers
+ assert_equal 0, @parser.mark.line
+ @parser.parse "---\n- hello\n- world"
+ line_calls = @handler.marks.map(&:line).zip(@handler.calls.map(&:first))
+ assert_equal [[0, :start_stream],
+ [0, :start_document],
+ [1, :start_sequence],
+ [2, :scalar],
+ [3, :scalar],
+ [3, :end_sequence],
+ [3, :end_document],
+ [3, :end_stream]], line_calls
+
+ assert_equal 3, @parser.mark.line
+ end
+
+ def test_column_numbers
+ assert_equal 0, @parser.mark.column
+ @parser.parse "---\n- hello\n- world"
+ col_calls = @handler.marks.map(&:column).zip(@handler.calls.map(&:first))
+ assert_equal [[0, :start_stream],
+ [3, :start_document],
+ [1, :start_sequence],
+ [0, :scalar],
+ [0, :scalar],
+ [0, :end_sequence],
+ [0, :end_document],
+ [0, :end_stream]], col_calls
+
+ assert_equal 0, @parser.mark.column
+ end
+
+ def test_index_numbers
+ assert_equal 0, @parser.mark.index
+ @parser.parse "---\n- hello\n- world"
+ idx_calls = @handler.marks.map(&:index).zip(@handler.calls.map(&:first))
+ assert_equal [[0, :start_stream],
+ [3, :start_document],
+ [5, :start_sequence],
+ [12, :scalar],
+ [19, :scalar],
+ [19, :end_sequence],
+ [19, :end_document],
+ [19, :end_stream]], idx_calls
+
+ assert_equal 19, @parser.mark.index
end
def test_set_encoding_twice
diff --git a/test/psych/visitors/test_depth_first.rb b/test/psych/visitors/test_depth_first.rb
new file mode 100644
index 0000000..a783960
--- /dev/null
+++ b/test/psych/visitors/test_depth_first.rb
@@ -0,0 +1,23 @@
+require_relative '../helper'
+
+module Psych
+ module Visitors
+ class TestDepthFirst < TestCase
+ def test_scalar
+ collector = Class.new(Struct.new(:calls)) {
+ def initialize(calls = [])
+ super
+ end
+
+ def call obj
+ calls << obj
+ end
+ }.new
+ visitor = Visitors::DepthFirst.new collector
+ visitor.accept Psych.parse '--- hello'
+
+ assert_equal 3, collector.calls.length
+ end
+ end
+ end
+end