Skip to content

Commit

Permalink
Options2 commit as is
Browse files Browse the repository at this point in the history
  • Loading branch information
fchastanet committed Jan 6, 2024
1 parent abfdcaa commit 8b2b89d
Show file tree
Hide file tree
Showing 33 changed files with 779 additions and 616 deletions.
22 changes: 8 additions & 14 deletions manualTests/Object::create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,22 @@ set -o errexit
set -o pipefail
export TMPDIR="/tmp"

Object::create \
Object::create zzzGroupGlobalOptionsFunction \
--type group \
--property-title "GLOBAL OPTIONS:" \
--function-name zzzGroupGlobalOptionsFunction
--property-title "GLOBAL OPTIONS:"

Object::create \
--type "Group" \
--function-name "simpleObjectFunction"
Object::create simpleObjectFunction \
--type "Group"

Object::create \
Object::create groupObjectFunction \
--type "Group" \
--property-title "title" \
--property-help "help" \
--function-name "groupObjectFunction"
--property-help "help"

BASH_FRAMEWORK_DISPLAY_LEVEL=__LEVEL_DEBUG

Object::create \
--function-name "optionFunction" \
Object::create optionFunction \
--type "Option" \
--property-variableName "varName"

declare -f optionFunction
set -x
Options2::validateOptionObject optionFunction
Options2::validateOptionObject "${optionFunction}"
3 changes: 1 addition & 2 deletions manualTests/Object::setArray.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ declare -a missingArrayTerminator=(
--array-list "elem1" "elem2" "elem3"
--property-property "propertyValue"
)
set -x
Object::setArray missingArrayTerminator list "newElem1" "newElem2"
Object::setArray missingArrayTerminator --array-list "newElem1" "newElem2"

declare -p missingArrayTerminator
2 changes: 1 addition & 1 deletion manualTests/Object::setProperty.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ declare -a newPropertyObject=(
--type "simpleObjectType"
--property-property "propertyValue"
)
Object::setProperty newPropertyObject newProperty "value"
Object::setProperty newPropertyObject --property-newProperty "value"
declare -p newPropertyObject
45 changes: 45 additions & 0 deletions manualTests/Options2::renderHelpArg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash

rootDir="$(cd "$(readlink -e "${BASH_SOURCE[0]%/*}")/.." && pwd -P)"
export FRAMEWORK_ROOT_DIR="${rootDir}"
export _COMPILE_ROOT_DIR="${rootDir}"

srcDir="$(cd "${rootDir}/src" && pwd -P)"

# shellcheck source=src/Object/__all.sh
source "${srcDir}/Object/__all.sh"
# shellcheck source=src/Options2/__all.sh
source "${srcDir}/Options2/__all.sh"
# shellcheck source=/src/Log/__all.sh
source "${srcDir}/Log/__all.sh"
# shellcheck source=/src/UI/theme.sh
source "${srcDir}/UI/theme.sh"
# shellcheck source=/src/Assert/tty.sh
source "${srcDir}/Assert/tty.sh"

declare -a arg=(
--type "Arg"
--property-variableName "var"
--property-name "name"
--property-help "help"
--property-title "Global options"
--property-mandatory 1
--property-max 2
)
declare -a group=(
--type "Group"
--property-title "Global options"
--property-help "help"
)
Options2::renderGroupHelp group
declare -a arg=(
--type "Arg"
--property-variableName "varName"
--property-variableType "String"
--array-alt "--help"
--property-name "valid"
--array-callback "François"
)
Options2::validateArgObject arg
UI::theme "default"
Options2::renderArgHelp arg 2>&1
16 changes: 8 additions & 8 deletions testCreateObject.sh → manualTests/testCreateObject.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ Object::create myFunction2 \
--array-list "c"

echo "--------------------------------------------"
${myFunction} getProperty name
${myFunction2} getProperty name
${myFunction} getProperty name
${myFunction2} getProperty list
${myFunction} getProperty --property-name
${myFunction2} getProperty --property-name
${myFunction} getProperty --property-name
${myFunction2} getProperty --property-list

echo "------------------- setProperty -------------------------"
${myFunction} setProperty name "myFunctionFrançois3"
${myFunction} getProperty name
${myFunction} getProperty --property-name
${myFunction2} setProperty name "myFunction2François3"
${myFunction2} getProperty name
${myFunction2} getProperty --property-name
echo -n "name2 "
${myFunction2} setProperty name2 "newProperty name2"

${myFunction2} getProperty name2
${myFunction2} getProperty --property-name2

echo "--------------------------------------------"
Object::create myFunction2 \
--function-name "argFunction2Overload" \
--type "Command2Overload" \
--property-name "François2Overload"
${myFunction2} getProperty name
${myFunction2} getProperty --property-name
2 changes: 2 additions & 0 deletions src/Object/__all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# shellcheck source=src/Object/create.sh
source "${FRAMEWORK_ROOT_DIR}/src/Object/create.sh"
# shellcheck source=src/Object/memberExists.sh
source "${FRAMEWORK_ROOT_DIR}/src/Object/memberExists.sh"
# shellcheck source=src/Object/getProperty.sh
source "${FRAMEWORK_ROOT_DIR}/src/Object/getProperty.sh"
# shellcheck source=src/Object/setProperty.sh
Expand Down
18 changes: 9 additions & 9 deletions src/Object/getArray.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ function Object::getArray::simpleObject { #@test
--type "simpleObjectType"
--array-myList "elem1"
)
run Object::getArray simpleObject myList 1
run Object::getArray simpleObject --array-myList 1
assert_output "elem1"
assert_success

