Skip to content

A peer-to-peer stack for code collaboration

Use a custom prelude (#47)

James Haydonc229a92c8fa1
diff --git a/package.yaml b/package.yaml
index a66ee5b..d7e1ecc 100644
--- a/package.yaml
+++ b/package.yaml
@@ -10,18 +10,16 @@ extra-source-files:
dependencies:
- base >=4 && <5
+- protolude
- containers
- deriving-compat
- haskeline
- megaparsec
-- mmorph
- mtl
- prettyprinter
- prettyprinter-ansi-terminal
- recursion-schemes
- scientific
-- STMonadTrans
-- syb
- text
data-files:
@@ -73,6 +71,7 @@ executables:
ghc-options: -Wall -threaded -Wcompat -Wincomplete-record-updates -Wincomplete-uni-patterns -Wredundant-constraints
default-extensions:
+- NoImplicitPrelude
- AutoDeriveTypeable
- ConstraintKinds
- DeriveDataTypeable
diff --git a/repl/Main.hs b/repl/Main.hs
index bab77f5..4b984d7 100644
--- a/repl/Main.hs
+++ b/repl/Main.hs
@@ -1,7 +1,7 @@
module Main (main) where
-import Data.Semigroup ((<>))
-import qualified Data.Text.IO as T
+import Protolude
+
import Options.Applicative
import System.Environment
@@ -10,7 +10,7 @@ import Radicle
main :: IO ()
main = do
opts' <- execParser allOpts
- cfgSrc <- T.readFile =<< case configFile opts' of
+ cfgSrc <- readFile =<< case configFile opts' of
Nothing -> getConfig
Just cfg -> pure cfg
hist <- case histFile opts' of
diff --git a/src/Radicle/Internal.hs b/src/Radicle/Internal.hs
index 4dd22d4..47d2b9d 100644
--- a/src/Radicle/Internal.hs
+++ b/src/Radicle/Internal.hs
@@ -3,14 +3,14 @@ module Radicle.Internal
, mkIdent
) where
+import Protolude
+
import Radicle.Internal.Core as X
import Radicle.Internal.Parse as X
import Radicle.Internal.Pretty as X
import Radicle.Internal.Primops as X
import Radicle.Internal.Subscriber as X
-import Control.Monad.Reader
-import Data.Text (Text)
import qualified Text.Megaparsec as M
-- | Smart constructor for Ident.
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index 3406142..d728882 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -1,22 +1,16 @@
module Radicle.Internal.Core where
-import Control.Monad.Except (ExceptT(..), MonadError, runExceptT,
- throwError)
+import Protolude hiding (TypeError, (<>))
+
import Control.Monad.State
import Data.Data (Data)
-import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NonEmpty
-import Data.Map (Map)
import qualified Data.Map as Map
import Data.Scientific (Scientific)
-import Data.Semigroup (Semigroup, (<>))
-import Data.Text (Text)
-import qualified Data.Text as T
-import Data.Void (Void)
-import GHC.Exts (IsList(..), fromString)
-import GHC.Generics (Generic)
+import Data.Semigroup ((<>))
+import qualified GHC.Exts as GhcExts
import qualified Text.Megaparsec.Error as Par
-- * Value
@@ -67,7 +61,7 @@ errorToValue e = case e of
Exit -> makeVal ("exit", [])
where
makeA = quote . Atom
- makeVal (t,v) = pure (Ident t, Dict $ Map.mapKeys (Atom . Ident) . fromList $ v)
+ makeVal (t,v) = pure (Ident t, Dict $ Map.mapKeys (Atom . Ident) . GhcExts.fromList $ v)
newtype Reference = Reference { getReference :: Int }
deriving (Show, Read, Ord, Eq, Generic)
@@ -118,17 +112,17 @@ newtype Ident = Ident { fromIdent :: Text }
-- Unsafe! Only use this if you know the string at compile-time and know it's a
-- valid identifier
-toIdent :: String -> Ident
-toIdent = Ident . fromString
+toIdent :: Text -> Ident
+toIdent = Ident
-- | The environment, which keeps all known bindings.
newtype Env s = Env { fromEnv :: Map Ident s }
deriving (Eq, Semigroup, Monoid, Ord, Show, Read, Generic, Functor, Foldable, Traversable)
-instance IsList (Env s) where
+instance GhcExts.IsList (Env s) where
type Item (Env s) = (Ident, s)
- fromList = Env . fromList
- toList = GHC.Exts.toList . fromEnv
+ fromList = Env . Map.fromList
+ toList = GhcExts.toList . fromEnv
-- | Primop mappings. The parameter specifies the monad the primops run in.
type Primops m = Map Ident ([Value ] -> Lang m Value)
@@ -224,7 +218,7 @@ eval val = do
Lambda _ _ Nothing -> throwError $ Impossible
"lambda should already have an env"
Lambda [bnd] body (Just closure) -> do
- let mappings = fromList [(bnd, val)]
+ let mappings = GhcExts.fromList [(bnd, val)]
modEnv = mappings <> closure
NonEmpty.last <$> withEnv (\e' -> e' { bindingsEnv = modEnv})
(traverse eval body)
@@ -237,7 +231,7 @@ baseEval val = case val of
Ref i -> pure $ Ref i
List (f:vs) -> f $$ vs
List xs -> throwError
- $ WrongNumberOfArgs ("application: " <> T.pack (show xs))
+ $ WrongNumberOfArgs ("application: " <> show xs)
2
(length xs)
String s -> pure $ String s
@@ -273,7 +267,7 @@ mfn $$ vs = do
(length vs)
else do
vs' <- traverse baseEval vs
- let mappings = fromList (zip bnds vs')
+ let mappings = GhcExts.fromList (zip bnds vs')
NonEmpty.last <$> withEnv
(\e -> e { bindingsEnv = mappings <> bindingsEnv e })
(traverse baseEval body)
@@ -283,7 +277,7 @@ mfn $$ vs = do
(length vs)
else do
vs' <- traverse baseEval vs
- let mappings = fromList (zip bnds vs')
+ let mappings = GhcExts.fromList (zip bnds vs')
modEnv = mappings <> closure
NonEmpty.last <$> withEnv (\e -> e { bindingsEnv = modEnv })
(traverse baseEval body)
diff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hs
index 5875c0c..88d292b 100644
--- a/src/Radicle/Internal/Parse.hs
+++ b/src/Radicle/Internal/Parse.hs
@@ -1,18 +1,11 @@
module Radicle.Internal.Parse where
-import Control.Applicative (many, (<|>))
-import Control.Monad (void)
-import Control.Monad.Except (MonadError, throwError)
-import Control.Monad.Identity (Identity)
-import Control.Monad.Reader (Reader, ask, runReader)
-import Control.Monad.State (gets)
+import Protolude hiding (some, try)
+
import Data.Char (isAlphaNum, isLetter)
-import Data.Either (partitionEithers)
import Data.List.NonEmpty (NonEmpty((:|)), fromList)
import qualified Data.Map as Map
-import Data.Text (Text)
import qualified Data.Text as T
-import Data.Void (Void)
import GHC.Exts (IsString(..))
import Text.Megaparsec (ParsecT, State(..), between, choice,
defaultTabWidth, eof, initialPos, manyTill,
@@ -49,7 +42,7 @@ parensP = between (symbol "(" >> spaceConsumer) (spaceConsumer >> symbol ")")
stringLiteralP :: VParser
stringLiteralP = lexeme $
- String . T.pack <$> (char '"' >> manyTill L.charLiteral (char '"'))
+ String . toS <$> (char '"' >> manyTill L.charLiteral (char '"'))
boolLiteralP :: VParser
boolLiteralP = lexeme $ Boolean <$> (char '#' >>
@@ -60,7 +53,7 @@ numLiteralP = Number <$> signed L.scientific
where
-- We don't allow spaces between the sign and digits so that we can remain
-- consistent with the general Scheme of things.
- signed p = M.option id ((id <$ char '+') <|> (negate <$ char '-')) <*> p
+ signed p = M.option identity ((identity <$ char '+') <|> (negate <$ char '-')) <*> p
identP :: Parser Ident
identP = lexeme $ do
@@ -120,13 +113,13 @@ valueP = do
-- Left (TypeError "Trying to apply a non-function")
interpret
:: Monad m
- => String
+ => Text
-> Text
-> Bindings m
-> m (Either (LangError Value) Value)
interpret sourceName expr bnds = do
let primopNames = Map.keys (bindingsPrimops bnds)
- parsed = runReader (runParserT (valueP <* eof) sourceName expr) primopNames
+ parsed = runReader (runParserT (valueP <* eof) (toS sourceName) expr) primopNames
case parsed of
Left e -> pure . Left $ ParseError e
Right v -> fst <$> runLang bnds (eval v)
@@ -137,24 +130,27 @@ interpret sourceName expr bnds = do
--
-- >>> fmap fst <$> runLang pureEnv $ interpretMany "test" "(define id (lambda (x) x))\n(id #t)"
-- Right (Boolean True)
-interpretMany :: Monad m => String -> Text -> Lang m Value
+interpretMany :: Monad m => Text -> Text -> Lang m Value
interpretMany sourceName src = do
primopNames <- gets $ Map.keys . bindingsPrimops
let parsed = parseValues sourceName src primopNames
case partitionEithers parsed of
- ([], vs) -> last <$> mapM eval vs
+ ([], vs) -> do es <- mapM eval vs
+ case lastMay es of
+ Just e -> pure e
+ _ -> throwError (OtherError "InterpretMany should be called with at least one expression.")
(e:_, _) -> throwError $ ParseError e
-- | Parse a Text as a series of values.
-- 'sourceName' is used for error reporting. 'prims' are the primop names.
--
-- Note that parsing continues even if one value fails to parse.
-parseValues :: String -> Text -> [Ident] -> [Either (Par.ParseError Char Void) Value]
+parseValues :: Text -> Text -> [Ident] -> [Either (Par.ParseError Char Void) Value]
parseValues sourceName srcCode prims = go $ initial
where
initial = State
{ stateInput = T.strip srcCode
- , statePos = initialPos sourceName :| []
+ , statePos = initialPos (toS sourceName) :| []
, stateTokensProcessed = 0
, stateTabWidth = defaultTabWidth
}
@@ -166,24 +162,24 @@ parseValues sourceName srcCode prims = go $ initial
--
-- Examples:
--
--- >>> parse "test" "#t" [] :: Either String Value
+-- >>> parse "test" "#t" [] :: Either Text Value
-- Right (Boolean True)
--
--- >>> parse "test" "hi" [toIdent "hi"] :: Either String Value
+-- >>> parse "test" "hi" [toIdent "hi"] :: Either Text Value
-- Right (Primop (Ident {fromIdent = "hi"}))
--
--- >>> parse "test" "hi" [] :: Either String Value
+-- >>> parse "test" "hi" [] :: Either Text Value
-- Right (Atom (Ident {fromIdent = "hi"}))
-parse :: MonadError String m => String -> Text -> [Ident] -> m Value
+parse :: MonadError Text m => Text -> Text -> [Ident] -> m Value
parse file src ids = do
- let res = runReader (M.runParserT (valueP <* eof) file src) ids
+ let res = runReader (M.runParserT (valueP <* eof) (toS file) src) ids
case res of
- Left err -> throwError $ M.parseErrorPretty' src err
+ Left err -> throwError . toS $ M.parseErrorPretty' src err
Right v -> pure v
-- | Like 'parse', but uses "(test)" as the source name and the default set of
-- primops.
-parseTest :: MonadError String m => Text -> m Value
+parseTest :: MonadError Text m => Text -> m Value
parseTest t = parse "(test)" t (Map.keys $ bindingsPrimops e)
where
e :: Bindings (Lang Identity)
diff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hs
index d650cfb..0241fbe 100644
--- a/src/Radicle/Internal/Pretty.hs
+++ b/src/Radicle/Internal/Pretty.hs
@@ -1,9 +1,9 @@
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Radicle.Internal.Pretty where
-import Data.List.NonEmpty (toList)
+import Protolude hiding (TypeError, (<>))
+
import qualified Data.Map as Map
-import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Prettyprint.Doc
import Data.Text.Prettyprint.Doc.Render.Text
@@ -24,7 +24,7 @@ instance Pretty Value where
Atom i -> pretty i
Ref i -> parens $ "ref" <+> pretty i
String t -> "\"" <> pretty (escapeStr t) <> "\""
- Number n -> pretty $ show n
+ Number n -> pretty (show n :: Text)
Boolean True -> "#t"
Boolean False -> "#f"
List vs -> parens (sep (pretty <$> vs))
diff --git a/src/Radicle/Internal/Primops.hs b/src/Radicle/Internal/Primops.hs
index c6ad838..d27b7df 100644
--- a/src/Radicle/Internal/Primops.hs
+++ b/src/Radicle/Internal/Primops.hs
@@ -3,16 +3,11 @@ module Radicle.Internal.Primops
, purePrimops
) where
-import Control.Monad.Except (catchError, throwError)
-import Control.Monad.State
-import Data.Bifunctor (first)
-import Data.Foldable (foldlM, foldrM)
+import Protolude hiding (TypeError)
+
import qualified Data.IntMap as IntMap
import qualified Data.Map as Map
-import Data.Maybe (catMaybes, isJust)
import Data.Scientific (Scientific)
-import Data.Semigroup ((<>))
-import Data.Text (Text)
import GHC.Exts (IsList(..))
import Radicle.Internal.Core
@@ -27,7 +22,7 @@ pureEnv = Bindings e purePrimops mempty 0
-- | The universal primops. These are available in chain evaluation, and are
-- not shadowable via 'define'.
purePrimops :: forall m. (Monad m) => Primops m
-purePrimops = Map.fromList $ first Ident <$>
+purePrimops = fromList $ first Ident <$>
[ ("base-eval", evalArgs $ \args -> case args of
[x] -> baseEval x
xs -> throwError $ WrongNumberOfArgs "base-eval" 1 (length xs))
@@ -35,7 +30,7 @@ purePrimops = Map.fromList $ first Ident <$>
, ("dict", evalArgs $ \args ->
let go (k:v:rest) = Map.insert k v $ go rest
go [] = mempty
- go _ = error "impossible"
+ go _ = panic "impossible"
in if length args `mod` 2 == 0
then pure . Dict $ go args
else throwError $ OtherError "'dict' expects even number of args")
diff --git a/src/Radicle/Internal/Subscriber.hs b/src/Radicle/Internal/Subscriber.hs
index 2d25e6e..ac749a4 100644
--- a/src/Radicle/Internal/Subscriber.hs
+++ b/src/Radicle/Internal/Subscriber.hs
@@ -1,13 +1,8 @@
module Radicle.Internal.Subscriber where
-import Control.Monad (forever, void)
-import Control.Monad.Except (throwError)
-import Control.Monad.State (get, gets)
-import Data.Bifunctor (first)
-import Data.List (isPrefixOf)
+import Protolude hiding (TypeError)
+
import qualified Data.Map as Map
-import Data.Monoid ((<>))
-import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Prettyprint.Doc (pretty)
import Data.Text.Prettyprint.Doc.Render.Terminal (putDoc)
@@ -84,7 +79,7 @@ replPrimops = Map.fromList $ first toIdent <$>
case p of
Right v -> pure v
Left e -> throwError $ ThrownError (Ident "parse-error")
- (String $ T.pack e)
+ (String e)
xs -> throwError $ WrongNumberOfArgs "get-line!" 0 (length xs))
, ("subscribe-to!", \args -> case args of
diff --git a/src/Radicle/Internal/Subscriber/Capabilities.hs b/src/Radicle/Internal/Subscriber/Capabilities.hs
index f5f236b..15a56c9 100644
--- a/src/Radicle/Internal/Subscriber/Capabilities.hs
+++ b/src/Radicle/Internal/Subscriber/Capabilities.hs
@@ -4,10 +4,8 @@
-- constraints) what effects they do.
module Radicle.Internal.Subscriber.Capabilities where
-import Control.Monad.State (gets, modify)
-import Control.Monad.Trans
-import Data.Text (Text)
-import qualified Data.Text as T
+import Protolude
+
import Data.Text.Prettyprint.Doc (PageWidth)
import System.Console.Haskeline
@@ -19,15 +17,15 @@ instance {-# OVERLAPPABLE #-} Stdin m => Stdin (Lang m) where
getLineS = lift getLineS
instance (MonadException m, Monad m) => Stdin (InputT m) where
getLineS = getInputLine "rad> " >>= \x -> case x of
- Nothing -> error "curious about why this would happen"
- Just v -> pure $ T.pack v
+ Nothing -> panic "curious about why this would happen"
+ Just v -> pure . toS $ v
class (Monad m) => Stdout m where
putStrS :: Text -> m ()
instance {-# OVERLAPPABLE #-} Stdout m => Stdout (Lang m) where
putStrS = lift . putStrS
instance (MonadException m, Monad m) => Stdout (InputT m) where
- putStrS = outputStrLn . T.unpack
+ putStrS = outputStrLn . toS
class (Monad m) => Exit m where
exitS :: m ()
@@ -45,7 +43,7 @@ instance Monad m => SetEnv (Lang m) Value where
setEnvS e = modify (\bnds -> bnds {bindingsEnv = e})
class (Monad m) => GetSourceName m where
- getSourceNameS :: m String
+ getSourceNameS :: m Text
class (Monad m) => HasPageWidth m where
getPageWidthS :: m PageWidth
class (Monad m) => GetSubs m where
diff --git a/test/docs/Main.hs b/test/docs/Main.hs
index 48938eb..8d9d5ea 100644
--- a/test/docs/Main.hs
+++ b/test/docs/Main.hs
@@ -1,5 +1,7 @@
module Main (main) where
+import Protolude
+
import Hpack.Config
import System.FilePath.Glob (glob)
import Test.DocTest
diff --git a/test/spec/Radicle/Internal/Arbitrary.hs b/test/spec/Radicle/Internal/Arbitrary.hs
index 41760ad..53864c3 100644
--- a/test/spec/Radicle/Internal/Arbitrary.hs
+++ b/test/spec/Radicle/Internal/Arbitrary.hs
@@ -1,13 +1,10 @@
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Radicle.Internal.Arbitrary where
-import Control.Monad.Identity (Identity)
-import Data.Bifunctor (first)
+import Protolude
+
import qualified Data.Map as Map
-import Data.Maybe (isJust)
import Data.Scientific (Scientific)
-import qualified Data.Text as T
-import Safe (readMay)
import Test.QuickCheck
import Test.QuickCheck.Instances ()
@@ -45,10 +42,10 @@ instance Arbitrary Value where
prims :: Map.Map Ident ([Value] -> Lang Identity Value)
prims = purePrimops
isPrimop x = x `elem` Map.keys prims
- isNum x = isJust (readMay (T.unpack $ fromIdent x) :: Maybe Scientific)
+ isNum x = isJust (readMaybe (toS $ fromIdent x) :: Maybe Scientific)
instance Arbitrary Ident where
- arbitrary = ((:) <$> firstL <*> rest) `suchThatMap` (mkIdent . T.pack)
+ arbitrary = ((:) <$> firstL <*> rest) `suchThatMap` (mkIdent . toS)
where
allChars = take 100 ['!' .. maxBound]
firstL = elements $ filter isValidIdentFirst allChars
diff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hs
index d51268e..70d956b 100644
--- a/test/spec/Radicle/Internal/TestCapabilities.hs
+++ b/test/spec/Radicle/Internal/TestCapabilities.hs
@@ -2,9 +2,7 @@
-- Radicle.Internal.Subscriber.Capabilities that may be used for testing.
module Radicle.Internal.TestCapabilities where
-import Control.Monad.State
-import Control.Monad.Except
-import Data.Text (Text)
+import Protolude
import Radicle
import Radicle.Internal.Subscriber.Capabilities
@@ -56,7 +54,7 @@ instance {-# OVERLAPPING #-} Stdin TestLang where
getLineS = do
ws <- lift get
case worldStateStdin ws of
- [] -> throwError $ OtherError "test: out of stdin"
+ [] -> throwError $ OtherError "test: out of stdin"
h:hs -> lift (put $ ws { worldStateStdin = hs }) >> pure h
instance {-# OVERLAPPING #-} Stdout TestLang where
diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hs
index 17cda21..8974a8e 100644
--- a/test/spec/Radicle/Tests.hs
+++ b/test/spec/Radicle/Tests.hs
@@ -1,14 +1,12 @@
{-# LANGUAGE QuasiQuotes #-}
module Radicle.Tests where
-import Data.Either (isLeft)
-import Data.Functor.Identity (runIdentity)
+import Protolude hiding (toList)
+
import Data.List (isSuffixOf)
-import Data.Semigroup ((<>))
import Data.String.Interpolate (i)
import Data.String.QQ (s)
import qualified Data.Text as T
-import qualified Data.Text.IO as T
import GHC.Exts (fromList, toList)
import System.Directory (getDirectoryContents)
import Test.Tasty
@@ -20,7 +18,7 @@ import Radicle.Internal.Arbitrary ()
import Radicle.Internal.Core (toIdent)
import Radicle.Internal.TestCapabilities
-import Paths_radicle -- generated by cabal via 'data-files'
+import Paths_radicle
test_eval :: [TestTree]
test_eval =
@@ -54,7 +52,7 @@ test_eval =
, testProperty "'eq?' considers equal values equal" $ \(val :: Value) -> do
let prog = [i|(eq? #{renderPrettyDef val} #{renderPrettyDef val})|]
- res = runTest' $ T.pack prog
+ res = runTest' $ toS prog
counterexample prog $ isLeft res || res == Right (Boolean True)
, testCase "'eq?' works for quoted values" $ do
@@ -67,7 +65,7 @@ test_eval =
-- We quote the values to prevent errors from being thrown
let prog = [i|(eq? (quote #{renderPrettyDef v1})
(quote #{renderPrettyDef v2}))|]
- res = runTest' $ T.pack prog
+ res = runTest' $ toS prog
-- Either evaluation failed or their equal.
counterexample prog $ isLeft res || res == Right (Boolean False)
@@ -110,7 +108,7 @@ test_eval =
expected = Right . String $ mconcat ss
info = "Expected:\n" <> prettyEither expected <>
"Got:\n" <> prettyEither res
- counterexample (T.unpack info) $ res == expected
+ counterexample (toS info) $ res == expected
, testCase "'foldl' foldls the list" $ do
let prog = [s|(foldl - 0 (list 1 2 3))|]
@@ -138,11 +136,11 @@ test_eval =
, testProperty "'eval' does not alter functions" $ \(_v :: Value) -> do
let prog1 = [i| (eval (lambda () #{renderPrettyDef _v})) |]
prog2 = [i| (lambda () #{renderPrettyDef _v}) |]
- res1 = runTest' $ T.pack prog1
- res2 = runTest' $ T.pack prog2
+ res1 = runTest' $ toS prog1
+ res2 = runTest' $ toS prog2
info = "Expected:\n" <> prettyEither res2
<> "\nGot:\n" <> prettyEither res1
- counterexample (T.unpack info) $ res1 == res2
+ counterexample (toS info) $ res1 == res2
, testCase "lambdas work" $ do
let prog = [s|((lambda (x) x) #t)|]
@@ -234,12 +232,12 @@ test_eval =
, testProperty "'>' works" $ \(x, y) -> do
let prog = [i|(> #{renderPrettyDef $ Number x} #{renderPrettyDef $ Number y})|]
- res = runTest' $ T.pack prog
+ res = runTest' $ toS prog
counterexample prog $ res == Right (Boolean (x > y))
, testProperty "'<' works" $ \(x, y) -> do
let prog = [i|(< #{renderPrettyDef $ Number x} #{renderPrettyDef $ Number y})|]
- res = runTest' $ T.pack prog
+ res = runTest' $ toS prog
counterexample prog $ res == Right (Boolean (x < y))
, testCase "'define' fails when first arg is not an atom" $ do
@@ -283,10 +281,10 @@ test_eval =
res @?= Right (Number 6)
, testProperty "read-ref . ref == id" $ \(v :: Value) -> do
- let derefed = runTest' $ T.pack [i|(read-ref (ref #{renderPrettyDef v}))|]
- orig = runTest' $ T.pack [i|#{renderPrettyDef v}|]
- info = "Expected:\n" <> T.unpack (prettyEither orig)
- <> "\nGot:\n" <> T.unpack (prettyEither derefed)
+ let derefed = runTest' $ toS [i|(read-ref (ref #{renderPrettyDef v}))|]
+ orig = runTest' $ toS [i|#{renderPrettyDef v}|]
+ info = "Expected:\n" <> toS (prettyEither orig)
+ <> "\nGot:\n" <> toS (prettyEither derefed)
counterexample info $ derefed == orig
, testCase "'show' works" $ do
@@ -359,9 +357,9 @@ test_pretty =
actual = parseTest rendered
original = removeEnv val
info = case actual of
- Left e -> "parse error in: " <> T.unpack rendered <> "\n"
- <> e
- Right v -> "pretty: " <> T.unpack rendered <> "\n"
+ Left e -> "parse error in: " <> toS rendered <> "\n"
+ <> toS e
+ Right v -> "pretty: " <> toS rendered <> "\n"
<> "reparsed: " <> show v <> "\n"
<> "original: " <> show original <> "\n"
in counterexample info $ actual == Right original
@@ -377,7 +375,7 @@ test_repl_primops :: [TestTree]
test_repl_primops =
[ testProperty "get-line! returns the input line" $ \(v :: Value) ->
let prog = [i|(eq? (get-line!) (quote #{renderPrettyDef v}))|]
- res = run [renderPrettyDef v] $ T.pack prog
+ res = run [renderPrettyDef v] $ toS prog
in counterexample prog $ res == Right (Boolean True)
, testCase "catch catches get-line errors" $ do
@@ -392,7 +390,7 @@ test_repl_primops =
res @?= Right (String "caught")
]
where
- run stdin prog = fst $ runTestWith replBindings stdin prog
+ run stdin' prog = fst $ runTestWith replBindings stdin' prog
test_repl :: [TestTree]
@@ -436,27 +434,27 @@ test_repl =
result @?= output
]
where
- getCfg = getDataFileName "repl/config.rad" >>= T.readFile
+ getCfg = getDataFileName "repl/config.rad" >>= readFile
-- The repl catches exceptions, including the "out of stdin" exception
-- that occurs at the end of a session, so we take the 'init' of the
-- result.
- runInRepl inp = fmap init <$> (runTestWith replBindings inp <$> getCfg)
+ runInRepl inp = fmap initSafe <$> (runTestWith replBindings inp <$> getCfg)
-- Tests all radicle files 'repl' dir. These should use the 'should-be'
-- function to ensure they are in the proper format.
test_source_files :: IO TestTree
test_source_files = testGroup "Radicle source file tests" <$> do
oneOf'Em <- getDataFileName "repl/config.rad"
- let dir = reverse $ drop (length ("config.rad" :: String)) $ reverse oneOf'Em
+ let dir = reverse $ drop (T.length ("config.rad" :: Text)) $ reverse oneOf'Em
files <- getDirectoryContents dir
let radFiles = filter (".rad" `isSuffixOf`) files
sequence $ radFiles <&> \file -> do
- contents <- T.readFile $ dir <> file
+ contents <- readFile $ dir <> file
let (_, out) = runTestWith replBindings [] contents
let makeTest line = let (name, result) = T.span (/= '\'')
$ T.drop 1
$ T.dropWhile (/= '\'') line
- in testCase (T.unpack name) $ result @?= "' succeeded\""
+ in testCase (toS name) $ result @?= "' succeeded\""
pure $ testGroup file
$ [ makeTest ln | ln <- out, "\"Test" `T.isPrefixOf` ln ]
@@ -474,6 +472,3 @@ removeEnv v = case v of
prettyEither :: Either (LangError Value) Value -> T.Text
prettyEither (Left e) = "Error: " <> renderPrettyDef e
prettyEither (Right v) = renderPrettyDef v
-
-(<&>) :: Functor f => f a -> (a -> b) -> f b
-(<&>) = flip fmap