public void GenerateCodeElseIfFullTest() { var trueInstructionsCode = "\tTRUE INSTRUCTIONS CODE"; var trueInstructionsCode2 = "\tTRUE INSTRUCTIONS CODE2"; var falseInstructionsCode = "\tFALSE INSTRUCTIONS CODE"; var indentation = new Indentation(); var trueAndElseIfInsideIf = new IfStatement() { Condition = conditionMockHelper.PrepareMock(ConditionCode2), TrueInstructions = instructionsListMockHelper.PrepareMock(trueInstructionsCode2, false, true, indentation.GetIndentationWithIncrementedLevel()), FalseInstructions = instructionsListMockHelper.PrepareMock(falseInstructionsCode, false, true, indentation.GetIndentationWithIncrementedLevel()) }; var onlyIfInstructionsList = new InstructionsList(); onlyIfInstructionsList.AppendInstruction(trueAndElseIfInsideIf); var trueAndElseIfFull = new IfStatement() { Condition = conditionMockHelper.PrepareMock(ConditionCode), TrueInstructions = instructionsListMockHelper.PrepareMock(trueInstructionsCode, false, true, indentation.GetIndentationWithIncrementedLevel()), FalseInstructions = onlyIfInstructionsList }; string expected = $"if ({ConditionCode}) {{\n{trueInstructionsCode}\n}} else if ({ConditionCode2}) {{\n{trueInstructionsCode2}\n}} else {{\n{falseInstructionsCode}\n}}"; Assert.AreEqual(expected, trueAndElseIfFull.GenerateCode(indentation)); }
public InstructionsList GetInstructionsListFromXmlNode(XmlNode node, XmlNamespaceManager nsmgr) { var instructions = new InstructionsList(); while (node != null) { var instruction = GetInstructionFromXmlNode(node, nsmgr); instructions.AppendInstruction(instruction); node = node.SelectSingleNode("gxml:next/gxml:block", nsmgr); } return(instructions); }
static void Main(string[] args) { var boolType = SolidityType.Bool; var intType = SolidityType.Int; var propertyName = "PropertyName1"; var propertyName2 = "PropertyName2"; var propertyName3 = "PropertyName3"; var pr1 = new ContractProperty() { Variable = PrepareVariable(propertyName, boolType), Visibility = Visibility.Public }; var pr2 = new ContractProperty() { Variable = PrepareVariable(propertyName2, intType), Visibility = Visibility.Private }; var pr3 = new ContractProperty() { Variable = PrepareVariable(propertyName3, boolType), Visibility = Visibility.Public }; var properties = new List <ContractProperty>() { pr1, pr2, pr3 }; var eventName = "EventName1"; var eventName2 = "EventName2"; var eventName3 = "EventName3"; var epl = new ParametersList() { Parameters = new List <Variable>() { PrepareVariable("ep1", boolType), PrepareVariable("ep2", intType) } }; var epl2 = new ParametersList() { Parameters = new List <Variable>() { PrepareVariable("ep1", boolType) } }; var epl3 = new ParametersList() { Parameters = new List <Variable>() { PrepareVariable("ep1", intType) } }; var e1 = new ContractEvent() { Name = eventName, Parameters = epl }; var e2 = new ContractEvent() { Name = eventName2, Parameters = epl2 }; var e3 = new ContractEvent() { Name = eventName3, Parameters = epl3 }; var events = new List <ContractEvent>() { e1, e2, e3 }; var name1 = "_p"; var name2 = "_q"; var name3 = "_r"; var name4 = "v"; var p1 = PrepareVariable(name1, boolType); var p2 = PrepareVariable(name2, boolType); var p3 = PrepareVariable(name3, boolType); var v = PrepareVariable(name4, boolType); var pl = new ParametersList() { Parameters = new List <Variable>() { p1, p2 } }; var pl2 = new ParametersList() { Parameters = new List <Variable>() { p1, p2, p3 } }; var cpl = new CallingParametersList() { Parameters = new List <IAssignable>() { p1, p2 } }; var cpl2 = new CallingParametersList() { Parameters = new List <IAssignable>() { p1, p2, p3 } }; var d = new Declaration() { Variable = v }; var op = new Operation() { LeftSide = p1, Operator = OperationOperator.OR, RightSide = p2 }; var instruction = new Assignment() { Destination = d, Source = op }; var ao = new Operation() { LeftSide = v, Operator = OperationOperator.Negation }; var instruction2 = new Assignment() { Destination = v, Source = ao }; var instructions = new InstructionsList(); instructions.AppendInstruction(instruction); instructions.AppendInstruction(instruction2); var c = new Constructor() { Visibility = Visibility.Public, Parameters = pl, Instructions = instructions }; var functionInstructions = new InstructionsList(); functionInstructions.AppendInstruction(instruction); var aof = new Operation() { LeftSide = v, Operator = OperationOperator.AND, RightSide = p3 }; var instruction3 = new Assignment() { Destination = v, Source = aof }; functionInstructions.AppendInstruction(instruction3); var function = new ContractFunction() { Name = "TestFunction", Visibility = Visibility.Public, Parameters = pl2, Instructions = functionInstructions }; var fp1 = PrepareVariable(name1, intType); var fp2 = PrepareVariable(name2, intType); var fp3 = PrepareVariable(name3, intType); var fv = PrepareVariable(name4, intType); var fpl = new ParametersList() { Parameters = new List <Variable>() { fp1, fp2, fp3 } }; var fd = new Declaration() { Variable = fv }; var fop = new Operation() { LeftSide = p1, Operator = OperationOperator.Plus, RightSide = p2 }; var finstruction = new Assignment() { Destination = fd, Source = fop }; var fao = new Operation() { LeftSide = v, Operator = OperationOperator.Multiply, RightSide = fp3 }; var finstruction2 = new Assignment() { Destination = pr2, Source = fao }; var finstructions = new InstructionsList(); finstructions.AppendInstruction(finstruction); finstructions.AppendInstruction(finstruction2); var function2 = new ContractFunction() { Name = "TestFunction2", Visibility = Visibility.External, Parameters = fpl, Instructions = finstructions }; var trueInstructions = new InstructionsList(); var falseInstructions = new InstructionsList(); trueInstructions.AppendInstruction( new FunctionCall() { FunctionToCall = function, Parameters = cpl2 } ); var ecpl = new CallingParametersList() { Parameters = new List <IAssignable>() { p1 } }; falseInstructions.AppendInstruction( new EventCall() { EventToCall = e2, Parameters = ecpl } ); var newFInstructions = new InstructionsList(); var condOp = new Operation() { LeftSide = p1, Operator = OperationOperator.Negation }; var cond = new Condition() { ConditionOperation = condOp }; var ifStatement = new IfStatement() { Condition = cond, TrueInstructions = trueInstructions, FalseInstructions = falseInstructions }; newFInstructions.AppendInstruction(ifStatement); var loopVariable = PrepareVariable("loopVariable", intType); var declaration = new Declaration() { Variable = loopVariable }; var assignment = new Assignment() { Destination = declaration, Source = new ConstantValue() { Value = "0" } }; var condOperation = new Operation() { LeftSide = loopVariable, Operator = OperationOperator.NotEquals, RightSide = new ConstantValue() { Value = "1" } }; var breakCondition = new Condition() { ConditionOperation = condOperation }; var stepOp = new Operation() { LeftSide = loopVariable, Operator = OperationOperator.Plus, RightSide = new ConstantValue() { Value = "1" } }; var stepInstruction = new Assignment() { Destination = loopVariable, Source = stepOp }; var loopInstructions = new InstructionsList(); var cple = new CallingParametersList() { Parameters = new List <IAssignable>() { loopVariable } }; loopInstructions.AppendInstruction( new EventCall() { EventToCall = e3, Parameters = cple } ); var loop = new ContractLoop() { InitialAssignment = assignment, BreakCondition = breakCondition, StepInstruction = stepInstruction, Instructions = loopInstructions }; newFInstructions.AppendInstruction(loop); var function3 = new ContractFunction() { Name = "NewFunction", Visibility = Visibility.Public, Parameters = pl2, Instructions = newFInstructions }; var functions = new List <ContractFunction>() { function, function2, function3 }; string contractName = "TestContract"; Contract contract = new Contract() { Name = contractName, Constructor = c, Functions = functions, Events = events, Properties = properties }; Console.WriteLine(contract.GenerateCode(new Indentation())); }
static IEnumerable <object[]> GetDataForTests() { var instructionCode1 = "INSTRUCTION CODE 1"; var instructionCode2 = "INSTRUCTION CODE 2"; var instructionCode3 = "INSTRUCTION CODE 3"; var instructionCode4 = "INSTRUCTION CODE 4"; var indentationLevelZero = new Indentation(); var instructionMock1 = mockHelper.PrepareMock(instructionCode1, indentationLevelZero); var instructionMock2 = mockHelper.PrepareMock(instructionCode2, indentationLevelZero); var oneLineInstructionMock3 = mockHelper.PrepareOneLineInstructionMock(instructionCode3); var oneLineInstructionMock4 = mockHelper.PrepareOneLineInstructionMock(instructionCode4); var emptyInstructionsList = new InstructionsList(); yield return(new object[] { emptyInstructionsList, indentationLevelZero, string.Empty }); yield return(new object[] { emptyInstructionsList, indentationLevelZero.GetIndentationWithIncrementedLevel(), string.Empty }); var instructionsList1 = new InstructionsList(); instructionsList1.AppendInstruction(instructionMock1); yield return(new object[] { instructionsList1, indentationLevelZero, instructionCode1 }); var instructionMockWithLevel2 = mockHelper.PrepareMock(instructionCode1, indentationLevelZero.GetIndentationWithIncrementedLevel()); var instructionsListWithLevel2 = new InstructionsList(); instructionsListWithLevel2.AppendInstruction(instructionMockWithLevel2); yield return(new object[] { instructionsListWithLevel2, indentationLevelZero.GetIndentationWithIncrementedLevel(), $"\t{instructionCode1}" }); var oneLineInstructionsList1 = new InstructionsList(); oneLineInstructionsList1.AppendInstruction(oneLineInstructionMock3); yield return(new object[] { oneLineInstructionsList1, indentationLevelZero, $"{instructionCode3};" }); var oneLineInstructionsList2 = new InstructionsList(); oneLineInstructionsList2.AppendInstruction(oneLineInstructionMock3); oneLineInstructionsList2.AppendInstruction(oneLineInstructionMock4); yield return(new object[] { oneLineInstructionsList2, indentationLevelZero.GetIndentationWithIncrementedLevel(), $"\t{instructionCode3};\n\t{instructionCode4};" }); var instructionsList2 = new InstructionsList(); instructionsList2.AppendInstruction(instructionMock1); instructionsList2.AppendInstruction(instructionMock2); yield return(new object[] { instructionsList2, indentationLevelZero, $"{instructionCode1}\n{instructionCode2}" }); var instructionsList3 = new InstructionsList(); var instructionMockWithLevel3 = mockHelper.PrepareMock(instructionCode1, indentationLevelZero.GetIndentationWithIncrementedLevel().GetIndentationWithIncrementedLevel()); var instructionMock2WithLevel3 = mockHelper.PrepareMock(instructionCode2, indentationLevelZero.GetIndentationWithIncrementedLevel().GetIndentationWithIncrementedLevel()); instructionsList3.AppendInstruction(instructionMockWithLevel3); instructionsList3.AppendInstruction(instructionMock2WithLevel3); instructionsList3.AppendInstruction(oneLineInstructionMock3); var indentationLvl2 = indentationLevelZero.GetIndentationWithIncrementedLevel().GetIndentationWithIncrementedLevel(); yield return(new object[] { instructionsList3, indentationLvl2, $"\t\t{instructionCode1}\n\t\t{instructionCode2}\n\t\t{instructionCode3};" }); var instructionsList4 = new InstructionsList(); instructionsList4.AppendInstruction(instructionMock1); instructionsList4.AppendInstruction(instructionMock2); instructionsList4.AppendInstruction(oneLineInstructionMock3); instructionsList4.AppendInstruction(oneLineInstructionMock4); yield return(new object[] { instructionsList4, indentationLevelZero, $"{instructionCode1}\n{instructionCode2}\n{instructionCode3};\n{instructionCode4};" }); }