简体   繁体   English

测试自定义数据类型的haskell io

[英]test haskell io for custom data type

Update : I noticed that there was a problem in the design of my code.更新:我注意到我的代码设计存在问题。 The overall structure of my code has changed so much that any answer to this question would be more or less irrelevant to its current state.我的代码的整体结构发生了很大变化,以至于对这个问题的任何答案都或多或少与其当前状态无关。 But SO says it's better not to delete an answered question, so I am keeping the question.但是 SO 说最好不要删除已回答的问题,所以我保留了这个问题。

The problem with my code was that typeclasses were too generic for what I wanted to do.我的代码的问题是类型类对于我想做的事情来说太通用了。 Now I concentrate on writting functions like现在我专注于编写函数,例如

makeModelId :: String -> Either StringValueError ModelId

Then extend that with something like:然后用类似的东西扩展它:

makeModelIdM :: (Monad m) => String -> m (Either StringValueError ModelId)
makeModelIdM astr = do
  idstr <- astr
  return (makeModelId idstr)

I am not sure if it is the best approach though..我不确定这是否是最好的方法。


I know that there are lots of similar questions to this like this or this or this , and as far I understood what I want to do can be achieved with HUnit .我知道有很多类似的问题,比如 这个这个这个,据我所知,我想要做的事情可以用HUnit来实现。 But I can't seem to find the exact way of doing it.但我似乎无法找到这样做的确切方法。

I basically have a setter typeclass which is我基本上有一个 setter 类型类,它是

class StringLike2Primitive model where
    fromString :: String -> model
    fromText :: Text -> model
    fromText aText = fromString (unpack aText)

class (StringLike2Primitive model) => StringLike2PrimitiveM model where
    fromStringM :: (MonadPlus m) => String -> m model
    fromTextM :: (MonadPlus m) => Text -> m model
    fromStringM astr =  return (fromString astr)
    fromTextM aText = fromStringM (unpack aText)

And I have couple of data types which implement these typeclasses, for example:我有几个数据类型实现了这些类型类,例如:

instance StringLike2Primitive ModelId where
    fromString = StringIdCons

instance StringLike2PrimitiveM ModelId where
    fromStringM aStr
        | null aStr = fail "empty string is not allowed as id"
        | not (isAlphaNumStr aStr) = fail
            "Only ascii alphanumeric strings are allowed"
        | not (isAsciiStr aStr) = fail
            "Only ascii alphanumeric strings are allowed"

I am not sure how can I test fromStringM function against guarded values in a unit test environment, and I think my approach to error handling is problematic.我不确定如何在单元测试环境中针对受保护的值测试fromStringM函数,而且我认为我的错误处理方法有问题。 Any tips would be appreciated.任何提示将不胜感激。

(This is more of a comment, but might be an answer as well.) (这更像是一个评论,但也可能是一个答案。)

Are you sure you need a second type class?你确定你需要第二类吗? The definition of fromStringM doesn't rely on the StringLike2Primitive instance so much as it simply wraps it. fromStringM的定义并不依赖StringLike2Primitive实例,它只是简单地包装了它。

fromStringM :: (Monad m, StringLike2Primitive a) => String -> m a
fromStringM a
  | null a = fail "empty string is not allowed"
  | not (isAlphaNumStr aStr) = fail "Only ascii alphanumeric strings are allowed"
  | not (isAsciiStr aStr) = fail "Only ascii strings are allowed"
  | otherwise = return $ fromString a

and likewise for fromText .对于fromText也是fromText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM