我是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\"}"
Run Code Online (Sandbox Code Playgroud)
有没有办法以更易读的方式编写它?也许有些东西:
record `shouldBe` """
{"bird":"Snowy Eget","city":"California","meta":"low tide"}
"""
Run Code Online (Sandbox Code Playgroud)
这不一定是多行字符串,但是如果你对JSON进行了美化,那么它就是.只是想知道一般.
Tho*_*son 11
只需使用准引号扩展和string-qq包:
{-# LANGUAGE QuasiQuotes #-}
import Data.String.QQ
someString :: String
someString = [s|
This is"
some string with "" quotes and stuff"!
|]
Run Code Online (Sandbox Code Playgroud)
随着输出:
*Main> someString
"This is\"\nsome string with \"\" quotes and stuff\"!\n"
Run Code Online (Sandbox Code Playgroud)