emancu / toml-rb

A parser for TOML using Citrus library.
MIT License
105 stars 39 forks source link

Hash keys with backslashes not dumped correctly #143

Closed UlyssesZh closed 10 months ago

UlyssesZh commented 11 months ago
puts TomlRB.dump '\t' => 'test'

Expected:

"\\t" = "test"

Got:

"\t" = "test"
emancu commented 10 months ago

@UlyssesZh Thanks for reporting this issue, I am finishing my vacation and I will get back to this next week!

Sorry for the late response!

emancu commented 10 months ago

@UlyssesZh I think the problem is that you are using puts, because TomlRB.dump is working as expected.

Check the PR I opened above, and see the tests passing.

Please let me know if I'm missing something, or provide more information about your environment (gem version, ruby version, os, etc.)

UlyssesZh commented 10 months ago

The test is wrong.

When the hash key is '\t', it means that it is literally a backslash and a t. This corresponds to "\\t" as written in TOML. However, in the percent string you wrote "\\t", which is actually "\t" (because percent string can escape). You should modify the test to "\\\\t".

Written Literal result
'\t' \t
"\\t" \t
%("\\t") "\t"
%("\\\\t") "\\t"

Look at (4,2) and (2,1). They are the same, so in the test you should write (4,1).

Or, you may just use single quote instead of percent string if you are unsure how it escapes.

Also, there is nothing wrong in using puts because it prints a string just as how it will be written in a file. If the result of puts is wrong, then the result when the TOML is written in a file is wrong.

emancu commented 10 months ago

@UlyssesZh Thanks for the clarification and for reporting the bug. I fixed it and released the version v3.0.0 🎉