|
| 1 | +using Xunit; |
| 2 | +using FluentAssertions; |
| 3 | +using ZXBasicStudio.BuildSystem; |
| 4 | +using System.Runtime.Serialization; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | + |
| 8 | +namespace ZXBasicStudioTest |
| 9 | +{ |
| 10 | + public class SigilMappingTests |
| 11 | + { |
| 12 | + [Fact] |
| 13 | + public void GetVariables_ShouldMatchSigilVariable() |
| 14 | + { |
| 15 | + // Arrange |
| 16 | + // We use GetUninitializedObject to skip the constructor which depends on files |
| 17 | + var basicMap = (ZXBasicMap)FormatterServices.GetUninitializedObject(typeof(ZXBasicMap)); |
| 18 | + |
| 19 | + basicMap.GlobalVariables = new[] |
| 20 | + { |
| 21 | + new ZXBasicVariable { Name = "a$" } |
| 22 | + }; |
| 23 | + basicMap.Subs = new ZXBasicSub[0]; |
| 24 | + basicMap.Functions = new ZXBasicFunction[0]; |
| 25 | + basicMap.BuildLocations = new ZXBasicLocation[0]; |
| 26 | + |
| 27 | + // IC Content mimicking a global variable '_a' |
| 28 | + string icContent = "--- end of user code ---\n('var', '_a', '0')"; |
| 29 | + // Map content mimicking the same variable |
| 30 | + string mapContent = "8000: ._a"; |
| 31 | + |
| 32 | + // Act |
| 33 | + var variableMap = (ZXVariableMap)FormatterServices.GetUninitializedObject(typeof(ZXVariableMap)); |
| 34 | + // We need to initialize the private 'vars' list since we used GetUninitializedObject |
| 35 | + var varsField = typeof(ZXVariableMap).GetField("vars", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); |
| 36 | + varsField!.SetValue(variableMap, new List<ZXVariable>()); |
| 37 | + |
| 38 | + variableMap.ProcessGlobalVariables(icContent, mapContent, basicMap); |
| 39 | + var variables = variableMap.Variables; |
| 40 | + |
| 41 | + // Assert |
| 42 | + variables.Should().NotBeNull(); |
| 43 | + var variable = variables.FirstOrDefault(v => v.Name == "a$"); |
| 44 | + variable.Should().NotBeNull("Variable 'a$' should be found even if IC uses '_a'"); |
| 45 | + variable!.Address.AddressValue.Should().Be(0x8000); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void GetVariables_ShouldBeCaseInsensitive() |
| 50 | + { |
| 51 | + // Arrange |
| 52 | + var basicMap = (ZXBasicMap)FormatterServices.GetUninitializedObject(typeof(ZXBasicMap)); |
| 53 | + |
| 54 | + basicMap.GlobalVariables = new[] |
| 55 | + { |
| 56 | + new ZXBasicVariable { Name = "MyVar$" } |
| 57 | + }; |
| 58 | + basicMap.Subs = new ZXBasicSub[0]; |
| 59 | + basicMap.Functions = new ZXBasicFunction[0]; |
| 60 | + basicMap.BuildLocations = new ZXBasicLocation[0]; |
| 61 | + |
| 62 | + // IC Content uses lowercase '_myvar' |
| 63 | + string icContent = "--- end of user code ---\n('var', '_myvar', '0')"; |
| 64 | + string mapContent = "9000: ._myvar"; |
| 65 | + |
| 66 | + // Act |
| 67 | + var variableMap = (ZXVariableMap)FormatterServices.GetUninitializedObject(typeof(ZXVariableMap)); |
| 68 | + var varsField = typeof(ZXVariableMap).GetField("vars", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); |
| 69 | + varsField!.SetValue(variableMap, new List<ZXVariable>()); |
| 70 | + |
| 71 | + variableMap.ProcessGlobalVariables(icContent, mapContent, basicMap); |
| 72 | + var variables = variableMap.Variables; |
| 73 | + |
| 74 | + // Assert |
| 75 | + var variable = variables.FirstOrDefault(v => v.Name == "MyVar$"); |
| 76 | + variable.Should().NotBeNull("Variable 'MyVar$' should be matched case-insensitively"); |
| 77 | + variable!.Address.AddressValue.Should().Be(0x9000); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void ProcessLocalVariables_ShouldMatchSubNameWithSigil() |
| 82 | + { |
| 83 | + // Arrange |
| 84 | + var basicMap = (ZXBasicMap)FormatterServices.GetUninitializedObject(typeof(ZXBasicMap)); |
| 85 | + |
| 86 | + var sub = new ZXBasicSub { Name = "MySub$" }; |
| 87 | + sub.LocalVariables = new List<ZXBasicVariable>(); |
| 88 | + sub.InputParameters = new List<ZXBasicParameter> |
| 89 | + { |
| 90 | + new ZXBasicParameter { Name = "param1", Offset = -2, Storage = ZXVariableStorage.U16 } |
| 91 | + }; |
| 92 | + |
| 93 | + basicMap.GlobalVariables = new ZXBasicVariable[0]; |
| 94 | + basicMap.Subs = new[] { sub }; |
| 95 | + basicMap.Functions = new ZXBasicFunction[0]; |
| 96 | + basicMap.BuildLocations = new[] |
| 97 | + { |
| 98 | + new ZXBasicLocation { Name = "MySub$", LocationType = ZXBasicLocationType.Sub, FirstLine = 0, LastLine = 10, File = "main.bas" } |
| 99 | + }; |
| 100 | + |
| 101 | + // IC Content showing start and end of MySub (sigil is stripped in label usually: _MySub) |
| 102 | + // Note: ProcessLocalVariables extracts locName = label.Substring(1) from '_MySub' -> 'MySub' |
| 103 | + string icContent = "('label', '_MySub')\n('label', '_MySub__leave')\n--- end of user code ---"; |
| 104 | + // Map content showing start and end addresses |
| 105 | + string mapContent = "8000: ._MySub\n8010: ._MySub__leave"; |
| 106 | + |
| 107 | + // Act |
| 108 | + var variableMap = (ZXVariableMap)FormatterServices.GetUninitializedObject(typeof(ZXVariableMap)); |
| 109 | + var varsField = typeof(ZXVariableMap).GetField("vars", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); |
| 110 | + varsField!.SetValue(variableMap, new List<ZXVariable>()); |
| 111 | + |
| 112 | + variableMap.ProcessLocalVariables(icContent, mapContent, basicMap); |
| 113 | + var variables = variableMap.Variables; |
| 114 | + |
| 115 | + // Assert |
| 116 | + variables.Should().NotBeNull(); |
| 117 | + // If MySub$ was matched correctly, its parameters should be added |
| 118 | + variables.Should().Contain(v => v.Name == "param1", "Sub MySub$ should be matched to label _MySub and its parameters processed"); |
| 119 | + var param = variables.First(v => v.Name == "param1"); |
| 120 | + param.Scope.ScopeName.Should().Be("MySub"); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments