예제 #1
0
            public void ShouldReturnTrueIfIsMethod()
            {
                const string Code = "void Bar() { }";

                var analyser = new CodeAnalyzer();
                analyser.IsMethod(Code).ShouldBeTrue();
            }
예제 #2
0
            public void ShouldReturnFalseIfMissingMethodBody()
            {
                const string Code = "void Bar()";

                var analyser = new CodeAnalyzer();
                analyser.IsMethod(Code).ShouldBeFalse();
            }
예제 #3
0
            public void ShouldReturnFalseIfIncompeteMethod()
            {
                const string Code = "void Bar() { ";

                var analyser = new CodeAnalyzer();
                analyser.IsMethod(Code).ShouldBeFalse();
            }
예제 #4
0
            public void ShouldReturnFalseIfIsNotMethod()
            {
                const string Code = "class A { } ";

                var rewriter = new CodeAnalyzer();
                rewriter.IsMethod(Code).ShouldBeFalse();
            }
예제 #5
0
            public void ShouldReturnFalseIfIsNotClass()
            {
                const string Code = "void Bar() { }";

                var analyser = new CodeAnalyzer();
                analyser.IsClass(Code).ShouldBeFalse();
            }
예제 #6
0
            public void ShouldReturnTrueIfIsClass()
            {
                const string Code = "class A { }";

                var analyser = new CodeAnalyzer();
                analyser.IsClass(Code).ShouldBeTrue();
            }
예제 #7
0
            public void ShouldPreserveMethodBody()
            {
                const string Code = "int Foo(int a) { Foo();\n\nreturn a;\n}";

                var rewriter = new CodeAnalyzer();
                var method = rewriter.ExtractPrototypeAndMethod(Code);

                method.ProtoType.ShouldEqual("Func<int, int> Foo;");
                method.MethodExpression.ShouldEqual("Foo = delegate (int a) { Foo();\n\nreturn a;\n};");
            }
예제 #8
0
            public void ShouldRewriteToPrototypeAndExpression()
            {
                const string Code = "void Bar() { }";

                var analyser = new CodeAnalyzer();
                var method = analyser.ExtractPrototypeAndMethod(Code);

                method.ProtoType.ShouldEqual("Action Bar;");
                method.MethodExpression.ShouldEqual("Bar = delegate () { };");
            }
예제 #9
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();
        }
예제 #10
0
            public void ShouldPreserveLineCountInMethodSignature()
            {
                const string Code = "int\nFoo\n(\n)\n { return 42; }";

                var rewriter = new CodeAnalyzer();
                var method = rewriter.ExtractPrototypeAndMethod(Code);

                method.ProtoType.ShouldEqual("Func<int> Foo;\n\n\n\n");
                method.MethodExpression.ShouldEqual("\n\n\n\nFoo = delegate () { return 42; };");
            }