Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

test_boolean_factory.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python3
  2. """
  3. Simple test script to verify boolean type inference in variable factory.
  4. """
  5. import sys
  6. import os
  7. # Add the api directory to the Python path
  8. sys.path.insert(0, os.path.join(os.path.dirname(__file__), "api"))
  9. try:
  10. from factories.variable_factory import build_segment, segment_to_variable
  11. from core.variables.segments import BooleanSegment, ArrayBooleanSegment
  12. from core.variables.variables import BooleanVariable, ArrayBooleanVariable
  13. from core.variables.types import SegmentType
  14. def test_boolean_inference():
  15. print("Testing boolean type inference...")
  16. # Test single boolean values
  17. true_segment = build_segment(True)
  18. false_segment = build_segment(False)
  19. print(f"True value: {true_segment}")
  20. print(f"Type: {type(true_segment)}")
  21. print(f"Value type: {true_segment.value_type}")
  22. print(f"Is BooleanSegment: {isinstance(true_segment, BooleanSegment)}")
  23. print(f"\nFalse value: {false_segment}")
  24. print(f"Type: {type(false_segment)}")
  25. print(f"Value type: {false_segment.value_type}")
  26. print(f"Is BooleanSegment: {isinstance(false_segment, BooleanSegment)}")
  27. # Test array of booleans
  28. bool_array_segment = build_segment([True, False, True])
  29. print(f"\nBoolean array: {bool_array_segment}")
  30. print(f"Type: {type(bool_array_segment)}")
  31. print(f"Value type: {bool_array_segment.value_type}")
  32. print(
  33. f"Is ArrayBooleanSegment: {isinstance(bool_array_segment, ArrayBooleanSegment)}"
  34. )
  35. # Test empty boolean array
  36. empty_bool_array = build_segment([])
  37. print(f"\nEmpty array: {empty_bool_array}")
  38. print(f"Type: {type(empty_bool_array)}")
  39. print(f"Value type: {empty_bool_array.value_type}")
  40. # Test segment to variable conversion
  41. bool_var = segment_to_variable(
  42. segment=true_segment, selector=["test", "bool_var"], name="test_boolean"
  43. )
  44. print(f"\nBoolean variable: {bool_var}")
  45. print(f"Type: {type(bool_var)}")
  46. print(f"Is BooleanVariable: {isinstance(bool_var, BooleanVariable)}")
  47. array_bool_var = segment_to_variable(
  48. segment=bool_array_segment,
  49. selector=["test", "array_bool_var"],
  50. name="test_array_boolean",
  51. )
  52. print(f"\nArray boolean variable: {array_bool_var}")
  53. print(f"Type: {type(array_bool_var)}")
  54. print(
  55. f"Is ArrayBooleanVariable: {isinstance(array_bool_var, ArrayBooleanVariable)}"
  56. )
  57. # Test that bool comes before int (critical ordering)
  58. print(f"\nTesting bool vs int precedence:")
  59. print(f"True is instance of bool: {isinstance(True, bool)}")
  60. print(f"True is instance of int: {isinstance(True, int)}")
  61. print(f"False is instance of bool: {isinstance(False, bool)}")
  62. print(f"False is instance of int: {isinstance(False, int)}")
  63. # Verify that boolean values are correctly inferred as boolean, not int
  64. assert true_segment.value_type == SegmentType.BOOLEAN, (
  65. "True should be inferred as BOOLEAN"
  66. )
  67. assert false_segment.value_type == SegmentType.BOOLEAN, (
  68. "False should be inferred as BOOLEAN"
  69. )
  70. assert bool_array_segment.value_type == SegmentType.ARRAY_BOOLEAN, (
  71. "Boolean array should be inferred as ARRAY_BOOLEAN"
  72. )
  73. print("\n✅ All boolean inference tests passed!")
  74. if __name__ == "__main__":
  75. test_boolean_inference()
  76. except ImportError as e:
  77. print(f"Import error: {e}")
  78. print("Make sure you're running this from the correct directory")
  79. except Exception as e:
  80. print(f"Error: {e}")
  81. import traceback
  82. traceback.print_exc()