From 4ea7563c46a07dbe1cb84547a60e9398144917ae Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Wed, 16 Nov 2022 23:17:45 -0400 Subject: Added generic logging abstraction/machinery. --- stdlib/source/test/lux/control/try.lux | 3 +- stdlib/source/test/lux/data/color.lux | 4 +- stdlib/source/test/lux/data/color/rgb.lux | 3 +- stdlib/source/test/lux/data/color/terminal.lux | 102 +++++++++++++++++++ stdlib/source/test/lux/meta/type/unit.lux | 80 +++++++++------ stdlib/source/test/lux/world.lux | 4 +- stdlib/source/test/lux/world/logging.lux | 119 ++++++++++++++++++++++ stdlib/source/test/lux/world/net/http/request.lux | 9 ++ 8 files changed, 289 insertions(+), 35 deletions(-) create mode 100644 stdlib/source/test/lux/data/color/terminal.lux create mode 100644 stdlib/source/test/lux/world/logging.lux (limited to 'stdlib/source/test') diff --git a/stdlib/source/test/lux/control/try.lux b/stdlib/source/test/lux/control/try.lux index ff8fe9455..7b2ec5918 100644 --- a/stdlib/source/test/lux/control/try.lux +++ b/stdlib/source/test/lux/control/try.lux @@ -40,7 +40,8 @@ (def .public test Test (<| (_.covering /._) - (_.for [/.Try]) + (_.for [/.Try + /.#Failure /.#Success]) (do random.monad [expected random.nat alternative (|> random.nat (random.only (|>> (n.= expected) not))) diff --git a/stdlib/source/test/lux/data/color.lux b/stdlib/source/test/lux/data/color.lux index c56352d3d..e53d5d0b7 100644 --- a/stdlib/source/test/lux/data/color.lux +++ b/stdlib/source/test/lux/data/color.lux @@ -24,7 +24,8 @@ ["[0]" rgb]]] ["[0]" / ["[1][0]" rgb] - ["[1][0]" named]]) + ["[1][0]" named] + ["[1][0]" terminal]]) (def .public random (Random /.Color) @@ -217,4 +218,5 @@ /rgb.test /named.test + /terminal.test )))) diff --git a/stdlib/source/test/lux/data/color/rgb.lux b/stdlib/source/test/lux/data/color/rgb.lux index 5781c9efa..c0d329631 100644 --- a/stdlib/source/test/lux/data/color/rgb.lux +++ b/stdlib/source/test/lux/data/color/rgb.lux @@ -75,7 +75,8 @@ {try.#Failure it} (exception.match? /.invalid it) {try.#Success _} false))) )) - (_.for [/.RGB] + (_.for [/.RGB + /.#red /.#green /.#blue] (all _.and (_.for [/.equivalence] (equivalenceS.spec /.equivalence ..random)) diff --git a/stdlib/source/test/lux/data/color/terminal.lux b/stdlib/source/test/lux/data/color/terminal.lux new file mode 100644 index 000000000..a22173fbf --- /dev/null +++ b/stdlib/source/test/lux/data/color/terminal.lux @@ -0,0 +1,102 @@ +(.require + [library + [lux (.except) + [abstract + [monad (.only do)] + [\\specification + ["[0]S" equivalence]]] + [data + ["[0]" text (.use "[1]#[0]" equivalence)] + [collection + ["[0]" list]]] + [math + ["[0]" random (.only Random) (.use "[1]#[0]" monad)]] + [test + ["_" property (.only Test)]]]] + [\\library + ["[0]" / (.only) + ["/[1]" //]]] + [// + ["[0]T" rgb]]) + +(with_expansions [ (these [/.black_foreground] + [/.red_foreground] + [/.green_foreground] + [/.yellow_foreground] + [/.blue_foreground] + [/.magenta_foreground] + [/.cyan_foreground] + [/.white_foreground] + [/.bright_black_foreground] + [/.bright_red_foreground] + [/.bright_green_foreground] + [/.bright_yellow_foreground] + [/.bright_blue_foreground] + [/.bright_magenta_foreground] + [/.bright_cyan_foreground] + [/.bright_white_foreground] + + [/.black_background] + [/.red_background] + [/.green_background] + [/.yellow_background] + [/.blue_background] + [/.magenta_background] + [/.cyan_background] + [/.white_background] + [/.bright_black_background] + [/.bright_red_background] + [/.bright_green_background] + [/.bright_yellow_background] + [/.bright_blue_background] + [/.bright_magenta_background] + [/.bright_cyan_background] + [/.bright_white_background] + )] + (def .public random + (Random /.Command) + (`` (all random.either + (random#each (|>> //.of_rgb /.foreground) rgbT.random) + (random#each (|>> //.of_rgb /.background) rgbT.random) + (,, (with_template [] + [(random#in )] + + + )) + ))) + + (def .public test + Test + (<| (_.covering /._) + (do [! random.monad] + [color (random#each //.of_rgb rgbT.random) + command ..random + expected_text (random.upper_cased 3)]) + (_.for [/.Command]) + (all _.and + (_.for [/.equivalence] + (equivalenceS.spec /.equivalence ..random)) + + (_.coverage [/.with] + (let [it (/.with command + expected_text)] + (and (text.contains? expected_text it) + (not (text#= expected_text it))))) + (_.coverage [/.foreground /.background] + (not (at /.equivalence = + (/.foreground color) + (/.background color)))) + (`` (_.coverage [(,, (with_template [] + [] + + ))] + (loop (again [commands (list )]) + (when commands + {.#End} + true + + {.#Item head tail} + (and (list.every? (|>> (at /.equivalence = head) not) tail) + (again tail)))))) + ))) + ) diff --git a/stdlib/source/test/lux/meta/type/unit.lux b/stdlib/source/test/lux/meta/type/unit.lux index c31abd6b0..734400ade 100644 --- a/stdlib/source/test/lux/meta/type/unit.lux +++ b/stdlib/source/test/lux/meta/type/unit.lux @@ -8,10 +8,16 @@ ["$[0]" equivalence] ["$[0]" order] ["$[0]" enum]]] + [data + ["[0]" text (.use "[1]#[0]" equivalence) + ["%" \\format]]] [math ["[0]" random (.only Random)] [number ["i" int]]] + [meta + ["[0]" static] + ["[0]" code]] [test ["_" property (.only Test)]]]] [\\library @@ -42,39 +48,51 @@ ($enum.spec /.enum (..meter 1,000))) )) -(def what (/.unit [])) -(def What (/.type what)) +(with_expansions [ (static.random code.text + (random.upper_cased 1))] + (def what (/.unit )) + (def What (/.type what)) -(def unit - Test - (do random.monad - [expected random.int] - (_.for [/.Unit] - (`` (all _.and - (,, (with_template [ ] - [(_.coverage [ ] - (|> expected - (at in) - (at out) - (i.= expected)))] + (def unit + Test + (do random.monad + [expected random.int] + (_.for [/.Unit] + (`` (all _.and + (,, (with_template [ ] + [(_.coverage [ ] + (and (|> expected + (at in) + (at out) + (i.= expected)) + (let [it (|> expected + (at in) + (at format))] + (and (not (text#= (%.int expected) it)) + (text.starts_with? (%.int expected) it)))))] - [/.Gram /.gram] - [/.Meter /.meter] - [/.Litre /.litre] - [/.Second /.second] - )) - (_.coverage [/.measure /.number] - (|> expected - /.measure - /.number - (i.= expected))) - (_.coverage [/.unit /.type] - (|> expected - (at ..what in) - (is (/.Measure Any What)) - (at ..what out) - (i.= expected))) - ))))) + [/.Gram /.gram] + [/.Meter /.meter] + [/.Litre /.litre] + [/.Second /.second] + )) + (_.coverage [/.measure /.number] + (|> expected + /.measure + /.number + (i.= expected))) + (_.coverage [/.unit /.type] + (and (|> expected + (at ..what in) + (is (/.Measure Any What)) + (at ..what out) + (i.= expected)) + (let [it (|> expected + (at ..what in) + (at ..what format))] + (and (text.contains? (%.int expected) it) + (text.contains? it))))) + )))))) (def arithmetic Test diff --git a/stdlib/source/test/lux/world.lux b/stdlib/source/test/lux/world.lux index 2229ef10e..bfd4710b7 100644 --- a/stdlib/source/test/lux/world.lux +++ b/stdlib/source/test/lux/world.lux @@ -15,7 +15,8 @@ ["[1]/[0]" resolution]]] ["[1][0]" net] ["[1][0]" time] - ["[1][0]" locale]]) + ["[1][0]" locale] + ["[1][0]" logging]]) (def .public test Test @@ -29,4 +30,5 @@ /net.test /time.test /locale.test + /logging.test )) diff --git a/stdlib/source/test/lux/world/logging.lux b/stdlib/source/test/lux/world/logging.lux new file mode 100644 index 000000000..f2d56ff15 --- /dev/null +++ b/stdlib/source/test/lux/world/logging.lux @@ -0,0 +1,119 @@ +(.require + [library + [lux (.except) + [abstract + [monad (.only do)]] + [control + ["[0]" try] + [concurrency + ["[0]" async (.only Async) (.use "[1]#[0]" monad)]]] + [data + ["[0]" text (.use "[1]#[0]" equivalence) + ["%" \\format]]] + [math + ["[0]" random (.only Random)]] + [test + ["_" property (.only Test)] + ["[0]" unit]]]] + [\\library + ["[0]" / (.only) + [// + ["[0]" console]]]]) + +(def mock + (console.Mock Text) + (implementation + (def (on_read _) + {try.#Failure ""}) + (def (on_read_line feed) + (when (text.split_by text.\n feed) + {.#Some [line rest]} + {try.#Success [rest line]} + + {.#None} + {try.#Failure ""})) + (def (on_write line state) + {try.#Success (%.format state line)}) + (def (on_close _) + {try.#Failure ""}))) + +(def .public test + Test + (<| (_.covering /._) + (do [! random.monad] + [expected_message (random.upper_cased 3) + expected_appendix (random.lower_cased 3) + expected_instant random.instant]) + (_.for [/.Logger]) + (`` (all _.and + (let [console (console.async (console.mock ..mock "")) + it (/.console console)] + (in (do async.monad + [logged? (at it log expected_message) + actual_message (at console read_line [])] + (unit.coverage [/.console /.log] + (when [logged? actual_message] + [{try.#Success _} + {try.#Success actual_message}] + (text#= expected_message actual_message) + + _ + false))))) + (,, (with_template [] + [(let [console (console.async (console.mock ..mock "")) + it (/.console console)] + (in (do async.monad + [logged? ( expected_message it) + actual_message (at console read_line [])] + (unit.coverage [] + (when [logged? actual_message] + [{try.#Success _} + {try.#Success actual_message}] + (and (not (text#= expected_message actual_message)) + (text.contains? expected_message actual_message)) + + _ + false)))))] + + [/.error] + [/.warn] + [/.info] + [/.debug] + )) + (let [console (console.async (console.mock ..mock "")) + it (is (/.Logger Async) + (/.with (text.suffix expected_appendix) + (/.console console)))] + (in (do async.monad + [logged? (at it log expected_message) + actual_message (at console read_line [])] + (unit.coverage [/.with] + (when [logged? actual_message] + [{try.#Success _} + {try.#Success actual_message}] + (text#= (text.suffix expected_appendix expected_message) + actual_message) + + _ + false))))) + (let [console (console.async (console.mock ..mock "")) + it (is (/.Logger Async) + (/.timed async.monad + (function (_ _) + (async#in expected_instant)) + (/.console console)))] + (in (do async.monad + [logged? (at it log expected_message) + actual_message (at console read_line [])] + (unit.coverage [/.timed] + (when [logged? actual_message] + [{try.#Success _} + {try.#Success actual_message}] + (and (text.contains? expected_message + actual_message) + (text.contains? (%.instant expected_instant) + actual_message)) + + _ + false))))) + )))) diff --git a/stdlib/source/test/lux/world/net/http/request.lux b/stdlib/source/test/lux/world/net/http/request.lux index afc7e88ad..fb30692d0 100644 --- a/stdlib/source/test/lux/world/net/http/request.lux +++ b/stdlib/source/test/lux/world/net/http/request.lux @@ -44,6 +44,15 @@ (_.for [/.Request /.#identification /.#message /.#protocol /.#resource]) (`` (all _.and + (_.coverage [/.Identification + /.#local /.#remote] + true) + (_.coverage [/.Protocol + /.#version /.#scheme] + true) + (_.coverage [/.Resource + /.#method /.#uri] + true) (_.coverage [/.utf8 /.text] (and (same? /.utf8 /.text) (let [it (/.utf8 identity.monad expected_text)] -- cgit v1.2.3