public FoundScript(ClassModel oldClassModel, ClassModel newClassModel, YamlNode yamlOptions) { this.newClassModel = newClassModel; this.oldClassModel = oldClassModel; this.YamlOptions = yamlOptions; this.HasBeenMapped = CheckHasBeenMapped(oldClassModel, newClassModel); }
/// <summary> /// Checks if the field has a exact match between the yaml and the classField /// </summary> /// <param name="data"></param> /// <param name="node"></param> /// <returns></returns> public MappedState CheckHasBeenMapped(ClassModel oldClassModel, ClassModel newClassModel) { if (HasBeenMapped == MappedState.NotChecked) { bool result = checkHasBeenMappedRecursive(oldClassModel, newClassModel); HasBeenMapped = result ? MappedState.Mapped : MappedState.NotMapped; } return(HasBeenMapped); }
public void GetSubStatePath_Test() { var stateName = "Balances"; var state = new MappedState(); state.Path = new StatePath(); state.Path.Parts.Add(stateName); var key = "ELF"; var statePath = state.GetSubStatePath(key); statePath.Parts.Count.ShouldBe(2); statePath.Parts[0].ShouldBe(stateName); statePath.Parts[1].ShouldBe(key); }
/// <summary> /// Maps all scriptMappings for all variables and children of the type of the variable /// Finds the scriptMapping that you need and otherwise creates it. /// </summary> /// <param name="newIDs">The IDs of the new project</param> /// <param name="scriptMappings">The existing scriptMappings that will be looked in and added to</param> /// <param name="oldClassModel">Current class data of the old project as this maps to the scene file</param> /// <returns></returns> /// <exception cref="NullReferenceException"></exception> public ScriptMapping FindMappingRecursively(List <ClassModel> newIDs, ref List <ScriptMapping> scriptMappings, ClassModel oldClassModel) { // Can't look for something if we don't know what we're looking for if (oldClassModel == null) { throw new NullReferenceException("No old classData found"); } ScriptMapping existingScriptMapping = scriptMappings.FirstOrDefault( script => script.oldClassModel.FullName == oldClassModel.FullName ); ClassModel replacementClassModel = existingScriptMapping?.newClassModel; if (replacementClassModel == null && oldClassModel.Fields != null) { replacementClassModel = FindNewID(newIDs, oldClassModel); if (replacementClassModel == null) { return(null); } } else if (replacementClassModel != null) { return(existingScriptMapping); } else { return(null); } if (existingScriptMapping != null) { return(existingScriptMapping); } existingScriptMapping = new ScriptMapping { oldClassModel = oldClassModel, newClassModel = replacementClassModel }; MappedState hasBeenMapped = existingScriptMapping.CheckHasBeenMapped(); switch (hasBeenMapped) { case MappedState.NotMapped: existingScriptMapping.GenerateMappingNode(); break; case MappedState.Ignored: return(null); } scriptMappings.Add(existingScriptMapping); //If it doesn't exist then create it if (oldClassModel.Fields != null && oldClassModel.Fields.Length != 0) { foreach (FieldModel field in oldClassModel.Fields) { // Check if the type is null as this would cause so errors in the mapping if (field.Type == null) { throw new NullReferenceException("type of field is null for some reason"); } FindMappingRecursively(newIDs, ref scriptMappings, field.Type); } } return(existingScriptMapping); }
public void MappedState_Test() { var path = new StatePath(); path.Parts.Add("dummy_address"); var mockProvider = new Mock <IStateProvider>(); var mockContext = new Mock <ISmartContractBridgeContext>(); mockContext.SetupGet(o => o.StateProvider).Returns(mockProvider.Object); mockContext.SetupGet(o => o.Self).Returns(SampleAddress.AddressList[0]); var state = new MappedState <Address, Address, Address, string> { Path = path, Context = new CSharpSmartContractContext(mockContext.Object) }; state[SampleAddress.AddressList[0]][SampleAddress.AddressList[1]][SampleAddress.AddressList[2]] .ShouldBe(string.Empty); state[SampleAddress.AddressList[0]][SampleAddress.AddressList[1]][SampleAddress.AddressList[2]] = "test"; state[SampleAddress.AddressList[0]][SampleAddress.AddressList[1]][SampleAddress.AddressList[2]] .ShouldBe("test"); state[SampleAddress.AddressList[0]][SampleAddress.AddressList[1]].Remove(SampleAddress.AddressList[2]); state[SampleAddress.AddressList[0]][SampleAddress.AddressList[1]][SampleAddress.AddressList[2]].ShouldBeNull(); state[SampleAddress.AddressList[0]][SampleAddress.AddressList[1]][SampleAddress.AddressList[3]] = "test2"; state.OnContextSet(); state.Context.ShouldBe(state[SampleAddress.AddressList[0]].Context); var changes = state.GetChanges(); changes.Deletes.Count.ShouldBe(1); changes.Writes.Count.ShouldBe(1); changes.Reads.Count.ShouldBe(changes.Deletes.Count + changes.Writes.Count); state.Clear(); state.GetChanges().ShouldBe(new TransactionExecutingStateSet()); var mapState = new MappedState <Address, Address, Address, Address, string> { Path = path, Context = new CSharpSmartContractContext(mockContext.Object) }; mapState[SampleAddress.AddressList[0]][SampleAddress.AddressList[1]][SampleAddress.AddressList[2]][SampleAddress.AddressList[3]] .ShouldBe(string.Empty); mapState[SampleAddress.AddressList[0]][SampleAddress.AddressList[1]][SampleAddress.AddressList[2]][SampleAddress.AddressList[3]] = "test"; mapState[SampleAddress.AddressList[0]][SampleAddress.AddressList[1]][SampleAddress.AddressList[2]][SampleAddress.AddressList[3]] .ShouldBe("test"); mapState[SampleAddress.AddressList[0]][SampleAddress.AddressList[1]][SampleAddress.AddressList[2]].Remove(SampleAddress.AddressList[3]); mapState[SampleAddress.AddressList[0]][SampleAddress.AddressList[1]][SampleAddress.AddressList[2]][SampleAddress.AddressList[3]].ShouldBeNull(); mapState[SampleAddress.AddressList[0]][SampleAddress.AddressList[1]][SampleAddress.AddressList[2]][SampleAddress.AddressList[4]] = "test2"; mapState.OnContextSet(); mapState.Context.ShouldBe(mapState[SampleAddress.AddressList[0]].Context); changes = mapState.GetChanges(); changes.Deletes.Count.ShouldBe(1); changes.Writes.Count.ShouldBe(1); changes.Reads.Count.ShouldBe(changes.Deletes.Count + changes.Writes.Count); mapState.Clear(); mapState.GetChanges().ShouldBe(new TransactionExecutingStateSet()); }
/// <summary> /// Maps all foundScripts for all variables and children of the type of the variable /// </summary> /// <param name="newIDs">The IDs of the new project</param> /// <param name="foundScripts">The existing foundScripts that will be looked in and added to</param> /// <param name="oldClassModel">Current class data of the old project as this maps to the scene file</param> /// <returns></returns> /// <exception cref="NullReferenceException"></exception> private FoundScript FoundScriptMappingRecursively(List <ClassModel> newIDs, ref List <FoundScript> foundScripts, ClassModel oldClassModel) { if (oldClassModel == null) { throw new NullReferenceException("No old classData found"); } FoundScript existingFoundScript = foundScripts.FirstOrDefault(script => script.oldClassModel.FullName == oldClassModel.FullName); ClassModel replacementClassModel = existingFoundScript ?.newClassModel; if (replacementClassModel == null && oldClassModel.Fields != null) { replacementClassModel = findNewID(newIDs, oldClassModel); //todo : testScript gets called double if (replacementClassModel == null) { return(null); } } else if (replacementClassModel != null) { return(existingFoundScript); } else { return(null); } if (existingFoundScript != null) { return(existingFoundScript); } existingFoundScript = new FoundScript { oldClassModel = oldClassModel, newClassModel = replacementClassModel }; MappedState hasBeenMapped = existingFoundScript.CheckHasBeenMapped(); if (hasBeenMapped == MappedState.NotMapped) { existingFoundScript.GenerateMappingNode(foundScripts); } foundScripts.Add(existingFoundScript); //If it doesn't exist then create it if (oldClassModel.Fields != null && oldClassModel.Fields.Length != 0) { foreach (FieldModel field in oldClassModel.Fields) { // Check if the type is null as this would cause so errors in the mapping if (field.Type == null) { throw new NullReferenceException("type of field is null for some reason"); } FoundScriptMappingRecursively(newIDs, ref foundScripts, field.Type); } } return(existingFoundScript); }