Escaping Double quotes in a string "Debug log output"

Hi,

How to escape the “Double Quote” " in elm?

printKeyValue : String -> String -> String
printKeyValue key value =

   key ++ "=" ++ "\"" ++ value ++ "\""

I’m using the \" to escape the charecter. But unit test fails as it is adding the \ charecter also as part of comparision.

↓ Helpers Test
✗ Testing render

    "title="My title""
    ╷
    │ Expect.equal
    ╵
    "title=\"My title\""



TEST RUN FAILED

Duration: 147 ms
Passed:   4
Failed:   1

When I Dump the values using Debug log I do see the same additional \ char in the string representation. How can I get rid of that additional \ charecter?

What does your test case look like?

[quote=“SANTMAN, post:1, topic:10027”]

    "title="My title""

My mistake here is that, I typed a simplified code here which will give compiler error for the usage of Double quotes and doesn’t compile. Apologies for that mistake.

Here is one of the simplfied test case. Which passes.

suite : Test
suite =
    describe "Helpers Test"
        [ test "Testing render" <|
            \_ ->
                let
                    t : Attribute msg
                    t =
                        title "My title"

                    h : Dsl.Types.Node msg
                    h =
                        h1 [ t ] []

                    v =
                        render h
                in
                Expect.equal v "<h1 title=\"My title\">  </h1> "
        ]

Here in this image we can see that when debug log is observed all the attributes values have \" instead of "

Debug.log "some message" x prints that x as Elm source code (more or less). Which means that you can copy-paste the output into an Elm file (more or less).

So if x is a String and contains the text abc, then it is going to be printed as "abc". If the string contains quotes, then those are escaped of course (just as when you write code yourself).

But that can be annoying when you’re trying to understand what a string actually looks like. A trick is to “abuse” the message and put the string there instead: Debug.log x (). Here I used the variable I wanted to log (x) as the message, and used () (unit) as the value to log.

2 Likes

Amazing! Thank you @lydell

This in deed solved the problem I had.

before

 let
        v =
            render docNode

        _ =
            Debug.log "DEBUG" "<!DOCTYPE html>" ++ v) 
in

After

 let
        v =
            render docNode

        _ =
            Debug.log  "<!DOCTYPE html>" ++ v)  ""
in

Here is the new output

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.