简体   繁体   English

为什么我的测试文件不会导入我的数据结构? 哈斯克尔

[英]Why won't my test file import my datastructure? Haskell

I am currently writing unit tests for my (very simple) blackjack game and my testfile (Tests.hs) does not seem to import my datastructures that I have declared in the file I am doing unit tests for (HelpFunctions.hs). 我目前正在为(非常简单的)二十一点游戏编写单元测试,并且我的测试文件(Tests.hs)似乎没有导入我在为(HelpFunctions.hs)进行单元测试的文件中声明的数据结构。 I can acces the functions/methods in this file but not the datastructures. 我可以访问此文件中的功能/方法,但不能访问数据结构。 Can someone please help me find the problem? 有人可以帮我发现问题吗?

This is the top of my testfile: 这是我的测试文件的顶部:

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

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

and this is the top of the file that i am going to write tests for 这是我要为其编写测试的文件的顶部

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)
(...)

And when compiling the testfile I get the error 当编译测试文件时,我得到了错误

    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)]   |                      ^^^

I have another file that import HelpFunctions and the datastructures from it and that work without problems. 我还有另一个文件,可以从其中导入HelpFunctions和数据结构,并且可以正常工作。

Your problem is here: 您的问题在这里:

module HelpFunctions(Suit, Value, ...

This line says HelpFunctions exports the types Suit and Value , but not their data constructors (ie the types are abstract). 这行HelpFunctions导出类型SuitValue ,但不导出其数据构造函数(即类型是抽象的)。

You want 你要

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

You could list all constructors explicitly, but the .. shorthand notation means "all data constructors of this type". 您可以显式列出所有构造函数,但是..简写表示法表示“此类型的所有数据构造函数”。


Reference: The Haskell 2010 Language Report, 5.2 Export Lists : 参考: Haskell 2010语言报告, 5.2导出列表

  1. An algebraic datatype T declared by a data or newtype declaration may be named in one of three ways: 通过声明的代数数据类型牛逼 datanewtype声明可以通过以下三种方式中的一种被命名为:

    • The form T names the type but not the constructors or field names . 形式T命名类型, 但不命名构造函数或字段名 The ability to export a type without its constructors allows the construction of abstract datatypes (see Section 5.8 ). 导出没有类型构造函数的类型的能力允许构造抽象数据类型(请参见5.8节)。
    • The form T(c 1 ,…,c n ) , names the type and some or all of its constructors and field names. 形式T(c 1 ,…,c n命名类型及其部分或全部构造函数和字段名称。
    • The abbreviated form T (..) names the type and all its constructors and field names that are currently in scope (whether qualified or not). 缩写形式T (..)命名类型及其当前在范围内(无论是否限定)的所有构造函数和字段名称。

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

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