Skip to content

A peer-to-peer stack for code collaboration

Move repl tests to radicle-repl from radicle-lang. Needed to rework having an Arbitrary module exported from radicle-lang. Unfortunately we're repeating the state world code in the radicle-lang for now.

Fintan Halpenny5ccee683e180
diff --git a/radicle-lang/package.yaml b/radicle-lang/package.yaml
index 3d0bfed..013551a 100644
--- a/radicle-lang/package.yaml
+++ b/radicle-lang/package.yaml
@@ -45,8 +45,11 @@ library:
- prettyprinter-ansi-terminal
- process
- protolude
+ - QuickCheck
+ - quickcheck-instances
- serialise
- scientific
+ - template-haskell
- text
- time
- uuid
diff --git a/radicle-lang/src/Radicle.hs b/radicle-lang/src/Radicle.hs
index 04b1bda..0ce865f 100644
--- a/radicle-lang/src/Radicle.hs
+++ b/radicle-lang/src/Radicle.hs
@@ -81,9 +81,6 @@ module Radicle
, purePrimFns
, addPrimFns
- -- * CLI
- , getHistoryFile
-
-- * Helpers
, quote
, ($$)
@@ -95,7 +92,6 @@ module Radicle
import Control.Monad.Identity (runIdentity)
import Data.Text.Prettyprint.Doc
import Radicle.Internal.Annotation
-import Radicle.Internal.CLI
import Radicle.Internal.Core
import Radicle.Internal.Eval
import Radicle.Internal.Identifier
diff --git a/radicle-lang/test/spec/Radicle/Internal/Arbitrary.hs b/radicle-lang/src/Radicle/Internal/Arbitrary.hs
similarity index 100%
rename from radicle-lang/test/spec/Radicle/Internal/Arbitrary.hs
rename to radicle-lang/src/Radicle/Internal/Arbitrary.hs
diff --git a/radicle-lang/test/spec/Radicle/Tests.hs b/radicle-lang/test/spec/Radicle/Tests.hs
index 40c6920..58049e9 100644
--- a/radicle-lang/test/spec/Radicle/Tests.hs
+++ b/radicle-lang/test/spec/Radicle/Tests.hs
@@ -4,12 +4,14 @@ module Radicle.Tests where
import Protolude hiding (toList)
import Codec.Serialise (Serialise, deserialise, serialise)
+import qualified Crypto.Random as CryptoRand
import qualified Data.Map.Strict as Map
import Data.Scientific (Scientific)
import Data.Sequence (Seq(..))
import Data.String.Interpolate (i)
import Data.String.QQ (s)
import qualified Data.Text as T
+import qualified Data.Time as Time
import GHC.Exts (fromList, toList)
import System.IO.Unsafe (unsafePerformIO)
import System.Process (CmdSpec(..), StdStream(..))
@@ -24,7 +26,6 @@ import qualified Radicle.Internal.Annotation as Ann
import Radicle.Internal.Arbitrary ()
import Radicle.Internal.Core (asValue, noStack)
import Radicle.Internal.Foo (Foo)
-import Radicle.Internal.TestCapabilities
import Radicle.TH
test_eval :: [TestTree]
@@ -712,68 +713,6 @@ test_env =
fromList (toList env') == env'
]
-test_repl_primops :: [TestTree]
-test_repl_primops =
- [ testProperty "(read-annotated \"get-line!\" (get-line!)) returns the input line" $ \(v :: Value) ->
- let prog = [i|(eq? (read-annotated \"get-line!\" (get-line!)) (quote #{renderPrettyDef v}))|]
- -- We're not actually using IO
- res = unsafePerformIO $ run [renderPrettyDef v] $ toS prog
- in counterexample prog $ res == Right (Boolean True)
-
- , testCase "catch catches read-line errors" $ do
- let prog = [s|
- (catch 'any (read-line!) (fn [x] "caught"))
- |]
- input = ["\"blah"]
- res <- run input prog
- res @?= Right (String "caught")
- , testCase "read-file! can read a file" $ do
- let prog = "(read-file! \"foobar.rad\")"
- files = Map.singleton "foobar.rad" "foobar"
- res <- runCodeWithFiles files prog
- res @?= Right (String "foobar")
- , testCase "load! can load definitions" $ do
- let prog = [s|
- (load! "foo.rad")
- (+ foo bar)
- |]
- files = Map.singleton "foo.rad" "(def foo 42) (def bar 8)"
- res <- runCodeWithFiles files prog
- res @?= Right (int 50)
- , testCase "load! ignores shebangs" $ do
- let prog = [s|
- (load! "foo.rad")
- taxi
- |]
- files = Map.singleton "foo.rad" "#!/blah\n(def taxi 1797)"
- res <- runCodeWithFiles files prog
- res @?= Right (int 1797)
- , testCase "generating and verifying cryptographic signatures works" $ do
- let prog = [s|
- (def my-keys (gen-key-pair! (default-ecc-curve)))
- (def not-my-keys (gen-key-pair! (default-ecc-curve)))
- (def sig (gen-signature! (lookup :private-key my-keys) "hello"))
- (def tt (verify-signature (lookup :public-key my-keys) sig "hello"))
- (def ff (verify-signature (lookup :public-key not-my-keys) sig "hello"))
- (list tt ff)
- |]
- res <- run [] prog
- res @?= Right (List [Boolean True, Boolean False])
- , testCase "uuid! generates a valid uuid" $ do
- let prog = [s| (uuid? (uuid!)) |]
- res <- run [] prog
- res @?= Right (Boolean True)
-
- , testCase "'exit!' throws an Exit error with the provided code" $ do
- let prog = [s|(exit! 5)|]
- Left (LangError _ errorData) <- run [] prog
- errorData @?= Exit 5
- ]
- where
- run stdin' prog =
- let ws = defaultWorldState { worldStateStdin = stdin' }
- in fst <$> runCodeWithWorld ws prog
-
test_from_to_radicle :: [TestTree]
test_from_to_radicle =
[ testGroup "()"
@@ -863,3 +802,34 @@ parseTest t = parse "(test)" t
prettyEither :: Either (LangError Value) Value -> T.Text
prettyEither (Left e) = "Error: " <> renderPrettyDef e
prettyEither (Right v) = renderPrettyDef v
+
+-- TODO(fintan): this is repeated :(
+data WorldState = WorldState
+ { worldStateStdin :: [Text]
+ , worldStateStdout :: [Text]
+ , worldStateFiles :: Map Text Text
+ , worldStateDRG :: CryptoRand.ChaChaDRG
+ , worldStateUUID :: Int
+ , worldStateRemoteChains :: Map Text (Seq Value)
+ , worldStateCurrentTime :: Time.UTCTime
+ }
+
+defaultWorldState :: WorldState
+defaultWorldState =
+ WorldState
+ { worldStateStdin = []
+ , worldStateStdout = []
+ , worldStateFiles = mempty
+ , worldStateDRG = CryptoRand.drgNewSeed (CryptoRand.seedFromInteger 4) -- chosen by fair dice roll
+ , worldStateUUID = 0
+ , worldStateRemoteChains = mempty
+ , worldStateCurrentTime = Time.UTCTime (Time.ModifiedJulianDay 0) 0
+ }
+
+type TestLang = Lang (StateT WorldState IO)
+
+-- | Run radicle code in a pure environment.
+runPureCode :: Text -> Either (LangError Value) Value
+runPureCode code =
+ let prog = interpretMany "[test]" code
+ in evalState (fst <$> runLang pureEnv prog) defaultWorldState
diff --git a/radicle-repl/package.yaml b/radicle-repl/package.yaml
index e75e2d3..407da83 100644
--- a/radicle-repl/package.yaml
+++ b/radicle-repl/package.yaml
@@ -54,6 +54,7 @@ tests:
- QuickCheck
- quickcheck-instances
- radicle-lang
+ - radicle-repl
- scientific
- serialise
- stm
diff --git a/radicle-repl/test/Spec.hs b/radicle-repl/test/Spec.hs
deleted file mode 100644
index cd4753f..0000000
--- a/radicle-repl/test/Spec.hs
+++ /dev/null
@@ -1,2 +0,0 @@
-main :: IO ()
-main = putStrLn "Test suite not yet implemented"
diff --git a/radicle-lang/test/spec/Radicle/Internal/TestCapabilities.hs b/radicle-repl/test/spec/Radicle/Repl/TestCapabilities.hs
similarity index 94%
rename from radicle-lang/test/spec/Radicle/Internal/TestCapabilities.hs
rename to radicle-repl/test/spec/Radicle/Repl/TestCapabilities.hs
index 8ba5aba..247bccb 100644
--- a/radicle-lang/test/spec/Radicle/Internal/TestCapabilities.hs
+++ b/radicle-repl/test/spec/Radicle/Repl/TestCapabilities.hs
@@ -1,6 +1,6 @@
-- | This module defines instances for the classes in
--- Radicle.Internal.Subscriber.Capabilities that may be used for testing.
-module Radicle.Internal.TestCapabilities (
+-- "Radicle.Repl.Capabilities" that may be used for testing.
+module Radicle.Repl.TestCapabilities (
runCodeWithWorld
, runPureCode
, runCodeWithFiles
@@ -18,8 +18,9 @@ import qualified Data.Time as Time
import Radicle
import Radicle.Internal.Crypto
-import Radicle.Internal.Effects.Capabilities
import qualified Radicle.Internal.UUID as UUID
+import Radicle.Repl (replPrimFns)
+import Radicle.Repl.Capabilities
data WorldState = WorldState
@@ -62,7 +63,7 @@ runPureCode code =
runCodeWithWorld :: WorldState -> Text -> IO (Either (LangError Value) Value, [Text])
runCodeWithWorld ws code = do
let prog = interpretMany "[test]" code
- bindings <- createImpureBindings []
+ bindings = addPrimFns (replPrimFns []) pureEnv
(result, ws') <- runStateT (fst <$> runLang bindings prog) ws
pure $ (result, worldStateStdout ws')
diff --git a/radicle-repl/test/spec/Radicle/Repl/Tests.hs b/radicle-repl/test/spec/Radicle/Repl/Tests.hs
new file mode 100644
index 0000000..3ad896b
--- /dev/null
+++ b/radicle-repl/test/spec/Radicle/Repl/Tests.hs
@@ -0,0 +1,94 @@
+{-# LANGUAGE QuasiQuotes #-}
+module Radicle.Repl.Tests where
+
+import Protolude hiding (toList)
+
+import Codec.Serialise (Serialise, deserialise, serialise)
+import qualified Data.Map.Strict as Map
+import Data.Scientific (Scientific)
+import Data.Sequence (Seq(..))
+import Data.String.Interpolate (i)
+import Data.String.QQ (s)
+import qualified Data.Text as T
+import GHC.Exts (fromList, toList)
+import System.IO.Unsafe (unsafePerformIO)
+import System.Process (CmdSpec(..), StdStream(..))
+import Test.Tasty
+import Test.Tasty.HUnit
+import Test.Tasty.QuickCheck
+ (Arbitrary, counterexample, testProperty, (==>))
+import qualified Text.Megaparsec.Pos as Par
+
+import Radicle
+import qualified Radicle.Internal.Annotation as Ann
+import Radicle.Internal.Arbitrary ()
+import Radicle.Internal.Core (asValue, noStack)
+import Radicle.Repl.TestCapabilities
+import Radicle.TH
+
+test_repl_primops :: [TestTree]
+test_repl_primops =
+ [ testProperty "(read-annotated \"get-line!\" (get-line!)) returns the input line" $ \(v :: Value) ->
+ let prog = [i|(eq? (read-annotated \"get-line!\" (get-line!)) (quote #{renderPrettyDef v}))|]
+ -- We're not actually using IO
+ res = unsafePerformIO $ run [renderPrettyDef v] $ toS prog
+ in counterexample prog $ res == Right (Boolean True)
+
+ , testCase "catch catches read-line errors" $ do
+ let prog = [s|
+ (catch 'any (read-line!) (fn [x] "caught"))
+ |]
+ input = ["\"blah"]
+ res <- run input prog
+ res @?= Right (String "caught")
+ , testCase "read-file! can read a file" $ do
+ let prog = "(read-file! \"foobar.rad\")"
+ files = Map.singleton "foobar.rad" "foobar"
+ res <- runCodeWithFiles files prog
+ res @?= Right (String "foobar")
+ , testCase "load! can load definitions" $ do
+ let prog = [s|
+ (load! "foo.rad")
+ (+ foo bar)
+ |]
+ files = Map.singleton "foo.rad" "(def foo 42) (def bar 8)"
+ res <- runCodeWithFiles files prog
+ res @?= Right (int 50)
+ , testCase "load! ignores shebangs" $ do
+ let prog = [s|
+ (load! "foo.rad")
+ taxi
+ |]
+ files = Map.singleton "foo.rad" "#!/blah\n(def taxi 1797)"
+ res <- runCodeWithFiles files prog
+ res @?= Right (int 1797)
+ , testCase "generating and verifying cryptographic signatures works" $ do
+ let prog = [s|
+ (def my-keys (gen-key-pair! (default-ecc-curve)))
+ (def not-my-keys (gen-key-pair! (default-ecc-curve)))
+ (def sig (gen-signature! (lookup :private-key my-keys) "hello"))
+ (def tt (verify-signature (lookup :public-key my-keys) sig "hello"))
+ (def ff (verify-signature (lookup :public-key not-my-keys) sig "hello"))
+ (list tt ff)
+ |]
+ res <- run [] prog
+ res @?= Right (List [Boolean True, Boolean False])
+ , testCase "uuid! generates a valid uuid" $ do
+ let prog = [s| (uuid? (uuid!)) |]
+ res <- run [] prog
+ res @?= Right (Boolean True)
+
+ , testCase "'exit!' throws an Exit error with the provided code" $ do
+ let prog = [s|(exit! 5)|]
+ Left (LangError _ errorData) <- run [] prog
+ errorData @?= Exit 5
+ ]
+ where
+ run stdin' prog =
+ let ws = defaultWorldState { worldStateStdin = stdin' }
+ in fst <$> runCodeWithWorld ws prog
+
+-- Helper functions
+
+int :: Integer -> Value
+int = Number . fromIntegral