run Object::getArray simpleObject myList 0
run Object::getArray simpleObject --array-myList 0
assert_output "elem1"
assert_success
}
Expand All @@ -31,13 +31,13 @@ function Object::getArray::multipleArrayValues { #@test
--array-myList "elem1" "elem2" --
--property-type "type"
)
run Object::getArray multipleArrayValues myList 1
run Object::getArray multipleArrayValues --array-myList 1
assert_lines_count 2
assert_line --index 0 "elem1"
assert_line --index 1 "elem2"
assert_success

run Object::getArray multipleArrayValues myList 0
run Object::getArray multipleArrayValues --array-myList 0
assert_lines_count 2
assert_line --index 0 "elem1"
assert_line --index 1 "elem2"
Expand All @@ -49,11 +49,11 @@ function Object::getArray::unknownProperty { #@test
--type "simpleObjectType"
--property-property "propertyValue"
)
run Object::getArray simpleObject unknownArray 1
assert_output --partial "ERROR - unknown array unknownArray"
run Object::getArray simpleObject --array-unknownArray 1
assert_output --partial "ERROR - unknown array --array-unknownArray"
assert_failure 1

run Object::getArray simpleObject unknownArray 0
run Object::getArray simpleObject --array-unknownArray 0
assert_output ""
assert_success
}
Expand All @@ -64,13 +64,13 @@ function Object::getArray::customTerminator { #@test
--array-myList "elem1" "elem2" "@@@"
--property-property "propertyValue"
)
OBJECT_TEMPLATE_ARRAY_TERMINATOR="@@@" run Object::getArray simpleObject myList 1
OBJECT_TEMPLATE_ARRAY_TERMINATOR="@@@" run Object::getArray simpleObject --array-myList 1
assert_line --index 0 "elem1"
assert_line --index 1 "elem2"
assert_lines_count 2
assert_success

OBJECT_TEMPLATE_ARRAY_TERMINATOR="@@@" run Object::getArray simpleObject myList 0
OBJECT_TEMPLATE_ARRAY_TERMINATOR="@@@" run Object::getArray simpleObject --array-myList 0
assert_line --index 0 "elem1"
assert_line --index 1 "elem2"
assert_lines_count 2
Expand Down
5 changes: 2 additions & 3 deletions src/Object/getArray.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Object::getArray() {
local -i propertiesLength="${#object_get_array_objectData[@]}"
local -i i=0 || true
while ((i < propertiesLength)); do
if [[ "${object_get_array_objectData[${i}]}" = "--array-${arrayName}" ]]; then
if [[ "${object_get_array_objectData[${i}]}" = "${arrayName}" ]]; then
((++i))
# eat next elements until finding terminator
while ((i < propertiesLength)); do
Expand All @@ -23,8 +23,7 @@ Object::getArray() {
((++i))
done

if [[ "${strict}" = "1" ]]; then
Log::displayError "unknown array ${arrayName}"
if [[ "${strict}" != "0" ]]; then
return 1
fi
}
18 changes: 9 additions & 9 deletions src/Object/getProperty.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ function Object::getProperty::simpleObject { #@test
--type "simpleObjectType"
--property-property "propertyValue"
)
run Object::getProperty simpleObject property 1
run Object::getProperty simpleObject --property-property 1
assert_output "propertyValue"
assert_success

run Object::getProperty simpleObject property 0
run Object::getProperty simpleObject --property-property 0
assert_output "propertyValue"
assert_success
}
Expand All @@ -30,11 +30,11 @@ function Object::getProperty::unknownProperty { #@test
--type "simpleObjectType"
--property-property "propertyValue"
)
run Object::getProperty simpleObject unknownProperty 1
assert_output --partial "ERROR - unknown property unknownProperty"
run Object::getProperty simpleObject --property-unknownProperty 1
assert_output --partial "ERROR - unknown property --property-unknownProperty"
assert_failure 1

run Object::getProperty simpleObject unknownProperty 0
run Object::getProperty simpleObject --property-unknownProperty 0
assert_output ""
assert_success
}
Expand All @@ -45,11 +45,11 @@ function Object::getProperty::withArray { #@test
--array-list "elem1" --
--property-property "propertyValue"
)
run Object::getProperty simpleObject property 1
run Object::getProperty simpleObject --property-property 1
assert_output "propertyValue"
assert_success

run Object::getProperty simpleObject property 0
run Object::getProperty simpleObject --property-property 0
assert_output "propertyValue"
assert_success
}
Expand All @@ -61,11 +61,11 @@ function Object::getProperty::property2 { #@test
--property-property "propertyValue"
--property-property2 "propertyValue2"
)
run Object::getProperty simpleObject property2 1
run Object::getProperty simpleObject --property-property2 1
assert_output "propertyValue2"
assert_success

run Object::getProperty simpleObject property2 0
run Object::getProperty simpleObject --property-property2 0
assert_output "propertyValue2"
assert_success
}
5 changes: 2 additions & 3 deletions src/Object/getProperty.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ Object::getProperty() {
local -i i=0 || true
local propertyFound="0"
while ((i < propertiesLength)); do
if [[ "${object_get_property_objectData[${i}]}" = "--property-${propertyName}" ]]; then
if [[ "${object_get_property_objectData[${i}]}" = "${propertyName}" ]]; then
echo "${object_get_property_objectData[$((i + 1))]}"
return 0
fi
((i = i + 1))
done
if [[ "${strict}" = "1" && "${propertyFound}" = "0" ]]; then
Log::displayError "unknown property ${propertyName}"
if [[ "${strict}" != "0" && "${propertyFound}" = "0" ]]; then
return 1
fi
}
63 changes: 63 additions & 0 deletions src/Object/initFromTemplate.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/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"
}
32 changes: 32 additions & 0 deletions src/Object/initFromTemplate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/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[@]}")
}
Loading

0 comments on commit 8b2b89d

Please sign in to comment.