A peer-to-peer stack for code collaboration
Issues for dogfooding
Julian Arniec184a044473
diff --git a/exe/API.hs b/exe/API.hsindex 90da23c..f44dba2 100644--- a/exe/API.hs+++ b/exe/API.hs@@ -3,8 +3,10 @@ module API whereimport qualified Data.ByteString.Lazy as LBSimport qualified Data.Text as T+import GHC.Exts (IsList(..))import Protoludeimport Radicle+import Radicle.Internal.Annotation (thisPos)import Servant.APIinstance MimeRender PlainText Value where@@ -15,9 +17,32 @@ instance MimeUnrender PlainText Value wherelet src = decodeUtf8 $ LBS.toStrict xin first T.unpack $ parse "[request body]" src+newtype Values = Values { getValues :: [Value] }+ deriving (Eq, Show, Ord)++instance IsList Values where+ type Item Values = Value+ fromList = Values+ toList = getValues++instance MimeRender PlainText Values where+ mimeRender _ (Values xs)+ = LBS.intercalate "\n" $ mimeRender p <$> xs+ where+ p = Proxy :: Proxy PlainText++instance MimeUnrender PlainText Values where+ mimeUnrender _ x =+ let src = decodeUtf8 $ LBS.toStrict x+ in case parseValues "[request body]" src of+ Right vs -> Right $ Values vs+ Left err -> Left . toS $ renderPrettyDef err'+ where err' :: LangError Value+ err' = LangError [thisPos] (ParseError err)+-- | Endpoint to submit an expression to a chain. Responds with @:ok@ if the-- expression has been submitted sucessfully.-type ChainSubmitEndpoint = "submit" :> ReqBody '[PlainText] Value :> Post '[PlainText] Value+type ChainSubmitEndpoint = "submit" :> ReqBody '[PlainText] Values :> Post '[PlainText] ValuechainSubmitEndpoint :: Proxy ChainSubmitEndpointchainSubmitEndpoint = Proxydiff --git a/exe/RadicleExe.hs b/exe/RadicleExe.hsindex 9242a8b..9f064cb 100644--- a/exe/RadicleExe.hs+++ b/exe/RadicleExe.hs@@ -86,13 +86,14 @@ clientPrimFns mgr = fromList . PrimFns.allDocs $ [sendPrimop, receivePrimop], [md|Given a URL (string) and a value, sends the value `v` to the remotechain located at the URL for evaluation.|], \case- [String url, v] -> do- res <- liftIO $ runClientM' url mgr (submit v)+ [String url, Vec v] -> do+ res <- liftIO $ runClientM' url mgr (submit $ toList v)case res ofLeft e -> throwErrorHere . OtherError$ "send!: failed:" <> show eRight r -> pure r- [_, _] -> throwErrorHere $ TypeError "send!: first argument should be a string"+ [_, Vec _] -> throwErrorHere $ TypeError "send!: first argument should be a string"+ [String _, _] -> throwErrorHere $ TypeError "send!: second argument should be a vector"xs -> throwErrorHere $ WrongNumberOfArgs "send!" 2 (length xs))receivePrimop =@@ -116,8 +117,8 @@ clientPrimFns mgr = fromList . PrimFns.allDocs $ [sendPrimop, receivePrimop]-- * Client functions-submit :: Value -> ClientM Value-submit = client chainSubmitEndpoint+submit :: [Value] -> ClientM Value+submit = client chainSubmitEndpoint . Valuessince :: Int -> ClientM Valuesince = client chainSinceEndpointdiff --git a/exe/ReferenceDoc.hs b/exe/ReferenceDoc.hsindex 4fac624..c75dbf5 100644--- a/exe/ReferenceDoc.hs+++ b/exe/ReferenceDoc.hs@@ -69,33 +69,35 @@ main = do"Tools for creating and verifying cryptographic signatures, and generating private/public key pairs.", sec "Chain tools" chainTools[md|These functions can be used to simulate remote chains in the local REPL.- This is useful for experimenting with inputs or even new evaluation functions"+ This is useful for experimenting with inputs or even new evaluation functionsbefore sending these to a remote chain.|]+ , sec "Issue chain" issueChain+ [md|These functions allow creating and interacting with the default issues chain.|]]basics =[ "eq?", "not", "and", "or", "all", "some", "show", "string-append", "string-length", "apply", "type", "atom?", "boolean?", "string?", "number?", "keyword?", "list?"- , "dict?", "read", "throw", "Y", "Y2", "to-json", "uuid?", "make-counter", "markdown?"+ , "dict?", "read", "read-many", "throw", "Y", "Y2", "to-json", "uuid?", "make-counter", "markdown?", "public-key?" ]maths = ["+", "*", "-", "<", ">", "integral?"]evalFns = ["base-eval", "eval", "updatable-eval"]envStuff = ["pure-env", "get-current-env", "set-current-env", "set-env!"]- seqs = ["nth", "foldl", "foldr", "map", "drop", "seq"]+ seqs = ["nth", "foldl", "foldr", "map", "seq", "take", "drop"]lists =[ "list", "nil", "head", "tail", "empty?", "cons", "reverse", "length", "concat", "filter", "range", "list-with-head" ]vecs = ["<>", "add-left", "add-right"]dicts =- ["dict", "lookup", "insert", "delete", "dict-from-list", "keys", "rekey"+ ["dict", "lookup", "insert", "delete", "dict-from-list", "keys", "values", "rekey", "map-values", "modify-map"]structs = ["member?"]refs = ["ref", "read-ref", "write-ref", "modify-ref"]- docs = ["doc", "doc!", "apropos!", "document", "should-be"]+ docs = ["doc", "doc!", "apropos!", "document", "is-test-env"]io =["print!", "get-line!", "load!", "read-file!", "read-code!", "send-code!", "send-prelude!", "subscribe-to!", "uuid!", "read-line!", "exit!"]- lens = ["@", "make-lens", "view", "view-ref", "set", "set-ref", "over", "over-ref", "id-lens", "..", "..."]+ lens = ["@", "@nth", "make-lens", "view", "view-ref", "set", "set-ref", "over", "over-ref", "id-lens", "..", "..."]validation =[ "validator/=", "validator/member", "validator/type", "validator/pred", "validator/every", "validator/and", "validator/or", "validator/key", "validator/keys"]@@ -103,7 +105,10 @@ main = dochainTools =[ "new-chain", "eval-in-chain", "enter-remote-chain", "update-chain", "add-quit", "add-send", "load-chain", "pure-prelude-files", "pure-prelude-code", "store-exprs", "eval-fn-app"- , "state-machine-eval", "state-machine-input", "state-machine-new-trans", "state-machine-agree", "state-machine-disagree", "simple-trans"]+ , "state-machine-eval", "state-machine-input", "state-machine-new-trans"+ , "state-machine-agree", "state-machine-disagree", "simple-trans", "update-chain-ref"]++ issueChain = ["create-issues-chain!", "list-issues", "new-issue!"]typ s = "Functions for manipulating " <> s <> "."@@ -113,7 +118,7 @@ main = doallFns =basics ++ maths ++ evalFns ++ envStuff ++ seqs ++ lists ++ vecs++ dicts ++ structs ++ refs ++ docs ++ io ++ lens ++ validation ++ crypto ++ chainTools- ++ doNotInclude+ ++ issueChain ++ doNotInclude-- Function symbol followed by a hard line break, followed by it's doc.funs e fns = mconcatdiff --git a/exe/Server.hs b/exe/Server.hsindex 800e271..7bacbf1 100644--- a/exe/Server.hs+++ b/exe/Server.hs@@ -76,17 +76,20 @@ serverApi = Proxy-- * Helpers-insertExpr :: Connection -> Chains -> Text -> Value -> IO (Either Text ())-insertExpr conn chains name val = modifyMVar (getChains chains) $ \c -> do+insertExpr :: Connection -> Chains -> Text -> [Value] -> IO (Either Text ())+insertExpr conn chains name vals = modifyMVar (getChains chains) $ \c -> dolet x = Map.lookup name clet chain = fromMaybe (Chain name pureEnv mempty) x- let (r, newSt) = runIdentity $ runLang (chainState chain) (eval val)+ let (r :: Either (LangError Value) [Value], newSt) = runIdentity+ $ runLang (chainState chain)+ $ traverse eval valscase r ofLeft e -> pure . (c ,) . Left $ "invalid expression: " <> show e- Right valRes -> do- insertExprDB conn name val+ Right valsRes -> do+ traverse_ (insertExprDB conn name) vals+ let news = Seq.fromList $ zip vals valsReslet chain' = chain { chainState = newSt- , chainEvalPairs = chainEvalPairs chain Seq.|> (val, valRes)+ , chainEvalPairs = chainEvalPairs chain Seq.>< news}pure (Map.insert name chain' c, Right ())@@ -116,10 +119,12 @@ loadState conn = do-- | Submit something to a chain. The value is expected to be a pair, with the-- first item specifying the chain the value is being submitted to, and the--- second the expression being submitted.-submit :: Connection -> Chains -> Text -> Value -> Handler Value-submit conn chains name val = do- res <- liftIO $ insertExpr conn chains name val+-- second the expressions being submitted. The server either accepts all or+-- rejects all expressions (though other systems may not provide this+-- guarantee).+submit :: Connection -> Chains -> Text -> Values -> Handler Value+submit conn chains name (Values vals) = do+ res <- liftIO $ insertExpr conn chains name valscase res ofLeft err -> throwError $ err400 { errBody = fromStrict $ encodeUtf8 err }Right _ -> pure $ Keyword $ unsafeToIdent "ok"diff --git a/exe/Server/DB.hs b/exe/Server/DB.hsindex 3062f69..8c43e6e 100644--- a/exe/Server/DB.hs+++ b/exe/Server/DB.hs@@ -20,13 +20,16 @@ insertExprDB conn name valgetSinceDB :: Connection -> Text -> Int -> IO [Value]getSinceDB conn name index- = query conn "SELECT expr FROM txs WHERE chain = ? AND id >= ?" (name, index)+ = query conn "SELECT expr FROM txs WHERE chain = ? AND id >= ? ORDER BY id ASC" (name, index)-- | Get all txs in all chains. Useful after a server restart.getAllDB :: Connection -> IO [(Text, [Value])]getAllDB conn = do- res <- query_ conn "SELECT chain, expr FROM txs"- let grouped = groupBy (\l r -> fst l == fst r) res+ res :: [(Int, Text, Value)] <- query_ conn "SELECT id, chain, expr FROM txs"+ let grouped :: [[(Text, Value)]]+ grouped+ = fmap (\(_, a, b) -> (a, b)) . sortOn (\(a,_,_) -> a)+ <$> groupBy (\(_, l, _) (_, r, _) -> l == r) respure [ (fst (head each), snd <$> each) | each <- grouped ]diff --git a/nix/shell.nix b/nix/shell.nixindex 92e1bf3..823235c 100644--- a/nix/shell.nix+++ b/nix/shell.nix@@ -25,6 +25,8 @@ stdenv.mkDerivation {alias sbs="stack build --fast --system-ghc --nix-packages zlib radicle:exe:radicle-server"alias sbc="stack build --fast --system-ghc --nix-packages zlib radicle:exe:radicle-client"alias st="stack test --fast --system-ghc --nix-packages zlib"+ alias sts="stack test --fast --system-ghc --nix-packages zlib radicle:spec"+ alias str="stack test --fast --system-ghc --nix-packages zlib radicle:spec --ta '--pattern \"Radicle source file tests\"'"alias server="stack exec radicle-server -- "alias client="stack exec radicle -- rad/repl.rad"'';diff --git a/package.yaml b/package.yamlindex 9c41495..78fe690 100644--- a/package.yaml+++ b/package.yaml@@ -13,12 +13,16 @@ dependencies:- protolude- aeson- containers+- cryptonite+- data-default- deriving-compat- directory- filepath+- generics-eot- haskeline- megaparsec- mtl+- pandoc- pointed- prettyprinter- prettyprinter-ansi-terminal@@ -28,11 +32,7 @@ dependencies:- template-haskell- text- unordered-containers-- cryptonite-- generics-eot- uuid-- data-default-- pandocwhen:- condition: impl(ghcjs)dependencies: ghcjs-dom@@ -84,12 +84,12 @@ executables:other-modules: APIdependencies:- radicle+ - bytestring- exceptions+ - http-client+ - http-media- optparse-applicative- - bytestring- servant- - http-media- - http-client- servant-clientwhen:- condition: impl(ghcjs)diff --git a/rad/chain.rad b/rad/chain.radindex e92348b..88aa3df 100644--- a/rad/chain.rad+++ b/rad/chain.rad@@ -8,7 +8,7 @@(def new-chain(fn [url]{:state (pure-env)- :inputs (list)+ :inputs []:url url:index 0}))@@ -16,19 +16,23 @@'(("url" string))"Return an empty chain dictionary with the given url.")-;; eval-in-chain(def eval-in-chain(fn [expr chain]- (def x (eval expr (lookup :state chain)))+ (def chain-eval (view (... [(@ :state) (@ :env) (@ 'eval) (@nth 1)]) chain))+ (def x (chain-eval expr (lookup :state chain)))(def result (nth 0 x))(def new-state (nth 1 x))(def new-chain- (insert :state new-state- (over (@ :input) (fn [input] (cons expr input))- (over (@ :index) (fn [index] (+ 1 index))- chain ))))+ (insert+ :state+ new-state+ (over (@ :inputs)+ (fn [inputs] (add-right expr inputs))+ (over (@ :index)+ (fn [index] (+ 1 index))+ chain)))){:chain new-chain:result result }))@@ -36,17 +40,21 @@'(("expr" any) ("chain" chain))"Evaluates 'expr' in the 'chain' and returns a dict with the ':result' and the resulting ':chain'.")-((fn []- (def empty-chain (new-chain ""))- (def res (eval-in-chain '(+ 3 2) empty-chain))- (def ~~> (fn [comp expected] (should-be "eval-in-chain" (view comp res) expected)))- (~~> (@ :result) 5)- (~~> (.. (@ :chain) (@ :input)) '((+ 3 2)))- (~~> (.. (@ :chain) (@ :index)) 1)))+(:test "eval-in-chain"+ [:setup+ (do (def empty-chain (new-chain ""))+ (def res1 (eval-in-chain '(def x 3) empty-chain))+ (def res (eval-in-chain '(+ x 2) (lookup :chain res1))))+ ]+ [ (view (@ :result) res) ==> 5 ]+ [ (view (.. (@ :chain) (@ :inputs)) res) ==> [(def x 3) (+ x 2)] ]+ [ (view (.. (@ :chain) (@ :index)) res) ==> 2 ]+);; update-chain(def update-chain+ "Takes a chain, and returns a new chain updated with the new expressions from the remote chain"(fn [chain](def new-inputs(receive! (lookup :url chain) (lookup :index chain)))@@ -56,17 +64,45 @@(view (@ :chain) x)))(foldl upd-ch chain new-inputs)))-(document 'update-chain- '(("chain" chain))- "Return a new chain updated with the new expressions from the remote chain")+(:test "update-chain"+ [:setup+ (do+ (def chain-name (uuid!))+ (def chain (new-chain chain-name))+ (send! chain-name ['(+ 3 2)])+ (send! chain-name ['(+ 3 3)])+ (def chain (update-chain chain)))+ ]+ [ (lookup :inputs chain) ==> [(+ 3 2) (+ 3 3)]]+)(def load-chain+ "Takes a `url`, and fetches the inputs of a remote chain and return a chain dictionary with the chain state."(fn [url](update-chain (new-chain url))))-(document 'load-chain- '(("url" string))- "Fetch the inputs of a remote chain and return a chain dictionary with the chain state.")+;; update-chain-ref++(def update-chain-ref+ (fn [chain-ref]+ (modify-ref chain-ref update-chain)+ :ok))++(document 'update-chain-ref+ '(("chain" chain))+ "Update a ref containing a chain with the new expressions from the remote chain")++(:test "update-chain-ref"+ [:setup+ (do+ (def chain-name (uuid!))+ (def chain (ref (new-chain chain-name)))+ (send! chain-name ['(+ 3 2)])+ (send! chain-name ['(+ 3 3)])+ (update-chain-ref chain))+ ]+ [ (lookup :inputs (read-ref chain)) ==> [(+ 3 2) (+ 3 3)]]+);; add-quit@@ -86,7 +122,7 @@(def store-exprs(fn [evalfn](fn [expr env]- (def env_ (over (.. (@ :env) (@ '_inputs)) (fn [x] (add-right expr x)) env))+ (def env_ (over (... [(@ :env) (@ '_inputs) (@nth 1)]) (fn [x] (add-right expr x)) env))(evalfn expr env_ ))))(document 'store-exprs@@ -98,9 +134,9 @@(fn [oeval](fn [expr env](if (eq? expr :send)- (do (def inpl (.. (@ :env) (@ '_inputs)))- (def chainl (.. (@ :env) (@ '_cur-chain)))- (map (fn [e] (send! (view chainl env) e)) (view inpl env))+ (do (def inpl (... [(@ :env) (@ '_inputs) (@nth 1)]))+ (def chainl (... [(@ :env) (@ '_cur-chain) (@nth 1)]))+ (send! (view chainl env) map (view inpl env))(def env_ (set inpl [] env))(list :ok env_))((store-exprs oeval) expr env)))))@@ -111,19 +147,19 @@;; enter-remote-chain(def enter-remote-chain- "Make the eval behave as that of a remote chain."- (fn [url]+ "Make the eval behave as that of a remote chain. The second param is the env to return to after :quit."+ (fn [url env](def chain (load-chain url))(def chain-state (lookup :state chain))(def mod-state1- (over (@ :env) (fn [x] (insert '_inputs [] x)) chain-state))+ (over (@ :env) (fn [x] (insert '_inputs [:Nothing []] x)) chain-state))(def mod-state2(over (@ :env)- (fn [x] (insert '_cur-chain (lookup :url chain) x))+ (fn [x] (insert '_cur-chain [:Nothing (lookup :url chain)] x))mod-state1))(def mod-state3- (over (.. (@ :env) (@ 'eval))- (fn [x] (add-send (add-quit (get-current-env) x)))+ (over (... [(@ :env) (@ 'eval) (@nth 1)])+ (fn [x] (add-send (add-quit env x)))mod-state2))(list :ok mod-state3)))@@ -137,7 +173,7 @@(if (list? expr)(cond(eq? (head expr) :enter-chain)- (enter-remote-chain (head (eval__ (nth 1 expr) env)))+ (enter-remote-chain (head (eval__ (nth 1 expr) env)) env):else (eval__ expr env))(eval__ expr env))))diff --git a/rad/chains/issue-remote.rad b/rad/chains/issue-remote.radnew file mode 100644index 0000000..25d9559--- /dev/null+++ b/rad/chains/issue-remote.rad@@ -0,0 +1,102 @@+;; Issue chain+;; Meant to be sent to a chain with send-code!++(def issues (ref {}))++(def mod-issues+ "A helper to apply a function to `issues`"+ (fn [f] (modify-ref issues f)))++(def id-valid-and-free?+ "Checks that its argument is a `uuid` that hasn't yet been used."+ (fn [id]+ (and (uuid? id)+ (not (member? id (read-ref issues))))))++(def issue-valid?+ "Verifies the signature of an issue."+ (fn [i]+ (verify-signature (lookup :author i)+ (lookup :signature i)+ (string-append chain-id+ (lookup :id i)+ (lookup :title i)+ (lookup :body i)))))++(def new-issue+ "Create an issue from a dictionary, storing it in `issues`."+ (fn [i]+ (def id (lookup :id i))+ (if (id-valid-and-free? id)+ (mod-issues+ (fn [is] (insert id i is)))+ (throw 'bad-command "Issue ID was not free or was invalid."))))++(def close-issue+ "Remove an issue from `issues`."+ (fn [id]+ (mod-issues (fn [is] (delete id is)))))++(def add-comment+ "Add a comment to an issue."+ (fn [issue-id comment]+ (mod-issues+ (fn [is]+ (modify-map issue-id+ (fn [i]+ (modify-map :comments+ (fn [cs] (add-right comment cs))+ i))+ is)))))++;; These are intended for clients to run locally with eval-in-chain++(def list-issues+ "List existing issues."+ (fn [] (read-ref issues)))++;; General logic++(def commands+ "The set of allowed chain commands."+ (ref+ {'create-issue new-issue+ 'close-issue close-issue+ 'list-issues list-issues}))++(def process-command+ "Function that maps post-eval commands to existing functions"+ (fn [expr]+ (def command (head expr))+ (def args (tail expr))+ (def do-this (lookup command (read-ref commands)))+ (apply do-this args)))++;; Move to eval-fn-app & updatable-eval from chain.rad to a pure file so it may+;; be imported++(def eval-fn-app+ "Given a state, a function, an argument and a callback, returns+ the result of evaluating the function call on the arg in the given+ state, while also calling the callback on the result."+ (fn [state f arg cb]+ (def exec (base-eval (list f (list 'quote arg)) state))+ (cb (head exec))+ exec))++(def updatable-eval+ "Given an evaluation function `f`, returns a new one which augments `f` with a new+ command `(update expr)` which evaluates arbitrary expression using `base-eval`."+ (fn [sub-eval]+ (fn [expr state]+ (list-with-head+ expr+ (fn [cmd]+ (if (eq? cmd 'update)+ (base-eval (nth 1 expr) state)+ (sub-eval expr state)))+ (fn [] (sub-eval expr state))))))++(def eval (updatable-eval+ (fn [expr state]+ (eval-fn-app state 'process-command expr (fn [x] x)))))diff --git a/rad/chains/issues.rad b/rad/chains/issues.radnew file mode 100644index 0000000..d7bcfca--- /dev/null+++ b/rad/chains/issues.rad@@ -0,0 +1,52 @@+;; Helpers for setting up and interacting with issues chain (as defined in+;; rad/issues-chain.rad)++;; Create a remote issue chain. Returns a ref for the chain that you+;; can assign to a variable.++(def create-issues-chain!+ "Create a remote issue chain with the given url. Returns a ref with the chain."+ (fn [chain-name]+ (send-prelude! chain-name)+ (send-code! chain-name "rad/chains/issue-remote.rad")+ (ref (new-chain chain-name))))++(def new-issue!+ "Create a new remote issue. Takes a chain ref, a keypair, an issue title and an issue body."+ (fn [chain-ref keys title body]+ (def chain (read-ref chain-ref))+ (def id (uuid!))+ (def msg+ (string-append (lookup :url chain)+ id+ title+ body))+ (def arg+ {:id id+ :author (lookup :public-key keys)+ :title title+ :body body+ :signature (gen-signature! (lookup :private-key keys) msg)})+ (send! (lookup :url chain)+ [(list 'create-issue arg)])))++(def list-issues+ "Given an issues ref `x`, returns its issues. Does not itself update the chain."+ (fn [chain-ref]+ (lookup :result (eval-in-chain '(list-issues) (read-ref chain-ref)))))++(:test+ "creating, manipulating and querying issue chains"+ [:setup+ (do+ (def pk (gen-key-pair! (default-ecc-curve)))+ (def chain-name (uuid!))+ (def chain-ref (create-issues-chain! chain-name))+ (new-issue! chain-ref pk "Ah!" "if thou issueless shall hap to die")+ (update-chain-ref chain-ref)+ (def issues (values (list-issues chain-ref))))+ ]+ [ (length issues) ==> 1 ]+ [ (lookup :title (nth 0 issues)) ==> "Ah!" ]+ [ (lookup :body (nth 0 issues)) ==> "if thou issueless shall hap to die" ]+)diff --git a/rad/issues.rad b/rad/issues.raddeleted file mode 100644index 4cc98f5..0000000--- a/rad/issues.rad+++ /dev/null@@ -1,45 +0,0 @@-;; Issues for rad prelude--(def create-rad-issue-chain (fn []- (def issues (ref nil))- (def should-be (fn [name x y] (eq? x y)))- (def current-prelude (ref (dict :env (dict 'should-be should-be 'eval eval) :refs nil)))--- (def failing? (fn [expr]- (def res (catch 'any- (head (eval-with-env expr (read-ref current-prelude)))- (fn [x] #f)))- (eq? res #f)))-- (def new-issue (fn [creator title desc failing-test]- (if (failing? failing-test)- (modify-ref- issues- (fn [x]- (cons (dict- :creator creator- :title title- :description desc- :failing-test failing-test) x)))- "Test is not failing")))-- (def list-issues (fn [] (read-ref issues)))-- (def dispatch (fn [m]- (cond- (eq? m 'new-issue) new-issue- (eq? m 'list-issues) list-issues- :else "error: unknown message")))-- dispatch))--(def rad-issue-chain (create-rad-issue-chain))--(def rad-issue-chain-eval (fn [expr]- (if (eq? (head expr) 'new-issue)- (apply (rad-issue-chain 'new-issue) (tail expr))- (if (eq? (head expr) 'list-issues)- ((rad-issue-chain 'list-issues))- "must be one of: 'new-issue', 'list-issues'"))- ))diff --git a/rad/prelude.rad b/rad/prelude.radindex 0cd58ab..ed5a6ba 100644--- a/rad/prelude.rad+++ b/rad/prelude.rad@@ -1,4 +1,4 @@-(load! "rad/prelude/should-be.rad")+(load! "rad/prelude/test.rad")(load! "rad/prelude/io.rad")(load! "rad/prelude/bool.rad")(load! "rad/prelude/recursion.rad")@@ -6,13 +6,13 @@(load! "rad/prelude/dict.rad")(load! "rad/prelude/ref.rad")(load! "rad/prelude/lens.rad")-(load! "rad/prelude/exception.rad")(load! "rad/chain.rad")(load! "rad/state-machine.rad")(load! "rad/prelude/validation.rad")(def pure-prelude-files(list+ "rad/prelude/test.rad" ;; pure unless test env"rad/prelude/pure-should-be.rad""rad/prelude/recursion.rad""rad/prelude/bool.rad"@@ -20,7 +20,6 @@"rad/prelude/dict.rad""rad/prelude/ref.rad""rad/prelude/lens.rad"- "rad/prelude/exception.rad""rad/state-machine.rad""rad/prelude/validation.rad"))@@ -30,12 +29,9 @@"List of files which together define the pure prelude.")(def pure-prelude-code- (read- (string-append- "(do "+ (read-many(apply string-append- (map (fn [f] (read-file! f)) pure-prelude-files))- ")")))+ (map (fn [f] (read-file! f)) pure-prelude-files))))(document 'pure-prelude-code'()@@ -57,3 +53,5 @@(def i (ref -1)){:next-will-be (fn [] (+ 1 (read-ref i))):next (fn [] (modify-ref i (fn [x] (+ x 1))))}))++(load! "rad/chains/issues.rad") ;; send-prelude! must be defined alreadydiff --git a/rad/prelude/bool.rad b/rad/prelude/bool.radindex 0bb0a65..86df94f 100644--- a/rad/prelude/bool.rad+++ b/rad/prelude/bool.rad@@ -19,10 +19,12 @@'(("arg1" any) ("arg2" any))"Returns 'arg2' if 'arg1' is not #f, otherwise returns 'arg1'")-(should-be "and" (and #t #t) #t)-(should-be "and" (and #t #f) #f)-(should-be "and" (and #f #t) #f)-(should-be "and" (and #f #f) #f)+(:test "and"+ [ (and #t #t) ==> #t ]+ [ (and #t #f) ==> #f ]+ [ (and #f #t) ==> #f ]+ [ (and #f #f) ==> #f ]+);; or@@ -34,10 +36,12 @@'(("arg1" any) ("arg2" any))"Returns 'arg1' if 'arg1' is not #f, otherwise returns 'arg2'")-(should-be "or" (or #t #t) #t)-(should-be "or" (or #t #f) #t)-(should-be "or" (or #f #t) #t)-(should-be "or" (or #f #f) #f)+(:test "or"+ [ (or #t #t) ==> #t ]+ [ (or #t #f) ==> #t ]+ [ (or #f #t) ==> #t ]+ [ (or #f #f) ==> #f ]+);; all@@ -45,11 +49,13 @@"Checks that all the items of a list are truthy."(fn [xs] (foldr and #t xs)))-(should-be "all" (all '()) #t)-(should-be "all" (all '(#t)) #t)-(should-be "all" (all '(#f)) #f)-(should-be "all" (all '(#t #t #t #t)) #t)-(should-be "all" (all '(#t #t #f #t)) #f)+(:test "all"+ [ (all '()) ==> #t ]+ [ (all '(#t)) ==> #t ]+ [ (all '(#f)) ==> #f ]+ [ (all '(#t #t #t #t)) ==> #t ]+ [ (all '(#t #t #f #t)) ==> #f ]+);; some@@ -57,9 +63,11 @@"Checks that there is a least one truthy value in a list."(fn [xs] (foldr or #f xs)))-(should-be "some" (some '()) #f)-(should-be "some" (some '(#t)) #t)-(should-be "some" (some '(#f)) #f)-(should-be "some" (some '(#f #f #f)) #f)-(should-be "some" (some '(#t #t #t #t)) #t)-(should-be "some" (some '(#t #t #f #t)) #t)+(:test "some"+ [ (some '()) ==> #f ]+ [ (some '(#t)) ==> #t ]+ [ (some '(#f)) ==> #f ]+ [ (some '(#f #f #f)) ==> #f ]+ [ (some '(#t #t #t #t)) ==> #t ]+ [ (some '(#t #t #f #t)) ==> #t ]+)diff --git a/rad/prelude/dict.rad b/rad/prelude/dict.radindex 05db920..e0fc3cd 100644--- a/rad/prelude/dict.rad+++ b/rad/prelude/dict.rad@@ -10,38 +10,41 @@'(("list" list))"Creates a dictionary from a list of key-value pairs.")-(should-be "dict-from-list"- (dict-from-list (list (list :one 1) (list :two 2)))- (dict :one 1 :two 2))+(:test "dict-from-list"+ [ (dict-from-list (list (list :one 1) (list :two 2)))+ ==> { :one 1 :two 2 } ]);; keys(def keys+ "Given a `dict`, returns a vector of its keys."(fn [d](map head (seq d))))-(document 'keys- '(("dict" dict))- "Returns the list of keys in a dictionary.")+(:test "keys"+ [ (keys {:one 1 :two 2}) ==> [:one :two] ]+)-;; Uncomment this test once map is fixed.-;; (should-be "keys"-;; (keys (dict :one 1 :two 2))-;; '(:one :two))+(def values+ "Given a `dict`, returns a vector of its values."+ (fn [d]+ (map (fn [x] (nth 1 x)) (seq d))))+(:test "values"+ [ (values {:a 0 :b 1}) ==> [0 1] ]+);; rekey(def rekey (fn [old-key new-key mp](def old-val (lookup old-key mp))- (delete old-key (insert new-key old-val mp))))+ (delete old-key (insert new-key old-val mp))))(document 'rekey'(("old-key" any) ("new-key" any) ("dict" dict))"Change the key from 'old-key' to 'new-key' in 'dict'. If 'new-key' already exists, it is overwritten.")-(should-be "rekey"- (rekey :a :b {:a 5})- {:b 5})+(:test "rekey"+ [ (rekey :a :b {:a 5}) ==> {:b 5} ]);; modify-map(def modify-map@@ -49,6 +52,6 @@(fn [key f mp](insert key (f (lookup key mp)) mp)))-(should-be "modify-map"- (modify-map 'a (fn [x] (+ x 1)) (dict 'a 5))- (dict 'a 6))+(:test "modify-map"+ [ (modify-map :a (fn [x] (+ x 1)) {:a 5})+ ==> {:a 6} ])diff --git a/rad/prelude/io.rad b/rad/prelude/io.radindex badab8e..221b857 100644--- a/rad/prelude/io.rad+++ b/rad/prelude/io.rad@@ -5,11 +5,11 @@(def read-code!(fn [filename]- (read (string-append "(do " (read-file! filename) ")"))))+ (read-many (read-file! filename))))(document 'read-code!'("filename" string)- "Read code (as data) from a file.")+ "Read code (as data) from a file. Returns a vector of expressions")(def send-code!(fn [chain-id filename]diff --git a/rad/prelude/lens.rad b/rad/prelude/lens.radindex 8847fa1..51b6f0f 100644--- a/rad/prelude/lens.rad+++ b/rad/prelude/lens.rad@@ -81,31 +81,43 @@'(("key" any))" Returns a lens targetting keys of dicts.")-(should-be "view lens"- (view (@ :one) {:one 1 :two 2})- 1)--(should-be "set lens"- (set (@ :two) 42 {:one 1 :two 2})- {:one 1 :two 42})--(should-be "over lens"- (over (@ :one)- (fn [x] (+ x 1))- {:one 1 :two 2})- {:one 2 :two 2})--(should-be "lens composition"- (over (.. (@ :a) (@ :b))- (fn [x] (+ x 1))- {:a {:b 2}})- {:a {:b 3}})--(should-be "deep lenses"- (over (... (list (@ :a) (@ :b) (@ :c)))- (fn [x] (+ x 1))- {:a {:b {:c 0}}})- {:a {:b {:c 1}}})+(:test "view lens"+ [ (view (@ :one) {:one 1 :two 2}) ==> 1 ])++(:test "set lens"+ [ (set (@ :two) 42 {:one 1 :two 2}) ==> {:one 1 :two 42} ])++(:test "over lens"+ [ (over (@ :one)+ (fn [x] (+ x 1))+ {:one 1 :two 2})+ ==> {:one 2 :two 2} ])++(:test "lens composition"+ [ (over (.. (@ :a) (@ :b))+ (fn [x] (+ x 1))+ {:a {:b 2}})+ ==> {:a {:b 3}} ])++(:test "deep lenses"+ [ (over (... (list (@ :a) (@ :b) (@ :c)))+ (fn [x] (+ x 1))+ {:a {:b {:c 0}}})+ ==> {:a {:b {:c 1}}} ])++;; @nth++(def @nth+ "Lenses into the nth element of a vector"+ (fn [n]+ (make-lens+ (fn [s] (nth n s))+ (fn [val s]+ (<> (take n s) (add-left val (drop (+ n 1) s)))))))++(:test "@nth"+ [ (view (@nth 1) [0 1]) ==> 1 ]+ [ (over (@nth 1) (fn [x] (+ x 1)) [0 1]) ==> [0 2] ]);; Lens access to refs@@ -118,9 +130,8 @@'(("ref" ref) ("lens" dict))"Like 'view', but for refs.")-(should-be "view-ref"- (view-ref (ref {:key 1}) (@ :key))- 1)+(:test "view-ref"+ [ (view-ref (ref {:key 1}) (@ :key)) ==> 1 ]);; set-ref@@ -132,12 +143,12 @@'(("ref" ref) ("lens" dict) ("new-value" any))"Like 'set', but for refs.")-(should-be "set-ref"- ((fn [r]- (set-ref r (@ :a) 2)- (view-ref r (@ :a)))- (ref {:a 1}))- 2)+(:test "set-ref"+ [ ((fn [r]+ (set-ref r (@ :a) 2)+ (view-ref r (@ :a)))+ (ref {:a 1}))+ ==> 2 ]);; over-ref@@ -149,9 +160,9 @@'(("ref" ref) ("lens" dict) ("fn" function))"Like 'over', but for refs.")-(should-be "over-ref"- ((fn [r]- (over-ref r (@ :a) (fn [x] (* x 2)))- (view-ref r (@ :a)))- (ref {:a 2}))- 4)+(:test "over-ref"+ [ ((fn [r]+ (over-ref r (@ :a) (fn [x] (* x 2)))+ (view-ref r (@ :a)))+ (ref {:a 2}))+ ==> 4 ])diff --git a/rad/prelude/list.rad b/rad/prelude/list.radindex 5cf0505..d374f0e 100644--- a/rad/prelude/list.rad+++ b/rad/prelude/list.rad@@ -6,14 +6,17 @@;; empty?-(def empty? (fn [ls] (eq? ls nil)))+(def empty? (fn [ls] (or (eq? ls nil) (eq? ls []))))(document 'empty?- '(("list" list))- "True if 'list' is empty, false otherwise.")--(should-be "empty" (empty? (list 2)) #f)+ '(("seq" sequence))+ "True if 'seq' is empty, false otherwise.")+(:test "empty"+ [ (empty? (list 2)) ==> #f ]+ [ (empty? [2]) ==> #f ]+ [ (empty? []) ==> #t ]+);; length@@ -25,8 +28,10 @@'(("list" list))"Returns the length of 'list'.")-(should-be "length" (length nil) 0)-(should-be "length" (length (list 2)) 1)+(:test "length"+ [ (length nil) ==> 0]+ [ (length (list 2)) ==> 1]+);; reverse@@ -42,7 +47,10 @@'(("list" list))"Returns the reversed 'list'.")-(should-be "reverse" (reverse (list 1 2 3)) (list 3 2 1))++(:test "reverse"+ [ (reverse (list 1 2 3)) ==> (3 2 1) ]+);; range@@ -55,7 +63,9 @@'(("start" number) ("end" number))"Returns a list with all integers from 'start' to 'end', inclusive.")-(should-be "range" (range 1 5) (list 1 2 3 4 5))+(:test "range"+ [ (range 1 5) ==> (1 2 3 4 5) ]+);; concat@@ -66,8 +76,9 @@'(("list1" list) ("list2" list))"Concatenates 'list1' and 'list2'.")-(should-be "concat" (concat '(1 2 3) '(4 5 6)) '(1 2 3 4 5 6))-+(:test "concat"+ [ (concat '(1 2 3) '(4 5 6)) ==> (1 2 3 4 5 6) ]+);; filter@@ -82,9 +93,10 @@'(("filter-cond" function) ("list" list))"Returns 'list' with only the elements that satisfy 'filter-cond'.")-(should-be "filter"- (filter (fn [x] (< x 10)) (list 3 10 11))- (list 3))+(:test "filter"+ [ (filter (fn [x] (< x 10)) (list 3 10 11))+ ==> (3) ]+)(def list-with-head"Given a value `x`, and two functions `f` and `g`, checks if `x` is a list with a@@ -94,4 +106,4 @@(if (> (length x) 0)(f (head x))(g))- (g))))\ No newline at end of file+ (g))))diff --git a/rad/prelude/recursion.rad b/rad/prelude/recursion.radindex 9d2da38..b2e8859 100644--- a/rad/prelude/recursion.rad+++ b/rad/prelude/recursion.rad@@ -13,6 +13,9 @@(fn [g](h (fn [arg1 arg2] ((g g) arg1 arg2)))))))-((fn []- (def fac (Y (fn [f] (fn [x] (if (< x 2) 1 (* x (f (- x 1))))))))- (should-be "fac" (fac 10) 3628800)))+(:test "Y combinator"+ [ (do+ (def fac (Y (fn [f] (fn [x] (if (< x 2) 1 (* x (f (- x 1))))))))+ (fac 10)) ==> 3628800+ ]+)diff --git a/rad/prelude/should-be.rad b/rad/prelude/should-be.raddeleted file mode 100644index 5f3f88e..0000000--- a/rad/prelude/should-be.rad+++ /dev/null@@ -1,15 +0,0 @@-;; should-be - testing functions--(def should-be- (fn [name x y]- (if (eq? x y)- (print! (string-append "Test '" name "' succeeded"))- (do- (print! (string-append "Test '" name "' failed"))- (print! (string-append "LHS:\n\t" (show x) "\nRHS:\n\t" (show y)))))))--(document 'should-be- '(("test-name" string) ("actual" any) ("expected" any))- "Checks that 'actual' equals 'expected'. Used for tests")--(should-be "doc" (string? (doc 'doc)) #t)diff --git a/rad/prelude/test.rad b/rad/prelude/test.radnew file mode 100644index 0000000..17cabf1--- /dev/null+++ b/rad/prelude/test.rad@@ -0,0 +1,60 @@+;; testing functions++(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+ (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]+ (print! (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)+ (print! (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)+ (foldl (fn [ix x] (check-single x ix (nth 1 (eval (nth 1 maybe-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)]+)diff --git a/src/Radicle.hs b/src/Radicle.hsindex 4527e17..d8e4a6d 100644--- a/src/Radicle.hs+++ b/src/Radicle.hs@@ -62,6 +62,7 @@ module Radicle-- * Parsing, parse+ , parseValues-- * Pretty-printing, renderPrettydiff --git a/src/Radicle/Internal/Effects.hs b/src/Radicle/Internal/Effects.hsindex f9140f5..ea38202 100644--- a/src/Radicle/Internal/Effects.hs+++ b/src/Radicle/Internal/Effects.hs@@ -182,8 +182,8 @@ replPrimFns = fromList $ allDocs $, ( "gen-key-pair!", [md|Given an elliptic curve, generates a cryptographic key-pair. Use`default-ecc-curve` for a default value for the elliptic curve.|]- , \case- [curvev] -> do+ , oneArg "gen-key-pair!" $ \case+ curvev -> docurve <- hoistEither . first (toLangError . OtherError) $ fromRad curvev(pk, sk) <- generateKeyPair curvelet pkv = toRad pk@@ -192,7 +192,6 @@ replPrimFns = fromList $ allDocs $[ (Keyword (Ident "private-key"), skv), (Keyword (Ident "public-key"), pkv)]- xs -> throwErrorHere $ WrongNumberOfArgs "gen-key-pair!" 0 (length xs)), ( "gen-signature!", [md|Given a private key and a message (a string), generates a cryptographicdiff --git a/src/Radicle/Internal/Effects/Capabilities.hs b/src/Radicle/Internal/Effects/Capabilities.hsindex 7ae3927..ff08b19 100644--- a/src/Radicle/Internal/Effects/Capabilities.hs+++ b/src/Radicle/Internal/Effects/Capabilities.hs@@ -77,7 +77,6 @@ instance ReadFile (InputT IO) whereinstance {-# OVERLAPPABLE #-} ReadFile m => ReadFile (Lang m) wherereadFileS = lift . readFileS-putStrLnS :: (Stdout m) => Text -> m ()putStrLnS t = putStrS t >> putStrS "\n"diff --git a/src/Radicle/Internal/Interpret.hs b/src/Radicle/Internal/Interpret.hsindex bb635f5..b6b483f 100644--- a/src/Radicle/Internal/Interpret.hs+++ b/src/Radicle/Internal/Interpret.hs@@ -47,13 +47,11 @@ interpretMany=> Text -- ^ Name of source file (for error reporting)-> Text -- ^ Source code to be interpreted-> Lang m Value-interpretMany sourceName src = do- let parsed = parseValues sourceName src- case partitionEithers parsed of- ([], vs) -> do- es <- mapM eval vs- case lastMay es of- Just e -> pure e- _ -> throwErrorHere- $ OtherError "InterpretMany should be called with at least one expression."- (e:_, _) -> throwErrorHere $ ParseError e+interpretMany sourceName src = case parseValues sourceName src of+ Left err -> throwErrorHere $ ParseError err+ Right vs -> do+ es <- mapM eval vs+ case lastMay es of+ Just e -> pure e+ _ -> throwErrorHere+ $ OtherError "InterpretMany should be called with at least one expression."diff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hsindex 621e852..b8507ef 100644--- a/src/Radicle/Internal/Parse.hs+++ b/src/Radicle/Internal/Parse.hs@@ -2,22 +2,16 @@ module Radicle.Internal.Parse whereimport Protolude hiding (SrcLoc, try)-import Data.List.NonEmpty (NonEmpty((:|)))import qualified Data.Map as Mapimport qualified Data.Sequence as Seq-import qualified Data.Text as Timport GHC.Exts (IsString(..))import Text.Megaparsec( ParsecT- , State(..), between, choice- , defaultTabWidth, eof, getPosition- , initialPos, manyTill- , runParserT', sepBy, try, (<?>)@@ -144,20 +138,9 @@ valueP = doparseValues:: Text -- ^ Name of source file (for error reporting)-> Text -- ^ Source code to be parsed- -> [Either (Par.ParseError Char Void) Value]-parseValues sourceName srcCode = withoutLeadingSpaces- where- initial = State- { stateInput = srcCode- , statePos = initialPos (toS sourceName) :| []- , stateTokensProcessed = 0- , stateTabWidth = defaultTabWidth- }- withoutLeadingSpaces =- let (s', _) = runIdentity (runParserT' spaceConsumer initial)- in if T.null (stateInput s') then [] else go s'- go s = let (s', v) = runIdentity (runParserT' valueP s)- in if T.null (stateInput s') then [v] else v:go s'+ -> Either (Par.ParseError Char Void) [Value]+parseValues sourceName srcCode+ = M.parse (spaceConsumer *> many valueP <* eof) (toS sourceName) srcCode-- | Parse a single value.--diff --git a/src/Radicle/Internal/PrimFns.hs b/src/Radicle/Internal/PrimFns.hsindex f93f1cf..6bb2ecb 100644--- a/src/Radicle/Internal/PrimFns.hs+++ b/src/Radicle/Internal/PrimFns.hs@@ -13,6 +13,7 @@ import Data.Sequence (Seq(..))import qualified Data.Sequence as Seqimport qualified Data.Text as Timport GHC.Exts (IsList(..))+import Text.Megaparsec (parseErrorPretty)import qualified Text.Pandoc as Pandocimport qualified Radicle.Internal.Annotation as Ann@@ -89,6 +90,12 @@ purePrimFns = fromList $ allDocs $String s -> readValue s_ -> throwErrorHere $ TypeError "read: expects string")+ , ( "read-many"+ , [md|Parses a string into a vector of radicle values. Does not evaluate the values.|]+ , oneArg "read-many" $ \case+ String s -> readValues s+ _ -> throwErrorHere $ TypeError "read-many: expects string"+ ), ("get-current-env", [md|Returns the current radicle state.|], \case@@ -154,19 +161,23 @@ purePrimFns = fromList $ allDocs $[_, _] -> throwErrorHere $ TypeError "cons: second argument must be list"xs -> throwErrorHere $ WrongNumberOfArgs "cons" 2 (length xs)), ("head"- , [md|Retrieves the first element of a list if it exists. Otherwise throws an+ , [md|Retrieves the first element of a sequence if it exists. Otherwise throws anexception.|], oneArg "head" $ \case- List (x:_) -> pure x- List [] -> throwErrorHere $ OtherError "head: empty list"- _ -> throwErrorHere $ TypeError "head: expects list argument")+ List (x:_) -> pure x+ List [] -> throwErrorHere $ OtherError "head: empty list"+ Vec (x Seq.:<| _) -> pure x+ Vec Seq.Empty -> throwErrorHere $ OtherError "head: empty vector"+ _ -> throwErrorHere $ TypeError "head: expects sequence argument"), ("tail"- , [md|Given a non-empty list, returns the list of all the elements but the- first. If the list is empty, throws an exception.|]+ , [md|Given a non-empty sequence, returns the sequence of all the elements but the+ first. If the sequence is empty, throws an exception.|], oneArg "tail" $ \case- List (_:xs) -> pure $ List xs- List [] -> throwErrorHere $ OtherError "tail: empty list"- _ -> throwErrorHere $ TypeError "tail: expects list argument")+ List (_:xs) -> pure $ List xs+ List [] -> throwErrorHere $ OtherError "tail: empty list"+ Vec (_ Seq.:<| xs) -> pure $ Vec xs+ Vec Seq.Empty -> throwErrorHere $ OtherError "tail: empty vector"+ _ -> throwErrorHere $ TypeError "tail: expects sequence argument")-- Lists and Vecs, ( "drop"@@ -181,6 +192,18 @@ purePrimFns = fromList $ allDocs $_ -> throwErrorHere $ TypeError $ "drop: second argument must be a list of vector"_ -> throwErrorHere $ TypeError $ "drop: first argument must be a number")+ , ( "take"+ , [md|Returns the first `n` items of a sequence, unless the sequence is too short,+ in which case an exception is thrown.|]+ , twoArg "take" $ \case+ (Number n, vs) -> case floatingOrInteger n of+ Left (_ :: Double) -> throwErrorHere $ OtherError "take: first argument must be an integer"+ Right i -> case vs of+ List xs -> pure . List $ take i xs+ Vec xs -> pure . Vec $ Seq.take i xs+ _ -> throwErrorHere $ TypeError $ "take: second argument must be a list of vector"+ _ -> throwErrorHere $ TypeError $ "take: first argument must be a number"+ ), ( "nth", [md|Given an integral number `n` and `xs`, returns the `n`th element(zero indexed) of `xs` when `xs` is a list or a vector. If `xs`@@ -194,7 +217,7 @@ purePrimFns = fromList $ allDocs $case xs `atMay` i ofJust x -> pure xNothing -> throwErrorHere $ OtherError "nth: index out of bounds"- [_,_] -> throwErrorHere $ TypeError "nth: expects a integer and a list"+ [_,_] -> throwErrorHere $ TypeError "nth: expects a integer and a list or vector"xs -> throwErrorHere $ WrongNumberOfArgs "nth" 2 (length xs))@@ -522,5 +545,15 @@ readValue s = doRight v -> pure vLeft e -> throwErrorHere $ ThrownError (Ident "parse-error") (String e)+readValues+ :: (MonadError (LangError Value) m)+ => Text+ -> m Value+readValues s = do+ let p = parseValues "[read-many-primop]" s+ case p of+ Right v -> pure . Vec $ Seq.fromList v+ Left e -> throwErrorHere $ ThrownError (Ident "parse-error") (String . toS $ parseErrorPretty e)+allDocs :: [(Text, Text, a)] -> [(Ident, Maybe Text, a)]allDocs = fmap $ \(x,y,z) -> (unsafeToIdent x, Just y, z)diff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hsindex bbfb0b1..88ccc9e 100644--- a/test/spec/Radicle/Internal/TestCapabilities.hs+++ b/test/spec/Radicle/Internal/TestCapabilities.hs@@ -2,26 +2,33 @@-- Radicle.Internal.Subscriber.Capabilities that may be used for testing.module Radicle.Internal.TestCapabilities where-import Protolude+import Protolude hiding (TypeError)import qualified Crypto.Random as CryptoRandimport qualified Data.Map.Strict as Map+import Data.Scientific (floatingOrInteger)+import qualified Data.Sequence as Seq+import GHC.Exts (fromList)import qualified System.FilePath.Find as FPimport Radicle+import Radicle.Internal.Core (addBinding)import Radicle.Internal.Cryptoimport Radicle.Internal.Effects.Capabilities+import Radicle.Internal.PrimFns (allDocs)import qualified Radicle.Internal.UUID as UUIDimport Paths_radicle+data WorldState = WorldState- { worldStateStdin :: [Text]- , worldStateStdout :: [Text]- , worldStateEnv :: Env Value- , worldStateFiles :: Map Text Text- , worldStateDRG :: CryptoRand.ChaChaDRG- , worldStateUUID :: Int+ { worldStateStdin :: [Text]+ , worldStateStdout :: [Text]+ , worldStateEnv :: Env Value+ , worldStateFiles :: Map Text Text+ , worldStateDRG :: CryptoRand.ChaChaDRG+ , worldStateUUID :: Int+ , worldStateRemoteChains :: Map Text (Seq.Seq Value)}@@ -42,9 +49,10 @@ runTestWithFiles bindings inputs files action =, worldStateFiles = files, worldStateDRG = CryptoRand.drgNewSeed (CryptoRand.seedFromInteger 4) -- chosen by fair dice roll, worldStateUUID = 0+ , worldStateRemoteChains = mempty}in case runState (fmap fst $ runLang bindings $ interpretMany "[test]" action) ws of- (val, st) -> (val, reverse $ worldStateStdout st)+ (val, st) -> (val, worldStateStdout st)-- | Run a possibly side-effecting program with the given stdin input lines.runTestWith@@ -79,6 +87,47 @@ sourceFiles = doallFiles <- FP.find FP.always (FP.extension FP.==? ".rad") dirpure (dir <> "/", drop (length dir + 1) <$> allFiles)+-- | Bindings with REPL and client stuff mocked, and with -- a 'test-env__'+-- variable set to true.+testBindings :: Bindings (PrimFns (State WorldState))+testBindings+ = addBinding (unsafeToIdent "test-env__") Nothing (Boolean True)+ $ addPrimFns clientPrimFns replBindings++-- | Mocked versions of 'send!' and 'receive!'+clientPrimFns :: PrimFns (State WorldState)+clientPrimFns = fromList . allDocs $ [sendPrimop, receivePrimop]+ where+ sendPrimop =+ ( "send!"+ , "Mocked version of `send!` that stores sent values in a map with chains as keys."+ , \case+ [String url, Vec v] -> do+ lift . modify $ \s ->+ s { worldStateRemoteChains+ = Map.insertWith (flip (Seq.><)) url v $ worldStateRemoteChains s }+ pure $ List []+ [_, Vec _] -> throwErrorHere $ TypeError "send!: first argument should be a string"+ [String _, _] -> throwErrorHere $ TypeError "send!: second argument should be a vector"+ xs -> throwErrorHere $ WrongNumberOfArgs "send!" 2 (length xs)+ )+ receivePrimop =+ ( "receive!"+ , "Mocked version of `receive!` that retrieves values from a map with chains as keys."+ , \case+ [String url, Number n] -> do+ case floatingOrInteger n of+ Left (_ :: Float) -> throwErrorHere . OtherError+ $ "receive!: expecting int argument"+ Right r -> do+ chains <- lift $ gets worldStateRemoteChains+ pure . List $ case Map.lookup url chains of+ Nothing -> []+ Just res -> toList $ Seq.drop r res+ [String _, _] -> throwErrorHere $ TypeError "receive!: expecting number as second arg"+ [_, _] -> throwErrorHere $ TypeError "receive!: expecting string as first arg"+ xs -> throwErrorHere $ WrongNumberOfArgs "receive!" 2 (length xs)+ )instance {-# OVERLAPPING #-} Stdin TestLang wheregetLineS = do@@ -95,18 +144,21 @@ instance {-# OVERLAPPING #-} ReadFile TestLang wherereadFileS fn = dofs <- lift $ gets worldStateFilescase Map.lookup fn fs of- Just f -> pure $ Right f- Nothing -> pure . Left $ "File not found: " <> fn+ Just f -> pure $ Right f+ Nothing -> throwErrorHere . OtherError $ "File not found: " <> fninstance MonadRandom (State WorldState) wheregetRandomBytes i = do- drg <- gets worldStateDRG- let (a, drg') = CryptoRand.randomBytesGenerate i drg- modify $ \ws -> ws { worldStateDRG = drg' }- pure a+ drg <- gets worldStateDRG+ let (a, drg') = CryptoRand.randomBytesGenerate i drg+ modify $ \ws -> ws { worldStateDRG = drg' }+ pure ainstance UUID.MonadUUID (State WorldState) whereuuid = do- i <- gets worldStateUUID- modify $ \ws -> ws { worldStateUUID = i + 1 }- pure $ "uuid-" <> show i+ i <- gets worldStateUUID+ modify $ \ws -> ws { worldStateUUID = i + 1 }+ pure . pad $ show i+ where+ prefix = "00000000-0000-0000-0000-"+ pad i = toS $ prefix <> replicate (12 - length i) '0' <> idiff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex d99aca5..a4abc19 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -4,7 +4,6 @@ module Radicle.Tests whereimport Protolude hiding (toList)import Codec.Serialise (Serialise, deserialise, serialise)-import Data.List (isInfixOf, isSuffixOf, last)import qualified Data.Map.Strict as Mapimport Data.Maybe (fromJust)import Data.Scientific (Scientific)@@ -81,6 +80,16 @@ test_eval ="(nth 0 '(0 1 2))" `succeedsWith` Number 0"(nth 1 '(0 1 2))" `succeedsWith` Number 1+ , testCase "'take n' takes n elements of a sequence" $ do+ "(take 0 [0 1 2])" `succeedsWith` Vec (fromList [])+ "(take 2 [0 1 2])" `succeedsWith` Vec (fromList [Number 0, Number 1])+ "(take 2 (list 0 1 2))" `succeedsWith` List [Number 0, Number 1]++ , testCase "'drop n' drops n elements of a sequence" $ do+ "(drop 0 [0 1 2])" `succeedsWith` Vec (fromList [Number 0, Number 1, Number 2])+ "(drop 2 [0 1 2])" `succeedsWith` Vec (fromList [Number 2])+ "(drop 2 (list 0 1 2))" `succeedsWith` List [Number 2]+, testProperty "'eq?' considers equal values equal" $ \(val :: Value) -> dolet prog = [i|(eq? #{renderPrettyDef val} #{renderPrettyDef val})|]res = runTest' $ toS prog@@ -626,11 +635,14 @@ test_repl_primops =(list tt ff)|]in run [] prog @?= Right (List [Boolean True, Boolean False])+ , testCase "uuid! generates a valid uuid" $+ let prog = [s| (uuid? (uuid!)) |]+ in run [] prog @?= Right (Boolean True)]where- run stdin' prog = fst $ runTestWith replBindings stdin' prog+ run stdin' prog = fst $ runTestWith testBindings stdin' progrunFiles :: Map Text Text -> Text -> Either (LangError Value) Value- runFiles files prog = fst $ runTestWithFiles replBindings [] files prog+ runFiles files prog = fst $ runTestWithFiles testBindings [] files progtest_repl :: [TestTree]test_repl =@@ -677,28 +689,19 @@ test_repl =, "#t"](_, result) <- runInRepl input- last result @?= "#t"+ result @==> ["#t"], testCase "load! a non-existent file is a non-fatal exception" $ dolet input = [ "(load! \"not-a-thing.rad\")", "#t"](_, result) <- runInRepl input- last result @?= "#t"+ result @==> ["#t"]]where- -- In addition to the output of the lines tested, 'should-be's get+ -- In addition to the output of the lines tested, tests get-- printed, so we take only the last few output lines.- r @==> out = reverse (take (length out) $ reverse r) @?= out- -- Find repl.rad, give it all other files as possible imports, and run- -- it.- runInRepl inp = do- (dir, srcs) <- sourceFiles- srcMap <- forM srcs (\src -> (T.pack src ,) <$> readFile (dir <> src))- let replSrc = head [ src | (name, src) <- srcMap- , "repl.rad" `T.isSuffixOf` name- ]- pure $ runTestWithFiles replBindings inp (Map.fromList srcMap) (fromJust replSrc)+ r @==> out = reverse (take (length out) r) @?= outtest_from_to_radicle :: [TestTree]test_from_to_radicle =@@ -753,21 +756,54 @@ test_cbor =counterexample info $ got == expected--- Tests all radicle files 'repl' dir. These should use the 'should-be'--- function to ensure they are in the proper format.+-- 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.test_source_files :: IO TestTree-test_source_files = testGroup "Radicle source file tests" <$> do- (dir, files) <- sourceFiles- let radFiles = filter (\x -> ".rad" `isSuffixOf` x && "repl." `isInfixOf` x) files- sequence $ radFiles <&> \file -> do+test_source_files = testGroup "Radicle source file tests" <$>+ testOne "rad/prelude.rad"+ where+ testOne :: FilePath -> IO [TestTree]+ testOne file = do+ (dir, files) <- sourceFiles+ allFiles <- forM files $ \f -> do+ contents <- readFile (dir <> f)+ pure (toS f, contents)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 (toS name) $ result @?= "' succeeded\""- pure $ testGroup file- $ [ makeTest ln | ln <- out, "\"Test" `T.isPrefixOf` ln ]+ let (r, out) = runTestWithFiles testBindings [] (fromList allFiles) contents+ let makeTest line =+ let name = T.reverse $ T.drop 1 $ T.dropWhile (/= '\'')+ $ T.reverse $ T.drop 1 $ T.dropWhile (/= '\'') line+ in testCase (toS name) $+ if "' succeeded\"" `T.isSuffixOf` line+ then pure ()+ else assertFailure . toS $ "test failed: " <> line+ let doesntThrow = if isRight r+ then pure ()+ else assertFailure $ "Expected Right, got: " <> toS (prettyEither r)+ pure $ [testGroup file+ $ testCase "doesn't throw" doesntThrow+ : [ makeTest ln | ln <- reverse out, "\"Test" `T.isPrefixOf` ln ]]++test_macros :: [TestTree]+test_macros =+ [ testCase ":enter-chain keeps old bindings" $ do+ let input = [ "(def x 0)"+ , "(:enter-chain \"blah\")"+ , "(def x 1)"+ , ":quit"+ , "x"+ ]+ output = [ "0.0" ]+ (_, result) <- runInRepl input+ result @==> output+ ]+ where+ -- In addition to the output of the lines tested, 'should-be's get+ -- printed, so we take only the last few output lines.+ r @==> out = reverse (take (length out) r) @?= out+-- * Utils@@ -785,3 +821,14 @@ parseTest t = parse "(test)" tprettyEither :: Either (LangError Value) Value -> T.TextprettyEither (Left e) = "Error: " <> renderPrettyDef eprettyEither (Right v) = renderPrettyDef v++-- Find repl.rad, give it all other files as possible imports, and run+-- it.+runInRepl :: [Text] -> IO (Either (LangError Value) Value, [Text])+runInRepl inp = do+ (dir, srcs) <- sourceFiles+ srcMap <- forM srcs (\src -> (T.pack src ,) <$> readFile (dir <> src))+ let replSrc = head [ src | (name, src) <- srcMap+ , "repl.rad" `T.isSuffixOf` name+ ]+ pure $ runTestWithFiles testBindings inp (Map.fromList srcMap) (fromJust replSrc)