8 changed files with 228 additions and 24 deletions
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.Scripting |
||||
{ |
||||
/// <summary>
|
||||
/// Used to extract the code for a method based on the range of lines.
|
||||
/// </summary>
|
||||
public class ScriptingLocalMethod |
||||
{ |
||||
string code = String.Empty; |
||||
|
||||
public ScriptingLocalMethod(string code) |
||||
{ |
||||
if (code != null) { |
||||
this.code = code; |
||||
} |
||||
} |
||||
|
||||
public string GetCode(int endLine) |
||||
{ |
||||
int endIndex = FindIndexForEndOfLine(endLine); |
||||
if (endIndex > 0) { |
||||
return code.Substring(0, endIndex); |
||||
} |
||||
return code; |
||||
} |
||||
|
||||
int FindIndexForEndOfLine(int line) |
||||
{ |
||||
int index = 0; |
||||
for (int i = 0; i <= line; ++i) { |
||||
index = code.IndexOf('\n', index) + 1; |
||||
} |
||||
return index; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,91 @@
@@ -0,0 +1,91 @@
|
||||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||
|
||||
using System; |
||||
using NUnit.Framework; |
||||
using ICSharpCode.Scripting; |
||||
|
||||
namespace ICSharpCode.Scripting.Tests.Resolver |
||||
{ |
||||
[TestFixture] |
||||
public class ScriptingLocalMethodTests |
||||
{ |
||||
ScriptingLocalMethod method; |
||||
|
||||
void CreateLocalMethod(string code) |
||||
{ |
||||
method = new ScriptingLocalMethod(code); |
||||
} |
||||
|
||||
[Test] |
||||
public void GetCode_EndLineIsZeroAndTwoLinesOfCode_ReturnsFirstLineOfCode() |
||||
{ |
||||
string fullCode = |
||||
"first\r\n" + |
||||
"second"; |
||||
|
||||
CreateLocalMethod(fullCode); |
||||
|
||||
int endLine = 0; |
||||
string code = method.GetCode(endLine); |
||||
|
||||
string expectedCode = "first\r\n"; |
||||
|
||||
Assert.AreEqual(expectedCode, code); |
||||
} |
||||
|
||||
[Test] |
||||
public void GetCode_EndLineIsOneAndThreeLinesOfCode_ReturnsFirstTwoLinesOfCode() |
||||
{ |
||||
string fullCode = |
||||
"first\r\n" + |
||||
"second\r\n" + |
||||
"third"; |
||||
|
||||
CreateLocalMethod(fullCode); |
||||
|
||||
int endLine = 1; |
||||
string code = method.GetCode(endLine); |
||||
|
||||
string expectedCode = |
||||
"first\r\n" + |
||||
"second\r\n"; |
||||
|
||||
Assert.AreEqual(expectedCode, code); |
||||
} |
||||
|
||||
[Test] |
||||
public void GetCode_EndLineIsOneAndTwoLinesOfCode_ReturnsFirstTwoLinesOfCode() |
||||
{ |
||||
string fullCode = |
||||
"first\r\n" + |
||||
"second"; |
||||
|
||||
CreateLocalMethod(fullCode); |
||||
|
||||
int endLine = 1; |
||||
string code = method.GetCode(endLine); |
||||
|
||||
string expectedCode = |
||||
"first\r\n" + |
||||
"second"; |
||||
|
||||
Assert.AreEqual(expectedCode, code); |
||||
} |
||||
|
||||
[Test] |
||||
public void GetCode_EndLineIsOneAndCodeIsNull_ReturnsEmptyString() |
||||
{ |
||||
string fullCode = null; |
||||
|
||||
CreateLocalMethod(fullCode); |
||||
|
||||
int endLine = 1; |
||||
string code = method.GetCode(endLine); |
||||
|
||||
string expectedCode = String.Empty; |
||||
|
||||
Assert.AreEqual(expectedCode, code); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue