Skip to content

Commit

Permalink
add tests for pixit validator
Browse files Browse the repository at this point in the history
  • Loading branch information
asirko-soft committed Dec 16, 2024
1 parent 13021d6 commit ef7d4bc
Showing 1 changed file with 125 additions and 1 deletion.
126 changes: 125 additions & 1 deletion src/python_testing/TestMatterTestingSupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import chip.clusters as Clusters
from chip.clusters.Types import Nullable, NullValue
from chip.testing.matter_testing import (MatterBaseTest, async_test_body, compare_time, default_matter_test_main,
from chip.testing.matter_testing import (MatterBaseTest, PIXITDefinition, PIXITType, PIXITValidationError, PIXITValidator, async_test_body, compare_time, default_matter_test_main,
get_wait_seconds_from_set_time, parse_matter_test_args, type_matches,
utc_time_in_matter_epoch)
from chip.testing.pics import parse_pics, parse_pics_xml
Expand Down Expand Up @@ -656,6 +656,130 @@ def test_parse_matter_test_args(self):
asserts.assert_equal(parsed.global_test_params.get("PIXIT.TEST.STR.MULTI.2"), "bar")
asserts.assert_equal(parsed.global_test_params.get("PIXIT.TEST.JSON"), {"key": "value"})

def test_pixit_validation(self):
# Test integer validation
PIXITValidator.validate_int_pixit_value("123") # Should pass
PIXITValidator.validate_int_pixit_value("-123") # Should pass
PIXITValidator.validate_int_pixit_value("0") # Should pass

try:
PIXITValidator.validate_int_pixit_value("abc")
asserts.fail("Expected ValueError for invalid integer")
except ValueError:
pass

try:
PIXITValidator.validate_int_pixit_value("12.34")
asserts.fail("Expected ValueError for float value")
except ValueError:
pass

# Test boolean validation
PIXITValidator.validate_bool_pixit_value("true") # Should pass
PIXITValidator.validate_bool_pixit_value("false") # Should pass
PIXITValidator.validate_bool_pixit_value("True") # Should pass
PIXITValidator.validate_bool_pixit_value("False") # Should pass

try:
PIXITValidator.validate_bool_pixit_value("yes")
asserts.fail("Expected ValueError for invalid boolean")
except ValueError:
pass

try:
PIXITValidator.validate_bool_pixit_value("1")
asserts.fail("Expected ValueError for numeric boolean")
except ValueError:
pass

# Test float validation
PIXITValidator.validate_float_pixit_value("123.45") # Should pass
PIXITValidator.validate_float_pixit_value("-123.45") # Should pass
PIXITValidator.validate_float_pixit_value("0.0") # Should pass

try:
PIXITValidator.validate_float_pixit_value("abc")
asserts.fail("Expected ValueError for invalid float")
except ValueError:
pass

# Test string validation - should accept most inputs
PIXITValidator.validate_string_pixit_value("abc") # Should pass
PIXITValidator.validate_string_pixit_value("123") # Should pass

# Test JSON validation
PIXITValidator.validate_json_pixit_value('{"key": "value"}') # Should pass
PIXITValidator.validate_json_pixit_value('[]') # Should pass
PIXITValidator.validate_json_pixit_value('null') # Should pass

try:
PIXITValidator.validate_json_pixit_value('{invalid json}')
asserts.fail("Expected ValueError for invalid JSON")
except ValueError:
pass

try:
PIXITValidator.validate_json_pixit_value('{"unclosed": "object"')
asserts.fail("Expected ValueError for malformed JSON")
except ValueError:
pass

# Test hex validation
PIXITValidator.validate_hex_pixit_value("0x1234") # Should pass
PIXITValidator.validate_hex_pixit_value("1234") # Should pass
PIXITValidator.validate_hex_pixit_value("ABCD") # Should pass
PIXITValidator.validate_hex_pixit_value("abcd") # Should pass

try:
PIXITValidator.validate_hex_pixit_value("0x123") # Odd number of digits
asserts.fail("Expected ValueError for odd number of hex digits")
except ValueError:
pass

try:
PIXITValidator.validate_hex_pixit_value("123") # Odd number of digits
asserts.fail("Expected ValueError for odd number of hex digits")
except ValueError:
pass

try:
PIXITValidator.validate_hex_pixit_value("WXYZ") # Invalid hex chars
asserts.fail("Expected ValueError for invalid hex characters")
except ValueError:
pass

# Test full PIXIT validation
pixit_def = PIXITDefinition(
name="PIXIT.TEST.INT",
pixit_type=PIXITType.INT,
description="Test integer PIXIT",
required=True
)

PIXITValidator.validate_value("123", pixit_def) # Should pass

try:
PIXITValidator.validate_value("abc", pixit_def)
asserts.fail("Expected PIXITValidationError for invalid type")
except PIXITValidationError:
pass

try:
PIXITValidator.validate_value(None, pixit_def) # Required but None
asserts.fail("Expected PIXITValidationError for missing required value")
except PIXITValidationError:
pass

# Test optional PIXIT
optional_pixit = PIXITDefinition(
name="PIXIT.TEST.INT",
pixit_type=PIXITType.INT,
description="Test integer PIXIT",
required=False
)

PIXITValidator.validate_value(None, optional_pixit) # Should pass as it's optional


if __name__ == "__main__":
default_matter_test_main()

0 comments on commit ef7d4bc

Please sign in to comment.