[英]How to test my own data Types using HUnit in Haskell?
Part of Queue.hs: Queue.hs的一部分:
module Queue ( Queue, emptyQueue, isEmptyQueue, enQueue, deQueue ) where
data Queue a = EmptyQueue | Single a | Q a ( Queue a ) a deriving (Show, Eq)
emptyQueue :: Queue a
emptyQueue = EmptyQueue
enQueue :: a -> Queue a -> Queue a
enQueue x EmptyQueue = Single x
enQueue x (Single q) = Q q (EmptyQueue) x
enQueue x (Q qf q qb) = Q qf (enQueue qb q) x
I was using print, see if them correct one by one. 我正在使用打印,请查看它们是否一个接一个地纠正。 Now I want to test them using HUint. 现在,我想使用HUint测试它们。
Main.hs: Main.hs:
module Main where
import Queue
import Test.HUnit
test1 = TestCase $ assertEqual "" (Single 'a'::Queue Char) $ enQueue 'a' emptyQueue
tests = TestList [TestLabel "test1" test1]
main = do runTestTT tests
Here's what I got: 这是我得到的:
*Queue> :r
[2 of 2] Compiling Main ( Main.hs, interpreted )
Main.hs:5:36: Not in scope: data constructor ‘Single’
Failed, modules loaded: Queue.
So how can I fix it? 那么我该如何解决呢?
Seems the Queue
datatype should be abstract and opaque, so exposing all constructors is not a good idea. 似乎Queue
数据类型应该是抽象且不透明的,因此公开所有构造函数并不是一个好主意。 However, you can split your implementation into two submodules: 但是,您可以将实现分为两个子模块:
module Queue.Internal where --expose all internal stuff
data Queue = <...>
module Queue (Queue, emptyQueue, <...>) where --expose user API only
import Queue.Internal
module Queue.Tests where
import Queue.Internal --to get access to „guts“
test = <...>
module Main where
import Queue --to just use the type
Some library types (like ByteString
s) are made this way. 某些库类型(如ByteString
)是通过这种方式制作的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.