From 2bc8826a97929b1878375a5c747e5c4cfed2b10b Mon Sep 17 00:00:00 2001
From: Jan Philipp Hafer <jan.hafer@rwth-aachen.de>
Date: Wed, 30 Dec 2020 11:22:48 +0100
Subject: [PATCH] fix(manual, testing): unit tests for correctly working
 sections+examples in manual chapter expansion

---
 manual/src/expansions/01-variable.md | 31 ++++++++-----------
 manual/src/expansions/02-process.md  | 15 ++-------
 manual/src/expansions/03-brace.md    | 46 +++++++---------------------
 tests/brace_exp.ion                  | 24 +++++++++++++++
 tests/brace_exp.out                  | 30 ++++++++++++++++++
 tests/glob.out                       |  2 +-
 tests/process_exp.ion                | 10 ++++++
 tests/process_exp.out                |  5 +++
 tests/variable_exp.ion               | 20 ++++++++++++
 tests/variable_exp.out               | 13 ++++++++
 10 files changed, 128 insertions(+), 68 deletions(-)
 create mode 100644 tests/brace_exp.ion
 create mode 100644 tests/brace_exp.out
 create mode 100644 tests/process_exp.ion
 create mode 100644 tests/process_exp.out
 create mode 100644 tests/variable_exp.ion
 create mode 100644 tests/variable_exp.out

diff --git a/manual/src/expansions/01-variable.md b/manual/src/expansions/01-variable.md
index 2b0c1923..d1c25e64 100644
--- a/manual/src/expansions/01-variable.md
+++ b/manual/src/expansions/01-variable.md
@@ -13,18 +13,12 @@ expansion. If the character that follows is an accepted Unicode character, all c
 follow will be collected until either a non-accepted Unicode character is found, or all characters
 have been read. Then the characters that were collected will be used as the name of the string
 variable to substitute with.
-
 ```sh
-let string = "example string"
-echo $string
-
-echo $string:$string
+{{#include ../../../tests/variable_exp.ion:string_variables}}
 ```
 ```txt
-example string
-example string:example string
+{{#include ../../../tests/variable_exp.out:string_variables}}
 ```
-
 **NOTE:**
 - Accepted characters are **unicode** alphanumeric characters and **_**.
 
@@ -34,15 +28,12 @@ Unlike POSIX, Ion also offers support for first class arrays, which are denoted
 sigil. The rules for these are identical, but instead of returning a single string, it will
 return an array of strings. This means that it's possible to use an array variable as arguments
 in a command, as each element in the array will be treated as a separate shell word.
-
 ```sh
-let array = [one two three]
-echo @array
+{{#include ../../../tests/variable_exp.ion:array_variables}}
 ```
 ```txt
-one two three
+{{#include ../../../tests/variable_exp.out:array_variables}}
 ```
-
 However, do note that double-quoted arrays are coerced into strings, with spaces separating each
 element. It is equivalent to using the `$join(array)` method. Containing multiple arrays within
 double quotes is therefore equivalent to folding the elements into a single string.
@@ -51,17 +42,19 @@ double quotes is therefore equivalent to folding the elements into a single stri
 
 Braces can also be used when you need to integrate a variable expansion along accepted Unicode
 characters.
-
 ```sh
-echo ${hello}world
-echo @{hello}world
+{{#include ../../../tests/variable_exp.ion:braced_variables}}
+```
+```txt
+{{#include ../../../tests/variable_exp.out:braced_variables}}
 ```
 
 ## Aliases
-
 Ion also supports aliasing commands, which can be defined using the `alias` builtin. Aliases
 are often used as shortcuts to repetitive command invocations.
-
 ```sh
-alias ls = "ls --color"
+{{#include ../../../tests/variable_exp.ion:aliases}}
+```
+```txt
+{{#include ../../../tests/variable_exp.out:aliases}}
 ```
