summaryrefslogtreecommitdiff
path: root/test/psych
diff options
context:
space:
mode:
Diffstat (limited to 'test/psych')
-rw-r--r--test/psych/test_coder.rb6
-rw-r--r--test/psych/test_date_time.rb20
-rw-r--r--test/psych/test_encoding.rb11
-rw-r--r--test/psych/test_numeric.rb9
-rw-r--r--test/psych/test_object_references.rb8
-rw-r--r--test/psych/test_parser.rb31
-rw-r--r--test/psych/test_psych.rb33
-rw-r--r--test/psych/test_scalar_scanner.rb2
-rw-r--r--test/psych/test_set.rb7
-rw-r--r--test/psych/test_string.rb12
-rw-r--r--test/psych/test_yaml.rb34
-rw-r--r--test/psych/visitors/test_emitter.rb16
-rw-r--r--test/psych/visitors/test_to_ruby.rb2
-rw-r--r--test/psych/visitors/test_yaml_tree.rb8
14 files changed, 149 insertions, 50 deletions
diff --git a/test/psych/test_coder.rb b/test/psych/test_coder.rb
index b2be0a4109..a6f5ad7f36 100644
--- a/test/psych/test_coder.rb
+++ b/test/psych/test_coder.rb
@@ -220,6 +220,8 @@ module Psych
end
def test_coder_style_map_any
+ pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/
+
foo = Psych.dump CustomEncode.new \
map: {a: 1, b: 2},
style: Psych::Nodes::Mapping::ANY,
@@ -228,6 +230,8 @@ module Psych
end
def test_coder_style_map_block
+ pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/
+
foo = Psych.dump CustomEncode.new \
map: {a: 1, b: 2},
style: Psych::Nodes::Mapping::BLOCK,
@@ -236,6 +240,8 @@ module Psych
end
def test_coder_style_map_flow
+ pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/
+
foo = Psych.dump CustomEncode.new \
map: { a: 1, b: 2 },
style: Psych::Nodes::Mapping::FLOW,
diff --git a/test/psych/test_date_time.rb b/test/psych/test_date_time.rb
index 6f1e8b509e..3379bd24bf 100644
--- a/test/psych/test_date_time.rb
+++ b/test/psych/test_date_time.rb
@@ -44,6 +44,26 @@ module Psych
assert_match(/12:00:00-05:00/, cycled.last.to_s)
end
+ def test_julian_date
+ d = Date.new(1582, 10, 4, Date::GREGORIAN)
+ assert_cycle d
+ end
+
+ def test_proleptic_gregorian_date
+ d = Date.new(1582, 10, 14, Date::GREGORIAN)
+ assert_cycle d
+ end
+
+ def test_julian_datetime
+ dt = DateTime.new(1582, 10, 4, 23, 58, 59, 0, Date::GREGORIAN)
+ assert_cycle dt
+ end
+
+ def test_proleptic_gregorian_datetime
+ dt = DateTime.new(1582, 10, 14, 23, 58, 59, 0, Date::GREGORIAN)
+ assert_cycle dt
+ end
+
def test_invalid_date
assert_cycle "2013-10-31T10:40:07-000000000000033"
end
diff --git a/test/psych/test_encoding.rb b/test/psych/test_encoding.rb
index e5831c9045..1867d59ea6 100644
--- a/test/psych/test_encoding.rb
+++ b/test/psych/test_encoding.rb
@@ -13,13 +13,13 @@ module Psych
(Handler.instance_methods(true) -
Object.instance_methods).each do |m|
- class_eval %{
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
def #{m} *args
@strings += args.flatten.find_all { |a|
String === a
}
end
- }
+ RUBY
end
end
@@ -119,6 +119,8 @@ module Psych
end
def test_emit_alias
+ pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/
+
@emitter.start_stream Psych::Parser::UTF8
@emitter.start_document [], [], true
e = assert_raise(RuntimeError) do
@@ -151,6 +153,7 @@ module Psych
@emitter.end_mapping
@emitter.end_document false
@emitter.end_stream
+ pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/
@parser.parse @buffer.string
assert_encodings @utf8, @handler.strings
@@ -170,6 +173,7 @@ module Psych
@emitter.end_sequence
@emitter.end_document false
@emitter.end_stream
+ pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/
@parser.parse @buffer.string
assert_encodings @utf8, @handler.strings
@@ -187,6 +191,7 @@ module Psych
@emitter.scalar 'foo', nil, nil, true, false, Nodes::Scalar::ANY
@emitter.end_document false
@emitter.end_stream
+ pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/
@parser.parse @buffer.string
assert_encodings @utf8, @handler.strings
@@ -263,6 +268,8 @@ module Psych
end
def test_dump_non_ascii_string_to_file
+ pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/
+
Tempfile.create(['utf8', 'yml'], :encoding => 'UTF-8') do |t|
h = {'one' => 'いち'}
Psych.dump(h, t)
diff --git a/test/psych/test_numeric.rb b/test/psych/test_numeric.rb
index 9c75c016cd..4c012e4562 100644
--- a/test/psych/test_numeric.rb
+++ b/test/psych/test_numeric.rb
@@ -1,6 +1,9 @@
# frozen_string_literal: true
require_relative 'helper'
-require 'bigdecimal'
+begin
+ require 'bigdecimal'
+rescue LoadError
+end
module Psych
###
@@ -29,13 +32,13 @@ module Psych
def test_big_decimal_tag
decimal = BigDecimal("12.34")
assert_match "!ruby/object:BigDecimal", Psych.dump(decimal)
- end
+ end if defined?(BigDecimal)
def test_big_decimal_round_trip
decimal = BigDecimal("12.34")
$DEBUG = false
assert_cycle decimal
- end
+ end if defined?(BigDecimal)
def test_does_not_attempt_numeric
str = Psych.load('--- 4 roses')
diff --git a/test/psych/test_object_references.rb b/test/psych/test_object_references.rb
index 269d72242e..86bb9034b9 100644
--- a/test/psych/test_object_references.rb
+++ b/test/psych/test_object_references.rb
@@ -39,7 +39,7 @@ module Psych
rescue Psych::DisallowedClass
data = Psych.unsafe_load yml
end
- assert_equal data.first.object_id, data.last.object_id
+ assert_same data.first, data.last
end
def test_float_references
@@ -49,7 +49,7 @@ module Psych
- *name
eoyml
assert_equal data.first, data.last
- assert_equal data.first.object_id, data.last.object_id
+ assert_same data.first, data.last
end
def test_binary_references
@@ -60,7 +60,7 @@ module Psych
- *name
eoyml
assert_equal data.first, data.last
- assert_equal data.first.object_id, data.last.object_id
+ assert_same data.first, data.last
end
def test_regexp_references
@@ -70,7 +70,7 @@ module Psych
- *name
eoyml
assert_equal data.first, data.last
- assert_equal data.first.object_id, data.last.object_id
+ assert_same data.first, data.last
end
end
end
diff --git a/test/psych/test_parser.rb b/test/psych/test_parser.rb
index 3604e7c985..c1e0abb89d 100644
--- a/test/psych/test_parser.rb
+++ b/test/psych/test_parser.rb
@@ -16,13 +16,13 @@ module Psych
(Handler.instance_methods(true) -
Object.instance_methods).each do |m|
- class_eval %{
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
def #{m} *args
super
@marks << @parser.mark if @parser
@calls << [:#{m}, args]
end
- }
+ RUBY
end
end
@@ -85,6 +85,8 @@ module Psych
def test_line_numbers
assert_equal 0, @parser.mark.line
+ pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/
+
@parser.parse "---\n- hello\n- world"
line_calls = @handler.marks.map(&:line).zip(@handler.calls.map(&:first))
assert_equal [
@@ -110,6 +112,8 @@ module Psych
def test_column_numbers
assert_equal 0, @parser.mark.column
+ pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/
+
@parser.parse "---\n- hello\n- world"
col_calls = @handler.marks.map(&:column).zip(@handler.calls.map(&:first))
assert_equal [
@@ -135,6 +139,8 @@ module Psych
def test_index_numbers
assert_equal 0, @parser.mark.index
+ pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/
+
@parser.parse "---\n- hello\n- world"
idx_calls = @handler.marks.map(&:index).zip(@handler.calls.map(&:first))
assert_equal [
@@ -352,6 +358,8 @@ module Psych
end
def test_event_location
+ pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/
+
@parser.parse "foo:\n" \
" barbaz: [1, 2]"
@@ -376,6 +384,25 @@ module Psych
[:end_stream, [2, 0, 2, 0]]], events
end
+ if Psych::Parser.method_defined?(:code_point_limit)
+ def test_code_point_limit
+ yaml = "foo: bar\n" * 500_000
+ assert_raise(org.snakeyaml.engine.v2.exceptions.YamlEngineException) do
+ Psych.load(yaml)
+ end
+
+ assert_nothing_raised do
+ begin
+ old_code_point_limit, Psych::Parser.code_point_limit = Psych::Parser::code_point_limit, 5_000_000
+
+ Psych.load(yaml)
+ ensure
+ Psych::Parser.code_point_limit = old_code_point_limit
+ end
+ end
+ end
+ end
+
def assert_called call, with = nil, parser = @parser
if with
call = parser.handler.calls.find { |x|
diff --git a/test/psych/test_psych.rb b/test/psych/test_psych.rb
index 1abd69ceca..42586a8779 100644
--- a/test/psych/test_psych.rb
+++ b/test/psych/test_psych.rb
@@ -419,12 +419,41 @@ eoyml
end
def test_safe_dump_symbols
+ assert_equal Psych.dump(:foo), Psych.safe_dump(:foo, permitted_classes: [Symbol])
+ assert_equal Psych.dump(:foo), Psych.safe_dump(:foo, permitted_symbols: [:foo])
+
error = assert_raise Psych::DisallowedClass do
- Psych.safe_dump(:foo, permitted_classes: [Symbol])
+ Psych.safe_dump(:foo)
end
assert_equal "Tried to dump unspecified class: Symbol(:foo)", error.message
- assert_match(/\A--- :foo\n(?:\.\.\.\n)?\z/, Psych.safe_dump(:foo, permitted_classes: [Symbol], permitted_symbols: [:foo]))
+ assert_match(/\A--- :foo\n(?:\.\.\.\n)?\z/, Psych.safe_dump(:foo, permitted_symbols: [:foo]))
+ end
+
+ def test_safe_dump_stringify_names
+ yaml = <<-eoyml
+---
+foo:
+ bar: bar
+ 'no': special escapes
+ 123: number
+eoyml
+
+ payload = Psych.safe_dump({
+ foo: {
+ bar: "bar",
+ no: "special escapes",
+ 123 => "number"
+ }
+ }, stringify_names: true)
+ assert_equal yaml, payload
+
+ assert_equal("---\nfoo: :bar\n", Psych.safe_dump({foo: :bar}, stringify_names: true, permitted_symbols: [:bar]))
+
+ error = assert_raise Psych::DisallowedClass do
+ Psych.safe_dump({foo: :bar}, stringify_names: true)
+ end
+ assert_equal "Tried to dump unspecified class: Symbol(:bar)", error.message
end
def test_safe_dump_aliases
diff --git a/test/psych/test_scalar_scanner.rb b/test/psych/test_scalar_scanner.rb
index 145db58fd9..02b923afe2 100644
--- a/test/psych/test_scalar_scanner.rb
+++ b/test/psych/test_scalar_scanner.rb
@@ -150,7 +150,7 @@ module Psych
end
def test_scan_strict_int_commas_and_underscores
- # this test is to ensure adherance to YML spec using the 'strict_integer' option
+ # this test is to ensure adherence to YML spec using the 'strict_integer' option
scanner = Psych::ScalarScanner.new ClassLoader.new, strict_integer: true
assert_equal 123_456_789, scanner.tokenize('123_456_789')
assert_equal '123,456,789', scanner.tokenize('123,456,789')
diff --git a/test/psych/test_set.rb b/test/psych/test_set.rb
index 87944d839e..b4968d3425 100644
--- a/test/psych/test_set.rb
+++ b/test/psych/test_set.rb
@@ -46,5 +46,12 @@ bar: baz
@set['self'] = @set
assert_cycle(@set)
end
+
+ def test_stringify_names
+ @set[:symbol] = :value
+
+ assert_match(/^:symbol: :value/, Psych.dump(@set))
+ assert_match(/^symbol: :value/, Psych.dump(@set, stringify_names: true))
+ end
end
end
diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb
index 0dc34b3083..84ae5cbb45 100644
--- a/test/psych/test_string.rb
+++ b/test/psych/test_string.rb
@@ -17,17 +17,17 @@ module Psych
end
end
- # 'y' and 'n' are kind of ambiguous. Syck treated y and n literals in
+ # 'y', 'Y', 'n', 'N' are kind of ambiguous. Syck treated those literals in
# YAML documents as strings. But this is not what the YAML 1.1 spec says.
# YAML 1.1 says they should be treated as booleans. When we're dumping
# documents, we know it's a string, so adding quotes will eliminate the
# "ambiguity" in the emitted document
- def test_y_is_quoted
- assert_match(/"y"/, Psych.dump("y"))
- end
- def test_n_is_quoted
- assert_match(/"n"/, Psych.dump("n"))
+ def test_all_yaml_1_1_booleans_are_quoted
+ yaml_1_1_booleans = %w[y Y yes Yes YES n N no No NO true True TRUE false False FALSE on On ON off Off OFF] # from https://yaml.org/type/bool.html
+ yaml_1_1_booleans.each do |boolean|
+ assert_match(/"#{boolean}"|'#{boolean}'/, Psych.dump(boolean))
+ end
end
def test_string_with_newline
diff --git a/test/psych/test_yaml.rb b/test/psych/test_yaml.rb
index e12b9769fe..cedec46cc7 100644
--- a/test/psych/test_yaml.rb
+++ b/test/psych/test_yaml.rb
@@ -34,7 +34,7 @@ class Psych_Unit_Tests < Psych::TestCase
# [ruby-core:34969]
def test_regexp_with_n
- assert_cycle(Regexp.new('',0,'n'))
+ assert_cycle(Regexp.new('',Regexp::NOENCODING))
end
#
# Tests modified from 00basic.t in Psych.pm
@@ -223,8 +223,8 @@ EOY
&C currency: GBP
&D departure: LAX
&A arrival: EDI
- - { *F: MADF, *C: AUD, *D: SYD, *A: MEL }
- - { *F: DFSF, *C: USD, *D: JFK, *A: MCO }
+ - { *F : MADF, *C : AUD, *D : SYD, *A : MEL }
+ - { *F : DFSF, *C : USD, *D : JFK, *A : MCO }
EOY
)
@@ -233,20 +233,20 @@ EOY
---
ALIASES: [&f fareref, &c currency, &d departure, &a arrival]
FARES:
-- *f: DOGMA
- *c: GBP
- *d: LAX
- *a: EDI
-
-- *f: MADF
- *c: AUD
- *d: SYD
- *a: MEL
-
-- *f: DFSF
- *c: USD
- *d: JFK
- *a: MCO
+- *f : DOGMA
+ *c : GBP
+ *d : LAX
+ *a : EDI
+
+- *f : MADF
+ *c : AUD
+ *d : SYD
+ *a : MEL
+
+- *f : DFSF
+ *c : USD
+ *d : JFK
+ *a : MCO
EOY
)
diff --git a/test/psych/visitors/test_emitter.rb b/test/psych/visitors/test_emitter.rb
index 70adbb9ca0..8bcf5491ca 100644
--- a/test/psych/visitors/test_emitter.rb
+++ b/test/psych/visitors/test_emitter.rb
@@ -61,9 +61,9 @@ module Psych
@visitor.accept s
- assert_match(/key: value/, @io.string)
+ assert_include(@io.string, "key: value")
assert_equal @io.string, s.yaml
- assert(/\.\.\./ !~ s.yaml)
+ assert_not_include(s.yaml, "...")
end
def test_scalar
@@ -76,7 +76,7 @@ module Psych
@visitor.accept s
- assert_match(/hello/, @io.string)
+ assert_include(@io.string, "hello")
assert_equal @io.string, s.yaml
end
@@ -90,8 +90,8 @@ module Psych
@visitor.accept s
- assert_match(/str/, @io.string)
- assert_match(/hello/, @io.string)
+ assert_include(@io.string, "str")
+ assert_include(@io.string, "hello")
assert_equal @io.string, s.yaml
end
@@ -107,7 +107,7 @@ module Psych
@visitor.accept s
- assert_match(/- hello/, @io.string)
+ assert_include(@io.string, "- hello")
assert_equal @io.string, s.yaml
end
@@ -122,7 +122,7 @@ module Psych
@visitor.accept s
- assert_match(/key: value/, @io.string)
+ assert_include(@io.string, "key: value")
assert_equal @io.string, s.yaml
end
@@ -137,7 +137,7 @@ module Psych
@visitor.accept s
- assert_match(/&A key: \*A/, @io.string)
+ assert_include(@io.string, "&A key: \*A")
assert_equal @io.string, s.yaml
end
end
diff --git a/test/psych/visitors/test_to_ruby.rb b/test/psych/visitors/test_to_ruby.rb
index 3d4608b903..89c3676651 100644
--- a/test/psych/visitors/test_to_ruby.rb
+++ b/test/psych/visitors/test_to_ruby.rb
@@ -319,7 +319,7 @@ description:
list = seq.to_ruby
assert_equal %w{ foo foo }, list
- assert_equal list[0].object_id, list[1].object_id
+ assert_same list[0], list[1]
end
def test_mapping_with_str_tag
diff --git a/test/psych/visitors/test_yaml_tree.rb b/test/psych/visitors/test_yaml_tree.rb
index 4c48670f2f..01e685134a 100644
--- a/test/psych/visitors/test_yaml_tree.rb
+++ b/test/psych/visitors/test_yaml_tree.rb
@@ -34,7 +34,7 @@ module Psych
v << "hello world"
v.finish
- assert_match "hello world", io.string
+ assert_include io.string, "hello world"
end
def test_binary_formatting
@@ -167,9 +167,9 @@ module Psych
end
def test_string
- assert_match(/'017'/, Psych.dump({'a' => '017'}))
- assert_match(/'019'/, Psych.dump({'a' => '019'}))
- assert_match(/'01818'/, Psych.dump({'a' => '01818'}))
+ assert_include(Psych.dump({'a' => '017'}), "'017'")
+ assert_include(Psych.dump({'a' => '019'}), "'019'")
+ assert_include(Psych.dump({'a' => '01818'}), "'01818'")
end
# http://yaml.org/type/null.html