1#!/usr/bin/env bash
2# CLI lists and expand
3# See https://github.com/clicon/clixon/issues/107
4# The following four tests are made:
5#   1. show apple ?
6#   2. show apple <cr>
7#   3. show apple size 42<cr>
8#   4. show auto ?
9# Variations (on/off with the above tests):
10# 1. In the cli.spec, with/without a "semicolon" after the first "fruit <name>" command
11# 2. With/without an expansion function (-e) introducing an "auto" expansion fruit
12# 3. Set/reset Preference (-P) meaning to choose first of several alternatives w equal preference,
13#    breaking "mabiguous command"
14#
15# The test matrix is therefore:
16# 4 tests x 2 expansion x 2 preference x 2 semicolon = 32 tests in total
17# Note that the results are modelled after how cligen 4.4 behaves, ie to be backward compatible.
18# There are some questionable results but deal with that later.
19
20# Magic line must be first in script (see README.md)
21s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
22
23fspec=$dir/spec.cli
24
25# WITH SEMICOLON (ie command after fruit apple;)
26cat > $fspec <<EOF
27  prompt="cli> ";             # Assignment of prompt
28  comment="#";                # Same comment as in syntax
29  treename="example";         # Name of syntax (used when referencing)
30
31   fruit (<name:string>|<name:string expand()>),callback();{
32      taste (<taste:string>|<taste:string expand()>),callback();
33      size (<size:string>|<size:string expand()>),callback();
34   }
35EOF
36
37newtest "$cligen_file -f $fspec"
38# First semicolon and "fruit apple ?"
39CMD="fruit apple"
40newtest "cligen $CMD ?"
41expectpart "$(echo -n "$CMD ?" | $cligen_file -f $fspec)" 0 "cli> $CMD" "<cr>" "--not--" "size" "taste"
42
43newtest "cligen -P $CMD ?"
44expectpart "$(echo -n "$CMD ?" | $cligen_file -P -f $fspec)" 0 "cli> $CMD" "<cr>" "--not--" "size" "taste"
45
46newtest "cligen -e $CMD ?"
47expectpart "$(echo -n "$CMD ?" | $cligen_file -e -f $fspec)" 0 "cli> $CMD" "<cr>" "size" "taste"
48
49newtest "cligen -e -P $CMD ?"
50expectpart "$(echo -n "$CMD ?" | $cligen_file -e -P -f $fspec)" 0 "cli> $CMD" "<cr>" "size" "taste"
51
52# Second: semicolon and "fruit apple<cr>"
53CMD="fruit apple"
54newtest "cligen $CMD"
55expectpart "$(echo "$CMD" | $cligen_file -f $fspec)" 0 "cli> $CMD" "Ambiguous command"
56
57newtest "cligen -P $CMD"
58expectpart "$(echo "$CMD" | $cligen_file -P -f $fspec 2>&1)" 0 "cli> $CMD" "1 name:fruit type:string value:fruit" "2 name:name type:string value:apple"
59
60newtest "cligen -e $CMD"
61expectpart "$(echo "$CMD" | $cligen_file -e -f $fspec 2>&1)" 0 "cli> $CMD" "1 name:fruit type:string value:fruit" "2 name:name type:string value:apple"
62
63newtest "cligen -e -P $CMD"
64expectpart "$(echo "$CMD" | $cligen_file -e -P -f $fspec 2>&1)" 0 "cli> $CMD" "1 name:fruit type:string value:fruit" "2 name:name type:string value:apple"
65
66# Third: semicolon and "fruit apple size 42<cr>"
67CMD="fruit apple size 42"
68newtest "cligen $CMD"
69expectpart "$(echo "$CMD" | $cligen_file -f $fspec)" 0 "cli> $CMD" 'CLI syntax error in: "fruit apple size 42": Unknown command'
70
71newtest "cligen -P $CMD"
72expectpart "$(echo "$CMD" | $cligen_file -P -f $fspec 2>&1)" 0 "cli> $CMD" 'CLI syntax error in: "fruit apple size 42": Unknown command'
73
74newtest "cligen -e $CMD"
75expectpart "$(echo "$CMD" | $cligen_file -e -f $fspec 2>&1)" 0 "cli> $CMD" "1 name:fruit type:string value:fruit" "2 name:name type:string value:apple" "3 name:size type:string value:size" "4 name:size type:string value:42"
76
77newtest "cligen -e -P $CMD"
78expectpart "$(echo "$CMD" | $cligen_file -e -P -f $fspec 2>&1)" 0 "cli> $CMD" "1 name:fruit type:string value:fruit" "2 name:name type:string value:apple" "3 name:size type:string value:size" "4 name:size type:string value:42"
79
80# Fourth: semicolon and "fruit auto ?"
81# without -e should be same as "first" above
82CMD="fruit auto"
83newtest "cligen $CMD ?"
84expectpart "$(echo -n "$CMD ?" | $cligen_file -f $fspec)" 0 "cli> $CMD" "<cr>" "--not--" "size" "taste"
85
86newtest "cligen -P $CMD ?"
87expectpart "$(echo -n "$CMD ?" | $cligen_file -P -f $fspec)" 0 "cli> $CMD" "<cr>" "--not--" "size" "taste"
88
89newtest "cligen -e $CMD ?"
90expectpart "$(echo -n "$CMD ?" | $cligen_file -e -f $fspec)" 0 "cli> $CMD" "<cr>" "size" "taste"
91
92newtest "cligen -e -P $CMD ?"
93expectpart "$(echo -n "$CMD ?" | $cligen_file -e -P -f $fspec)" 0 "cli> $CMD " "<cr>" "size" "taste"
94
95# WITHOUT SEMICOLON (ie no stand-alone command at fruit apple)
96cat > $fspec <<EOF
97  prompt="cli> ";             # Assignment of prompt
98  comment="#";                # Same comment as in syntax
99  treename="example";         # Name of syntax (used when referencing)
100
101   fruit (<name:string>|<name:string expand()>){
102      taste (<taste:string>|<taste:string expand()>),callback();
103      size (<size:string>|<size:string expand()>),callback();
104   }
105EOF
106
107# Fifth no semicolon and "fruit apple ?"
108CMD="fruit apple"
109
110newtest "cligen $CMD ?"
111# This seems wrong, why does it give <cr>??
112expectpart "$(echo -n "$CMD ?" | $cligen_file -f $fspec)" 0 "cli> $CMD" "<cr>" "--not--" "size" "taste"
113
114newtest "cligen -P $CMD ?"
115expectpart "$(echo -n "$CMD ?" | $cligen_file -P -f $fspec)" 0 "cli> $CMD" "--not--" "<cr>" "size" "taste"
116
117newtest "cligen -e $CMD ?"
118expectpart "$(echo -n "$CMD ?" | $cligen_file -e -f $fspec)" 0 "cli> $CMD" "size" "taste" "--not--" "<cr>"
119
120newtest "cligen -e -P $CMD ?"
121expectpart "$(echo -n "$CMD ?" | $cligen_file -e -P -f $fspec)" 0 "cli> $CMD" "size" "taste" "--not--" "<cr>"
122
123# Sixth: no semicolon and "fruit apple<cr>"
124CMD="fruit apple"
125newtest "cligen $CMD"
126expectpart "$(echo "$CMD" | $cligen_file -f $fspec)" 0 "cli> $CMD" "Ambiguous command"
127
128newtest "cligen -P $CMD"
129expectpart "$(echo "$CMD" | $cligen_file -P -f $fspec 2>&1)" 0 "cli> $CMD" 'CLI syntax error in: "fruit apple": Incomplete command'
130
131newtest "cligen -e $CMD"
132expectpart "$(echo "$CMD" | $cligen_file -e -f $fspec 2>&1)" 0 "cli> $CMD" 'CLI syntax error in: "fruit apple": Incomplete command'
133
134newtest "cligen -e -P $CMD"
135expectpart "$(echo "$CMD" | $cligen_file -e -P -f $fspec 2>&1)" 0 "cli> $CMD" 'CLI syntax error in: "fruit apple": Incomplete command'
136
137# Seventh: no semicolon and "fruit apple size 42<cr>"
138CMD="fruit apple size 42"
139newtest "cligen $CMD"
140expectpart "$(echo "$CMD" | $cligen_file -f $fspec)" 0 "cli> $CMD" 'CLI syntax error in: "fruit apple size 42": Unknown command'
141
142newtest "cligen -P $CMD"
143expectpart "$(echo "$CMD" | $cligen_file -P -f $fspec 2>&1)" 0 "cli> $CMD" 'CLI syntax error in: "fruit apple size 42": Unknown command'
144
145newtest "cligen -e $CMD"
146expectpart "$(echo "$CMD" | $cligen_file -e -f $fspec 2>&1)" 0 "cli> $CMD" "1 name:fruit type:string value:fruit" "2 name:name type:string value:apple" "3 name:size type:string value:size" "4 name:size type:string value:42"
147
148newtest "cligen -e -P $CMD"
149expectpart "$(echo "$CMD" | $cligen_file -e -P -f $fspec 2>&1)" 0 "cli> $CMD" "1 name:fruit type:string value:fruit" "2 name:name type:string value:apple" "3 name:size type:string value:size" "4 name:size type:string value:42"
150
151# Eigth: no semicolon and "fruit auto ?"
152# without -e should be same as "fifth" above
153CMD="fruit auto"
154
155newtest "cligen $CMD ?"
156# This seems wrong, why does it give <cr>??
157expectpart "$(echo -n "$CMD ?" | $cligen_file -f $fspec)" 0 "cli> $CMD" "<cr>" "--not--" "size" "taste"
158
159newtest "cligen -P $CMD ?"
160expectpart "$(echo -n "$CMD ?" | $cligen_file -P -f $fspec)" 0 "cli> $CMD" "--not--" "<cr>" "size" "taste"
161
162newtest "cligen -e $CMD ?"
163expectpart "$(echo -n "$CMD ?" | $cligen_file -e -f $fspec)" 0 "cli> $CMD" "size" "taste" "--not--" "<cr>"
164
165newtest "cligen -e -P $CMD ?"
166expectpart "$(echo -n "$CMD ?" | $cligen_file -e -P -f $fspec)" 0 "cli> $CMD" "size" "taste" "--not--" "<cr>"
167
168endtest
169
170rm -rf $dir
171