diff --git a/cli-test/Tests.hs b/cli-test/Tests.hs index 9af7057..813a74d 100644 --- a/cli-test/Tests.hs +++ b/cli-test/Tests.hs @@ -36,7 +36,7 @@ instance Show CliArgs where instance Arbitrary CliArgs where arbitrary = do - len <- arbitrary `suchThat` (>2) `suchThat` (<=40) :: Gen Int + len <- arbitrary `suchThat` (>0) `suchThat` (<=40) :: Gen Int num <- arbitrary `suchThat` (>2) `suchThat` (<=20) :: Gen Int args <- sublistOf ["-n %d" `printf` num, show len] @@ -93,6 +93,14 @@ prop_shouldNotPrintZeroPasswords (CliArgs args) response <- readHandle out' return (response == "") +prop_shouldPrintLongPasswords :: GreaterThan79 Int -> Property +prop_shouldPrintLongPasswords (GT79 a) + = ioProperty $ do + (in', out', err', p) <- run' "dist/build/elocrypt/elocrypt" [show a] + response <- readHandle out' + return . all (==1) . map length . map words . lines $ response + + -- Utility functions run' :: FilePath -> [String] -> IO (Handle, Handle, Handle, ProcessHandle) run' exe args = do diff --git a/src/cli/Main.hs b/src/cli/Main.hs index d1890af..362cc4c 100644 --- a/src/cli/Main.hs +++ b/src/cli/Main.hs @@ -37,8 +37,10 @@ passwords :: RandomGen g => Options -> g -> String passwords opts gen = format . group' cols $ ps where ps = take num . newPasswords (optLength opts) $ gen format = concat . intersperse "\n" . map (concat . intersperse " ") - cols = termLen `div` (optLength opts + 2) - num = fromMaybe (cols * termHeight) (optNumber opts) + cols = if optLength opts <= termLen - 2 + then termLen `div` (optLength opts + 2) + else 1 + num = fromMaybe ((if cols == 0 then 1 else cols) * termHeight) (optNumber opts) group' :: Int -> [a] -> [[a]] group' _ [] = [] diff --git a/test/Test/Elocrypt/Instances.hs b/test/Test/Elocrypt/Instances.hs index 081bc31..04fdd70 100644 --- a/test/Test/Elocrypt/Instances.hs +++ b/test/Test/Elocrypt/Instances.hs @@ -18,3 +18,9 @@ newtype GreaterThan2 a = GT2 { getGT2 :: a } instance (Num a, Ord a, Arbitrary a) => Arbitrary (GreaterThan2 a) where arbitrary = GT2 `fmap` (arbitrary `suchThat` (>2)) + +newtype GreaterThan79 a = GT79 { getGT79 :: a } + deriving (Eq, Ord, Show) + +instance (Integral a, Random a) => Arbitrary (GreaterThan79 a) where + arbitrary = GT79 `fmap` choose (80, 500)