Exemplo n.º 1
0
            public void ShouldExtractForLoops()
            {
                const string Code = "for(var i = 0; i < 3; i++) { Foo(); }";

                var parser = new RegionParser();
                var result = parser.Parse(Code);

                result.Count().ShouldEqual(1);
                var region = result[0];
                Code.Substring(region.Offset, region.Length).ShouldEqual(Code);
            }
Exemplo n.º 2
0
            public void ShouldExtractVariableStatements()
            {
                const string Code = "var x = 10;";

                var parser = new RegionParser();
                var result = parser.Parse(Code);

                result.Count().ShouldEqual(1);
                var region = result[0];
                Code.Substring(region.Offset, region.Length).ShouldEqual(Code);
            }
Exemplo n.º 3
0
            public void ShouldExtractBlockCodeThatBeginsWithLeftCurlyBrackets()
            {
                const string Code = "{ Foo(); ) }";

                var parser = new RegionParser();
                var result = parser.Parse(Code);

                result.Count().ShouldEqual(1);
                var region = result[0];
                Code.Substring(region.Offset, region.Length).ShouldEqual(Code);
            }
Exemplo n.º 4
0
            public void ShouldExtractBlock()
            {
                const string Code = "if (true) { some code; }";

                var parser = new RegionParser();
                var result = parser.Parse(Code);

                result.Count().ShouldEqual(1);
                var region = result[0];
                Code.Substring(region.Offset, region.Length).ShouldEqual(Code);
            }
Exemplo n.º 5
0
            public void ShouldExtractComplexVariableStatement()
            {
                const string Code = "var version = File.ReadAllText(\"src/CommonAssemblyInfo.cs\").Split(new[] { \"AssemblyInformationalVersion(\\\"\" }, 2, StringSplitOptions.None).ElementAt(1).Split(new[] { '\"' }).First();";

                var parser = new RegionParser();
                var result = parser.Parse(Code);

                result.Count().ShouldEqual(1);

                var region = result[0];
                Code.Substring(region.Offset, region.Length).ShouldEqual(Code);
            }
Exemplo n.º 6
0
            public void ShouldExtractDoWhileAsSingleBlock()
            {
                const string Code = "do { }   while (true);";

                var parser = new RegionParser();
                var result = parser.Parse(Code);

                result.Count().ShouldEqual(1);

                var region = result[0];
                Code.Substring(region.Offset, region.Length).ShouldEqual(Code);
            }
Exemplo n.º 7
0
            public void ShouldExtractComplexMethods()
            {
                const string Code = "var bau = Require<Bau>();\n\nbau\n.Task(\"default\").DependsOn(string.IsNullOrWhiteSpace(ci) ? new[] { \"unit\", \"component\", \"pack\" } : new[] { \"unit\", \"component\", \"accept\", \"pack\" });\n";

                var parser = new RegionParser();
                var result = parser.Parse(Code);

                result.Count().ShouldEqual(2);

                var region = result[0];
                Code.Substring(region.Offset, region.Length).ShouldEqual("var bau = Require<Bau>();");

                region = result[1];
                Code.Substring(region.Offset, region.Length).ShouldEqual("bau\n.Task(\"default\").DependsOn(string.IsNullOrWhiteSpace(ci) ? new[] { \"unit\", \"component\", \"pack\" } : new[] { \"unit\", \"component\", \"accept\", \"pack\" });");
            }
Exemplo n.º 8
0
            public void ShouldExtractInvalidDoWhileAsTwoBlocks()
            {
                const string Code = "do { }   if (true);";

                var parser = new RegionParser();
                var result = parser.Parse(Code);

                result.Count().ShouldEqual(2);

                var region = result[0];
                Code.Substring(region.Offset, region.Length).ShouldEqual("do { }");

                region = result[1];
                Code.Substring(region.Offset, region.Length).ShouldEqual("if (true);");
            }
Exemplo n.º 9
0
            public void ShouldExtractMultipleStatements()
            {
                const string Code = "var x = 123;Action a = () => x++;Console.WriteLine(x);";

                var parser = new RegionParser();
                var result = parser.Parse(Code);

                result.Count().ShouldEqual(3);
                var region = result[0];
                Code.Substring(region.Offset, region.Length).ShouldEqual("var x = 123;");

                region = result[1];
                Code.Substring(region.Offset, region.Length).ShouldEqual("Action a = () => x++;");

                region = result[2];
                Code.Substring(region.Offset, region.Length).ShouldEqual("Console.WriteLine(x);");
            }
Exemplo n.º 10
0
        public List<SegmentResult> Segment(string code)
        {
            const string ScriptPattern = @"#line 1.*?\n";
            var isScriptFile = Regex.IsMatch(code, ScriptPattern);
            if (isScriptFile)
            {
                // Remove debug line
                code = Regex.Replace(code, ScriptPattern, "");
            }

            var analyser = new CodeAnalyzer();
            var result = new List<SegmentResult>();
            var parser = new RegionParser();
            foreach (var region in parser.Parse(code))
            {
                // Calculate region linenumber
                var lineNr = code.Substring(0, region.Offset).Count(x => x.Equals('\n'));

                var segment = code.Substring(region.Offset, region.Length);

                if (analyser.IsClass(segment))
                {
                    result.Add(new SegmentResult
                    {
                        Type = SegmentType.Class,
                        BeginLine = lineNr,
                        Code = segment
                    });
                }
                else
                {
                    var isMethod = analyser.IsMethod(segment);

                    if (isMethod)
                    {
                        // method ok
                        var method = analyser.ExtractPrototypeAndMethod(segment);

                        result.Add(new SegmentResult
                        {
                            Type = SegmentType.Prototype,
                            BeginLine = lineNr,
                            Code = method.ProtoType
                        });

                        result.Add(new SegmentResult
                        {
                            Type = SegmentType.Method,
                            BeginLine = lineNr,
                            Code = method.MethodExpression
                        });
                    }
                    else
                    {
                        result.Add(new SegmentResult
                        {
                            Type = SegmentType.Evaluation,
                            BeginLine = lineNr,
                            Code = segment
                        });
                    }
                }
            }

            return result
                .OrderBy(x => x.Type)
                .ToList();
        }
Exemplo n.º 11
0
            public void ShouldExtractExpressionsInParantheses()
            {
                const string Code = "(10 + 5 * ( 4 - 8) )";

                var parser = new RegionParser();
                var result = parser.Parse(Code);

                result.Count().ShouldEqual(1);
                var region = result[0];
                Code.Substring(region.Offset, region.Length).ShouldEqual(Code);
            }
Exemplo n.º 12
0
            public void ShouldExtractMultipleIfElseAsSingleBlock()
            {
                const string Code = "if { } else if { } else if { } else { }";

                var parser = new RegionParser();
                var result = parser.Parse(Code);

                result.Count().ShouldEqual(1);

                var region = result[0];
                Code.Substring(region.Offset, region.Length).ShouldEqual(Code);
            }
Exemplo n.º 13
0
            public void ShouldExtractMultipleBlocks()
            {
                const string Code = "using(var s = File.Open(\"test.cs\")){ } public Foo(int x) { return x }";

                var parser = new RegionParser();
                var result = parser.Parse(Code);

                result.Count().ShouldEqual(2);

                var region = result[0];
                Code.Substring(region.Offset, region.Length).ShouldEqual("using(var s = File.Open(\"test.cs\")){ }");

                region = result[1];
                Code.Substring(region.Offset, region.Length).ShouldEqual("public Foo(int x) { return x }");
            }