Added testing setup with Tasty

engine-govind^2
Govind Krishna Joshi 2015-07-12 19:41:47 +05:30
parent 4bbea62c27
commit a61e874b16
11 changed files with 102 additions and 14 deletions

0
Player Normal file
View File

View File

@ -1,2 +1,2 @@
import Distribution.Simple
import Distribution.Simple
main = defaultMain

View File

@ -3,13 +3,10 @@
name: hastron
version: 0.1.0.0
-- synopsis:
-- description:
license: MIT
license-file: LICENSE
author: Abhinav Sarkar
maintainer: abhinav@abhinavsarkar.net
-- copyright:
category: Game
build-type: Simple
extra-source-files: README.md
@ -17,23 +14,27 @@ cabal-version: >=1.10
executable hastron
main-is: Main.hs
ghc-options: -Wall
other-modules: Hastron.Server.Types
-- other-extensions:
build-depends: base >=4.7 && <4.9,
text >=1.2 && <1.3,
unordered-containers >=0.2.5 && <0.3,
hashable >=1.2 && <1.3
Hastron.Utils
build-depends:
base >=4.7 && <4.9,
text >=1.2 && <1.3,
unordered-containers >=0.2.5 && <0.3,
hashable >=1.2 && <1.3
hs-source-dirs: src
default-language: Haskell2010
test-suite test
type: exitcode-stdio-1.0
ghc-options: -Wall
default-language: Haskell2010
hs-source-dirs: tests
hs-source-dirs: src, tests
main-is: TestMain.hs
build-depends:
base >=4.7 && <4.9,
tasty >=0.10 && <0.11,
tasty-hunit >=0.9 && <0.10,
tasty-quickcheck >=0.8 && <0.9
tasty-quickcheck >=0.8 && <0.9,
QuickCheck >=2.8 && <2.9

View File

@ -0,0 +1,17 @@
module Hastron.Game.Player where
import Hastron.Game.Types
import qualified Hastron.Utils as Utils
turn :: Direction -> Player -> Player
turn dir player = player { playerVelocity = changeDirection (playerVelocity player) dir }
turnRight :: Player -> Player
turnRight player = turn (Utils.nextEnum (direction player)) player
direction :: Player -> Direction
direction Player {playerVelocity=(Velocity _ dir)} = dir
changeDirection :: Velocity -> Direction -> Velocity
changeDirection (Velocity val _) dir = Velocity val dir

View File

@ -1,10 +1,14 @@
module Hastron.Game.Types where
import Data.HashMap.Strict (HashMap)
import Data.HashMap.Strict (HashMap)
type Point = (Int, Int)
data Direction = Left | Right | Up | Down deriving (Show, Eq, Ord, Enum)
data Direction = Left
| Up
| Right
| Down
deriving (Show, Eq, Ord, Enum, Bounded)
data Velocity = Velocity Double Direction deriving (Show, Eq, Ord)

11
src/Hastron/Utils.hs Normal file
View File

@ -0,0 +1,11 @@
module Hastron.Utils where
nextEnum :: (Enum a, Bounded a) => a -> a
nextEnum = turnEnum 1
prevEnum :: (Enum a, Bounded a) => a -> a
prevEnum = turnEnum (-1)
turnEnum :: (Enum a, Bounded a) => Int -> a -> a
turnEnum n e = toEnum $ mod (sum [fromEnum e, n]) enumLength
where enumLength = succ (fromEnum (maxBound `asTypeOf` e))

View File

@ -1,6 +1,7 @@
module Main where
import Hastron.Server.Types
import Hastron.Utils
main :: IO()
main = putStrLn "Hello World"

View File

@ -0,0 +1,5 @@
module Hastron.Game.Properties where
import qualified Test.Tasty as Test
import qualified Test.Test.Quickcheck as QC

View File

@ -0,0 +1,7 @@
module Hastron.Game.UnitTests (unitTests) where
import qualified Test.Tasty as Test
unitTests :: Test.TestTree
unitTests = []

View File

@ -0,0 +1,24 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Hastron.TestUtils (properties) where
import qualified Test.QuickCheck.Arbitrary as Arbit
import qualified Test.Tasty as Test
import qualified Test.Tasty.QuickCheck as QC
import qualified Hastron.Utils as Utils
newtype Enum' a = Enum' a
deriving (Eq, Bounded, Enum, Show)
instance (Enum a, Bounded a) => Arbit.Arbitrary (Enum' a) where
arbitrary = Arbit.arbitraryBoundedEnum
properties :: Test.TestTree
properties = Test.testGroup "Utils Properties"
[ QC.testProperty "Additive inverse for turning" $
\n e -> prop_additive_turning_inverse (n :: Int) (e :: Enum' Char)
]
prop_additive_turning_inverse :: (Eq a, Show a, Enum a, Bounded a) => Int -> Enum' a -> Bool
prop_additive_turning_inverse n (Enum' e) = e == ((Utils.turnEnum n) . (Utils.turnEnum (-n))) e

View File

@ -1,3 +1,21 @@
module TestMain where
module Main where
import qualified Test.Tasty as Tasty
import qualified Hastron.TestUtils as Utils
main :: IO ()
main = Tasty.defaultMain tests
tests :: Tasty.TestTree
tests = Tasty.testGroup "Tests" [properties, unitTests]
unitTests :: Tasty.TestTree
unitTests = Tasty.testGroup "Unit Tests" [hUnitTests]
properties :: Tasty.TestTree
properties = Tasty.testGroup "Quickcheck properties" [ Utils.properties
]
hUnitTests :: Tasty.TestTree
hUnitTests = Tasty.testGroup "HUnit unit tests" []