Explorar el Código

Add isa.sh, a Shellfu/sh containing few simple validation helpers

Alois Mahdal hace 7 años
padre
commit
9444d88b80

+ 17
- 0
packaging/debian/control Ver fichero

@@ -174,6 +174,23 @@ Description: Shellfu/sh module for standard exit statuses
174 174
  This sub-package contains 'exit', Shellfu module containing functions
175 175
  and variables for standard exit statuses.
176 176
 
177
+Package: shellfu-sh-isa
178
+Architecture: all
179
+Depends: shellfu-sh
180
+Description: Shellfu/sh module with validation helpers
181
+ Shellfu is an attempt to add modularity to your shell scripts.
182
+ .
183
+ With Shellfu you can develop your shell modules separately from your
184
+ scripts, and test, use, explore or study them without need to be aware
185
+ of details such as where the actual files are placed.
186
+ .
187
+ Shellfu is mostly intended for cases when there's need for non-trivial
188
+ amount of reliable body of shell scripts, and access to advanced modular
189
+ languages such as Python or Perl is limited.
190
+ .
191
+ This sub-package contains 'isa', Shellfu module containing few
192
+ value validation helpers.
193
+
177 194
 Package: shellfu-sh-mdfmt
178 195
 Architecture: all
179 196
 Depends: shellfu-sh

+ 1
- 0
packaging/debian/shellfu-sh-isa.install Ver fichero

@@ -0,0 +1 @@
1
+/usr/share/shellfu/include-sh/isa.sh

+ 10
- 0
packaging/template.spec Ver fichero

@@ -96,6 +96,13 @@ Obsoletes: shellfu-bash-core < 0.10
96 96
 This sub-package contains 'exit', Shellfu module containing functions
97 97
 and variables for standard exit statuses.
98 98
 
99
+%package sh-isa
100
+Summary: Shellfu/sh module with validation helpers
101
+Requires: shellfu-sh
102
+%description sh-isa
103
+This sub-package contains 'isa', Shellfu module containing few
104
+value validation helpers.
105
+
99 106
 %package sh-mdfmt
100 107
 Summary: Markdown formatting helper
101 108
 Requires: shellfu-sh
@@ -165,6 +172,9 @@ make test \
165 172
 %files sh-exit
166 173
 %{_datadir}/%{name}/include-sh/exit.sh
167 174
 
175
+%files sh-isa
176
+%{_datadir}/%{name}/include-sh/isa.sh
177
+
168 178
 %files sh-mdfmt
169 179
 %{_datadir}/%{name}/include-sh/mdfmt.sh
170 180
 

+ 67
- 0
src/include-sh/isa.sh Ver fichero

@@ -0,0 +1,67 @@
1
+#!/bin/sh
2
+
3
+#
4
+# Validation helpers
5
+#
6
+# This module provides several basic validation functions
7
+# to help make code more readable and avoid having to implement
8
+# them (some may be more tricky than they look like).
9
+#
10
+
11
+isa__bool() {
12
+    #
13
+    # True if $1 is a boolean
14
+    #
15
+    case $1 in
16
+        true)   return 0 ;;
17
+        false)  return 0 ;;
18
+        *)      return 1 ;;
19
+    esac
20
+}
21
+
22
+isa__false() {
23
+    #
24
+    # True if $1 is a False boolean
25
+    #
26
+    test "$1" == false
27
+}
28
+
29
+isa__int() {
30
+    #
31
+    # True if $1 is an integer
32
+    #
33
+    # Note that $1 does not have to be decimal; in POSIX shells,
34
+    # eg. 0xf1 is a valid hexadecimal integer and 0755 is a valid
35
+    # octal integer.
36
+    #
37
+    {
38
+        test -z "$1"    && return 1
39
+        test "$1" -ge 0 && return 0
40
+        return 1
41
+    } 2>/dev/null
42
+}
43
+
44
+isa__name() {
45
+    #
46
+    # True if $1 is a name in most languages
47
+    #
48
+    echo "$1" | grep -qx '[[:alpha:]_][[:alnum:]_]*'
49
+}
50
+
51
+isa__posint() {
52
+    #
53
+    # True if $1 is a positive integer
54
+    #
55
+    {
56
+        test -z "$1"    && return 1
57
+        test "$1" -ge 0 && return 0
58
+        return 1
59
+    } 2>/dev/null
60
+}
61
+
62
+isa__true() {
63
+    #
64
+    # True if $1 is a True boolean
65
+    #
66
+    test "$1" == true
67
+}

+ 109
- 0
tests/isa/TF_RUN Ver fichero

