Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump/Load regexp with escaped slash #523

Open
ayanko opened this issue Oct 12, 2021 · 1 comment
Open

Dump/Load regexp with escaped slash #523

ayanko opened this issue Oct 12, 2021 · 1 comment

Comments

@ayanko
Copy link

ayanko commented Oct 12, 2021

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

@ayanko
Copy link
Author

ayanko commented Oct 12, 2021

They have different source ...

irb(main):020:0> /\//.source
=> "/"
irb(main):021:0> Psych.load(Psych.dump(/\//)).source
=> "\\/"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant