Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create more readable multi-line strings in Haskell?

Tags:

haskell

I am new to Haskell, and am working on testing JSON serialization. Here is what the test case looks like:

{-# LANGUAGE OverloadedStrings #-}

module WetlandsTest where

import Control.Exception (evaluate)
import Test.Hspec
import Wetlands

main :: IO ()
main = hspec $ do
  describe "wetlands" $ do
    describe "spotting" $ do
      it "returns a json encoded spotting" $ do
        let record = spotting "Snowy Egret" "California" "low tide"
        record `shouldBe` "{\"bird\":\"Snowy Eget\",\"state\":\"California\",\"meta\":\"low tide\"}"

Is there a way to write that in a more readable way? Maybe something along the lines of:

record `shouldBe` """
{"bird":"Snowy Eget","city":"California","meta":"low tide"}
"""

This isn't necessarily a multiline string, but if you prettified the JSON then it would be. Just wondering in general.

like image 478
Lance Avatar asked Nov 02 '14 22:11

Lance


1 Answers

Just use the quasi-quotes extension and the string-qq package:

{-# LANGUAGE QuasiQuotes #-}
import Data.String.QQ

someString :: String
someString = [s|
This is"
some string with "" quotes and stuff"!
|]

With output:

*Main> someString 
"This is\"\nsome string with \"\" quotes and stuff\"!\n"
like image 102
Thomas M. DuBuisson Avatar answered Nov 02 '22 19:11

Thomas M. DuBuisson