Esempio n. 1
0
        static IEnumerable <object[]> GetDataForTests()
        {
            var indentation = new Indentation();

            var trueInstructionsCode  = "\tTRUE INSTRUCTIONS CODE";
            var falseInstructionsCode = "\tFALSE INSTRUCTIONS CODE";

            var onlyTrueInstructions = new IfStatement()
            {
                Condition         = conditionMockHelper.PrepareMock(ConditionCode),
                TrueInstructions  = instructionsListMockHelper.PrepareMock(trueInstructionsCode, false, true, indentation.GetIndentationWithIncrementedLevel()),
                FalseInstructions = instructionsListMockHelper.PrepareMock(falseInstructionsCode, false, false, indentation.GetIndentationWithIncrementedLevel())
            };

            yield return(new object[] { onlyTrueInstructions, indentation, $"if ({ConditionCode}) {{\n{trueInstructionsCode}\n}}" });

            trueInstructionsCode = "\t\tTRUE INSTRUCTIONS CODE";

            var onlyTrueInstructionsDoubleIndentation = new IfStatement()
            {
                Condition         = conditionMockHelper.PrepareMock(ConditionCode),
                TrueInstructions  = instructionsListMockHelper.PrepareMock(trueInstructionsCode, false, true, indentation.GetIndentationWithIncrementedLevel().GetIndentationWithIncrementedLevel()),
                FalseInstructions = instructionsListMockHelper.PrepareMock(falseInstructionsCode, false, false, indentation.GetIndentationWithIncrementedLevel().GetIndentationWithIncrementedLevel())
            };

            yield return(new object[] { onlyTrueInstructionsDoubleIndentation, indentation.GetIndentationWithIncrementedLevel(), $"if ({ConditionCode}) {{\n{trueInstructionsCode}\n\t}}" });

            trueInstructionsCode  = "\tTRUE INSTRUCTIONS CODE";
            falseInstructionsCode = "\tFALSE INSTRUCTIONS CODE";

            var trueAndFalseInstructions = new IfStatement()
            {
                Condition         = conditionMockHelper.PrepareMock(ConditionCode),
                TrueInstructions  = instructionsListMockHelper.PrepareMock(trueInstructionsCode, false, true, indentation.GetIndentationWithIncrementedLevel()),
                FalseInstructions = instructionsListMockHelper.PrepareMock(falseInstructionsCode, false, true, indentation.GetIndentationWithIncrementedLevel())
            };

            yield return(new object[] { trueAndFalseInstructions, indentation, $"if ({ConditionCode}) {{\n{trueInstructionsCode}\n}} else {{\n{falseInstructionsCode}\n}}" });


            trueInstructionsCode  = "\t\tTRUE INSTRUCTIONS CODE";
            falseInstructionsCode = "\t\tFALSE INSTRUCTIONS CODE";

            var trueAndFalseInstructionsDoubleIndentation = new IfStatement()
            {
                Condition         = conditionMockHelper.PrepareMock(ConditionCode),
                TrueInstructions  = instructionsListMockHelper.PrepareMock(trueInstructionsCode, false, true, indentation.GetIndentationWithIncrementedLevel().GetIndentationWithIncrementedLevel()),
                FalseInstructions = instructionsListMockHelper.PrepareMock(falseInstructionsCode, false, true, indentation.GetIndentationWithIncrementedLevel().GetIndentationWithIncrementedLevel())
            };

            yield return(new object[] { trueAndFalseInstructionsDoubleIndentation, indentation.GetIndentationWithIncrementedLevel(), $"if ({ConditionCode}) {{\n{trueInstructionsCode}\n\t}} else {{\n{falseInstructionsCode}\n\t}}" });


            var onlyCondition = new IfStatement()
            {
                Condition = conditionMockHelper.PrepareMock(ConditionCode)
            };

            yield return(new object[] { onlyCondition, indentation, $"if ({ConditionCode}) {{\n}}" });
        }
        public virtual string GenerateCode(Indentation indentation)
        {
            if (Condition == null)
            {
                throw new MissingMandatoryElementException("Condition is required in if statement!");
            }

            StringBuilder codeBuilder = new StringBuilder();

            codeBuilder.Append($"if ({Condition.GenerateCode()}) {{");

            if (TrueInstructions != null)
            {
                codeBuilder.Append($"\n{TrueInstructions.GenerateCode(indentation?.GetIndentationWithIncrementedLevel())}");
            }

            codeBuilder.Append($"\n{indentation?.GenerateCode()}}}");


            if (FalseInstructions != null && FalseInstructions.Any())
            {
                if (FalseInstructions.ContainsOnlyIf())
                {
                    var elseIf = FalseInstructions.GetFirstInstruction() as IfStatement;
                    codeBuilder.Append($" else {elseIf.GenerateCode(indentation)}");
                }
                else
                {
                    codeBuilder.Append($" else {{\n");
                    codeBuilder.Append($"{FalseInstructions.GenerateCode(indentation?.GetIndentationWithIncrementedLevel())}\n{indentation?.GenerateCode()}}}");
                }
            }

            return(codeBuilder.ToString());
        }
        static object[] GenerateRow(string constructorCode, string name, List <string> expectedEventsCode, List <string> expectedFunctionsCode, List <string> expectedPropertyCode, List <string> expectedModifierCode, string fallbackFunctionCode, string receiveFunctionCode, string expected)
        {
            var indentation         = new Indentation();
            var indentationLevelOne = indentation.GetIndentationWithIncrementedLevel();
            var c = new Contract()
            {
                Name = name
            };

            if (constructorCode != null)
            {
                c.Constructor = constructorMock.PrepareMock(constructorCode, indentationLevelOne);
            }

            if (expectedPropertyCode != null)
            {
                c.Properties = expectedPropertyCode.Select(code => propertyMock.PrepareMock(null, code)).ToList();
            }

            if (expectedEventsCode != null)
            {
                c.Events = expectedEventsCode.Select(code => eventMock.PrepareMock(code, null)).ToList();
            }

            if (expectedFunctionsCode != null)
            {
                c.Functions = expectedFunctionsCode.Select(code => functionMock.PrepareMock(code, null, indentationLevelOne)).ToList();
            }

            if (expectedModifierCode != null)
            {
                c.Modifiers = expectedModifierCode.Select(code => modifierMock.PrepareMock(code, indentationLevelOne)).ToList();
            }

            if (fallbackFunctionCode != null)
            {
                c.FallbackFunction = fallbackMock.PrepareMock(fallbackFunctionCode, indentationLevelOne);
            }

            if (receiveFunctionCode != null)
            {
                c.ReceiveFunction = receiveMock.PrepareMock(receiveFunctionCode, indentationLevelOne);
            }

            return(new object[] { c, indentation, expected });
        }
        static IEnumerable <object[]> GetDataForTests()
        {
            var    indentationLevelZero = new Indentation();
            string breakConditionCode1  = "BREAK CONDITION1";
            string instructionCode1     = "\tINSTRUCTION CODE1";
            string initialDeclaration1  = "INITIAL DECLARATION CODE1";
            string stepInstruction1     = "STEP INSTRUCTION CODE1";

            yield return(new object[] { GetMockedLoop(breakConditionCode1, instructionCode1, initialDeclaration1, stepInstruction1, indentationLevelZero), indentationLevelZero, $"for ({initialDeclaration1}; {breakConditionCode1}; {stepInstruction1}) {{\n{instructionCode1}\n}}" });

            string breakConditionCode2 = "BREAK CONDITION2";
            string instructionCode2    = "\tINSTRUCTION CODE2";
            string initialDeclaration2 = "INITIAL DECLARATION CODE2";
            string stepInstruction2    = "STEP INSTRUCTION CODE2";

            yield return(new object[] { GetMockedLoop(breakConditionCode2, instructionCode2, initialDeclaration2, stepInstruction2, indentationLevelZero), indentationLevelZero, $"for ({initialDeclaration2}; {breakConditionCode2}; {stepInstruction2}) {{\n{instructionCode2}\n}}" });

            var    indentationLevelOne         = indentationLevelZero.GetIndentationWithIncrementedLevel();
            string instructionCodeWithLevelTwo = "\t\tINSTRUCTION CODE1";

            yield return(new object[] { GetMockedLoop(breakConditionCode1, instructionCodeWithLevelTwo, initialDeclaration1, stepInstruction1, indentationLevelOne), indentationLevelOne, $"for ({initialDeclaration1}; {breakConditionCode1}; {stepInstruction1}) {{\n{instructionCodeWithLevelTwo}\n\t}}" });
        }
Esempio n. 5
0
        static object[] GenerateRow(string parametersListCode, string name, string instructionsListCode, Visibility?visibility, Modifier m, string modifierParametersListCode, bool anyModifierParameter, SolidityType?returningType, ModificationType modificationType, bool isPayable, string expected)
        {
            var indentation        = new Indentation();
            var parametersListMock = parametersListCode != null?mockHelper.PrepareMock(parametersListCode, true, true) : null;

            var instructionsListMock = instructionsListCode != null?instructionsListMockHelper.PrepareMock(instructionsListCode, false, !string.IsNullOrWhiteSpace(instructionsListCode), indentation.GetIndentationWithIncrementedLevel()) : null;

            var ma = m != null ? new ModifierAppliance()
            {
                ModifierToApply = m,
                Parameters      = callingParametersMockHelper.PrepareMock(modifierParametersListCode, anyModifierParameter)
            } : null;

            var f = new ContractFunction()
            {
                Instructions     = instructionsListMock,
                Name             = name,
                Parameters       = parametersListMock,
                Visibility       = visibility,
                Modifier         = ma,
                ReturningType    = returningType,
                ModificationType = modificationType,
                IsPayable        = isPayable
            };

            return(new object[] { f, indentation, expected });
        }
Esempio n. 6
0
        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};" });
        }
        static object[] GenerateRow(string parametersListCode, Visibility visibility, string instructionsListCode, string expected)
        {
            var indentation        = new Indentation();
            var parametersListMock = parametersListCode != null?mockHelper.PrepareMock(parametersListCode, true, true) : null;

            var instructionsListMock = instructionsListCode != null?instructionsListMockHelper.PrepareMock(instructionsListCode, false, !string.IsNullOrWhiteSpace(instructionsListCode), indentation.GetIndentationWithIncrementedLevel()) : null;

            var c = new Constructor()
            {
                Visibility   = visibility,
                Parameters   = parametersListMock,
                Instructions = instructionsListMock
            };

            return(new object[] { c, indentation, expected });
        }
