Skip to content

Commit

Permalink
Object array type with list terminator
Browse files Browse the repository at this point in the history
  • Loading branch information
fchastanet committed Dec 22, 2023
1 parent 60e46d5 commit 7b1d732
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 39 deletions.
22 changes: 22 additions & 0 deletions manualTests/Object::setArray.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/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/Log/__all.sh
source "${srcDir}/Log/__all.sh"

declare -a missingArrayTerminator=(
--type "missingArrayTerminatorType"
--array-list "elem1" "elem2" "elem3"
--property-property "propertyValue"
)
set -x
Object::setArray missingArrayTerminator list "newElem1" "newElem2"

declare -p missingArrayTerminator
Empty file modified src/Object/create.bats
100644 → 100755
Empty file.
Empty file modified src/Object/create.sh
100644 → 100755
Empty file.
22 changes: 20 additions & 2 deletions src/Object/getArray.bats
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ function Object::getArray::simpleObject { #@test
function Object::getArray::multipleArrayValues { #@test
declare -a multipleArrayValues=(
--type "multipleArrayValuesType"
--array-myList "elem1"
--array-myList "elem1" "elem2" --
--property-type "type"
--array-myList "elem2"
)
run Object::getArray multipleArrayValues myList 1
assert_lines_count 2
Expand Down Expand Up @@ -58,3 +57,22 @@ function Object::getArray::unknownProperty { #@test
assert_output ""
assert_success
}

function Object::getArray::customTerminator { #@test
declare -a simpleObject=(
--type "simpleObjectType"
--array-myList "elem1" "elem2" "@@@"
--property-property "propertyValue"
)
OBJECT_TEMPLATE_ARRAY_TERMINATOR="@@@" run Object::getArray simpleObject 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
assert_line --index 0 "elem1"
assert_line --index 1 "elem2"
assert_lines_count 2
assert_success
}
22 changes: 15 additions & 7 deletions src/Object/getArray.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@ Object::getArray() {
local -n object_get_array_objectData=$1
local arrayName="${2:-}"
local strict="${3:-0}"

local -i propertiesLength="${#object_get_array_objectData[@]}"
local -i i=0 || true
local arrayFound="0"
while ((i < propertiesLength)); do
if [[ "${object_get_array_objectData[${i}]}" = "--array-${arrayName}" ]]; then
arrayFound="1"
echo "${object_get_array_objectData[$((i+1))]}"
((++i))
# eat next elements until finding terminator
while ((i < propertiesLength)); do
if [[ "${object_get_array_objectData[${i}]}" = "${OBJECT_TEMPLATE_ARRAY_TERMINATOR:---}" ]]; then
return 0
fi
echo "${object_get_array_objectData[${i}]}"
((++i))
done
return 0
fi
((i=i+2))
((++i))
done
if [[ "${strict}" = "1" && "${arrayFound}" = "0" ]]; then

if [[ "${strict}" = "1" ]]; then
Log::displayError "unknown array ${arrayName}"
return 1
fi
}
}
31 changes: 31 additions & 0 deletions src/Object/getProperty.bats
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,34 @@ function Object::getProperty::unknownProperty { #@test
assert_output ""
assert_success
}

function Object::getProperty::withArray { #@test
declare -a simpleObject=(
--type "simpleObjectType"
--array-list "elem1" --
--property-property "propertyValue"
)
run Object::getProperty simpleObject property 1
assert_output "propertyValue"
assert_success

run Object::getProperty simpleObject property 0
assert_output "propertyValue"
assert_success
}

function Object::getProperty::property2 { #@test
declare -a simpleObject=(
--type "simpleObjectType"
--array-list "elem1" --
--property-property "propertyValue"
--property-property2 "propertyValue2"
)
run Object::getProperty simpleObject property2 1
assert_output "propertyValue2"
assert_success

run Object::getProperty simpleObject property2 0
assert_output "propertyValue2"
assert_success
}
8 changes: 4 additions & 4 deletions src/Object/getProperty.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ Object::getProperty() {
local -n object_get_property_objectData=$1
local propertyName="${2:-}"
local strict="${3:-0}"

local -i propertiesLength="${#object_get_property_objectData[@]}"
local -i i=0 || true
local propertyFound="0"
while ((i < propertiesLength)); do
if [[ "${object_get_property_objectData[${i}]}" = "--property-${propertyName}" ]]; then
echo "${object_get_property_objectData[$((i+1))]}"
echo "${object_get_property_objectData[$((i + 1))]}"
return 0
fi
((i=i+2))
((i = i + 1))
done
if [[ "${strict}" = "1" && "${propertyFound}" = "0" ]]; then
Log::displayError "unknown property ${propertyName}"
return 1
fi
}
}
38 changes: 31 additions & 7 deletions src/Object/setArray.bats
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,53 @@ source "${srcDir}/Array/contains.sh"
function Object::setArray::simpleObject { #@test
declare -a simpleObject=(
--type "simpleObjectType"
--array-list "elem1"
--array-list "elem1" --
--property-property "propertyValue"
)
local status=0
Object::setArray simpleObject list "newElem1" "newElem2" || status=1
[[ "${status}" = "0" ]]
run echo "${simpleObject[@]}"
assert_output "--type simpleObjectType --property-property propertyValue --array-list newElem1 --array-list newElem2"
assert_output "--type simpleObjectType --property-property propertyValue --array-list newElem1 newElem2 --"
}

