From 11c07a0af157f97a2d2dca1dc7b996e98d1dfbec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chastanet?= Date: Sun, 7 Jan 2024 16:29:06 +0100 Subject: [PATCH] Options2 - commit as is --- manualTests/Object::create.sh | 12 ++--- manualTests/Object::setProperty.sh | 2 +- manualTests/Options2::renderHelpArg.sh | 3 +- manualTests/testCreateObject.sh | 24 +++++----- src/Object/create.bats | 26 +++++----- src/Object/getArray.bats | 4 +- src/Object/getArray.sh | 6 +++ src/Object/getProperty.bats | 2 +- src/Object/getProperty.sh | 6 +++ src/Object/initFromTemplate.bats | 63 ------------------------- src/Object/initFromTemplate.sh | 32 ------------- src/Object/memberExists.sh | 8 +++- src/Object/setArray.sh | 4 ++ src/Object/setProperty.sh | 4 ++ src/Options2/validateArgObject.bats | 10 ++-- src/Options2/validateCommandObject.bats | 5 +- src/Options2/validateGroupObject.bats | 30 ++++++------ src/Options2/validateOptionObject.bats | 7 ++- 18 files changed, 88 insertions(+), 160 deletions(-) delete mode 100755 src/Object/initFromTemplate.bats delete mode 100755 src/Object/initFromTemplate.sh diff --git a/manualTests/Object::create.sh b/manualTests/Object::create.sh index ec200361..b1945642 100755 --- a/manualTests/Object::create.sh +++ b/manualTests/Object::create.sh @@ -11,7 +11,6 @@ source "${srcDir}/Options2/__all.sh" # shellcheck source=/src/Log/__all.sh source "${srcDir}/Log/__all.sh" - #set -x set -o errexit set -o pipefail @@ -22,12 +21,12 @@ Object::create zzzGroupGlobalOptionsFunction \ --property-title "GLOBAL OPTIONS:" Object::create simpleObjectFunction \ - --type "Group" + --type "Group" Object::create groupObjectFunction \ - --type "Group" \ - --property-title "title" \ - --property-help "help" + --type "Group" \ + --property-title "title" \ + --property-help "help" BASH_FRAMEWORK_DISPLAY_LEVEL=__LEVEL_DEBUG @@ -35,4 +34,5 @@ Object::create optionFunction \ --type "Option" \ --property-variableName "varName" -Options2::validateOptionObject "${optionFunction}" \ No newline at end of file +# shellcheck disable=SC2154 +Options2::validateOptionObject "${optionFunction}" diff --git a/manualTests/Object::setProperty.sh b/manualTests/Object::setProperty.sh index cdd0c6d1..e527d550 100755 --- a/manualTests/Object::setProperty.sh +++ b/manualTests/Object::setProperty.sh @@ -16,4 +16,4 @@ declare -a newPropertyObject=( --property-property "propertyValue" ) Object::setProperty newPropertyObject --property-newProperty "value" -declare -p newPropertyObject \ No newline at end of file +declare -p newPropertyObject diff --git a/manualTests/Options2::renderHelpArg.sh b/manualTests/Options2::renderHelpArg.sh index f1461194..8928dce2 100755 --- a/manualTests/Options2::renderHelpArg.sh +++ b/manualTests/Options2::renderHelpArg.sh @@ -26,6 +26,7 @@ declare -a arg=( --property-mandatory 1 --property-max 2 ) +# shellcheck disable=SC2034 declare -a group=( --type "Group" --property-title "Global options" @@ -42,4 +43,4 @@ declare -a arg=( ) Options2::validateArgObject arg UI::theme "default" -Options2::renderArgHelp arg 2>&1 \ No newline at end of file +Options2::renderArgHelp arg 2>&1 diff --git a/manualTests/testCreateObject.sh b/manualTests/testCreateObject.sh index 649b97b5..5db579f0 100755 --- a/manualTests/testCreateObject.sh +++ b/manualTests/testCreateObject.sh @@ -7,15 +7,15 @@ source src/Options2/__all.sh declare myFunction Object::create myFunction \ - --type "Command" \ - --property-name "François" + --type "Command" \ + --property-name "François" declare myFunction2 Object::create myFunction2 \ - --array-list "a" \ - --type "Command2" \ - --property-name "François2" \ - --array-list "b" \ - --array-list "c" + --array-list "a" \ + --type "Command2" \ + --property-name "François2" \ + --array-list "b" \ + --array-list "c" echo "--------------------------------------------" ${myFunction} getProperty --property-name @@ -30,12 +30,12 @@ ${myFunction2} setProperty name "myFunction2François3" ${myFunction2} getProperty --property-name echo -n "name2 " ${myFunction2} setProperty name2 "newProperty name2" - + ${myFunction2} getProperty --property-name2 echo "--------------------------------------------" Object::create myFunction2 \ - --function-name "argFunction2Overload" \ - --type "Command2Overload" \ - --property-name "François2Overload" -${myFunction2} getProperty --property-name \ No newline at end of file + --function-name "argFunction2Overload" \ + --type "Command2Overload" \ + --property-name "François2Overload" +${myFunction2} getProperty --property-name diff --git a/src/Object/create.bats b/src/Object/create.bats index 1025bd3e..c2101568 100755 --- a/src/Object/create.bats +++ b/src/Object/create.bats @@ -16,7 +16,7 @@ function Object::create::simpleObject { #@test Object::create simpleObjectFunction \ --type "simpleObjectType" \ --property-property "propertyValue" - + run ${simpleObjectFunction} type assert_output "simpleObjectType" @@ -43,7 +43,7 @@ function Object::create::missingPositionalArg { #@test run Object::create \ --type "simpleObjectType" \ --propertyInvalid-property "propertyValue" 2>&1 - + assert_output --partial "local: \`--type': invalid variable name for name reference" assert_failure 1 } @@ -54,7 +54,7 @@ function Object::create::simpleObjectNonStrict { #@test --strict 0 \ --type "simpleObjectType" \ --property-property "propertyValue" - + run ${simpleObjectType} strict assert_output "0" @@ -68,7 +68,7 @@ function Object::create::invalidProperty { #@test run Object::create simpleObjectType \ --type "simpleObjectType" \ --propertyInvalid-property "propertyValue" 2>&1 - + assert_output --partial "ERROR - invalid object property --propertyInvalid-property" assert_failure 1 } @@ -79,17 +79,17 @@ function Object::create::duplicatedProperty { #@test --type "simpleObjectType" \ --property-property "propertyValue1" \ --property-property "propertyValue2" 2>&1 - + assert_output --partial "ERROR - property property is provided more than one time" assert_failure 1 } function Object::create::invalidObjectType { #@test - declare invalidObjectType + declare invalidObjectType run Object::create invalidObjectType \ --type "invalidéObjectType" \ --property-property "propertyValue" 2>&1 - + assert_output --partial "ERROR - invalid object type invalidéObjectType" assert_failure 1 } @@ -97,7 +97,7 @@ function Object::create::invalidObjectType { #@test function Object::create::missingObjectType { #@test run Object::create missingObjectType \ --property-property "propertyValue" 2>&1 - + assert_output --partial "ERROR - missing object type" assert_failure 1 } @@ -107,9 +107,9 @@ function Object::create::unknownArray { #@test Object::create unknownArray \ --type "simpleObjectType" \ --array-list "unknownArray" 2>&1 - + run ${unknownArray} getArray "unknownArray" - + assert_output --partial "ERROR - unknown array unknownArray" assert_failure 2 } @@ -121,7 +121,7 @@ function Object::create::propertyArrayOrdered { #@test --property-property "propertyValue" \ --array-list "value1" \ --array-list "value2" - + run ${propertyArrayOrdered} type assert_success assert_output "simpleObjectType" @@ -148,7 +148,7 @@ function Object::create::propertyArrayUnordered { #@test --array-list "value2" \ --property-property "propertyValue" \ --array-list "value3" - + run ${propertyArrayUnordered} type assert_success assert_output "simpleObjectType" @@ -172,4 +172,4 @@ function Object::create::propertyArrayUnordered { #@test assert_lines_count 2 assert_line --index 0 "list" assert_line --index 1 "property" -} \ No newline at end of file +} diff --git a/src/Object/getArray.bats b/src/Object/getArray.bats index 861a5b72..52dc16e5 100755 --- a/src/Object/getArray.bats +++ b/src/Object/getArray.bats @@ -44,13 +44,13 @@ function Object::getArray::multipleArrayValues { #@test assert_success } -function Object::getArray::unknownProperty { #@test +function Object::getArray::unknownArray { #@test declare -a simpleObject=( --type "simpleObjectType" --property-property "propertyValue" ) run Object::getArray simpleObject --array-unknownArray 1 - assert_output --partial "ERROR - unknown array --array-unknownArray" + assert_output "" assert_failure 1 run Object::getArray simpleObject --array-unknownArray 0 diff --git a/src/Object/getArray.sh b/src/Object/getArray.sh index 533e0bb2..1db9181a 100755 --- a/src/Object/getArray.sh +++ b/src/Object/getArray.sh @@ -1,5 +1,11 @@ #!/bin/bash +# @description get array elements from object +# @arg $1 object_get_array_objectData:&String[] the object +# @arg $2 arrayName:String the name of the array property to search (eg: --array-property) +# @arg $3 strict:Boolean if !0 then return code 1 if array property not found +# @stdout the array property values if array property found(one value by line) +# @exitcode 1 if array property not found Object::getArray() { local -n object_get_array_objectData=$1 local arrayName="${2:-}" diff --git a/src/Object/getProperty.bats b/src/Object/getProperty.bats index ca10e592..bfb7df19 100755 --- a/src/Object/getProperty.bats +++ b/src/Object/getProperty.bats @@ -31,7 +31,7 @@ function Object::getProperty::unknownProperty { #@test --property-property "propertyValue" ) run Object::getProperty simpleObject --property-unknownProperty 1 - assert_output --partial "ERROR - unknown property --property-unknownProperty" + assert_output "" assert_failure 1 run Object::getProperty simpleObject --property-unknownProperty 0 diff --git a/src/Object/getProperty.sh b/src/Object/getProperty.sh index 0e240c1a..049dcd15 100755 --- a/src/Object/getProperty.sh +++ b/src/Object/getProperty.sh @@ -1,5 +1,11 @@ #!/bin/bash +# @description get property value from object +# @arg $1 object_get_property_objectData:&String[] the object +# @arg $2 propertyName:String the name of the property to search (eg: --property) +# @arg $3 strict:Boolean if !0 then return code 1 if property not found +# @stdout the property value if property found +# @exitcode 1 if property not found Object::getProperty() { local -n object_get_property_objectData=$1 local propertyName="${2:-}" diff --git a/src/Object/initFromTemplate.bats b/src/Object/initFromTemplate.bats deleted file mode 100755 index 8efe7a42..00000000 --- a/src/Object/initFromTemplate.bats +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env bash -# shellcheck disable=SC2154 -# shellcheck disable=SC2034 - -# shellcheck source=src/batsHeaders.sh -source "$(cd "${BATS_TEST_DIRNAME}/.." && pwd)/batsHeaders.sh" -# shellcheck source=src/Object/__all.sh -source "${srcDir}/Object/__all.sh" -# shellcheck source=src/Assert/posixFunctionName.sh -source "${srcDir}/Assert/posixFunctionName.sh" -# shellcheck source=src/Array/contains.sh -source "${srcDir}/Array/contains.sh" - -function Object::setProperty::simpleObject { #@test - declare -a simpleObject=( - --type "simpleObjectType" - --property-property "propertyValue" - ) - local status=0 - Object::setProperty simpleObject --property-property "newPropertyValue" || status=1 - [[ "${status}" = "0" ]] - run echo "${simpleObject[@]}" - assert_output "--type simpleObjectType --property-property newPropertyValue" -} - -function Object::setProperty::multipleProperties { #@test - declare -a multipleProperties=( - --type "multiplePropertiesType" - --property-property "propertyValue" - --property-property2 "propertyValue2" - ) - local status=0 - Object::setProperty multipleProperties --property-property "newPropertyValue" || status=1 - [[ "${status}" = "0" ]] - run echo "${multipleProperties[@]}" - assert_output "--type multiplePropertiesType --property-property newPropertyValue --property-property2 propertyValue2" -} - -function Object::setProperty::withArray { #@test - declare -a withArray=( - --type "withArrayType" - --property-property "propertyValue" - --array-list "elem1" "elem2" -- - --property-property2 "propertyValue2" - ) - local status=0 - Object::setProperty withArray --property-property2 "newPropertyValue" || status=1 - [[ "${status}" = "0" ]] - run echo "${withArray[@]}" - assert_output "--type withArrayType --property-property propertyValue --array-list "elem1" "elem2" -- --property-property2 newPropertyValue" -} - -function Object::setProperty::newProperty { #@test - declare -a newPropertyObject=( - --type "simpleObjectType" - --property-property "propertyValue" - ) - local status=0 - Object::setProperty newPropertyObject --property-newProperty "value" || status=1 - [[ "${status}" = "0" ]] - run echo "${newPropertyObject[@]}" - assert_output "--type simpleObjectType --property-property propertyValue --property-newProperty value" -} diff --git a/src/Object/initFromTemplate.sh b/src/Object/initFromTemplate.sh deleted file mode 100755 index 05f93a95..00000000 --- a/src/Object/initFromTemplate.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash - -Object::initFromTemplate() { - local -n object_init_from_template_template=$1 - local -n object_init_from_template_object=$2 - shift 2 || true - local propertyName="${2:-}" - local propertyValue="${3:-}" - - local i=0 || true - local -i propertiesLength="${#object_set_property_objectData[@]}" - local -a newProperties=() - local propertyFound="0" - while ((i < propertiesLength)); do - if [[ "${object_set_property_objectData[${i}]}" = "--property-${propertyName}" ]]; then - propertyFound="1" - newProperties+=( - "${object_set_property_objectData[${i}]}" "${propertyValue}" - ) - if ((i < propertiesLength - 2)); then - newProperties+=("${object_set_property_objectData[@]:i+2}") - fi - break - fi - newProperties+=("${object_set_property_objectData[${i}]}") - ((i = i + 1)) - done - if [[ "${propertyFound}" = "0" ]]; then - newProperties+=("--property-${propertyName}" "${propertyValue}") - fi - object_set_property_objectData=("${newProperties[@]}") -} diff --git a/src/Object/memberExists.sh b/src/Object/memberExists.sh index ec3dfb8c..59b3fc94 100755 --- a/src/Object/memberExists.sh +++ b/src/Object/memberExists.sh @@ -1,13 +1,17 @@ #!/bin/bash +# @description check if member(property or array property) exists on given object +# @arg $1 object_member_exists_objectData:&String[] the object +# @arg $2 memberName:String the name of the member to search (eg: --property) +# @exitcode 1 if member not found Object::memberExists() { local -n object_member_exists_objectData=$1 - local propertyName="${2:-}" + local memberName="${2:-}" local -i propertiesLength="${#object_member_exists_objectData[@]}" local -i i=0 || true while ((i < propertiesLength)); do - if [[ "${object_member_exists_objectData[${i}]}" = "${propertyName}" ]]; then + if [[ "${object_member_exists_objectData[${i}]}" = "${memberName}" ]]; then return 0 fi ((i = i + 1)) diff --git a/src/Object/setArray.sh b/src/Object/setArray.sh index 77a98ca7..0224132a 100755 --- a/src/Object/setArray.sh +++ b/src/Object/setArray.sh @@ -1,5 +1,9 @@ #!/bin/bash +# @description create or overwrite array property on object +# @arg $1 object_set_array_objectData:&String[] the object +# @arg $2 arrayName:String the name of the array property to search (eg: --array-property) +# @arg $@ arrayValues:String[] array values to set Object::setArray() { local -n object_set_array_objectData=$1 local arrayName="${2:-}" diff --git a/src/Object/setProperty.sh b/src/Object/setProperty.sh index c0f8e3b7..a9566592 100755 --- a/src/Object/setProperty.sh +++ b/src/Object/setProperty.sh @@ -1,5 +1,9 @@ #!/bin/bash +# @description create or overwrite property value on object +# @arg $1 object_set_property_objectData:&String[] the object +# @arg $2 propertyName:String the name of the property to search (eg: --property) +# @arg $3 propertyValue:String property value to set Object::setProperty() { local -n object_set_property_objectData=$1 local propertyName="${2:-}" diff --git a/src/Options2/validateArgObject.bats b/src/Options2/validateArgObject.bats index 948076d9..9a032084 100755 --- a/src/Options2/validateArgObject.bats +++ b/src/Options2/validateArgObject.bats @@ -32,7 +32,7 @@ function Options2::validateArgObject::tooMuchArgs { #@test declare -a arg=( --type "Arg" ) - run Options2::validateArgObject arg arg + run Options2::validateArgObject arg arg assert_lines_count 1 assert_output --partial "ERROR - Options2::validateArgObject - exactly one parameter has to be provided" assert_failure 1 @@ -42,7 +42,7 @@ function Options2::validateArgObject::invalidObjectType { #@test declare -a arg=( --type "notAnArg" ) - + run Options2::validateArgObject arg assert_lines_count 1 assert_output --partial "ERROR - Options2::validateArgObject - passed object is not an argument" @@ -99,7 +99,6 @@ function Options2::validateArgObject::nameInvalid { #@test assert_lines_count 1 } - function Options2::validateArgObject::callbackInvalid { #@test declare -a arg2=( --type "Arg" @@ -109,7 +108,7 @@ function Options2::validateArgObject::callbackInvalid { #@test --property-name "valid" --array-callback "François" ) - + run Options2::validateArgObject arg2 2>&1 assert_output --partial "ERROR - Options2::validateArgObject - callback 'François' - function does not exists" assert_lines_count 1 @@ -253,7 +252,6 @@ function Options2::validateArgObject::StringArray::minValueLessThan0 { #@test assert_lines_count 1 } - function Options2::validateArgObject::StringArray::minValueGreaterThanMaxValue { #@test declare -a arg=( --type "Arg" @@ -268,4 +266,4 @@ function Options2::validateArgObject::StringArray::minValueGreaterThanMaxValue { assert_output --partial "ERROR - Options2::validateArgObject - max value should be greater than min value" assert_failure 2 assert_lines_count 1 -} \ No newline at end of file +} diff --git a/src/Options2/validateCommandObject.bats b/src/Options2/validateCommandObject.bats index d42c7f50..f3ff75c0 100755 --- a/src/Options2/validateCommandObject.bats +++ b/src/Options2/validateCommandObject.bats @@ -32,7 +32,7 @@ function Options2::validateCommandObject::tooMuchArgs { #@test declare -a object=( --type "Command" ) - run Options2::validateCommandObject object object + run Options2::validateCommandObject object object assert_lines_count 1 assert_output --partial "ERROR - Options2::validateCommandObject - exactly one parameter has to be provided" assert_failure 1 @@ -51,7 +51,7 @@ function Options2::validateCommandObject::invalidObjectType { #@test function Options2::validateCommandObject::nameInvalid { #@test declare -a object=( - --type "Command" \ + --type "Command" --property-name "François" ) @@ -140,4 +140,3 @@ function Options2::validateCommandObject::everyArgumentCallbackInvalid { #@test assert_lines_count 1 assert_failure 2 } - diff --git a/src/Options2/validateGroupObject.bats b/src/Options2/validateGroupObject.bats index e084fe6f..3c7355a8 100755 --- a/src/Options2/validateGroupObject.bats +++ b/src/Options2/validateGroupObject.bats @@ -6,6 +6,8 @@ source "$(cd "${BATS_TEST_DIRNAME}/.." && pwd)/batsHeaders.sh" source "${srcDir}/Options/_bats.sh" # shellcheck source=src/Options2/__all.sh source "${srcDir}/Options2/__all.sh" +# shellcheck source=src/Object/memberExists.sh +source "${srcDir}/Object/memberExists.sh" function setup() { export TMPDIR="${BATS_TEST_TMPDIR}" @@ -30,22 +32,22 @@ function Options2::validateGroupObject::invalidObject { #@test } function Options2::validateGroupObject::notAGroup { #@test - Object::create \ - --type "NotAGroup" \ - --function-name "notAGroupFunction" + local -a notAGroup=( + --type "NotAGroup" + ) - run Options2::validateGroupObject notAGroupFunction + run Options2::validateGroupObject notAGroup assert_lines_count 1 assert_output --partial "ERROR - Options2::validateGroupObject - passed object is not a group" assert_failure 2 } function Options2::validateGroupObject::missingTitle { #@test - Object::create \ - --type "Group" \ - --function-name "simpleObjectFunction" + local -a simpleObject=( + --type "Group" + ) - run Options2::validateGroupObject simpleObjectFunction + run Options2::validateGroupObject simpleObject assert_output --partial "ERROR - Options2::validateGroupObject - title is mandatory" assert_failure 3 assert_lines_count 1 @@ -53,12 +55,12 @@ function Options2::validateGroupObject::missingTitle { #@test function Options::validateGroupObject::groupOptionValid { #@test local status=0 - Object::create \ - --type "Group" \ - --property-title "Global options" \ - --property-help "help" \ - --function-name "groupObjectFunction" - run Options2::validateGroupObject groupObjectFunction >"${BATS_TEST_TMPDIR}/result" 2>&1 + local -a groupObject=( + --type "Group" + --property-title "Global options" + --property-help "help" + ) + run Options2::validateGroupObject groupObject >"${BATS_TEST_TMPDIR}/result" 2>&1 assert_success assert_output "" } diff --git a/src/Options2/validateOptionObject.bats b/src/Options2/validateOptionObject.bats index e5946a2e..d0b02e35 100755 --- a/src/Options2/validateOptionObject.bats +++ b/src/Options2/validateOptionObject.bats @@ -32,7 +32,7 @@ function Options2::validateOptionObject::tooMuchArgs { #@test declare -a object=( --type "Option" ) - run Options2::validateOptionObject object object + run Options2::validateOptionObject object object assert_lines_count 1 assert_output --partial "ERROR - Options2::validateOptionObject - exactly one parameter has to be provided" assert_failure 1 @@ -104,7 +104,7 @@ function Options2::validateOptionObject::missingAltOption { #@test --property-variableName "varName" --property-variableType "String" ) - + run Options2::validateOptionObject object assert_output --partial "ERROR - Options2::validateOptionObject - you must provide at least one alt option" assert_lines_count 1 @@ -332,7 +332,6 @@ function Options2::validateOptionObject::StringArray::minValueLessThan0 { #@test assert_lines_count 1 } - function Options2::validateOptionObject::StringArray::minValueGreaterThanMaxValue { #@test declare -a object=( --type "Option" @@ -346,4 +345,4 @@ function Options2::validateOptionObject::StringArray::minValueGreaterThanMaxValu assert_output --partial "ERROR - Options2::validateOptionObject - max value should be greater than min value" assert_failure 2 assert_lines_count 1 -} \ No newline at end of file +}