From 52f984f3ceb483146d7f74bf4148b2e2224c50bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20N=2E=20Robalino?= Date: Sun, 29 Aug 2021 14:58:53 -0500 Subject: [PATCH] Infraestructure initial setup. Some groundwork to start growing Nu's standard library. As an initial exercise we take the mantainer_time.nu script that computes the core team's table timezone so that a given date can be converted to their local timezones. While doing so, it sounded like the logic could be abstracted out and make it a subcommand of `date` instead. For that reason, that particular custom command of interested was moved to `std/date`. Another one added were basic infraestructure custom commands for the test framework, subject to change (naming, and more things). It's in `std/playground` and it's used in `tests` directory. Since this repository contains scripts, we keep the `language` directory to maintain and grow the nu std library and grow it as we see custom command candidates. For the majority of the scripts that have nothing to do with the standard language but they do exercise Nu in practice (and some use custom commands, ideally, from the very, hopefully, future nu standard library) I have expectations to make them `runnable` as well and for that reason we introduce here an `examples` directory. To run the first one, you can do: ``` nu examples/date_in_local_timezones.nu ``` --- assets/core_team.nu | 12 +++++++ examples/date_in_local_timezones.nu | 25 +++++++++++++ language/playground.nu | 1 + language/playground/lib.nu | 55 +++++++++++++++++++++++++++++ language/std.nu | 1 + language/std/date.nu | 7 ++++ lib/scripts.nu | 1 + maintainer_time.nu | 18 ---------- tests/language/std/date.nu | 38 ++++++++++++++++++++ tests/main.nu | 2 ++ 10 files changed, 142 insertions(+), 18 deletions(-) create mode 100644 assets/core_team.nu create mode 100644 examples/date_in_local_timezones.nu create mode 100644 language/playground.nu create mode 100644 language/playground/lib.nu create mode 100644 language/std.nu create mode 100644 language/std/date.nu create mode 100644 lib/scripts.nu delete mode 100644 maintainer_time.nu create mode 100644 tests/language/std/date.nu create mode 100644 tests/main.nu diff --git a/assets/core_team.nu b/assets/core_team.nu new file mode 100644 index 0000000..ed623d9 --- /dev/null +++ b/assets/core_team.nu @@ -0,0 +1,12 @@ +def "nu core-team" [] { + [ + [ 'name', 'tz']; + [ 'andres', 'America/Guayaquil'] + [ 'fdncred', 'US/Central'] + [ 'gedge', 'US/Eastern'] + [ 'jt', 'NZ'] + [ 'wycats', 'US/Pacific'] + [ 'kubouch', 'Europe/Helsinki'] + ['elferherrera', 'Europe/London'] + ] +} diff --git a/examples/date_in_local_timezones.nu b/examples/date_in_local_timezones.nu new file mode 100644 index 0000000..2b8eb2c --- /dev/null +++ b/examples/date_in_local_timezones.nu @@ -0,0 +1,25 @@ +source lib/scripts.nu +source assets/core_team.nu + +let next_call = ("2021-08-31 15:00:21.290597200 -05:00" | str to-datetime); +let people = (nu core-team | date local $next_call); + +def say [block] { + each {|person| + do $block ( + $person | merge { + [[name]; [($person.name | str capitalize)]] + } + ) + } | str collect (char newline) +} + +$people | say {|person| $"($person.name)'s timezone is ($person.tz)"} + +$" + +for the next call happening on ($next_call | date format '%c').. in their timezones they would be: + +" + +$people | say {|person| $"($person.name)'s: ($person.time)"} diff --git a/language/playground.nu b/language/playground.nu new file mode 100644 index 0000000..656bc73 --- /dev/null +++ b/language/playground.nu @@ -0,0 +1 @@ +source language/playground/lib.nu \ No newline at end of file diff --git a/language/playground/lib.nu b/language/playground/lib.nu new file mode 100644 index 0000000..3b6f944 --- /dev/null +++ b/language/playground/lib.nu @@ -0,0 +1,55 @@ +def playground [topic, block] { + with-env [N 5 REJECT slow] { + echo $topic " tests" (char newline) | str collect + + do $block + } +} + +def scene [ + topic: any + --tag: string + block: block +] { + $" ($topic)(char newline)" + do $block +} + +def play [ + topic: any + --tag: string + block: block +] { + let title = $topic; + + let is_tag_empty = ($tag | empty?); + let should_run_all = ($nu.env | default RUN_ALL $false | get RUN_ALL); + + if $is_tag_empty { + do $block + } { + if $tag == $nu.env.REJECT && $should_run_all { + $" ($topic) ... (ansi yellow)skipped(ansi reset) (char newline)" + } { do $block } + } +} + +def expect [ + actual: any + --to-be: any +] { + let are_equal = ($actual | zip { $to-be } | pivot header_names values | each {|case| + let values = $case.values; + + $values.0 == $values.1 + } + | all? $it) && (($actual | get | length) == ($to-be | get | length)); + + let line = (if $true == $are_equal { + $"(ansi green)ok(ansi reset)(char newline)" + } { + $"(ansi red)failed(ansi reset)(char newline)" + }); + + $" ($title) ... ($line)" +} \ No newline at end of file diff --git a/language/std.nu b/language/std.nu new file mode 100644 index 0000000..4f3cd15 --- /dev/null +++ b/language/std.nu @@ -0,0 +1 @@ +source language/std/date.nu diff --git a/language/std/date.nu b/language/std/date.nu new file mode 100644 index 0000000..380997e --- /dev/null +++ b/language/std/date.nu @@ -0,0 +1,7 @@ +def "date local" [now] { + insert time {|value| + let converted = ($now | date to-timezone $value.tz); + + $converted | date format '%c' + } +} diff --git a/lib/scripts.nu b/lib/scripts.nu new file mode 100644 index 0000000..1d49ee5 --- /dev/null +++ b/lib/scripts.nu @@ -0,0 +1 @@ +source language/std.nu diff --git a/maintainer_time.nu b/maintainer_time.nu deleted file mode 100644 index 22bc65f..0000000 --- a/maintainer_time.nu +++ /dev/null @@ -1,18 +0,0 @@ -let m_table = ( - [ - ['name', 'tz', 'time']; - ['andres' 'America/Guayaquil' ' '] - ['fdncred' 'US/Central' ' '] - ['gedge' 'US/Eastern' ' '] - ['jt' 'NZ' ' '] - ['wycats' 'US/Pacific' ' '] - ['kubouch' 'Europe/Helsinki' ' '] - ['elferherrera' 'Europe/London' ' '] - ] -) -let now = (date now) -$m_table | update time { - each { |name| - $now | date to-timezone ($name | get tz) | date format '%c' - } -} diff --git a/tests/language/std/date.nu b/tests/language/std/date.nu new file mode 100644 index 0000000..cb8e62b --- /dev/null +++ b/tests/language/std/date.nu @@ -0,0 +1,38 @@ +source lib/scripts.nu + +def mock-now [] { + "2021-08-29 03:31:21.290597200 -05:00" | str to-datetime +} + +def people [] { + [ + [ 'name', 'tz']; + [ 'andres', 'America/Guayaquil'] + [ 'fdncred', 'US/Central'] + ] +} + +playground "std/date" { + + scene 'command: "date local"' { + + play "adds times in local timezone" { + + let expected_times = [ + [time]; + ["Sun Aug 29 03:31:21 2021"] + ["Sun Aug 29 03:31:21 2021"] + ]; + + let actual = (people | + date local (mock-now) | + select time + ); + + expect $actual --to-be $expected_times + + } + + } + +} diff --git a/tests/main.nu b/tests/main.nu new file mode 100644 index 0000000..346b35f --- /dev/null +++ b/tests/main.nu @@ -0,0 +1,2 @@ +source language/playground.nu +source tests/language/std/date.nu