8

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.

1
  • 1
    This is unrelated to the specific question but all of those dos are unnecessary as it stands, so you could clean it up some by eliminating those as well. Commented Nov 2, 2014 at 22:47

1 Answer 1

14

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.