如何在Haskell中创建可读性更强的多行字符串



我是Haskell的新手,正在测试JSON序列化。下面是测试用例的样子:

{-# 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"}"

有没有一种方法可以用更可读的方式写出来?也许是这样的:

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

这不一定是一个多行字符串,但如果你对JSON进行了修饰,它就会是。

只需使用准引号扩展和string-qq包:

{-# LANGUAGE QuasiQuotes #-}
import Data.String.QQ
someString :: String
someString = [s|
This is"
some string with "" quotes and stuff"!
|]

带输出:

*Main> someString 
"This is"nsome string with "" quotes and stuff"!n"

最新更新