diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2015-01-08 14:21:34 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2015-01-08 14:21:34 -0800 |
commit | e3b2c1c9f5c3a6144a4b02d46a3677df9826a1ab (patch) | |
tree | 8976c31dc44648187f2bcceec3c69297df658d67 /test | |
parent | 010d0c37bde30c5f347ebfd69087a487ff8b8bbb (diff) | |
parent | af308f8307899cb9e1c0fffea4bce3110a1c3926 (diff) | |
download | psych-e3b2c1c9f5c3a6144a4b02d46a3677df9826a1ab.zip |
Merge branch 'master' into jirutka-patch-1
* master: (21 commits)
* ext/psych/lib/psych/visitors/to_ruby.rb: call `allocate` on hash subclasses. Fixes github.com/tenderlove/psych/issues/196
* ext/psych/lib/psych/visitors/to_ruby.rb: revive hashes with ivars
removed isolate task
removed isolate plugin
added minitest dependency into gemspec
added install task into travis
added ruby-head env
bumping version to 2.0.8
fixed build error caused by trunk changes
bumping version to 2.0.7
merging from ruby trunk
backport r48512 from ruby/ruby trunk.
Add changelog for 2a4d9568f7d5d19c00231cf48eb855cc45ec3394
backport r48214 from ruby/ruby trunk.
Allow dumping any BasicObject that defines #marshal_dump or #marshal_load
bumping version
* ext/psych/lib/psych/visitors/yaml_tree.rb: fix NameError dumping and loading. Fixes GH #85. Thanks @brentdax for the patch! * test/psych/test_exception.rb: test for fix
* ext/psych/lib/psych/scalar_scanner.rb: fix loading strings that look like integers but have a newline. Fixes GH #189 * test/psych/test_string.rb: test for fix
* ext/psych/lib/psych/visitors/to_ruby.rb: merge keys with a hash should merge the hash in to the parent. * test/psych/test_merge_keys.rb: test for change. Fixes GH #202
* ext/psych/lib/psych/visitors/to_ruby.rb: quoted "<<" strings should not be treated as merge keys. * ext/psych/lib/psych/visitors/yaml_tree.rb: hashes with keys containing "<<" should roundtrip. * test/psych/test_merge_keys.rb: test for change. Fixes GH #203
...
Conflicts:
lib/psych/visitors/yaml_tree.rb
Diffstat (limited to 'test')
-rw-r--r-- | test/psych/json/test_stream.rb | 2 | ||||
-rw-r--r-- | test/psych/test_emitter.rb | 3 | ||||
-rw-r--r-- | test/psych/test_exception.rb | 6 | ||||
-rw-r--r-- | test/psych/test_hash.rb | 44 | ||||
-rw-r--r-- | test/psych/test_json_tree.rb | 2 | ||||
-rw-r--r-- | test/psych/test_marshalable.rb | 54 | ||||
-rw-r--r-- | test/psych/test_merge_keys.rb | 30 | ||||
-rw-r--r-- | test/psych/test_string.rb | 4 |
8 files changed, 141 insertions, 4 deletions
diff --git a/test/psych/json/test_stream.rb b/test/psych/json/test_stream.rb index 4690ad2..b0c33e6 100644 --- a/test/psych/json/test_stream.rb +++ b/test/psych/json/test_stream.rb @@ -65,7 +65,7 @@ module Psych @stream.push list json = @io.string - assert_match(/]$/, json) + assert_match(/\]$/, json) assert_match(/^--- \[/, json) assert_match(/["]one["]/, json) assert_match(/["]two["]/, json) diff --git a/test/psych/test_emitter.rb b/test/psych/test_emitter.rb index 0554ae5..1c96c12 100644 --- a/test/psych/test_emitter.rb +++ b/test/psych/test_emitter.rb @@ -11,8 +11,7 @@ module Psych end def test_line_width - assert_equal 0, @emitter.line_width - assert_equal 10, @emitter.line_width = 10 + @emitter.line_width = 10 assert_equal 10, @emitter.line_width end diff --git a/test/psych/test_exception.rb b/test/psych/test_exception.rb index a9fe5c4..30dfb24 100644 --- a/test/psych/test_exception.rb +++ b/test/psych/test_exception.rb @@ -16,6 +16,12 @@ module Psych @wups = Wups.new end + def test_naming_exception + err = String.xxx rescue $! + new_err = Psych.load(Psych.dump(err)) + assert_equal err.message, new_err.message + end + def test_load_takes_file ex = assert_raises(Psych::SyntaxError) do Psych.load '--- `' diff --git a/test/psych/test_hash.rb b/test/psych/test_hash.rb index 264a471..066df66 100644 --- a/test/psych/test_hash.rb +++ b/test/psych/test_hash.rb @@ -5,11 +5,55 @@ module Psych class X < Hash end + class HashWithCustomInit < Hash + attr_reader :obj + def initialize(obj) + @obj = obj + end + end + + class HashWithCustomInitNoIvar < Hash + def initialize(obj) + # *shrug* + end + end + def setup super @hash = { :a => 'b' } end + def test_custom_initialized + a = [1,2,3,4,5] + t1 = HashWithCustomInit.new(a) + t2 = Psych.load(Psych.dump(t1)) + assert_equal t1, t2 + assert_cycle t1 + end + + def test_custom_initialize_no_ivar + t1 = HashWithCustomInitNoIvar.new(nil) + t2 = Psych.load(Psych.dump(t1)) + assert_equal t1, t2 + assert_cycle t1 + end + + def test_hash_with_ivars + @hash.instance_variable_set :@foo, 'bar' + dup = Psych.load Psych.dump @hash + assert_equal 'bar', dup.instance_variable_get(:@foo) + end + + def test_hash_subclass_with_ivars + x = X.new + x[:a] = 'b' + x.instance_variable_set :@foo, 'bar' + dup = Psych.load Psych.dump x + assert_cycle x + assert_equal 'bar', dup.instance_variable_get(:@foo) + assert_equal X, dup.class + end + def test_load_with_class_syck_compatibility hash = Psych.load "--- !ruby/object:Hash\n:user_id: 7\n:username: Lucas\n" assert_equal({ user_id: 7, username: 'Lucas'}, hash) diff --git a/test/psych/test_json_tree.rb b/test/psych/test_json_tree.rb index 60f8321..a23fc1a 100644 --- a/test/psych/test_json_tree.rb +++ b/test/psych/test_json_tree.rb @@ -45,7 +45,7 @@ module Psych def test_list_to_json list = %w{ one two } json = Psych.to_json(list) - assert_match(/]$/, json) + assert_match(/\]$/, json) assert_match(/^\[/, json) assert_match(/"one"/, json) assert_match(/"two"/, json) diff --git a/test/psych/test_marshalable.rb b/test/psych/test_marshalable.rb new file mode 100644 index 0000000..7df74ee --- /dev/null +++ b/test/psych/test_marshalable.rb @@ -0,0 +1,54 @@ +require_relative 'helper' +require 'delegate' + +module Psych + class TestMarshalable < TestCase + def test_objects_defining_marshal_dump_and_marshal_load_can_be_dumped + sd = SimpleDelegator.new(1) + loaded = Psych.load(Psych.dump(sd)) + + assert_instance_of(SimpleDelegator, loaded) + assert_equal(sd, loaded) + end + + class PsychCustomMarshalable < BasicObject + attr_reader :foo + + def initialize(foo) + @foo = foo + end + + def marshal_dump + [foo] + end + + def mashal_load(data) + @foo = data[0] + end + + def init_with(coder) + @foo = coder['foo'] + end + + def encode_with(coder) + coder['foo'] = 2 + end + + def respond_to?(method) + [:marshal_dump, :marshal_load, :init_with, :encode_with].include?(method) + end + + def class + PsychCustomMarshalable + end + end + + def test_init_with_takes_priority_over_marshal_methods + obj = PsychCustomMarshalable.new(1) + loaded = Psych.load(Psych.dump(obj)) + + assert(PsychCustomMarshalable === loaded) + assert_equal(2, loaded.foo) + end + end +end diff --git a/test/psych/test_merge_keys.rb b/test/psych/test_merge_keys.rb index ba8d2e7..1620a6a 100644 --- a/test/psych/test_merge_keys.rb +++ b/test/psych/test_merge_keys.rb @@ -6,6 +6,36 @@ module Psych attr_reader :bar end + def test_merge_key_with_bare_hash + doc = Psych.load <<-eodoc +map: + <<: + hello: world + eodoc + hash = { "map" => { "hello" => "world" } } + assert_equal hash, doc + end + + def test_roundtrip_with_chevron_key + h = {} + v = { 'a' => h, '<<' => h } + assert_cycle v + end + + def test_explicit_string + doc = Psych.load <<-eoyml +a: &me { hello: world } +b: { !!str '<<': *me } +eoyml + expected = { + "a" => { "hello" => "world" }, + "b" => { + "<<" => { "hello" => "world" } + } + } + assert_equal expected, doc + end + def test_mergekey_with_object s = <<-eoyml foo: &foo diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb index 68917c8..26a4e20 100644 --- a/test/psych/test_string.rb +++ b/test/psych/test_string.rb @@ -16,6 +16,10 @@ module Psych end end + def test_string_with_newline + assert_equal "1\n2", Psych.load("--- ! '1\n\n 2'\n") + end + def test_no_doublequotes_with_special_characters assert_equal 2, Psych.dump(%Q{<%= ENV["PATH"] %>}).count('"') end |