Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions Subsystems/cmdGui/CHeaderParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,28 @@ def get_file_list(filename='CHeaderParser-hdr-paths.txt'):
return []


def prompt_array_size(array_name, initial_val=None, input_func=input):
"""
Validates array size or prompts the user until a valid integer
between 0 and 128 (inclusive) is provided.
"""
array_size = str(initial_val).strip() if initial_val is not None else ""
while not (array_size.isdigit() and int(array_size) in range(129)):
if not array_size.isdigit():
print("Could not translate", array_name)
else:
print("Array size out of bounds. It must be between 0 and 128.")
try:
array_size = input_func(
(f"Please enter the defined value for "
f"{array_name} (0 - 128): ")
).strip()
except (EOFError, KeyboardInterrupt):
break
return array_size



if __name__ == '__main__':

# Get list of files to parse
Expand Down Expand Up @@ -443,28 +465,12 @@ def get_file_list(filename='CHeaderParser-hdr-paths.txt'):

print("Array size:", array_size)

# This while loop will make sure that
# the user input is both
# - a valid integer
# - between 0 and 128 (inclusively)
while not array_size.isdigit() or int(
array_size) not in range(129):
# User input is not an integer
if not array_size.isdigit():
print("Could not translate",
array_name_size[1])
else:
print(
"Array size out of bounds. It must be between 0 and 128."
)
try:
# Try to translate user input to an integer
array_size = int(
input(
(f"Please enter the defined value for "
f"{array_name_size[1]} (0 - 128): ")))
except ValueError:
pass # Ignore non-integer and try again
# Ensure user input is a valid integer between 0 and 128 (inclusive)
array_size = prompt_array_size(array_name_size[1],
initial_val=array_size)

# Update parameter length with validated array size
param_lens[-1] = array_size

# Add string length argument to parameter list
string_lens.append(array_size)
Expand Down
68 changes: 68 additions & 0 deletions Subsystems/cmdGui/test_cheaderparser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#
# NASA Docket No. GSC-19,200-1, and identified as "cFS Draco"
#
# Copyright (c) 2023 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import unittest
from unittest.mock import MagicMock

from CHeaderParser import find_data_type_new, prompt_array_size


class TestCHeaderParser(unittest.TestCase):

def test_find_data_type_new(self):
self.assertEqual(find_data_type_new('char', 'name[10]'), '--string')
self.assertEqual(find_data_type_new('boolean', 'flag'), '--uint8')
self.assertEqual(find_data_type_new('int32', 'count'), '--int32')
self.assertEqual(find_data_type_new('uint16', 'port'), '--uint16')
self.assertIsNone(find_data_type_new('custom_struct_t', 'data'))

def test_prompt_array_size_already_valid_numeric_string(self):
mock_input = MagicMock()
result = prompt_array_size("my_array", initial_val="64", input_func=mock_input)
self.assertEqual(result, "64")
mock_input.assert_not_called()

def test_prompt_array_size_boundary_values(self):
mock_input = MagicMock()
self.assertEqual(prompt_array_size("arr_zero", initial_val="0", input_func=mock_input), "0")
self.assertEqual(prompt_array_size("arr_max", initial_val="128", input_func=mock_input), "128")
mock_input.assert_not_called()

def test_prompt_array_size_out_of_bounds_then_valid(self):
# Reproduces Issue #241: entering an integer out of range should not cause
# AttributeError: 'int' object has no attribute 'isdigit' on subsequent checks
inputs = iter(["200", "64"])
result = prompt_array_size(
"OS_MAX_PATH_LEN",
initial_val="OS_MAX_PATH_LEN",
input_func=lambda _: next(inputs)
)
self.assertEqual(result, "64")

def test_prompt_array_size_non_numeric_then_valid(self):
inputs = iter(["not_a_number", "45"])
result = prompt_array_size(
"OS_MAX_PATH_LEN",
initial_val="OS_MAX_PATH_LEN",
input_func=lambda _: next(inputs)
)
self.assertEqual(result, "45")


if __name__ == '__main__':
unittest.main()
Loading