diff --git a/manual/src/expansions/02-process.md b/manual/src/expansions/02-process.md
index 8423f5ff..d7ef76d6 100644
--- a/manual/src/expansions/02-process.md
+++ b/manual/src/expansions/02-process.md
@@ -13,20 +13,9 @@ let array = [ @(cmd args...) ]
 **NOTES:**
 - To split outputs by line, see [@lines($(cmd))](https://doc.redox-os.org/ion-manual/html/expansions/05-method.html#lines).
 - `@(cmd)` is equivalent to [@split($(cmd))](https://doc.redox-os.org/ion-manual/html/expansions/05-method.html#split).
-
-
 ```sh
-mkdir -p _tmp _tmp/t1 _tmp/t2
-cd _tmp
-let res = $(ls)
-let res2 = [ @(ls) ]
-echo $res    # output the string
-echo @res2     # output the array
-cd ..
-rm -fr _tmp
+{{#include ../../../tests/process_exp.ion:process_expansion}}
 ```
 ```txt
-t1
-t2
-t1 t2
+{{#include ../../../tests/process_exp.out:process_expansion}}
 ```
diff --git a/manual/src/expansions/03-brace.md b/manual/src/expansions/03-brace.md
index 4a422f23..7b04aaf5 100644
--- a/manual/src/expansions/03-brace.md
+++ b/manual/src/expansions/03-brace.md
@@ -7,65 +7,41 @@ non-whitespace characters connected to brace expansions will also be included wi
 permutations.
 
 **NOTE:** Brace expansions will not work within double quotes.
-
 ```sh
-echo filename.{ext1,ext2}
+{{#include ../../../tests/brace_exp.ion:single_brace_expansion}}
 ```
 ```txt
-filename.ext1 filename.ext2
+{{#include ../../../tests/brace_exp.out:single_brace_expansion}}
 ```
 
 Multiple brace tokens may occur within a braced collection, where each token expands the
 possible permutation variants.
-
 ```sh
-echo job_{01,02}.{ext1,ext2}
+{{#include ../../../tests/brace_exp.ion:multi_brace_expansion}}
 ```
 ```txt
-job_01.ext1 job_01.ext2 job_02.ext1 job_02.ext2
+{{#include ../../../tests/brace_exp.out:multi_brace_expansion}}
 ```
-
 Brace tokens may even contain brace tokens of their own, as each brace element will also be
 expanded.
-
 ```sh
-echo job_{01_{out,err},02_{out,err}}.txt
+{{#include ../../../tests/brace_exp.ion:nested_brace_expansion}}
 ```
 ```txt
-job_01_out.txt job_01_err.txt job_02_out.txt job_02_err.txt
+{{#include ../../../tests/brace_exp.out:nested_brace_expansion}}
 ```
-
 Braces elements may also be designated as ranges, which may be either inclusive or exclusive,
 descending or ascending, numbers or latin alphabet characters.
-
 ```sh
-echo {1..10}
-echo {10..1}
-echo {1...10}
-echo {10...1}
-echo {a..d}
-echo {d..a}
-echo {a...d}
-echo {d...a}
+{{#include ../../../tests/brace_exp.ion:range_brace_expansion}}
 ```
 ```txt
-1 2 3 4 5 6 7 8 9
-10 9 8 7 6 5 4 3 2
-1 2 3 4 5 6 7 8 9 10
-10 9 8 7 6 5 4 3 2 1
-a b c
-d c b
-a b c d
-d c b a
+{{#include ../../../tests/brace_exp.out:range_brace_expansion}}
 ```
-
-It's also important to note that, as brace expansions return arrays, they may be used in for loops.
-
+It's also important to note that, as range brace expansions return arrays, they may be used in for loops.
 ```sh
-for num in {1..10}
-    echo $num
-end
+{{#include ../../../tests/brace_exp.ion:range_brace_expansion_as_array}}
 ```
 ```txt
-1 2 3 4 5 6 7 8 9
+{{#include ../../../tests/brace_exp.out:range_brace_expansion_as_array}}
 ```
diff --git a/tests/brace_exp.ion b/tests/brace_exp.ion
new file mode 100644
index 00000000..cdb33f81
--- /dev/null
+++ b/tests/brace_exp.ion
@@ -0,0 +1,24 @@
+echo '# ANCHOR: single_brace_expansion'
+echo filename.{ext1,ext2}
+echo '# ANCHOR_END: single_brace_expansion'
+echo '# ANCHOR: multi_brace_expansion'
+echo job_{01,02}.{ext1,ext2}
+echo '# ANCHOR_END: multi_brace_expansion'
+echo '# ANCHOR: nested_brace_expansion'
+echo job_{01_{out,err},02_{out,err}}.txt
+echo '# ANCHOR_END: nested_brace_expansion'
+echo '# ANCHOR: range_brace_expansion'
+echo {1..10}
+echo {10..1}
+echo {1...10}
+echo {10...1}
+echo {a..d}
+echo {d..a}
+echo {a...d}
+echo {d...a}
+echo '# ANCHOR_END: range_brace_expansion'
+echo '# ANCHOR: range_brace_expansion_as_array'
+for num in {1..10}
+    echo $num
+end
+echo '# ANCHOR_END: range_brace_expansion_as_array'
diff --git a/tests/brace_exp.out b/tests/brace_exp.out
new file mode 100644
index 00000000..cffa2456
--- /dev/null
+++ b/tests/brace_exp.out
@@ -0,0 +1,30 @@
+# ANCHOR: single_brace_expansion
+filename.ext1 filename.ext2
+# ANCHOR_END: single_brace_expansion
+# ANCHOR: multi_brace_expansion
+job_01.ext1 job_01.ext2 job_02.ext1 job_02.ext2
+# ANCHOR_END: multi_brace_expansion
+# ANCHOR: nested_brace_expansion
+job_01_out.txt job_01_err.txt job_02_out.txt job_02_err.txt
+# ANCHOR_END: nested_brace_expansion
+# ANCHOR: range_brace_expansion
+1 2 3 4 5 6 7 8 9
+10 9 8 7 6 5 4 3 2
+1 2 3 4 5 6 7 8 9 10
+10 9 8 7 6 5 4 3 2 1
+a b c
+d c b
+a b c d
+d c b a
+# ANCHOR_END: range_brace_expansion
+# ANCHOR: range_brace_expansion_as_array
+1
+2
+3
+4
+5
+6
+7
+8
+9
+# ANCHOR_END: range_brace_expansion_as_array
diff --git a/tests/glob.out b/tests/glob.out
index 9b348c15..735b7c91 100644
--- a/tests/glob.out
+++ b/tests/glob.out
@@ -1,5 +1,5 @@
 Cargo.toml
-tests/braces.ion tests/braces.out tests/break.ion tests/break.out
+tests/brace_exp.ion tests/brace_exp.out tests/braces.ion tests/braces.out tests/break.ion tests/break.out
 Cargo.toml
 Cargo.toml
 Cargo.lock Cargo.toml
diff --git a/tests/process_exp.ion b/tests/process_exp.ion
new file mode 100644
index 00000000..58fe9fd5
--- /dev/null
+++ b/tests/process_exp.ion
@@ -0,0 +1,10 @@
+echo '# ANCHOR: process_expansion'
+mkdir -p _tmp _tmp/t1 _tmp/t2
+cd _tmp
+let res = $(ls)
+let res2 = [ @(ls) ]
+echo $res    # output the string
+echo @res2     # output the array
+cd ..
+rm -fr _tmp
+echo '# ANCHOR_END: process_expansion'
diff --git a/tests/process_exp.out b/tests/process_exp.out
new file mode 100644
index 00000000..3488d82f
--- /dev/null
+++ b/tests/process_exp.out
@@ -0,0 +1,5 @@
+# ANCHOR: process_expansion
+t1
+t2
+t1 t2
+# ANCHOR_END: process_expansion
diff --git a/tests/variable_exp.ion b/tests/variable_exp.ion
new file mode 100644
index 00000000..c95d2e71
--- /dev/null
+++ b/tests/variable_exp.ion
@@ -0,0 +1,20 @@
+echo '# ANCHOR: string_variables'
+let string = "example string"
+echo $string
+echo $string:$string
+echo '# ANCHOR_END: string_variables'
+echo '# ANCHOR: array_variables'
+let array = [one two three]
+echo @array
+echo '# ANCHOR_END: array_variables'
+echo '# ANCHOR: braced_variables'
+let hello = "hello123"
+echo ${hello}world
+let hello = [hello 123 ' ']
+echo @{hello}world
+echo '# ANCHOR_END: braced_variables'
+echo '# ANCHOR: aliases'
+alias ls = "ls --color"
+#echo $ls #ion: expansion error: Variable does not exist
+#aliase are stored separately
+echo '# ANCHOR_END: aliases'
diff --git a/tests/variable_exp.out b/tests/variable_exp.out
new file mode 100644
index 00000000..32717e94
--- /dev/null
+++ b/tests/variable_exp.out
@@ -0,0 +1,13 @@
+# ANCHOR: string_variables
+example string
+example string:example string
+# ANCHOR_END: string_variables
+# ANCHOR: array_variables
+one two three
+# ANCHOR_END: array_variables
+# ANCHOR: braced_variables
+hello123world
+hello 123  world
+# ANCHOR_END: braced_variables
+# ANCHOR: aliases
+# ANCHOR_END: aliases
-- 
GitLab