A peer-to-peer stack for code collaboration
Reorganize self testing in Radicle
Thomas Scholtescf8515401c55
diff --git a/docs/source/reference.rst b/docs/source/reference.rstindex deb0dbe..a9090d7 100644--- a/docs/source/reference.rst+++ b/docs/source/reference.rst@@ -90,7 +90,7 @@ evaluation and the new state.``(eval expr env)``~~~~~~~~~~~~~~~~~~~-The evaluation function.+Evaluation function that adds :test macro to register tests.``ref``~~~~~~~diff --git a/exe/ReferenceDoc.hs b/exe/ReferenceDoc.hsindex a6a7613..df272b4 100644--- a/exe/ReferenceDoc.hs+++ b/exe/ReferenceDoc.hs@@ -36,7 +36,7 @@ main = dointerpret"reference-doc"( "(do"- <> "(file-module! \"rad/prelude/test.rad\") (import prelude/test :unqualified)"+ <> "(file-module! \"rad/prelude/test-eval.rad\") (import prelude/test-eval '[eval tests] :unqualified)"<> foldMap (\m -> "(file-module! \"rad/" <> m <> ".rad\")") (modules content)<> "(get-current-env))")replBindings@@ -91,7 +91,7 @@ main = dovs -> panic $ "The following functions have no documentation strings: " <> T.intercalate ", " vscheckAllInReference content e =- let notDocumented = Map.keys e \\ (primFns content ++ modules content ++ ["prelude/test"]) in+ let notDocumented = Map.keys e \\ (primFns content ++ modules content ++ ["prelude/test-eval", "tests"]) inif null notDocumentedthen pure ()else panic $ "The following functions need to be added to the reference doc: " <> T.intercalate ", " notDocumenteddiff --git a/rad/monadic/issues.rad b/rad/monadic/issues.radindex c6a9e87..5bc6f2d 100644--- a/rad/monadic/issues.rad+++ b/rad/monadic/issues.rad@@ -132,7 +132,8 @@(:test"The monadic issues chain works."[:setup- (do (create-issues-chain!)+ (do (write-ref issues-chain (chain/new-chain "http://localhost:8024/chains/fxoo"))+ (create-issues-chain!)(simple-create-issue! "title0" "body0")(simple-create-issue! "title1" "body1")(simple-add-comment! 0 "comment0")diff --git a/rad/prelude.rad b/rad/prelude.radindex a6063ef..ecd0e07 100644--- a/rad/prelude.rad+++ b/rad/prelude.rad@@ -1,5 +1,5 @@-(file-module! "rad/prelude/test.rad")-(import prelude/test '[eval] :unqualified)+(file-module! "rad/prelude/test-eval.rad")+(import prelude/test-eval '[eval tests] :unqualified)(file-module! "rad/prelude/basic.rad")(file-module! "rad/prelude/patterns.rad")@@ -16,6 +16,7 @@(file-module! "rad/prelude/state-machine.rad")(file-module! "rad/prelude/validation.rad")(file-module! "rad/prelude/util.rad")+(file-module! "rad/prelude/test.rad")(import prelude/basic :unqualified)(import prelude/patterns :unqualified)diff --git a/rad/prelude/chain.rad b/rad/prelude/chain.radindex f1ca930..6349622 100644--- a/rad/prelude/chain.rad+++ b/rad/prelude/chain.rad@@ -213,8 +213,8 @@(def pure-prelude-code"Code for the pure prelude. Useful as the first inputs to a new chain."- (<> [(file-to-module "rad/prelude/test.rad")- '(import prelude/test :unqualified)]+ (<> [(file-to-module "rad/prelude/test-eval.rad")+ '(import prelude/test-eval '[eval tests] :unqualified)](map file-to-module pure-prelude-files)))(def send-prelude!diff --git a/rad/prelude/test-eval.rad b/rad/prelude/test-eval.radnew file mode 100644index 0000000..bb902ae--- /dev/null+++ b/rad/prelude/test-eval.rad@@ -0,0 +1,58 @@+{:module 'prelude/test-eval+ :doc+ "Provides eval that adds a `:test` macro. If an the eval encounters an+ expression of the form `(:test \"desc\" tests...)` we add it to the vector+ stored in the `tests` ref. The tests are then run by `rad/prelude/test.rad`."+ :exports '[eval tests]}++(def tests+ "Reference that collects tests defined with the `:test` macro. This+ value is updated by a custom `eval`."+ (ref []))++;; Annoyingly verbose since we have neither the prelude nor pattern-matching.+(def eval+ "Evaluation function that adds :test macro to register tests."+ (fn [expr env]+ (if (list? expr)+ (if (eq? (head expr) :test)+ (do+ (def bindings (lookup :env env))+ (def test-def {:tests (drop 2 expr) :name (nth 1 expr) :env env})+ (def next-tests {:val test-def})+ (def bindings_ (insert 'next-tests__ next-tests bindings))+ (def env (insert :env bindings_ env))+ (eval '(do+ (write-ref tests (add-right next-tests__ (read-ref tests)))+ :nil+ ) env))+ (eval expr env))+ (eval expr env))))++(:test "'test' works (including :setup)"+ [:setup+ (do+ (def x 3)+ (def y (ref 0))+ (write-ref y 1)+ ;; Test the mocked send!/receive!+ (def chain-name (uuid!))+ (send! chain-name [0])+ (def received (receive! chain-name 0)))+ ]+ [(+ 3 2) ==> 5]+ [x ==> 3]+ [(read-ref y) ==> 1]+ [received ==> (0)]+)++(:test "'test' handles exceptions properly"+ [:setup+ (do+ (def x (catch 'any (throw 'blah) (fn [x] #t)))+ (def y (catch 'any (throw 'blah) (fn [x] #f)))+ )+ ]+ [x ==> #t]+ [y ==> #f]+)diff --git a/rad/prelude/test.rad b/rad/prelude/test.radindex 8fa2240..ab4177f 100644--- a/rad/prelude/test.rad+++ b/rad/prelude/test.rad@@ -1,77 +1,51 @@{:module 'prelude/test- :doc "An eval used for testing."- :exports ['eval]}+ :doc "Provides eval that adds a `:test` macro."+ :exports '[run run-all]}-;; testing functions+(def run+ "Run the tests in `test-def` and print the result. `test-def` is a `{:env env+ :tests tests :name name}` dictionary where `env` is a radicle environment and+ `tests` is a list of quoted test cases with an optional `:setup` item.+ ```+ '(+ [:setup (do+ (def foo #t)+ )]+ [ foo ==> #t ]+ [ (not foo) ==> #f ]+ )+ ```+ "+ (fn [test-def]+ (def test-env (lookup :env test-def))+ (def test-name (lookup :name test-def))+ (def error+ (fn [msg]+ (put-str! (string-append "Test '" test-name "' failed: " msg))))+ ;; Check a single test in a given env+ (def check-single+ (fn [single ix senv]+ (def lhs (nth 0 single))+ (def marker (nth 1 single))+ (def rhs (nth 2 single))+ (if (eq? marker '==>)+ (do+ (def lhs-res (head (eval lhs senv)))+ (if (eq? lhs-res rhs)+ (put-str! (string-append "Test '" test-name " (" (show ix) ")' succeeded"))+ (error (string-append "failed: got " (show lhs-res) " but expected " (show rhs)))))+ (error "Expected and actual not separate by ==>"))+ (+ ix 1)))+ (def test-contents (lookup :tests test-def))+ (def maybe-setup (nth 0 test-contents))+ ;; If first block starts with :setup, run tests in that env+ (if (eq? (nth 0 maybe-setup) :setup)+ (do (def setup-env (nth 1 (eval (nth 1 maybe-setup) test-env)))+ (foldl (fn [ix x] (check-single x ix setup-env)) 1 (tail test-contents)))+ (foldl (fn [ix x] (check-single x ix test-env)) 1 test-contents))+))-(def is-test-env- "True iff file is being run as part of the Haskell suite"- (catch 'any (eq? test-env__ #t) (fn [x] #f)))--;; Annoyingly verbose since we have neither the prelude nor pattern-matching.-(def eval- "The evaluation function."- (fn [expr env]- (if (list? expr)- (if (> (foldr (fn [x acc] (+ acc 1)) 0 expr) 0)- (if (eq? (head expr) :test)- (if is-test-env- (do- (def test-name (nth 1 expr))- (def error- (fn [msg]- (put-str! (string-append "Test '" test-name "' failed: " msg))))- ;; Check a single test in a given env- (def check-single- (fn [single ix senv]- (def lhs (nth 0 single))- (def marker (nth 1 single))- (def rhs (nth 2 single))- (if (eq? marker '==>)- (do- (def lhs-res (head (eval lhs senv)))- (if (eq? lhs-res rhs)- (put-str! (string-append "Test '" test-name " (" (show ix) ")' succeeded"))- (error (string-append "failed: got " (show lhs-res) " but expected " (show rhs)))))- (error "Expected and actual not separate by ==>"))- (+ ix 1)))- (def test-contents (drop 2 expr))- (def maybe-setup (nth 0 test-contents))- ;; If first block starts with :setup, run tests in that env- (if (eq? (nth 0 maybe-setup) :setup)- (do (def setup-env (nth 1 (eval (nth 1 maybe-setup) env)))- (foldl (fn [ix x] (check-single x ix setup-env)) 1 (tail test-contents)))- (foldl (fn [ix x] (check-single x ix env)) 1 test-contents))- (list :ok env))- (list :ok env))- (eval expr env))- (eval expr env))- (eval expr env))))--(:test "'test' works (including :setup)"- [:setup- (do- (def x 3)- (def y (ref 0))- (write-ref y 1)- ;; Test the mocked send!/receive!- (def chain-name (uuid!))- (send! chain-name [0])- (def received (receive! chain-name 0)))- ]- [(+ 3 2) ==> 5]- [x ==> 3]- [(read-ref y) ==> 1]- [received ==> (0)]-)--(:test "'test' handles exceptions properly"- [:setup- (do- (def x (catch 'any (throw 'blah) (fn [x] #t)))- (def y (catch 'any (throw 'blah) (fn [x] #f)))- )- ]- [x ==> #t]- [y ==> #f]-)+(def run-all+ "Run all `tests`."+ (fn [tests]+ (map (fn [t] (run t)) tests)))diff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hsindex b613732..7c1f417 100644--- a/test/spec/Radicle/Internal/TestCapabilities.hs+++ b/test/spec/Radicle/Internal/TestCapabilities.hs@@ -2,7 +2,7 @@-- Radicle.Internal.Subscriber.Capabilities that may be used for testing.module Radicle.Internal.TestCapabilities (runCode- , runCode'+ , runCodeWithWorld, runPureCode, runCodeWithInput, runCodeWithFiles@@ -63,7 +63,7 @@ type TestLang = Lang (StateT WorldState IO)-- | Run Radicle code in a REPL environment.runCode :: Text -> IO (Either (LangError Value) Value)-runCode code = fst <$> runCode' defaultWorldState (pure ()) code+runCode code = fst <$> runCodeWithWorld defaultWorldState code-- | Run Radicle code in a REPL environment, with all library files-- readable, and with the provided stdin lines.@@ -73,14 +73,14 @@ runCodeWithInput :: [Text] -> Text -> IO (Either (LangError Value) Value, [Text]runCodeWithInput input code = dows' <- worldStateWithSourcelet ws = ws' { worldStateStdin = input }- runCode' ws (pure ()) code+ runCodeWithWorld ws code-- | Run Radicle code in a REPL environment with the given files-- readable.runCodeWithFiles :: Map Text Text -> Text -> IO (Either (LangError Value) Value)runCodeWithFiles files code =let ws = defaultWorldState { worldStateFiles = files }- in fst <$> runCode' ws (pure ()) code+ in fst <$> runCodeWithWorld ws code-- | Run radicle code in a pure environment.runPureCode :: Text -> Either (LangError Value) Value@@ -88,9 +88,9 @@ runPureCode code =let prog = interpretMany "[test]" codein evalState (fst <$> runLang pureEnv prog) defaultWorldState-runCode' :: WorldState -> TestLang () -> Text -> IO (Either (LangError Value) Value, [Text])-runCode' ws init code = do- let prog = init >> interpretMany "[test]" code+runCodeWithWorld :: WorldState -> Text -> IO (Either (LangError Value) Value, [Text])+runCodeWithWorld ws code = do+ let prog = interpretMany "[test]" code(result, ws') <- runStateT (fst <$> runLang testBindings prog) wspure $ (result, worldStateStdout ws')diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex 18fea92..dfe4fae 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -22,7 +22,7 @@ import qualified Text.Megaparsec.Pos as Parimport Radicleimport qualified Radicle.Internal.Annotation as Annimport Radicle.Internal.Arbitrary ()-import Radicle.Internal.Core (addBinding, asValue, noStack)+import Radicle.Internal.Core (asValue, noStack)import Radicle.Internal.Foo (Foo)import Radicle.Internal.TestCapabilities@@ -687,7 +687,7 @@ test_repl_primops =whererun stdin' prog =let ws = defaultWorldState { worldStateStdin = stdin' }- in fst <$> runCode' ws (pure ()) prog+ in fst <$> runCodeWithWorld ws progtest_repl :: [TestTree]test_repl =@@ -806,21 +806,13 @@ test_cbor =counterexample info $ got == expected--- Tests radicle files. These should use the ':test' macro to ensure--- they are in the proper format.--- Note that loaded files will also be tested, so there's no need to test files--- loaded by the prelude individually.+-- | Call @(run-all-tests)@ which runs all tests defined in the+-- Radicle source with the @:test@ macro.test_source_files :: IO TestTreetest_source_files = dotests <- join <$> traverse testOne ["rad/prelude.rad", "rad/monadic/issues.rad"]pure $ testGroup "Radicle source file tests" testswhere- -- If the @test-env__@ is defined and true in the radicle- -- environment then the @:test@ macro will execute some code.- -- enableSelfTest :: Bindings a -> Bindings a- enableSelfTest :: Monad m => Lang m ()- enableSelfTest = modify $ addBinding (unsafeToIdent "test-env__") Nothing (Boolean True)-testOne :: FilePath -> IO [TestTree]testOne file = dows' <- worldStateWithSource@@ -828,7 +820,10 @@ test_source_files = doRight kp -> pure $ renderCompactPretty kpLeft _ -> panic "Couldn't generate keypair file."let ws = addFileToWorld "my-keys.rad" keyPair ws'- (r, out) <- runCode' ws enableSelfTest $ toS $ "(load! \"" <> file <> "\")"+ let code = "(load! \"" <> file <> "\")"+ <> "(import prelude/test)"+ <> "(prelude/test/run-all (read-ref tests))"+ (r, out) <- runCodeWithWorld ws $ toS codelet doesntThrow = if isRight rthen pure ()else assertFailure $ "Expected Right, got: " <> toS (prettyEither r)