@@ -534,11 +534,67 @@ def test_BeamLine_with_string_references():
534534
535535 assert beamline .name == "fodo_cell"
536536 assert len (beamline .line ) == 3
537- # First element should be a string reference
537+
538+ # First element should be an ElementReference that behaves like the string "drift1"
539+ assert isinstance (beamline .line [0 ], pals .ElementReference )
538540 assert beamline .line [0 ] == "drift1"
539- # Second element should be a string reference
541+ assert beamline .line [0 ].name == "drift1"
542+ assert beamline .line [0 ].element is None # Not yet resolved
543+ assert not beamline .line [0 ].is_resolved ()
544+
545+ # Second element should be an ElementReference that behaves like the string "quad1"
546+ assert isinstance (beamline .line [1 ], pals .ElementReference )
540547 assert beamline .line [1 ] == "quad1"
548+ assert beamline .line [1 ].name == "quad1"
549+ assert beamline .line [1 ].element is None # Not yet resolved
550+ assert not beamline .line [1 ].is_resolved ()
551+
541552 # Third element should be a Drift object
542553 assert isinstance (beamline .line [2 ], pals .Drift )
543554 assert beamline .line [2 ].name == "drift2"
544555 assert beamline .line [2 ].length == 0.5
556+
557+ # Test that we can resolve the reference later
558+ drift_element = pals .Drift (name = "drift1" , length = 1.0 )
559+ beamline .line [0 ].element = drift_element
560+ assert beamline .line [0 ].is_resolved ()
561+ assert beamline .line [0 ].element .name == "drift1"
562+ assert beamline .line [0 ].element .length == 1.0
563+
564+
565+ def test_ElementReference_direct ():
566+ """Test ElementReference creation and behavior directly"""
567+ # Test creation with positional argument
568+ ref1 = pals .ElementReference ("test_element" )
569+ assert ref1 .name == "test_element"
570+ assert str (ref1 ) == "test_element"
571+ assert ref1 == "test_element"
572+ assert not ref1 .is_resolved ()
573+
574+ # Test creation with keyword argument
575+ ref2 = pals .ElementReference (name = "another_element" )
576+ assert ref2 .name == "another_element"
577+ assert str (ref2 ) == "another_element"
578+ assert ref2 == "another_element"
579+
580+ # Test hash (for use in sets/dicts)
581+ ref_set = {ref1 , ref2 }
582+ assert len (ref_set ) == 2
583+ assert ref1 in ref_set
584+
585+ # Test resolution
586+ drift = pals .Drift (name = "test_element" , length = 2.5 )
587+ ref1 .element = drift
588+ assert ref1 .is_resolved ()
589+ assert ref1 .element .length == 2.5
590+
591+ # Test repr
592+ assert "test_element" in repr (ref1 )
593+ assert "resolved" in repr (ref1 )
594+ assert "unresolved" in repr (ref2 )
595+
596+ # Test that element is a reference, not a copy
597+ assert ref1 .element is drift # Same object identity
598+ # Modify the original and verify the reference sees the change
599+ drift .length = 3.0
600+ assert ref1 .element .length == 3.0 # Change is visible through reference
0 commit comments