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/library/lux/data/color.lux | 3 +- stdlib/source/library/lux/data/color/terminal.lux | 103 +++++++++++++++++++ stdlib/source/library/lux/meta/type/unit.lux | 58 +++++++---- stdlib/source/library/lux/test/unit.lux | 13 ++- stdlib/source/library/lux/world/console.lux | 3 +- stdlib/source/library/lux/world/logging.lux | 66 ++++++++++++ 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 ++ 14 files changed, 511 insertions(+), 59 deletions(-) create mode 100644 stdlib/source/library/lux/data/color/terminal.lux create mode 100644 stdlib/source/library/lux/world/logging.lux 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') diff --git a/stdlib/source/library/lux/data/color.lux b/stdlib/source/library/lux/data/color.lux index a0971891a..e2bd180f3 100644 --- a/stdlib/source/library/lux/data/color.lux +++ b/stdlib/source/library/lux/data/color.lux @@ -23,7 +23,8 @@ [/ ["[0]" rgb (.only RGB)]]) -(def top (-- rgb.limit)) +(def top + (-- rgb.limit)) (def rgb_factor (|> top .int int.frac)) diff --git a/stdlib/source/library/lux/data/color/terminal.lux b/stdlib/source/library/lux/data/color/terminal.lux new file mode 100644 index 000000000..1f368de0b --- /dev/null +++ b/stdlib/source/library/lux/data/color/terminal.lux @@ -0,0 +1,103 @@ +... https://en.wikipedia.org/wiki/ANSI_escape_code#Colors +(.require + [library + [lux (.except with) + [abstract + [equivalence (.only Equivalence)]] + [data + ["[0]" text (.use "[1]#[0]" equivalence) + ["[0]" char] + ["%" \\format]]] + [math + [number + ["n" nat]]] + [meta + [macro + ["[0]" template]] + [type + ["[0]" primitive]]]]] + ["[0]" // (.only Color) + ["[0]" rgb]]) + +(primitive.def .public Command + [Text Text] + + (def .public equivalence + (Equivalence Command) + (implementation + (def (= reference it) + (let [[beforeR afterR] (primitive.representation Command reference) + [beforeI afterI] (primitive.representation Command it)] + (and (text#= beforeR beforeI) + (text#= afterR afterI)))))) + + (def command + Text + (%.format (text.of_char char.escape) "[")) + + (def .public (with command text) + (-> Command Text + Text) + (let [[before after] (primitive.representation Command command)] + (%.format before + text + after))) + + (with_template [ ] + [(def + Text + (%.format ..command (%.nat ) "m"))] + + [39 default_foreground_color] + [49 default_background_color] + ) + + (with_template [ ] + [(`` (def .public (,, (template.symbol [ "_foreground"])) + Command + (|> [(%.format ..command (%.nat ) "m") + ..default_foreground_color] + (primitive.abstraction Command)))) + + (`` (def .public (,, (template.symbol [ "_background"])) + Command + (|> [(%.format ..command (%.nat ) "m") + ..default_background_color] + (primitive.abstraction Command))))] + + [030 040 black] + [031 041 red] + [032 042 green] + [033 043 yellow] + [034 044 blue] + [035 045 magenta] + [036 046 cyan] + [037 047 white] + + [090 100 bright_black] + [091 101 bright_red] + [092 102 bright_green] + [093 103 bright_yellow] + [094 104 bright_blue] + [095 105 bright_magenta] + [096 106 bright_cyan] + [097 107 bright_white] + ) + + (with_template [ ] + [(def .public ( it) + (-> Color Command) + (let [it (//.rgb it)] + (|> [(%.format ..command + + ";" (%.nat (rgb.number (the rgb.#red it))) + ";" (%.nat (rgb.number (the rgb.#green it))) + ";" (%.nat (rgb.number (the rgb.#blue it))) + "m") + ] + (primitive.abstraction Command))))] + + ["38;2" foreground ..default_foreground_color] + ["48;2" background ..default_background_color] + ) + ) diff --git a/stdlib/source/library/lux/meta/type/unit.lux b/stdlib/source/library/lux/meta/type/unit.lux index 4aa27fbfd..e4bf7a1e9 100644 --- a/stdlib/source/library/lux/meta/type/unit.lux +++ b/stdlib/source/library/lux/meta/type/unit.lux @@ -6,6 +6,9 @@ [equivalence (.only Equivalence)] [order (.only Order)] [enum (.only Enum)]] + [data + ["[0]" text (.only) + ["%" \\format]]] [math [number ["i" int]]] @@ -21,21 +24,27 @@ Int (def .public measure - (All (_ scale unit) (-> Int (Measure scale unit))) + (All (_ scale unit) + (-> Int + (Measure scale unit))) (|>> abstraction)) (def .public number - (All (_ scale unit) (-> (Measure scale unit) Int)) + (All (_ scale unit) + (-> (Measure scale unit) + Int)) (|>> representation)) (def .public equivalence - (All (_ scale unit) (Equivalence (Measure scale unit))) + (All (_ scale unit) + (Equivalence (Measure scale unit))) (implementation (def (= reference sample) (i.= (representation reference) (representation sample))))) (def .public order - (All (_ scale unit) (Order (Measure scale unit))) + (All (_ scale unit) + (Order (Measure scale unit))) (implementation (def equivalence ..equivalence) @@ -43,7 +52,8 @@ (i.< (representation reference) (representation sample))))) (def .public enum - (All (_ scale unit) (Enum (Measure scale unit))) + (All (_ scale unit) + (Enum (Measure scale unit))) (implementation (def order ..order) (def succ (|>> representation ++ abstraction)) @@ -51,7 +61,9 @@ (with_template [ ] [(def .public ( param subject) - (All (_ scale unit) (-> (Measure scale unit) (Measure scale unit) (Measure scale unit))) + (All (_ scale unit) + (-> (Measure scale unit) (Measure scale unit) + (Measure scale unit))) (abstraction ( (representation param) (representation subject))))] @@ -61,7 +73,9 @@ (with_template [

] [(def .public ( param subject) - (All (_ scale p s) (-> (Measure scale

) (Measure scale ) (Measure scale ))) + (All (_ scale p s) + (-> (Measure scale

) (Measure scale ) + (Measure scale ))) (abstraction ( (representation param) (representation subject))))] @@ -69,16 +83,24 @@ [/ i./ p [p s] s] ) - (.type .public (Unit a) + (.type .public (Unit of) (Interface - (is (-> Int (Measure Any a)) + (is (-> (Measure Any of) + Text) + format) + (is (-> Int + (Measure Any of)) in) - (is (-> (Measure Any a) Int) + (is (-> (Measure Any of) + Int) out))) - (def .public (unit _) - (Ex (_ a) (-> Any (Unit a))) + (def .public (unit descriptor) + (Ex (_ of) + (-> Text + (Unit of))) (implementation + (def format (|>> ..number %.int (text.suffix descriptor))) (def in ..measure) (def out ..number))) ) @@ -91,9 +113,9 @@ (, it)) (, g!a)))))))) -(with_template [ ] +(with_template [ ] [(def .public - (..unit [])) + (..unit )) (.def .public (let [[module _] (symbol .._) @@ -101,8 +123,8 @@ {.#Named [module short] (..type )}))] - [gram Gram] - [meter Meter] - [litre Litre] - [second Second] + ["g" gram Gram] + ["m" meter Meter] + ["l" litre Litre] + ["s" second Second] ) diff --git a/stdlib/source/library/lux/test/unit.lux b/stdlib/source/library/lux/test/unit.lux index 86df9efd0..e8eeb0a04 100644 --- a/stdlib/source/library/lux/test/unit.lux +++ b/stdlib/source/library/lux/test/unit.lux @@ -10,6 +10,8 @@ [concurrency ["[0]" async (.only Async) (.use "[1]#[0]" monad)]]] [data + [color + ["[0]" terminal]] ["[0]" text (.only) ["%" \\format (.only format)]] [collection @@ -60,10 +62,13 @@ (-> Text Test Test) (|>> %.text context')) -(with_template [ ] +(with_template [ ] [(def (-> Text Test) - (|>> (format ) + (|>> (format "[" + (terminal.with + ) + "] ") [] async#in)) @@ -71,8 +76,8 @@ (-> Text Test) (|>> %.text ))] - ["[Failure] " tally.failure failure' failure] - ["[Success] " tally.success success' success] + ["Failure" tally.failure failure' failure terminal.red_foreground] + ["Success" tally.success success' success terminal.green_foreground] ) (def (test' message condition) diff --git a/stdlib/source/library/lux/world/console.lux b/stdlib/source/library/lux/world/console.lux index 37d8b3b07..4d3ba46d8 100644 --- a/stdlib/source/library/lux/world/console.lux +++ b/stdlib/source/library/lux/world/console.lux @@ -40,7 +40,8 @@ [read] [read_line] [write] - [close]))))) + [close] + ))))) (exception.def .public cannot_close) diff --git a/stdlib/source/library/lux/world/logging.lux b/stdlib/source/library/lux/world/logging.lux new file mode 100644 index 000000000..6f58fd134 --- /dev/null +++ b/stdlib/source/library/lux/world/logging.lux @@ -0,0 +1,66 @@ +(.require + [library + [lux (.except with) + [abstract + [monad (.only Monad do)]] + [control + ["[0]" try (.only Try)]] + [data + [color + ["[0]" terminal]] + [text + ["%" \\format]]] + [world + ["[0]" console (.only Console)] + [time + [instant (.only Instant)]]]]]) + +(type .public (Logger !) + (Interface + (is (-> Text + (! (Try Any))) + log))) + +(with_template [ ] + [(def .public ( message it) + (All (_ !) + (-> Text (Logger !) + (! (Try Any)))) + (at it log (terminal.with + (%.format message))))] + + ["[ERROR] " error terminal.red_foreground] + ["[WARNING] " warn terminal.yellow_foreground] + ["[INFO] " info terminal.white_foreground] + ["[DEBUG] " debug terminal.cyan_foreground] + ) + +(def .public (with scope it) + (All (_ !) + (-> (-> Text Text) + (-> (Logger !) (Logger !)))) + (implementation + (def log + (|>> scope + (at it log))))) + +(def .public (timed ! now it) + (All (_ !) + (-> (Monad !) (-> Any (! Instant)) + (-> (Logger !) (Logger !)))) + (implementation + (def (log message) + (do ! + [now (now [])] + (at it log (%.format (terminal.with terminal.green_foreground + (%.format "[" (%.instant now) "]")) + " " + message)))))) + +(def .public (console it) + (All (_ !) + (-> (Console !) + (Logger !))) + (implementation + (def (log message) + (console.write_line message it)))) 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