-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from memowe/property-tests
Add property tests / Use stricter String types
- Loading branch information
Showing
6 changed files
with
268 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
{- | | ||
Ini data essentially consists of 'String's, that cannot contain every character | ||
because of the simple format it is contained in. The types in this module are | ||
restricted to contain only allowed characters. They also can not be empty or | ||
start or end with whitespace. Values of these types can not be created with | ||
data constructors. Use the @mk*@ functions instead! | ||
-} | ||
|
||
module Trivialini.SafeTypes | ||
( | ||
-- * Safe 'String' types | ||
IniHeading(getHeading), IniKey(getKey), IniValue(getValue) | ||
-- (No data constructors!) | ||
-- ** Value creation | ||
, mkHdg, mkKey, mkVal | ||
-- ** Validity predicates | ||
, isValidHeading, isValidKey, isValidValue | ||
-- ** Invalid character lists (useful for parsers) | ||
, invalidHdgChars, invalidKeyChars, invalidValChars | ||
-- ** Utility predicate | ||
, isValidStr | ||
) where | ||
|
||
import Data.Bool | ||
import Data.Char | ||
import Data.String | ||
import Data.Maybe | ||
import Control.Applicative | ||
|
||
-- Utility function | ||
guarded :: Alternative m => (a -> Bool) -> a -> m a | ||
guarded = liftA2 (bool empty) pure | ||
|
||
-- | A section heading | ||
newtype IniHeading = Hdg { getHeading :: String } deriving (Eq, Ord) | ||
-- | A key of a key-value pair | ||
newtype IniKey = Key { getKey :: String } deriving (Eq, Ord) | ||
-- | A value of a key-value pair | ||
newtype IniValue = Val { getValue :: String } deriving (Eq, Ord) | ||
|
||
invalidHdgChars :: String | ||
invalidKeyChars :: String | ||
invalidValChars :: String | ||
invalidHdgChars = "=]\n" | ||
invalidKeyChars = "=[\n" | ||
invalidValChars = "\n" | ||
|
||
isValidHeading :: String -> Bool | ||
isValidKey :: String -> Bool | ||
isValidValue :: String -> Bool | ||
isValidHeading = all (`notElem` invalidHdgChars) &&& isValidStr | ||
isValidKey = all (`notElem` invalidKeyChars) &&& isValidStr | ||
isValidValue = all (`notElem` invalidValChars) &&& isValidStr | ||
|
||
isValidStr :: String -> Bool | ||
isValidStr = (not . null) | ||
&&& (not . any isControl) | ||
&&& (not . isSpace . head) | ||
Check warning on line 58 in src/Trivialini/SafeTypes.hs GitHub Actions / GHC 9.8 on ubuntu-latest
|
||
&&& (not . isSpace . last) | ||
|
||
(&&&) :: (a -> Bool) -> (a -> Bool) -> a -> Bool | ||
(&&&) = liftA2 (&&) | ||
|
||
mkHdg :: String -> Maybe IniHeading | ||
mkKey :: String -> Maybe IniKey | ||
mkVal :: String -> Maybe IniValue | ||
mkHdg = fmap Hdg . guarded isValidHeading | ||
mkKey = fmap Key . guarded isValidKey | ||
mkVal = fmap Val . guarded isValidValue | ||
|
||
instance Show IniHeading where show = getHeading | ||
instance Show IniKey where show = getKey | ||
instance Show IniValue where show = getValue | ||
|
||
instance IsString IniHeading where | ||
fromString = fromMaybe <$> error . ("Not a heading: " ++) <*> mkHdg | ||
instance IsString IniKey where | ||
fromString = fromMaybe <$> error . ("Not a key: " ++) <*> mkKey | ||
instance IsString IniValue where | ||
fromString = fromMaybe <$> error . ("Not a value: " ++) <*> mkVal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
module TestSafeTypes where | ||
|
||
import Trivialini.SafeTypes | ||
import Test.Hspec | ||
import Test.QuickCheck | ||
import Test.Hspec.QuickCheck | ||
|
||
import Data.String | ||
import Control.Applicative | ||
Check warning on line 10 in test/TestSafeTypes.hs GitHub Actions / GHC 9.6 on ubuntu-latest
|
||
import Control.Exception | ||
|
||
instance Arbitrary IniHeading where | ||
arbitrary = arbitrary `suchThatMap` mkHdg | ||
|
||
instance Arbitrary IniKey where | ||
arbitrary = arbitrary `suchThatMap` mkKey | ||
|
||
instance Arbitrary IniValue where | ||
arbitrary = arbitrary `suchThatMap` mkVal | ||
|
||
testArbinitrary :: Spec | ||
testArbinitrary = describe "Safe types tests" $ do | ||
modifyMaxDiscardRatio (const 1000) $ do -- Neccessary, but tests are fast | ||
|
||
context "Ini headings" $ do | ||
|
||
it "Correct invalid characters" $ | ||
invalidHdgChars `shouldBe` "=]\n" | ||
|
||
describe "Validity predicates" $ do | ||
prop "valid" $ \s -> safeHdg s ==> | ||
isValidHeading s `shouldBe` True | ||
prop "invalid" $ \s -> not (safeHdg s) ==> | ||
isValidHeading s `shouldBe` False | ||
|
||
describe "Data construction" $ do | ||
prop "valid" $ \s -> safeHdg s ==> | ||
getHeading <$> mkHdg s `shouldBe` Just s | ||
prop "invalid" $ \s -> not (safeHdg s) ==> | ||
mkHdg s `shouldBe` Nothing | ||
|
||
prop "Show instance" $ \s -> safeHdg s ==> | ||
show <$> mkHdg s `shouldBe` Just s | ||
|
||
describe "IsString instance" $ do | ||
prop "valid" $ \s -> safeHdg s ==> | ||
getHeading (fromString s) `shouldBe` s | ||
prop "invalid" $ \s -> not (safeHdg s) ==> | ||
evaluate (fromString s :: IniHeading) | ||
`shouldThrow` errorCall ("Not a heading: " ++ s) | ||
|
||
context "Ini keys" $ do | ||
|
||
it "Correct invalid characters" $ | ||
invalidKeyChars `shouldBe` "=[\n" | ||
|
||
describe "Validity predicates" $ do | ||
prop "valid" $ \s -> safeKey s ==> | ||
isValidKey s `shouldBe` True | ||
prop "invalid" $ \s -> not (safeKey s) ==> | ||
isValidKey s `shouldBe` False | ||
|
||
describe "Data construction" $ do | ||
prop "valid" $ \s -> safeKey s ==> | ||
getKey <$> mkKey s `shouldBe` Just s | ||
prop "invalid" $ \s -> not (safeKey s) ==> | ||
mkKey s `shouldBe` Nothing | ||
|
||
prop "Show instance" $ \s -> safeKey s ==> | ||
show <$> mkKey s `shouldBe` Just s | ||
|
||
describe "IsString instance" $ do | ||
prop "valid" $ \s -> safeKey s ==> | ||
getKey (fromString s) `shouldBe` s | ||
prop "invalid" $ \s -> not (safeKey s) ==> | ||
evaluate (fromString s :: IniKey) | ||
`shouldThrow` errorCall ("Not a key: " ++ s) | ||
|
||
context "Ini values" $ do | ||
|
||
it "Correct invalid characters" $ | ||
invalidValChars `shouldBe` "\n" | ||
|
||
describe "Validity predicates" $ do | ||
prop "valid" $ \s -> safeVal s ==> | ||
isValidValue s `shouldBe` True | ||
prop "invalid" $ \s -> not (safeVal s) ==> | ||
isValidValue s `shouldBe` False | ||
|
||
describe "Data construction" $ do | ||
prop "valid" $ \s -> safeVal s ==> | ||
getValue <$> mkVal s `shouldBe` Just s | ||
prop "invalid" $ \s -> not (safeVal s) ==> | ||
mkVal s `shouldBe` Nothing | ||
|
||
prop "Show instance" $ \s -> safeVal s ==> | ||
show <$> mkVal s `shouldBe` Just s | ||
|
||
describe "IsString instance" $ do | ||
prop "valid" $ \s -> safeVal s ==> | ||
getValue (fromString s) `shouldBe` s | ||
prop "invalid" $ \s -> not (safeVal s) ==> | ||
evaluate (fromString s :: IniValue) | ||
`shouldThrow` errorCall ("Not a value: " ++ s) | ||
|
||
where safeHdg = all (`notElem` invalidHdgChars) &&& isValidStr | ||
safeKey = all (`notElem` invalidKeyChars) &&& isValidStr | ||
safeVal = all (`notElem` invalidValChars) &&& isValidStr | ||
(&&&) = liftA2 (&&) |
Oops, something went wrong.