简体   繁体   中英

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

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

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 :

  1. An algebraic datatype T declared by a data or newtype declaration may be named in one of three ways:

    • The form T names the type but not the constructors or field names . The ability to export a type without its constructors allows the construction of abstract datatypes (see Section 5.8 ).
    • The form T(c 1 ,…,c n ) , names the type and some or all of its constructors and field names.
    • The abbreviated form T (..) names the type and all its constructors and field names that are currently in scope (whether qualified or not).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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