@@ -0,0 +1,109 @@
1
+#!/bin/bash
2
+#shellcheck disable=SC1090
3
+
4
+. "$TF_DIR/include/subtest.sh"
5
+. "$TF_DIR/include/tools.sh"
6
+
7
+. "$(sfpath)" || tf_exit_error "failed to init shellfu"
8
+export PRETTY=plain
9
+shellfu import isa    || tf_exit_error "failed to import isa"
10
+
11
+
12
+tf_enum_subtests() {
13
+    local sut
14
+    local ttype
15
+    #
16
+    # isa_
17
+    #
18
+    for sut in isa__bool isa__false isa__int isa__name isa__posint isa__true; do
19
+        for ttype in ok empty none pfx sfx nota; do
20
+            echo "${sut},$ttype"
21
+        done
22
+    done
23
+}
24
+
25
+mkfact() {
26
+    #
27
+    # Make artifact or oracle $what for $TestName
28
+    #
29
+    local what=$1
30
+    case $TestName:$what in
31
+
32
+        isa__bool,ok:arg)       echo true ;;
33
+        isa__bool,pfx:arg)      echo xtrue ;;
34
+        isa__bool,sfx:arg)      echo truex ;;
35
+        isa__bool,nota:arg)     echo hello ;;
36
+        isa__bool,tricky:arg)   echo True ;;
37
+        isa__bool,ok:o_es)      echo 0 ;;
38
+        isa__bool,*:o_es)       echo 1 ;;
39
+
40
+        isa__false,ok:arg)      echo false ;;
41
+        isa__false,pfx:arg)     echo xfalse ;;
42
+        isa__false,sfx:arg)     echo falsex ;;
43
+        isa__false,nota:arg)    echo true ;;
44
+        isa__false,tricky:arg)  echo False ;;
45
+        isa__false,ok:o_es)     echo 0 ;;
46
+        isa__false,*:o_es)      echo 1 ;;
47
+
48
+        isa__true,ok:arg)       echo true ;;
49
+        isa__true,pfx:arg)      echo xtrue ;;
50
+        isa__true,sfx:arg)      echo truex ;;
51
+        isa__true,nota:arg)     echo True ;;
52
+        isa__true,tricky:arg)   echo 0 ;;
53
+        isa__true,ok:o_es)      echo 0 ;;
54
+        isa__true,*:o_es)       echo 1 ;;
55
+
56
+        isa__int,ok:arg)        echo 42 ;;
57
+        isa__int,pfx:arg)       echo x42 ;;
58
+        isa__int,sfx:arg)       echo 42x ;;
59
+        isa__int,nota:arg)      echo 22.2 ;;
60
+        isa__int,tricky:arg)    echo \ 42 ;;
61
+        isa__int,ok:o_es)       echo 0 ;;
62
+        isa__int,*:o_es)        echo 1 ;;
63
+
64
+        isa__posint,ok:arg)     echo 42 ;;
65
+        isa__posint,pfx:arg)    echo x42 ;;
66
+        isa__posint,sfx:arg)    echo 42x ;;
67
+        isa__posint,nota:arg)   echo -42 ;;
68
+        isa__posint,tricky:arg) echo 42 + 55 ;;
69
+        isa__posint,ok:o_es)    echo 0 ;;
70
+        isa__posint,*:o_es)     echo 1 ;;
71
+
72
+        isa__name,ok:arg)       echo foo5az ;;
73
+        isa__name,pfx:arg)      echo .foo5az ;;
74
+        isa__name,sfx:arg)      echo foo5az/ ;;
75
+        isa__name,nota:arg)     echo 55zoos ;;
76
+        isa__name,tricky:arg)   echo foo bar ;;
77
+        isa__name,ok:o_es)      echo 0 ;;
78
+        isa__name,*:o_es)       echo 1 ;;
79
+
80
+    esac
81
+}
82
+
83
+mkresult() {
84
+    local sut
85
+    local ttype
86
+    local args=()
87
+    ttype=${TestName##*,}
88
+    sut=${TestName%,$ttype}
89
+    type -t "$sut" >/dev/null || {
90
+        tf_exit_error "no such function: $sut"
91
+    }
92
+    #shellcheck disable=SC2046
93
+    case $ttype in
94
+        none)   : ;;
95
+        empty)  args=("") ;;
96
+        more)   args=(foo bar) ;;
97
+        *)      readarray -t args <<<"$(mkfact arg)" ;;
98
+    esac
99
+    "$sut" "${args[@]}"
100
+}
101
+
102
+tf_do_subtest() {
103
+    local TestName=$1
104
+    local o_es
105
+    o_es=$(mkfact o_es)
106
+    tf_testflt -n "$TestName" -S "$o_es" "mkresult"
107
+}
108
+
109
+tf_do_subtests