Browse Source

Expand variables such as SolutionDir, ProjectDir in T4 assembly directives.

pull/23/head
Matt Ward 15 years ago
parent
commit
0961c8fc51
  1. 12
      src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingStringParser.cs
  2. 13
      src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingVariables.cs
  3. 21
      src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingAssemblyResolver.cs
  4. 27
      src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingDirectoryVariable.cs
  5. 16
      src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingStringParser.cs
  6. 75
      src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingVariableLocation.cs
  7. 92
      src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingVariables.cs
  8. 73
      src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingVariablesStringBuilder.cs
  9. 7
      src/AddIns/Misc/TextTemplating/Project/TextTemplating.csproj
  10. 2
      src/AddIns/Misc/TextTemplating/Test/Helpers/FakeAssemblyParserService.cs
  11. 26
      src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingStringParser.cs
  12. 34
      src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingVariables.cs
  13. 26
      src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingAssemblyResolverTests.cs
  14. 2
      src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingHostTests.cs
  15. 78
      src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingVariableLocationTests.cs
  16. 241
      src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingVariablesTests.cs
  17. 4
      src/AddIns/Misc/TextTemplating/Test/TextTemplating.Tests.csproj

12
src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingStringParser.cs

@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
// 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.TextTemplating
{
public interface ITextTemplatingStringParser
{
string GetValue(string propertyName);
}
}

13
src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingVariables.cs

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
// 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.TextTemplating
{
public interface ITextTemplatingVariables
{
string Expand(string name);
string GetValue(string name);
}
}

