나는 하스켈을 배워야 할 것입니다. 그래서 아마 아주 사소한 것이지만, 어떻게 다시 작성하고 어떻게 작동하는지에 대한 몇 가지 조언을 해주시면 감사하겠습니다.리팩터링 where 절
내가 작업 코드 (사용 패키지 : 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 오, 고맙습니다 (수입이 충분하다고 생각했습니다). 나는 * 흐름 *이 하스 켈러들 사이에서별로 인기가 없다는 것을 알았지 만, HTF *도 그렇게하지 않았는가? 상용구없이 모듈의 모든 단위 테스트를 실행할 수있는 더 나은 대안이 있습니까? – monnef
"수입이 충분하다고 생각했습니다."- 검색과 검색에 필요한 충분한 정보를 제공하고 관련 기능을 찾는다는 의미에서 충분합니다. 독자의 질문에 대한 편의성 문제 일뿐입니다. (나는 당신이 사용한 꾸러미들에 대해 아무 말도하지 않습니다.) – duplode