Skip to content

Commit

Permalink
♻️ replace string with array::fuzzyFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaillon committed Apr 18, 2024
1 parent 1678330 commit a51650c
Show file tree
Hide file tree
Showing 21 changed files with 242 additions and 256 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ Please check the [CONTRIBUTING.md][contributing] documentation if you intend to
- for yes/no, add param to choose the default option.
- Replace awk with bash.
- Provide an alternative bash function if diff is not found.
- Allow to separate commands from options/arguments with `--`.

[releases]: https://github.com/jcaillon/valet/releases
[latest-release]: https://github.com/jcaillon/valet/releases/latest
Expand Down
54 changes: 28 additions & 26 deletions tests.d/1000-core-functions/00.tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,60 @@ There were 2 new lines before this."
string::wrapText "${shortText}" 30 && echo "${LAST_RETURNED_VALUE}"
endTest "Wrapping text at column 30 with no padding" 0


echo "→ string::wrapText \"\${shortText}\" 90 4 false"
echo "------------------------------------------------------------------------------------------"
string::wrapText "${shortText}" 90 4 false && echo "${LAST_RETURNED_VALUE}"
endTest "Wrapping text at column 90 with padding of 4 on new lines" 0


echo "→ string::wrapText \"\${shortText}\" 90 2 true"
echo "------------------------------------------------------------------------------------------"
string::wrapText "${shortText}" 90 2 true && echo "${LAST_RETURNED_VALUE}"
endTest "Wrapping text at column 90 with padding of 2 on all lines" 0
}

