1

我目前正在为我的(非常简单的)二十一点游戏编写单元测试,而我的测试文件(Tests.hs)似乎没有导入我在我正在为(HelpFunctions.hs)进行单元测试的文件中声明的数据结构。我可以访问此文件中的函数/方法,但不能访问数据结构。有人可以帮我找到问题吗?

这是我的测试文件的顶部:

module Tests(performTests) where
import Test.HUnit
import HelpFunctions

cardList = [(Hearts, Ace)]
(...)

这是我要为其编写测试的文件的顶部

module HelpFunctions(Suit, Value, blackjack, cardDeck, shuffleOne, 
                     shuffleCards, getValue, addHand, dealCard, bust, 
                     getHighest
                    ) where

import System.Random
import Control.Monad(when)

{- Suit is one of the four suits or color of a playing card 
   ie Hearts, Clubs, Diamonds or Spades
   INVARIANT: Must be one of the specified.
 -}
data Suit = Hearts | Clubs | Diamonds | Spades deriving (Show)

{- Value is the numeric value of a playing card according to the rules of blackjack. 
   INVARIANT: Must be one of the specified.
 -}
data Value = Two | Three | Four | Five | Six | Seven | Eight | 
             Nine | Ten | Jack | Queen | King | Ace 
             deriving (Eq, Show)
(...)

编译测试文件时出现错误

    Tests.hs:6:14: error: Data constructor not in scope: Hearts
  |
6 | cardList = [(Hearts, Ace)]   |              ^^^^^^

Tests.hs:6:22: error: Data constructor not in scope: Ace
  |
6 | cardList = [(Hearts, Ace)]   |                      ^^^

我有另一个文件可以从中导入 HelpFunctions 和数据结构,并且可以正常工作。

4

1 回答 1

5

你的问题在这里:

module HelpFunctions(Suit, Value, ...

这一行说HelpFunctions导出类型SuitValue,但不是它们的数据构造函数(即类型是抽象的)。

你要

module HelpFunctions(Suit(..), Value(..), ...

您可以显式列出所有构造函数,但..简写表示法表示“此类型的所有数据构造函数”。


参考:Haskell 2010 语言报告,5.2 导出列表

  1. 由 a or声明声明的代数数据类型T可以用以下三种方式之一命名:datanewtype

    • 形式T命名类型,但不命名构造函数或字段名称。在没有构造函数的情况下导出类型的能力允许构造抽象数据类型(参见第5.8节)。
    • 形式T(c 1 ,…,cn )命名类型及其部分或全部构造函数和字段名称。
    • 缩写形式T(..)命名当前在范围内的类型及其所有构造函数和字段名称(无论是否合格)。
于 2018-02-26T01:23:28.057 回答