21
src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingAssemblyResolver.cs

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.IO;
using ICSharpCode.SharpDevelop.Project;
namespace ICSharpCode.TextTemplating
@ -10,22 +11,33 @@ namespace ICSharpCode.TextTemplating @@ -10,22 +11,33 @@ namespace ICSharpCode.TextTemplating
{
IProject project;
IAssemblyParserService assemblyParserService;
ITextTemplatingVariables templatingVariables;
public TextTemplatingAssemblyResolver(
IProject project,
IAssemblyParserService assemblyParserService)
IAssemblyParserService assemblyParserService,
ITextTemplatingVariables templatingVariables)
{
this.project = project;
this.assemblyParserService = assemblyParserService;
this.templatingVariables = templatingVariables;
}
public TextTemplatingAssemblyResolver(IProject project)
: this(project, new TextTemplatingAssemblyParserService())
: this(
project,
new TextTemplatingAssemblyParserService(),
new TextTemplatingVariables())
{
}
public string Resolve(string assemblyReference)
{
assemblyReference = ExpandVariables(assemblyReference);
if (Path.IsPathRooted(assemblyReference)) {
return assemblyReference;
}
string resolvedAssemblyFileName = ResolveAssemblyFromProject(assemblyReference);
if (resolvedAssemblyFileName == null) {
resolvedAssemblyFileName = ResolveAssemblyFromGac(assemblyReference);
@ -36,6 +48,11 @@ namespace ICSharpCode.TextTemplating @@ -36,6 +48,11 @@ namespace ICSharpCode.TextTemplating
return assemblyReference;
}
string ExpandVariables(string assemblyReference)
{
return templatingVariables.Expand(assemblyReference);
}
string ResolveAssemblyFromProject(string assemblyReference)
{
foreach (ReferenceProjectItem refProjectItem in project.GetItemsOfType(ItemType.Reference)) {

27
src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingDirectoryVariable.cs

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
// 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 System.Linq;
namespace ICSharpCode.TextTemplating
{
public static class TextTemplatingDirectoryVariable
{
public static bool IsDirectoryVariable(string name)
{
return name.EndsWith("Dir", StringComparison.OrdinalIgnoreCase);
}
public static string AppendTrailingSlashIfMissing(string variableValue)
{
if (!String.IsNullOrEmpty(variableValue)) {
if (variableValue.Last() == '\\') {
return variableValue;
}
return variableValue + "\\";
}
return String.Empty;
}
}
}

16
src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingStringParser.cs

@ -0,0 +1,16 @@ @@ -0,0 +1,16 @@
// 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 ICSharpCode.Core;
namespace ICSharpCode.TextTemplating
{
public class TextTemplatingStringParser : ITextTemplatingStringParser
{
public string GetValue(string propertyName)
{
return StringParser.GetValue(propertyName);
}
}
}

75
src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingVariableLocation.cs

@ -0,0 +1,75 @@ @@ -0,0 +1,75 @@
// 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.TextTemplating
{
public class TextTemplatingVariableLocation
{
public TextTemplatingVariableLocation()
: this(String.Empty, 0, 0)
{
}
public TextTemplatingVariableLocation(
string variableName,
int index,
int length)
{
this.VariableName = variableName;
this.Index = index;
this.Length = length;
}
public static TextTemplatingVariableLocation CreateFromString(
string text,
int unexpandedVariableStartIndex,
int unexpandedVariableEndIndex)
{
int variableNameStart = unexpandedVariableStartIndex + 2;
int unexpandedLength = unexpandedVariableEndIndex - unexpandedVariableStartIndex + 1;
string variableName = text.Substring(variableNameStart, unexpandedVariableEndIndex - variableNameStart);
return new TextTemplatingVariableLocation(variableName, unexpandedVariableStartIndex, unexpandedLength);
}
public string VariableName { get; set; }
public int Index { get; set; }
public int Length { get; set; }
public override bool Equals(object obj)
{
var rhs = obj as TextTemplatingVariableLocation;
if (rhs != null) {
return (VariableName == rhs.VariableName) &&
(Index == rhs.Index) &&
(Length == rhs.Length);
}
return false;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override string ToString()
{
return String.Format(
"VariableName: {0}, Index: {1}, Length: {2}",
VariableName, Index, Length);
}
public static TextTemplatingVariableLocation FindVariable(string text, int startSearchIndex)
{
int start = text.IndexOf("$(", startSearchIndex);
if (start >= 0) {
int end = text.IndexOf(")", start);
if (end >= 0) {
return CreateFromString(text, start, end);
}
}
return null;
}
}
}

92
src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingVariables.cs

@ -0,0 +1,92 @@ @@ -0,0 +1,92 @@
// 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 System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ICSharpCode.TextTemplating
{
public class TextTemplatingVariables : ITextTemplatingVariables
{
ITextTemplatingStringParser stringParser;
public TextTemplatingVariables()
: this(new TextTemplatingStringParser())
{
}
public TextTemplatingVariables(
ITextTemplatingStringParser stringParser)
{
this.stringParser = stringParser;
}
public string Expand(string name)
{
var variablesBuilder = new TextTemplatingVariablesStringBuilder(name, this);
foreach (TextTemplatingVariableLocation variableLocation in GetVariables(name)) {
variablesBuilder.AppendTextBeforeVariable(variableLocation);
variablesBuilder.AppendVariable(variableLocation);
}
variablesBuilder.AppendRemaining();
return variablesBuilder.ToString();
}
public IEnumerable<TextTemplatingVariableLocation> GetVariables(string text)
{
if (String.IsNullOrEmpty(text)) {
yield break;
}
int currentIndex = 0;
while (true) {
TextTemplatingVariableLocation variableLocation =
FindVariable(text, currentIndex);
if (variableLocation != null) {
currentIndex = variableLocation.Index + variableLocation.Length;
yield return variableLocation;
} else {
yield break;
}
}
}
TextTemplatingVariableLocation FindVariable(string text, int startIndex)
{
return TextTemplatingVariableLocation.FindVariable(text, startIndex);
}
public string GetValue(string name)
{
if (name == null) {
return String.Empty;
}
string variableValue = stringParser.GetValue(name);
if (IsDirectoryVariable(name)) {
return AppendTrailingSlashIfMissing(variableValue);
}
return GetEmptyStringIfNull(variableValue);
}
bool IsDirectoryVariable(string name)
{
return TextTemplatingDirectoryVariable.IsDirectoryVariable(name);
}
string AppendTrailingSlashIfMissing(string variableValue)
{
return TextTemplatingDirectoryVariable.AppendTrailingSlashIfMissing(variableValue);
}
string GetEmptyStringIfNull(string variableValue)
{
if (variableValue != null) {
return variableValue;
}
return String.Empty;
}
}
}

73
src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingVariablesStringBuilder.cs

@ -0,0 +1,73 @@ @@ -0,0 +1,73 @@
// 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 System.Text;
namespace ICSharpCode.TextTemplating
{
public class TextTemplatingVariablesStringBuilder
{
StringBuilder variablesBuilder = new StringBuilder();
string unexpandedVariablesString;
ITextTemplatingVariables templatingVariables;
int currentIndex;
public TextTemplatingVariablesStringBuilder(
string unexpandedVariablesString,
ITextTemplatingVariables templatingVariables)
{
this.unexpandedVariablesString = unexpandedVariablesString;
this.templatingVariables = templatingVariables;
}
public void Append(string text)
{
variablesBuilder.Append(text);
}
public override string ToString()
{
return variablesBuilder.ToString();
}
public void AppendVariable(TextTemplatingVariableLocation variableLocation)
{
AppendVariableText(variableLocation);
UpdateCurrentIndex(variableLocation);
}
public void AppendTextBeforeVariable(TextTemplatingVariableLocation variableLocation)
{
string textBeforeVariable = unexpandedVariablesString.Substring(currentIndex, variableLocation.Index);
variablesBuilder.Append(textBeforeVariable);
}
void AppendVariableText(TextTemplatingVariableLocation variableLocation)
{
string variableValue = GetVariableValue(variableLocation);
variablesBuilder.Append(variableValue);
}
void UpdateCurrentIndex(TextTemplatingVariableLocation variableLocation)
{
currentIndex = variableLocation.Index + variableLocation.Length;
}
string GetVariableValue(TextTemplatingVariableLocation variableLocation)
{
return templatingVariables.GetValue(variableLocation.VariableName);
}
public void AppendRemaining()
{
string textNotAppended = GetTextNotAppended();
variablesBuilder.Append(textNotAppended);
}
string GetTextNotAppended()
{
return unexpandedVariablesString.Substring(currentIndex);
}
}
}

7
src/AddIns/Misc/TextTemplating/Project/TextTemplating.csproj

@ -60,6 +60,8 @@ @@ -60,6 +60,8 @@
<Compile Include="Src\ITextTemplatingFilePreprocessor.cs" />
<Compile Include="Src\ITextTemplatingHost.cs" />
<Compile Include="Src\ITextTemplatingFileGenerator.cs" />
<Compile Include="Src\ITextTemplatingStringParser.cs" />
<Compile Include="Src\ITextTemplatingVariables.cs" />
<Compile Include="Src\NamespaceHint.cs" />
<Compile Include="Src\TemplatingHostProcessTemplateError.cs" />
<Compile Include="Src\TextTemplatingAppDomain.cs" />
@ -68,6 +70,7 @@ @@ -68,6 +70,7 @@
<Compile Include="Src\TextTemplatingAssemblyResolver.cs" />
<Compile Include="Src\TextTemplatingCustomTool.cs" />
<Compile Include="Src\TextTemplatingCustomToolContext.cs" />
<Compile Include="Src\TextTemplatingDirectoryVariable.cs" />
<Compile Include="Src\TextTemplatingFileGenerator.cs" />
<Compile Include="Src\TextTemplatingFileGeneratorCustomTool.cs" />
<Compile Include="Configuration\AssemblyInfo.cs" />
@ -76,6 +79,10 @@ @@ -76,6 +79,10 @@
<Compile Include="Src\TextTemplatingFileProcessor.cs" />
<Compile Include="Src\TextTemplatingHost.cs" />
<Compile Include="Src\TextTemplatingReflectionProjectContent.cs" />
<Compile Include="Src\TextTemplatingStringParser.cs" />
<Compile Include="Src\TextTemplatingVariableLocation.cs" />
<Compile Include="Src\TextTemplatingVariables.cs" />
<Compile Include="Src\TextTemplatingVariablesStringBuilder.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\TextTemplating.xshd" />

2
src/AddIns/Misc/TextTemplating/Test/Helpers/FakeAssemblyParserService.cs

@ -11,9 +11,11 @@ namespace TextTemplating.Tests.Helpers @@ -11,9 +11,11 @@ namespace TextTemplating.Tests.Helpers
{
public FakeReflectionProjectContent FakeReflectionProjectContent = new FakeReflectionProjectContent();
public ReferenceProjectItem ItemPassedToGetReflectionProjectContentForReference;
public bool IsGetReflectionProjectContentForReferenceCalled;
public IReflectionProjectContent GetReflectionProjectContentForReference(ReferenceProjectItem item)
{
IsGetReflectionProjectContentForReferenceCalled = true;
ItemPassedToGetReflectionProjectContentForReference = item;
return FakeReflectionProjectContent;
}

26
src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingStringParser.cs

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
// 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 System.Collections.Generic;
using ICSharpCode.TextTemplating;
namespace TextTemplating.Tests.Helpers
{
public class FakeTextTemplatingStringParser : ITextTemplatingStringParser
{
public Dictionary<string, string> Properties = new Dictionary<string, string>();
public string GetValue(string propertyName)
{
string propertyValue = null;
Properties.TryGetValue(propertyName, out propertyValue);
return propertyValue;
}
public void AddProperty(string name, string value)
{
Properties.Add(name, value);
}
}
}

34
src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingVariables.cs

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
// 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 System.Collections.Generic;
using ICSharpCode.TextTemplating;
namespace TextTemplating.Tests.Helpers
{
public class FakeTextTemplatingVariables : ITextTemplatingVariables
{
public Dictionary<string, string> Variables = new Dictionary<string, string>();
public void AddVariable(string name, string value)
{
Variables.Add(name, value);
}
public string Expand(string name)
{
foreach (KeyValuePair<string, string> variable in Variables) {
name = name.Replace(variable.Key, variable.Value);
}
return name;
}
public string GetValue(string name)
{
string variableValue = null;
Variables.TryGetValue(name, out variableValue);
return variableValue;
}
}
}

26
src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingAssemblyResolverTests.cs

@ -16,12 +16,14 @@ namespace TextTemplating.Tests @@ -16,12 +16,14 @@ namespace TextTemplating.Tests
TextTemplatingAssemblyResolver resolver;
IProject project;
FakeAssemblyParserService fakeAssemblyParserService;
FakeTextTemplatingVariables fakeTemplatingVariables;
void CreateResolver()
{
project = ProjectHelper.CreateProject();
fakeAssemblyParserService = new FakeAssemblyParserService();
resolver = new TextTemplatingAssemblyResolver(project, fakeAssemblyParserService);
fakeTemplatingVariables = new FakeTextTemplatingVariables();
resolver = new TextTemplatingAssemblyResolver(project, fakeAssemblyParserService, fakeTemplatingVariables);
}
ReferenceProjectItem AddReferenceToProject(string referenceName)
@ -146,5 +148,27 @@ namespace TextTemplating.Tests @@ -146,5 +148,27 @@ namespace TextTemplating.Tests
Assert.AreEqual("System.Data", result);
}
[Test]
public void Resolve_AssemblyReferenceHasTemplateVariable_ReturnsExpandedAssemblyReferenceFileName()
{
CreateResolver();
fakeTemplatingVariables.AddVariable("$(SolutionDir)", @"d:\projects\MyProject\");
string result = resolver.Resolve(@"$(SolutionDir)lib\Test.dll");
Assert.AreEqual(@"d:\projects\MyProject\lib\Test.dll", result);
}
[Test]
public void Resolve_AssemblyReferenceHasTemplateVariable_AssemblyParserServiceIsNotUsed()
{
CreateResolver();
fakeTemplatingVariables.AddVariable("$(SolutionDir)", @"d:\projects\MyProject\");
string result = resolver.Resolve(@"$(SolutionDir)lib\Test.dll");
Assert.IsFalse(fakeAssemblyParserService.IsGetReflectionProjectContentForReferenceCalled);
}
}
}

2
src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingHostTests.cs

@ -103,7 +103,7 @@ namespace TextTemplating.Tests @@ -103,7 +103,7 @@ namespace TextTemplating.Tests
}
[Test]
public void ResolveAssemblyReference_PassedMyAssemblyReference_ReturnFileNameReturnedFromAssemblyResolverResolveMethod()
public void ResolveAssemblyReference_PassedMyAssemblyReference_ReturnsFileNameReturnedFromAssemblyResolverResolveMethod()
{
CreateHost();
assemblyResolver.ResolveReturnValue = @"d:\projects\references\MyReference.dll";

78
src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingVariableLocationTests.cs

@ -0,0 +1,78 @@ @@ -0,0 +1,78 @@
// 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 ICSharpCode.TextTemplating;
using NUnit.Framework;
namespace TextTemplating.Tests
{
[TestFixture]
public class TextTemplatingVariableLocationTests
{
TextTemplatingVariableLocation lhs;
TextTemplatingVariableLocation rhs;
void CreateVariableLocationsToCompare()
{
lhs = new TextTemplatingVariableLocation();
rhs = new TextTemplatingVariableLocation();
}
[Test]
public void Equals_AllPropertiesSame_ReturnsTrue()
{
CreateVariableLocationsToCompare();
bool result = lhs.Equals(rhs);
Assert.IsTrue(result);
}
[Test]
public void Equals_VariableNamesAreDifferent_ReturnsFalse()
{
CreateVariableLocationsToCompare();
lhs.VariableName = "A";
rhs.VariableName = "B";
bool result = lhs.Equals(rhs);
Assert.IsFalse(result);
}
[Test]
public void Equals_NullPassed_ReturnsFalse()
{
CreateVariableLocationsToCompare();
bool result = lhs.Equals(null);
Assert.IsFalse(result);
}
[Test]
public void Equals_IndexesAreDifferent_ReturnsFalse()
{
CreateVariableLocationsToCompare();
lhs.Index = 1;
rhs.Index = 3;
bool result = lhs.Equals(rhs);
Assert.IsFalse(result);
}
[Test]
public void Equals_LengthsAreDifferent_ReturnsFalse()
{
CreateVariableLocationsToCompare();
lhs.Length = 1;
rhs.Length = 3;
bool result = lhs.Equals(rhs);
Assert.IsFalse(result);
}
}
}

241
src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingVariablesTests.cs

@ -0,0 +1,241 @@ @@ -0,0 +1,241 @@
// 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 System.Collections.Generic;
using System.Linq;
using ICSharpCode.TextTemplating;
using NUnit.Framework;
using TextTemplating.Tests.Helpers;
namespace TextTemplating.Tests
{
[TestFixture]
public class TextTemplatingVariablesTests
{
TextTemplatingVariables variables;
FakeTextTemplatingStringParser fakeStringParser;
void CreateTextTemplatingVariables()
{
fakeStringParser = new FakeTextTemplatingStringParser();
variables = new TextTemplatingVariables(fakeStringParser);
}
void AddProperty(string name, string value)
{
fakeStringParser.AddProperty(name, value);
}
[Test]
public void Expand_SolutionDirProperty_SolutionDirPropertyIsExpanded()
{
CreateTextTemplatingVariables();
AddProperty("SolutionDir", @"d:\projects\MyProject\");
string result = variables.Expand(@"$(SolutionDir)bin\Debug\Test.dll");
string expectedResult = @"d:\projects\MyProject\bin\Debug\Test.dll";
Assert.AreEqual(expectedResult, result);
}
[Test]
public void Expand_ProjectDirProperty_ProjectDirPropertyIsExpanded()
{
CreateTextTemplatingVariables();
AddProperty("ProjectDir", @"d:\projects\MyProject\");
string result = variables.Expand(@"$(ProjectDir)bin\Debug\Test.dll");
string expectedResult = @"d:\projects\MyProject\bin\Debug\Test.dll";
Assert.AreEqual(expectedResult, result);
}
[Test]
public void GetVariableLocations_SolutionDirProperty_ReturnsSolutionDirVariable()
{
CreateTextTemplatingVariables();
TextTemplatingVariableLocation location = variables.GetVariables("$(SolutionDir)").First();
var expectedLocation = new TextTemplatingVariableLocation() {
VariableName = "SolutionDir",
Index = 0,
Length = 14
};
Assert.AreEqual(expectedLocation, location);
}
[Test]
public void GetVariableLocations_NullPassed_ReturnsNoVariables()
{
CreateTextTemplatingVariables();
List<TextTemplatingVariableLocation> locations =
variables.GetVariables(null).ToList();
Assert.AreEqual(0, locations.Count);
}
[Test]
public void GetVariableLocations_EmptyStringPassed_ReturnsNoVariables()
{
CreateTextTemplatingVariables();
List<TextTemplatingVariableLocation> locations =
variables.GetVariables(String.Empty).ToList();
Assert.AreEqual(0, locations.Count);
}
[Test]
public void GetVariableLocations_TextHasNoVariables_ReturnsNoVariables()
{
CreateTextTemplatingVariables();
List<TextTemplatingVariableLocation> locations =
variables.GetVariables("No Variables").ToList();
Assert.AreEqual(0, locations.Count);
}
[Test]
public void GetVariableLocations_TextHasVariableStartButNoEnd_ReturnsNoVariables()
{
CreateTextTemplatingVariables();
List<TextTemplatingVariableLocation> locations =
variables.GetVariables("$(No Variables").ToList();
Assert.AreEqual(0, locations.Count);
}
[Test]
public void GetVariableLocations_TwoProperties_ReturnsTwoVariables()
{
CreateTextTemplatingVariables();
List<TextTemplatingVariableLocation> locations =
variables.GetVariables("$(SolutionDir)$(ProjectDir)").ToList();
var expectedLocation1 = new TextTemplatingVariableLocation() {
VariableName = "SolutionDir",
Index = 0,
Length = 14
};
var expectedLocation2 = new TextTemplatingVariableLocation() {
VariableName = "ProjectDir",
Index = 14,
Length = 13
};
var expectedLocations = new TextTemplatingVariableLocation[] {
expectedLocation1,
expectedLocation2
};
CollectionAssert.AreEqual(expectedLocations, locations);
}
[Test]
public void GetValue_ProjectDirProperty_ReturnsProjectDir()
{
CreateTextTemplatingVariables();
AddProperty("ProjectDir", @"d:\projects\MyProject\");
string variableValue = variables.GetValue("ProjectDir");
string expectedVariableValue = @"d:\projects\MyProject\";
Assert.AreEqual(expectedVariableValue, variableValue);
}
[Test]
public void GetValue_ProjectDirPropertyHasNoTrailingSlash_ReturnsProjectDirWithTrailingSlash()
{
CreateTextTemplatingVariables();
AddProperty("ProjectDir", @"d:\projects\MyProject");
string variableValue = variables.GetValue("ProjectDir");
string expectedVariableValue = @"d:\projects\MyProject\";
Assert.AreEqual(expectedVariableValue, variableValue);
}
[Test]
public void GetValue_PropertyReturnsEmptyStringAsValue_ReturnsEmptyString()
{
CreateTextTemplatingVariables();
AddProperty("ProjectDir", String.Empty);
string variableValue = variables.GetValue("ProjectDir");
Assert.AreEqual(String.Empty, variableValue);
}
[Test]
public void GetValue_PropertyReturnsNullAsValue_ReturnsEmptyString()
{
CreateTextTemplatingVariables();
AddProperty("Test", null);
string variableValue = variables.GetValue("Test");
Assert.AreEqual(String.Empty, variableValue);
}
[Test]
public void GetValue_NonDirectoryPropertyReturnsNonEmptyStringAsValue_DoesNotAppendTrailingSlash()
{
CreateTextTemplatingVariables();
AddProperty("Test", "abc");
string variableValue = variables.GetValue("Test");
Assert.AreEqual("abc", variableValue);
}
[Test]
public void GetValue_SolutionDirPropertyWithoutTrailingSlash_ReturnsExpandedSolutionDirWithTrailingSlash()
{
CreateTextTemplatingVariables();
AddProperty("SolutionDir", @"d:\projects\MySolution");
string variableValue = variables.GetValue("SolutionDir");
string expectedVariableValue = @"d:\projects\MySolution\";
Assert.AreEqual(expectedVariableValue, variableValue);
}
[Test]
public void GetValue_SolutionDirPropertyInLowerCaseWithoutTrailingSlash_ReturnsExpandedSolutionDirWithTrailingSlash()
{
CreateTextTemplatingVariables();
AddProperty("solutiondir", @"d:\projects\MySolution");
string variableValue = variables.GetValue("solutiondir");
string expectedVariableValue = @"d:\projects\MySolution\";
Assert.AreEqual(expectedVariableValue, variableValue);
}
[Test]
public void GetValue_NullPassed_ReturnsEmptyString()
{
CreateTextTemplatingVariables();
string variableValue = variables.GetValue(null);
Assert.AreEqual(String.Empty, variableValue);
}
[Test]
public void Expand_ProjectDirPropertyHasNoTrailingSlash_ProjectDirPropertyIsExpandedWithTrailingSlash()
{
CreateTextTemplatingVariables();
AddProperty("ProjectDir", @"d:\projects\MyProject");
string result = variables.Expand(@"$(ProjectDir)bin\Debug\Test.dll");
string expectedResult = @"d:\projects\MyProject\bin\Debug\Test.dll";
Assert.AreEqual(expectedResult, result);
}
}
}

4
src/AddIns/Misc/TextTemplating/Test/TextTemplating.Tests.csproj

@ -64,6 +64,8 @@ @@ -64,6 +64,8 @@
<Compile Include="Helpers\FakeTextTemplatingFilePreprocessor.cs" />
<Compile Include="Helpers\FakeTextTemplatingHost.cs" />
<Compile Include="Helpers\FakeTextTemplatingFileGenerator.cs" />
<Compile Include="Helpers\FakeTextTemplatingStringParser.cs" />
<Compile Include="Helpers\FakeTextTemplatingVariables.cs" />
<Compile Include="Helpers\ProjectHelper.cs" />
<Compile Include="Helpers\TestableFileProjectItem.cs" />
<Compile Include="Helpers\TestableProject.cs" />
@ -79,6 +81,8 @@ @@ -79,6 +81,8 @@
<Compile Include="Src\TextTemplatingHostTests.cs" />
<Compile Include="Src\TextTemplatingPreprocessorTests.cs" />
<Compile Include="Src\TextTemplatingReflectionProjectContentTests.cs" />
<Compile Include="Src\TextTemplatingVariableLocationTests.cs" />
<Compile Include="Src\TextTemplatingVariablesTests.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Configuration" />

Loading…
Cancel
Save