psych
psych copied to clipboard
Dump/Load regexp with escaped slash
Problem
$ irb -r psych
irb(main):001:0> Psych.load(Psych.dump(/\d/)) == /\d/
=> true
irb(main):002:0> Psych.load(Psych.dump(/\//)) == /\//
=> false
STR
$ cat psych_regexp.rb
# frozen_string_literal: true
require 'psych'
require 'test/unit'
class TestPyschRegexpDumpLoad < Test::Unit::TestCase
def test_psych_version
assert '3.1.0', Psych::VERSION
end
def test_dump_load_regexp_no_escape
assert_equal "--- !ruby/regexp /^[0-9]+/\n", Psych.dump(/^[0-9]+/)
assert_equal /^[0-9]+/, Psych.load(Psych.dump(/^[0-9]+/))
end
def test_dump_load_regexp_with_slash
assert_equal "--- !ruby/regexp /^\\//\n", Psych.dump(/^\//)
assert_equal /^\//, Psych.load(Psych.dump(/^\//))
end
end
Run
$ ruby psych_regexp.rb
Actual Result
Loaded suite psych_regexp
Started
.F
=====================================================================================================================================================================================
15:
16: def test_dump_load_regexp_with_slash
17: assert_equal "--- !ruby/regexp /^\\//\n", Psych.dump(/^\//)
=> 18: assert_equal /^\//, Psych.load(Psych.dump(/^\//))
19: end
20: end
psych_regexp.rb:18:in `test_dump_load_regexp_with_slash'
</^\//> expected but was
</^\//>
diff:
/^\//
Failure: test_dump_load_regexp_with_slash(TestPyschRegexpDumpLoad)
=====================================================================================================================================================================================
.
Finished in 0.006877 seconds.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3 tests, 5 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
66.6667% passed
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
436.24 tests/s, 727.06 assertions/s
Expected Result
Test passed
They have different source ...
irb(main):020:0> /\//.source
=> "/"
irb(main):021:0> Psych.load(Psych.dump(/\//)).source
=> "\\/"