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

I'm new to Haskell, and I'm working on testing JSON serialization. Here's 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 this in a more readable way? Maybe something like:

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

This is not necessarily a multi-line string, but if you finished JSON, then it will. Just interesting at all.

+7
source share
1 answer

Just use the quasi-quote 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 the conclusion:

 *Main> someString "This is\"\nsome string with \"\" quotes and stuff\"!\n" 
+11
source

Source: https://habr.com/ru/post/1206065/


All Articles