Making a string comparison ignore the slash for escaping characters

Hi all!

I’m trying to test a function which returns an encoded string, but I can’t seem to figure out how to represent the test in a pretty way.

Given this test:

  test "string formatting in tests" do
    assert "{\"text\”:\”Something\\nAnother_Something\\nYet_Another_Something\",\"response_type\":\"in_channel\"}" ==
    """
    {\"text\”:\”\
    Something
    Another_Something
    Yet_Another_Something",\
    \"response_type\":\"in_channel\"}\
    """
  end

I get:

     Assertion with == failed
     code:  assert "{\"text”:”Something\\nAnother_Something\\nYet_Another_Something\",\"response_type\":\"in_channel\"}" == "{\"text”:”Something\nAnother_Something\nYet_Another_Something\",\"response_type\":\"in_channel\"}"
     left:  "{\"text”:”Something\\nAnother_Something\\nYet_Another_Something\",\"response_type\":\"in_channel\"}"
     right: "{\"text”:”Something\nAnother_Something\nYet_Another_Something\",\"response_type\":\"in_channel\"}"

If you notice, the error is due to the extra slash missing in the right expression. As far as I understand, the extra slash is simply to escape the actual slash in the breakline character… but seems like Elixir gets it literally.

The reason why I’m interested in this is because the "text" property of the JSON has a huge list and I figured it would make the tests more readable to represent it with a HEREDOC… but I’m having doubts as if it’s possible. How would you approach this?

In the trippledoublequoted string, there is no need to escape the single-doublequotes.

Also the \ at linebreaks do not occur anywhere. What really is missing, are the backslashes in front of linebreaks.

  test "string formatting in tests" do
    assert "{\"text\”:\”Something\\nAnother_Something\\nYet_Another_Something\",\"response_type\":\"in_channel\"}" ==
    """
    {"text”:”\
    Something\\n\
    Another_Something\\n\
    Yet_Another_Something",\
    "response_type":"in_channel"}\
    """
  end

Remember, the string needs to contain a literal \n, and not a linebreak.

Aww, gotcha! I was quite sure there was something I was missing, just couldn’t figure it out. Thank you!