我学习Haskell,所以它可能是非常微不足道的,但我会很感激如何重写它以及它如何工作的一些指针。重构在哪里条款
我有以下工作代码(使用的包:HTF,Parsec和Flow):
{-# OPTIONS_GHC -F -pgmF htfpp #-}
{-# LANGUAGE FlexibleContexts #-}
module Main where
import Test.Framework -- assertEqual, assertBool, htfMain, htf_thisModulesTests
import Text.ParserCombinators.Parsec (eof, spaces, parse)
import Flow ((|>))
import Data.Either (isLeft)
whiteSpaces = spaces
test_parse_whitespace = do
mapM_ positive [
"", " ", "\t", "\n", "\r\n", " \r\n ",
" \t \r\n \t \n \r \t "
]
mapM_ negative ["x", " x", "x ", " x ", "\t_\t"]
where
parser = whiteSpaces >> eof
parseIt = parse parser ""
positive str = assertEqual (parseIt str) (Right())
negative str = assertBool (parseIt str |> isLeft)
main :: IO()
main = htfMain htf_thisModulesTests
我增加了新的测试,具有几乎相同的,其中的一部分,所以我试图重构它是这样的:
pos_neg_case parser = do
return [positive, negative]
where
fullParser = parser >> eof
parseIt = parse fullParser ""
positive str = assertEqual (parseIt str) (Right())
negative str = assertBool (parseIt str |> isLeft)
test_parse_whitespace' = do
mapM_ positive [
"", " ", "\t", "\n", "\r\n", " \r\n ",
" \t \r\n \t \n \r \t "
]
mapM_ negative ["x", " x", "x ", " x ", "\t_\t"]
where
[positive, negative] = pos_neg_case whiteSpaces
哪个不起作用(即使当我按照编译器的建议关闭lang功能时)。
Couldn't match expected type ‘[Char] -> m b0’
with actual type ‘[String -> IO()]’
Relevant bindings include
test_parse_whitespace' :: m() (bound at test/Spec.hs:21:1)
In the first argument of ‘mapM_’, namely ‘positive’
In a stmt of a 'do' block:
mapM_ positive ["", " ", "\t", "\n", ....]
Couldn't match expected type ‘[Char] -> m b1’
with actual type ‘[String -> IO()]’
Relevant bindings include
test_parse_whitespace' :: m() (bound at test/Spec.hs:21:1)
In the first argument of ‘mapM_’, namely ‘negative’
In a stmt of a 'do' block:
mapM_ negative ["x", " x", "x ", " x ", ....]
'assertEqual'和'assertBool'来自[* HTF *](http://hackage.haskell.org/package/HTF-0.13.1。0/docs/Test-Framework-HUnitWrapper.html)包,而'(|>)'来自[* flow *](https://hackage.haskell.org/package/flow-1.0.7/docs/Flow .html),它只是'flip($)'和'(&)'的同义词。 (提及你正在使用的不那么知名的软件包会产生更清晰的问题。) – duplode
@duplode哦,谢谢(我认为导入足够了)。我知道*流*在Haskellers中不太受欢迎,但* HTF *不是太?有没有更好的选择可以在没有样板的模块中运行所有的单元测试? – monnef
“我认为进口就足够了” - 它们就足够了,它们提供了足够的信息来搜索并最终找到它们以及相关功能。对于这个问题的读者来说,这只是一个方便的问题。 (我没有什么可以反对你使用的任何软件包的。) – duplode