From 8ac980fd3b6d2050edc0e631a00028c1e6c28c73 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Wed, 4 Nov 2020 04:04:49 -0400 Subject: Re-named "lux/control/concurrency/process" to "thread". --- stdlib/source/spec/lux/world/shell.lux | 97 ++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 stdlib/source/spec/lux/world/shell.lux (limited to 'stdlib/source/spec') diff --git a/stdlib/source/spec/lux/world/shell.lux b/stdlib/source/spec/lux/world/shell.lux new file mode 100644 index 000000000..286cc7ce2 --- /dev/null +++ b/stdlib/source/spec/lux/world/shell.lux @@ -0,0 +1,97 @@ +(.module: + [lux #* + ["_" test (#+ Test)] + [abstract + [monad (#+ do)]] + [control + ["." try] + [security + ["!" capability]] + [concurrency + ["." promise (#+ Promise)]]] + [data + ["." product] + ["." text ("#@." equivalence) + ["%" format (#+ format)]] + [number + ["n" nat] + ["i" int]] + [format + ["." context]]] + [math + ["." random]]] + {1 + ["." /]}) + +(template [ ] + [(def: + (-> [/.Environment /.Command (List /.Argument)]) + (|>> list [context.empty ]))] + + [echo! "echo" Text (|>)] + [sleep! "sleep" Nat %.nat] + ) + +(def: (read-test expected process) + (-> Text (/.Process Promise) _.Assertion) + (do promise.monad + [?read (!.use (:: process read) []) + ?await (!.use (:: process await) [])] + ($_ _.and' + (_.claim [/.Can-Read] + (case ?read + (#try.Success actual) + (text@= expected actual) + + (#try.Failure error) + false)) + (_.claim [/.Can-Wait /.Exit /.normal] + (case ?await + (#try.Success exit) + (i.= /.normal exit) + + (#try.Failure error) + false)) + ))) + +(def: (destroy-test process) + (-> (/.Process Promise) _.Assertion) + (do promise.monad + [?destroy (!.use (:: process destroy) []) + ?await (!.use (:: process await) [])] + (_.claim [/.Can-Destroy] + (and (case ?destroy + (#try.Success _) + true + + (#try.Failure error) + false) + (case ?await + (#try.Success _) + false + + (#try.Failure error) + true))))) + +(with-expansions [ (as-is [/.Can-Execute + /.Environment /.Command /.Argument])] + (def: #export (spec shell) + (-> (/.Shell Promise) Test) + (<| (_.with-cover [/.Shell /.Process]) + (do {! random.monad} + [message (random.ascii/alpha 10) + seconds (:: ! map (|>> (n.% 5) (n.+ 5)) random.nat)] + (wrap (do promise.monad + [?echo (!.use (:: shell execute) (..echo! message)) + ?sleep (!.use (:: shell execute) (..sleep! seconds))] + (case [?echo ?sleep] + [(#try.Success echo) (#try.Success sleep)] + ($_ _.and' + (_.claim + true) + (..read-test message echo) + (..destroy-test sleep)) + + _ + (_.claim + false)))))))) -- cgit v1.2.3