Esempio n. 8
0
        static IEnumerable <object[]> GenerateTestData()
        {
            var instructionCode1 = "INSTRUCTION CODE 1";
            var instructionCode2 = "INSTRUCTION CODE 2";
            var indentation      = new Indentation();

            var instructionsListMock      = instructionsListMockHelper.PrepareMock(instructionCode1, false, true, indentation.GetIndentationWithIncrementedLevel());
            var instructionsListMock2     = instructionsListMockHelper.PrepareMock(instructionCode2, false, true, indentation.GetIndentationWithIncrementedLevel());
            var emptyInstructionsListMock = instructionsListMockHelper.PrepareMock(string.Empty, false, false, indentation.GetIndentationWithIncrementedLevel());

            yield return(new object[] { new ReceiveFunction()
                                        {
                                            Instructions = instructionsListMock
                                        }, indentation, $"receive() external payable {{\n{instructionCode1}\n}}" });

            yield return(new object[] { new ReceiveFunction()
                                        {
                                            Instructions = instructionsListMock2
                                        }, indentation, $"receive() external payable {{\n{instructionCode2}\n}}" });

            yield return(new object[] { new ReceiveFunction()
                                        {
                                            Instructions = emptyInstructionsListMock
                                        }, indentation, $"receive() external payable {{\n}}" });
        }
 static ContractLoop GetMockedLoop(string breakConditionCode, string instructionCode, string initialDeclaration, string stepInstruction, Indentation indentation)
 {
     return(new ContractLoop()
     {
         BreakCondition = conditionCodeMockCreator.PrepareMock(breakConditionCode),
         Instructions = instructionsListMockCreator.PrepareMock(instructionCode, false, true, indentation.GetIndentationWithIncrementedLevel()),
         InitialAssignment = assignmentMockCreator.PrepareMock(initialDeclaration),
         StepInstruction = instructionMockHelper.PrepareOneLineInstructionMock(stepInstruction)
     });
 }
Esempio n. 10
0
        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));
        }