function Object::setArray::multipleElements { #@test
declare -a multipleElements=(
--type "multipleElementsType"
--array-list "elem1" "elem2" "elem3" --
--property-property "propertyValue"
--array-list "elem1"
--array-list "elem2"
--array-list "elem3"
)
local status=0
Object::setArray multipleElements list "newElem1" "newElem2" || status=1
[[ "${status}" = "0" ]]
run echo "${multipleElements[@]}"
assert_output "--type multipleElementsType --property-property propertyValue --array-list newElem1 --array-list newElem2"
assert_output "--type multipleElementsType --property-property propertyValue --array-list newElem1 newElem2 --"
}

function Object::setArray::missingArrayTerminator { #@test
declare -a missingArrayTerminator=(
--type "missingArrayTerminatorType"
--array-list "elem1" "elem2" "elem3"
--property-property "propertyValue"
)
local status=0
Object::setArray missingArrayTerminator list "newElem1" "newElem2" || status=1
[[ "${status}" = "0" ]]
run echo "${missingArrayTerminator[@]}"
assert_output "--type missingArrayTerminatorType --array-list newElem1 newElem2 --"
}

function Object::setArray::customArrayTerminator { #@test
declare -a customArrayTerminator=(
--type "customArrayTerminatorType"
--array-list "elem1" "elem2" "elem3" "@@@"
--property-property "propertyValue"
)
local status=0
OBJECT_TEMPLATE_ARRAY_TERMINATOR="@@@" Object::setArray customArrayTerminator list "newElem1" "newElem2" || status=1
[[ "${status}" = "0" ]]
run echo "${customArrayTerminator[@]}"
assert_output "--type customArrayTerminatorType --property-property "propertyValue" --array-list newElem1 newElem2 @@@"
}

function Object::setArray::newProperty { #@test
Expand All @@ -48,5 +72,5 @@ function Object::setArray::newProperty { #@test
Object::setArray newPropertyObject list "newElem1" "newElem2" || status=1
[[ "${status}" = "0" ]]
run echo "${newPropertyObject[@]}"
assert_output "--type simpleObjectType --property-property propertyValue --array-list newElem1 --array-list newElem2"
assert_output "--type simpleObjectType --property-property propertyValue --array-list newElem1 newElem2 --"
}
36 changes: 24 additions & 12 deletions src/Object/setArray.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,34 @@ Object::setArray() {
local arrayName="${2:-}"
shift 2 || true
local -a arrayValues=("$@")

local -i propertiesLength="${#object_set_array_objectData[@]}"
local -a newProperties=()

# remove all array element

while ((i < propertiesLength)); do
if [[ "${object_set_array_objectData[${i}]}" != "--array-${arrayName}" ]]; then
newProperties+=("${object_set_array_objectData[@]:i:2}")
if [[ "${object_set_array_objectData[${i}]}" = "--array-${arrayName}" ]]; then
((++i))
# eat next elements until finding terminator
while ((i < propertiesLength)); do
if [[ "${object_set_array_objectData[${i}]}" = "${OBJECT_TEMPLATE_ARRAY_TERMINATOR:---}" ]]; then
((++i))
break
fi
((++i))
done
break
fi
((i=i+2))
newProperties+=("${object_set_array_objectData[${i}]}")
((++i))
done
# add all new values
local arrayValue
for arrayValue in "${arrayValues[@]}"; do
newProperties+=("--array-${arrayName}" "${arrayValue}")

# copy rest of the properties if any
while ((i < propertiesLength)); do
newProperties+=("${object_set_array_objectData[${i}]}")
((++i))
done

# finally set the array
newProperties+=("--array-${arrayName}" "${arrayValues[@]}" "${OBJECT_TEMPLATE_ARRAY_TERMINATOR:---}")
object_set_array_objectData=("${newProperties[@]}")
}
}
16 changes: 15 additions & 1 deletion src/Object/setProperty.bats
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ function Object::setProperty::multipleProperties { #@test
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 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"
Expand All @@ -45,5 +59,5 @@ function Object::setProperty::newProperty { #@test
Object::setProperty newPropertyObject newProperty "value" || status=1
[[ "${status}" = "0" ]]
run echo "${newPropertyObject[@]}"
assert_output "--type simpleObjectType --property-property propertyValue --property-newProperty value"
assert_output "--type simpleObjectType --property-property propertyValue --property-newProperty value"
}
12 changes: 6 additions & 6 deletions src/Object/setProperty.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Object::setProperty() {
local -n object_set_property_objectData=$1
local propertyName="${2:-}"
local propertyValue="${3:-}"

local i=0 || true
local -i propertiesLength="${#object_set_property_objectData[@]}"
local -a newProperties=()
Expand All @@ -13,18 +13,18 @@ Object::setProperty() {
if [[ "${object_set_property_objectData[${i}]}" = "--property-${propertyName}" ]]; then
propertyFound="1"
newProperties+=(
"${object_set_property_objectData[${i}]}" "${propertyValue}"
"${object_set_property_objectData[${i}]}" "${propertyValue}"
)
if ((i < propertiesLength-2)); then
if ((i < propertiesLength - 2)); then
newProperties+=("${object_set_property_objectData[@]:i+2}")
fi
break
fi
newProperties+=("${object_set_property_objectData[@]:i:2}")
((i=i+2))
newProperties+=("${object_set_property_objectData[${i}]}")
((i = i + 1))
done
if [[ "${propertyFound}" = "0" ]]; then
newProperties+=("--property-${propertyName}" "${propertyValue}")
fi
object_set_property_objectData=("${newProperties[@]}")
}
}

0 comments on commit 7b1d732

Please sign in to comment.