Skip to content

Commit

Permalink
stylistic refactoring of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierverdier committed Jan 5, 2015
1 parent f02eadd commit 28cadff
Showing 1 changed file with 44 additions and 26 deletions.
70 changes: 44 additions & 26 deletions src/TestBranchParse.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import BranchParse (BranchInfo, branchInfo, AheadBehind)
import Text.Parsec (ParseError)
import Test.QuickCheck (Arbitrary(arbitrary), property, stdArgs, maxSuccess, quickCheckWith, suchThat, oneof, getPositive)
import Data.List (isPrefixOf, isSuffixOf, isInfixOf)
import Control.Applicative ((<$>), (<*>), pure)
Expand All @@ -9,7 +8,12 @@ import Control.Applicative ((<$>), (<*>), pure)
newtype ValidBranch = MkBranch String deriving (Show, Eq)

isValidBranch :: String -> Bool
isValidBranch b = not . or $ [null, (' ' `elem`), (".." `isInfixOf`), ("." `isPrefixOf`), ("." `isSuffixOf`)] <*> pure b
isValidBranch b = not . or $ [null,
(' ' `elem`),
(".." `isInfixOf`),
("." `isPrefixOf`),
("." `isSuffixOf`)]
<*> pure b

instance Arbitrary ValidBranch where
arbitrary = MkBranch <$> arbitrary `suchThat` isValidBranch
Expand All @@ -19,12 +23,15 @@ instance Arbitrary ValidBranch where
data BeHead = Ahead Int | Behind Int | AheadBehind Int Int deriving (Eq)

instance Show BeHead where
show (Ahead i) = "[ahead " ++ (show i) ++ "]"
show (Behind i) = "[behind " ++ (show i) ++ "]"
show (AheadBehind i j) ="[ahead " ++ (show i) ++ ", behind " ++ (show j) ++ "]"
show (Ahead i) = "[ahead " ++ show i ++ "]"
show (Behind i) = "[behind " ++ show i ++ "]"
show (AheadBehind i j) ="[ahead " ++ show i ++ ", behind " ++ show j ++ "]"

instance Arbitrary BeHead where
arbitrary = oneof [Ahead <$> pos, Behind <$> pos, AheadBehind <$> pos <*> pos]
arbitrary = oneof [
Ahead <$> pos,
Behind <$> pos,
AheadBehind <$> pos <*> pos]
where
pos = getPositive <$> arbitrary

Expand All @@ -36,39 +43,50 @@ expectedAheadBehind behead = case behead of

{- Helper to tackle the Either type -}

expectRight :: BranchInfo -> Either ParseError BranchInfo -> Bool
expectRight expected computed = case computed of
Left _ -> False
Right res -> res == expected
checkRight :: BranchInfo -> String -> Bool
checkRight b s = expectRight b $ branchInfo s
where
expectRight expected computed = case computed of
Left _ -> False
Right res -> res == expected

{- Test -}

propNoBranch :: ValidBranch -> Bool
-- propNoBranch s = '(' `notElem` s ==> expectRight ((Nothing, Nothing), Nothing) $ branchInfo $ s ++ " (no branch)"
propNoBranch (MkBranch s) = expectRight ((Nothing, Nothing), Nothing) $ branchInfo $ s ++ " (no branch)"
propNoBranch (MkBranch s) =
checkRight
((Nothing, Nothing), Nothing)
$ s ++ " (no branch)"

propNewRepo :: ValidBranch -> Bool
-- propNewRepo s = notNull s ==> expectRight ((Just s, Nothing), Nothing) $ branchInfo $ "Initial commit on " ++ s
propNewRepo (MkBranch s) = expectRight ((Just s, Nothing), Nothing) $ branchInfo $ "Initial commit on " ++ s
propNewRepo (MkBranch s) =
checkRight
((Just s, Nothing), Nothing)
$ "Initial commit on " ++ s

propBranchOnly :: ValidBranch -> Bool
-- propBranchOnly s = ' ' `notElem` s ==> expectRight ((Just s, Nothing), Nothing) $ branchInfo s
propBranchOnly (MkBranch s) = expectRight ((Just s, Nothing), Nothing) $ branchInfo s
propBranchOnly (MkBranch s) =
checkRight
((Just s, Nothing), Nothing)
s

propBranchRemote :: ValidBranch -> ValidBranch -> Bool
-- propBranchRemote b t = (isValidBranch b && isValidBranch t && (not $ "..." `isInfixOf` b)) ==> expectRight ((Just b, Just t), Nothing) $ branchInfo $ b ++"..." ++ t
propBranchRemote (MkBranch b) (MkBranch t) = expectRight ((Just b, Just t), Nothing) $ branchInfo $ b ++"..." ++ t
propBranchRemote (MkBranch b) (MkBranch t) =
checkRight
((Just b, Just t), Nothing)
$ b ++"..." ++ t

propBranchRemoteTracking :: ValidBranch -> ValidBranch -> BeHead -> Bool
propBranchRemoteTracking (MkBranch b) (MkBranch t) behead =
expectRight ((Just b, Just t), Just $ expectedAheadBehind behead) result
where
result = branchInfo $ b ++ "..." ++ t ++ " " ++ (show behead)
checkRight
((Just b, Just t), Just $ expectedAheadBehind behead)
$ b ++ "..." ++ t ++ " " ++ show behead

main :: IO()
main = mapM_ (quickCheckWith stdArgs { maxSuccess = 2^8 }) [property propNoBranch,
property propNewRepo,
property propBranchOnly,
property propBranchRemote,
property propBranchRemoteTracking]
main = mapM_ (quickCheckWith stdArgs { maxSuccess = 2^8 }) [
property propNoBranch,
property propNewRepo,
property propBranchOnly,
property propBranchRemote,
property propBranchRemoteTracking]

0 comments on commit 28cadff

Please sign in to comment.