public void ShouldReturnTrueIfIsMethod() { const string Code = "void Bar() { }"; var analyser = new CodeAnalyzer(); analyser.IsMethod(Code).ShouldBeTrue(); }
public void ShouldReturnFalseIfMissingMethodBody() { const string Code = "void Bar()"; var analyser = new CodeAnalyzer(); analyser.IsMethod(Code).ShouldBeFalse(); }
public void ShouldReturnFalseIfIncompeteMethod() { const string Code = "void Bar() { "; var analyser = new CodeAnalyzer(); analyser.IsMethod(Code).ShouldBeFalse(); }
public void ShouldReturnFalseIfIsNotMethod() { const string Code = "class A { } "; var rewriter = new CodeAnalyzer(); rewriter.IsMethod(Code).ShouldBeFalse(); }
public void ShouldReturnFalseIfIsNotClass() { const string Code = "void Bar() { }"; var analyser = new CodeAnalyzer(); analyser.IsClass(Code).ShouldBeFalse(); }
public void ShouldReturnTrueIfIsClass() { const string Code = "class A { }"; var analyser = new CodeAnalyzer(); analyser.IsClass(Code).ShouldBeTrue(); }
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};"); }
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 () { };"); }
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(); }
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; };"); }