function testString::fuzzyMatch() {
local lines="l1 this is a word
l2 very unbelievable
l2 unbelievable
l3 self mock1
l4 self mock2
l5 ublievable"
function testArray::fuzzyFilter() {
lines=("this is a word"
"very unbelievable"
"unbelievable"
"self mock1"
"self mock2"
"ublievable")

echo "lines=\"${lines}\""
declare -p lines

echo
echo "→ string::fuzzyMatch evle \"\${lines}\""
string::fuzzyMatch "evle" "${lines}" && echo "${LAST_RETURNED_VALUE}"
echo "→ array::fuzzyFilter evle lines"
array::fuzzyFilter "evle" lines
declare -p LAST_RETURNED_ARRAY_VALUE LAST_RETURNED_ARRAY_VALUE2 LAST_RETURNED_ARRAY_VALUE3

echo
echo "→ string::fuzzyMatch sh2 \"\${lines}\""
string::fuzzyMatch "sc2" "${lines}" && echo "${LAST_RETURNED_VALUE}"
echo "→ array::fuzzyFilter SC2 lines"
array::fuzzyFilter "SC2" lines
declare -p LAST_RETURNED_ARRAY_VALUE LAST_RETURNED_ARRAY_VALUE2 LAST_RETURNED_ARRAY_VALUE3

echo
echo "# should prioritize lower index of u"
echo "→ string::fuzzyMatch u \"\${lines}\""
string::fuzzyMatch "u" "${lines}" && echo "${LAST_RETURNED_VALUE}"
echo "→ array::fuzzyFilter u lines"
array::fuzzyFilter "u" lines
declare -p LAST_RETURNED_ARRAY_VALUE LAST_RETURNED_ARRAY_VALUE2 LAST_RETURNED_ARRAY_VALUE3

echo
echo "# should be the first equal match"
echo "→ string::fuzzyMatch self \"\${lines}\""
string::fuzzyMatch "self" "${lines}" && echo "${LAST_RETURNED_VALUE}"
echo "→ array::fuzzyFilter seLf lines"
array::fuzzyFilter "seLf" lines
declare -p LAST_RETURNED_ARRAY_VALUE LAST_RETURNED_ARRAY_VALUE2 LAST_RETURNED_ARRAY_VALUE3

echo
echo "# should prioritize lower distance between letters"
echo "→ string::fuzzyMatch lubl \"\${lines}\""
string::fuzzyMatch "lubl" "${lines}" && echo "${LAST_RETURNED_VALUE}"
echo "→ array::fuzzyFilter nomatch lines"
array::fuzzyFilter "nomatch" lines
declare -p LAST_RETURNED_ARRAY_VALUE LAST_RETURNED_ARRAY_VALUE2 LAST_RETURNED_ARRAY_VALUE3

unset lines

endTest "Testing string::fuzzyMatch" 0
endTest "Testing array::fuzzyFilter" 0
}

function main() {
testString::wrapText
testString::fuzzyMatch
testArray::fuzzyFilter
}

main
main
52 changes: 27 additions & 25 deletions tests.d/1000-core-functions/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,36 +98,38 @@ Exit code: `0`
There were 2 new lines before this.
```

### Testing string::fuzzyMatch
### Testing array::fuzzyFilter

Exit code: `0`

**Standard** output:

```plaintext
lines="l1 this is a word
l2 very unbelievable
l2 unbelievable
l3 self mock1
l4 self mock2
l5 ublievable"
→ string::fuzzyMatch evle "${lines}"
l2 very unbelievable
→ string::fuzzyMatch sh2 "${lines}"
l4 self mock2
# should prioritize lower index of u
→ string::fuzzyMatch u "${lines}"
l2 unbelievable
# should be the first equal match
→ string::fuzzyMatch self "${lines}"
l3 self mock1
# should prioritize lower distance between letters
→ string::fuzzyMatch lubl "${lines}"
l5 ublievable
declare -a lines=([0]="this is a word" [1]="very unbelievable" [2]="unbelievable" [3]="self mock1" [4]="self mock2" [5]="ublievable")
→ array::fuzzyFilter evle lines
declare -a LAST_RETURNED_ARRAY_VALUE=([0]="very unbelievable" [1]="unbelievable" [2]="ublievable")
declare -a LAST_RETURNED_ARRAY_VALUE2=([0]="1" [1]="3" [2]="4")
declare -a LAST_RETURNED_ARRAY_VALUE3=([0]="1" [1]="1" [2]="1")
→ array::fuzzyFilter SC2 lines
declare -a LAST_RETURNED_ARRAY_VALUE=([0]="self mock2")
declare -a LAST_RETURNED_ARRAY_VALUE2=([0]="0")
declare -a LAST_RETURNED_ARRAY_VALUE3=([0]="2")
→ array::fuzzyFilter u lines
declare -a LAST_RETURNED_ARRAY_VALUE=([0]="very unbelievable" [1]="unbelievable" [2]="ublievable")
declare -a LAST_RETURNED_ARRAY_VALUE2=([0]="5" [1]="0" [2]="0")
declare -a LAST_RETURNED_ARRAY_VALUE3=([0]="5" [1]="0" [2]="0")
→ array::fuzzyFilter seLf lines
declare -a LAST_RETURNED_ARRAY_VALUE=([0]="self mock1" [1]="self mock2")
declare -a LAST_RETURNED_ARRAY_VALUE2=([0]="0" [1]="0")
declare -a LAST_RETURNED_ARRAY_VALUE3=([0]="1" [1]="1")
→ array::fuzzyFilter nomatch lines
declare -a LAST_RETURNED_ARRAY_VALUE=()
declare -a LAST_RETURNED_ARRAY_VALUE2=()
declare -a LAST_RETURNED_ARRAY_VALUE3=()
```

6 changes: 3 additions & 3 deletions tests.d/1001-main-functions/99.tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ function testGetMaxPossibleCommandLevel() {
function testFuzzyFindOption() {

echo "→ main::fuzzyFindOption '--opt1 --derp2 --allo3' 'de'"
main::fuzzyFindOption "--opt1 --derp2 --allo3" "de" && echo "${LAST_RETURNED_VALUE}"
main::fuzzyFindOption de --opt1 --derp2 --allo3 && echo "${LAST_RETURNED_VALUE}"

echo
echo "→ main::fuzzyFindOption '--opt1 --derp2 --allo3' '-a'"
main::fuzzyFindOption "--opt1 --derp2 --allo3" "-a" && echo "${LAST_RETURNED_VALUE}"
main::fuzzyFindOption -a --opt1 --derp2 --allo3 && echo "${LAST_RETURNED_VALUE}"

echo
echo "→ main::fuzzyFindOption '--opt1 --derp2 --allo3' 'thing'"
main::fuzzyFindOption "--opt1 --derp2 --allo3" "thing" && echo "${LAST_RETURNED_VALUE}"
main::fuzzyFindOption thing --opt1 --derp2 --allo3 && echo "${LAST_RETURNED_VALUE}"

endTest "Testing main::fuzzyFindOption" 0
}
Expand Down
7 changes: 3 additions & 4 deletions tests.d/1001-main-functions/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,15 @@ selfBuild
self build
→ main::fuzzyMatchCommandtoFunctionName 'sf' 'nop' 'other' 'stuff' 'dont care'
_menu
1
self
0
```

**Error** output:

```log
INFO Fuzzy matching the command ⌜se bu⌝ to ⌜self build⌝.
INFO Fuzzy matching the command ⌜sf⌝ to ⌜self⌝.
```

### Testing main::getMaxPossibleCommandLevel
Expand Down
7 changes: 5 additions & 2 deletions tests.d/1002-lib-kurl/00.kurl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ function testKurl::toFile() {
kurl::toFile false '' "${tmpFile}" --code 400 --error https://hello.com/bla --otherOpt && exitCode=0 || exitCode=$?
echoOutputKurlToFile ${exitCode} "${tmpFile}"
endTest "Testing kurl::toFile, testing debug mode https code 400" ${exitCode}
log::setLevel info

echo "→ kurl::toFile false '' \"\${tmpFile}\" --code 200 http://hello.com"
log::setLevel debug
kurl::toFile false '' "${tmpFile}" --code 200 http://hello.com && exitCode=0 || exitCode=$?
echoOutputKurlToFile ${exitCode} "${tmpFile}"
endTest "Testing kurl::toFile, testing debug mode http code 200" ${exitCode}

log::setLevel info
}

function echoOutputKurlToFile() {
Expand Down Expand Up @@ -70,7 +71,9 @@ function testKurl::toVar() {
endTest "Testing kurl, with no content http code 200" ${exitCode}

echo "→ kurl::toVar false '' --code 500 http://hello.com"
export GLOBAL_ERROR_DISPLAYED=1
(kurl::toVar true '' --code 500 http://hello.com) && exitCode=0 || exitCode=$?
unset GLOBAL_ERROR_DISPLAYED
endTest "Testing kurl, with no content http code 500, fails" ${exitCode}

unset NO_CURL_CONTENT
Expand All @@ -81,7 +84,7 @@ function testKurl::toVar() {
kurl::toVar false '' --code 400 http://hello.com && exitCode=0 || exitCode=$?
echoOutputKurlToVar ${exitCode}
endTest "Testing kurl, debug mode, with content http code 400" ${exitCode}

log::setLevel info
}

function echoOutputKurlToVar() {
Expand Down
49 changes: 0 additions & 49 deletions tests.d/1002-lib-kurl/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,26 +168,6 @@ stderr:
```

**Error** output:

```log
DEBUG Executing the command ⌜curl⌝.
Fail if it fails: ⌜false⌝
Acceptable error codes: ⌜0⌝
Standard stream from file: ⌜false⌝
Standard stream: ⌜⌝
Extra parameters: ⌜--silent --show-error --location --write-out %{http_code} --output /tmp/valet-work --code 200 http://hello.com⌝
DEBUG The command ⌜curl⌝ originally ended with exit code ⌜0⌝.
The error code ⌜0⌝ is acceptable and has been reset to 0.
Standard output:
⌜200⌝
Error output:
⌜▶ called curl --silent --show-error --location --write-out %{http_code} --output /tmp/valet-work --code 200 http://hello.com
DEBUG The curl command for url ⌜http://hello.com⌝ ended with exit code ⌜0⌝, the http return code was ⌜200⌝.
DEBUG The http return code ⌜200⌝ is acceptable and exit code has been reset to 0 from ⌜0⌝.
```

### Testing kurl, with no content http code 500, fails

Exit code: `1`
Expand All @@ -201,39 +181,10 @@ Exit code: `1`
**Error** output:

```log
DEBUG Executing the command ⌜curl⌝.
Fail if it fails: ⌜false⌝
Acceptable error codes: ⌜0⌝
Standard stream from file: ⌜false⌝
Standard stream: ⌜⌝
Extra parameters: ⌜--silent --show-error --location --write-out %{http_code} --output /tmp/valet-work --code 500 http://hello.com⌝
DEBUG The command ⌜curl⌝ originally ended with exit code ⌜0⌝.
The error code ⌜0⌝ is acceptable and has been reset to 0.
Standard output:
⌜500⌝
Error output:
⌜▶ called curl --silent --show-error --location --write-out %{http_code} --output /tmp/valet-work --code 500 http://hello.com
DEBUG The curl command for url ⌜http://hello.com⌝ ended with exit code ⌜0⌝, the http return code was ⌜500⌝.
ERROR The http return code ⌜500⌝ is not acceptable for url ⌜http://hello.com⌝.
Error output:
⌜▶ called curl --silent --show-error --location --write-out %{http_code} --output /tmp/valet-work --code 500 http://hello.com
stack:
├─ In function core::fail() $GLOBAL_VALET_HOME/valet.d/core:296
├─ In function kurl::toFile() $GLOBAL_VALET_HOME/valet.d/lib-kurl:55
├─ In function kurl::toVar() $GLOBAL_VALET_HOME/valet.d/lib-kurl:92
├─ In function testKurl::toVar() $GLOBAL_VALET_HOME/tests.d/1002-lib-kurl/00.kurl.sh:73
├─ In function main() $GLOBAL_VALET_HOME/tests.d/1002-lib-kurl/00.kurl.sh:101
├─ In function source() $GLOBAL_VALET_HOME/tests.d/1002-lib-kurl/00.kurl.sh:131
├─ In function source() $GLOBAL_VALET_HOME/valet.d/core:449
├─ In function runTest() valet.d/commands.d/self-test-utils:241
├─ In function runTestSuites() valet.d/commands.d/self-test-utils:195
├─ In function runCoreTests() valet.d/commands.d/self-test-utils:107
├─ In function selfTest() valet.d/commands.d/self-test.sh:110
├─ In function main::runFunction() $GLOBAL_VALET_HOME/valet.d/main:589
├─ In function main::parseMainArguments() $GLOBAL_VALET_HOME/valet.d/main:541
└─ In function main() $GLOBAL_VALET_HOME/valet:99
```

### Testing kurl, debug mode, with content http code 400
Expand Down
3 changes: 2 additions & 1 deletion tests.d/1005-lib-io/01.invoke.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ function testIo::invoke() {
local -i exitCode

echo "→ io::invoke fakeexec2 --error"
(io::invoke fakeexec2 --error) && exitCode=0 || exitCode=$?
(io::invoke fakeexec2 --error 2> "${_TEST_TEMP_FILE}") && exitCode=0 || exitCode=$?
echoFileWithLineNumberSubstitution "${_TEST_TEMP_FILE}" 1>&2
endTest "Testing io::invoke, should fail" ${exitCode}

echo "→ io::invoke fakeexec2 --option argument1 argument2"
Expand Down
30 changes: 15 additions & 15 deletions tests.d/1005-lib-io/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,21 +342,21 @@ Error output:
returning 1 from fakeexec2
stack:
├─ In function core::fail() $GLOBAL_VALET_HOME/valet.d/core:296
├─ In function io::invoke5() $GLOBAL_VALET_HOME/valet.d/lib-io:120
├─ In function io::invoke5var() $GLOBAL_VALET_HOME/valet.d/lib-io:178
├─ In function io::invoke() $GLOBAL_VALET_HOME/valet.d/lib-io:226
├─ In function testIo::invoke() $GLOBAL_VALET_HOME/tests.d/1005-lib-io/01.invoke.sh:61
├─ In function main() $GLOBAL_VALET_HOME/tests.d/1005-lib-io/01.invoke.sh:115
├─ In function source() $GLOBAL_VALET_HOME/tests.d/1005-lib-io/01.invoke.sh:120
├─ In function source() $GLOBAL_VALET_HOME/valet.d/core:449
├─ In function runTest() valet.d/commands.d/self-test-utils:241
├─ In function runTestSuites() valet.d/commands.d/self-test-utils:195
├─ In function runCoreTests() valet.d/commands.d/self-test-utils:107
├─ In function selfTest() valet.d/commands.d/self-test.sh:110
├─ In function main::runFunction() $GLOBAL_VALET_HOME/valet.d/main:589
├─ In function main::parseMainArguments() $GLOBAL_VALET_HOME/valet.d/main:541
└─ In function main() $GLOBAL_VALET_HOME/valet:99
├─ In function core::fail() $GLOBAL_VALET_HOME/valet.d/core:XXX
├─ In function io::invoke5() $GLOBAL_VALET_HOME/valet.d/lib-io:XXX
├─ In function io::invoke5var() $GLOBAL_VALET_HOME/valet.d/lib-io:XXX
├─ In function io::invoke() $GLOBAL_VALET_HOME/valet.d/lib-io:XXX
├─ In function testIo::invoke() $GLOBAL_VALET_HOME/tests.d/1005-lib-io/01.invoke.sh:XXX
├─ In function main() $GLOBAL_VALET_HOME/tests.d/1005-lib-io/01.invoke.sh:XXX
├─ In function source() $GLOBAL_VALET_HOME/tests.d/1005-lib-io/01.invoke.sh:XXX
├─ In function source() $GLOBAL_VALET_HOME/valet.d/core:XXX
├─ In function runTest() valet.d/commands.d/self-test-utils:XXX
├─ In function runTestSuites() valet.d/commands.d/self-test-utils:XXX
├─ In function runCoreTests() valet.d/commands.d/self-test-utils:XXX
├─ In function selfTest() valet.d/commands.d/self-test.sh:XXX
├─ In function main::runFunction() $GLOBAL_VALET_HOME/valet.d/main:XXX
├─ In function main::parseMainArguments() $GLOBAL_VALET_HOME/valet.d/main:XXX
└─ In function main() $GLOBAL_VALET_HOME/valet:XXX
```

### Testing io::invoke, output to var
Expand Down
15 changes: 0 additions & 15 deletions tests.d/1300-valet-cli/.before-test
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,4 @@ function echoTempFileWithTimeStampSubstitution() {
line="${line//????-??-??/YYYY:MM:DD}"
echo "${line}"
done < "${file}"
}

function echoTempFileWithLineNumberSubstitution() {
local file="${_TEST_TEMP_FILE}"
local line
local IFS=$'\n'
while read -rd $'\n' line; do
if [[ ${line} =~ :[0-9]{1,}$ ]]; then
line="${line/%:?/:XXX}"
line="${line/%:??/:XXX}"
line="${line/%:???/:XXX}"
line="${line/%:????/:XXX}"
fi
echo "${line}"
done <"${file}"
}
4 changes: 2 additions & 2 deletions tests.d/1300-valet-cli/01.command-help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ function testHelp() {
endTest "Testing help for the self mock2 command" $?

# Testing to fuzzy find an help
echo "→ valet hel s h"
("${GLOBAL_VALET_HOME}/valet" hel s h)
echo "→ valet hel sel mo3"
("${GLOBAL_VALET_HOME}/valet" hel sel mo3)
endTest "Testing to fuzzy find an help" $?

# testing help options
Expand Down
Loading

0 comments on commit a51650c

Please sign in to comment.