diff --git a/src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingStringParser.cs b/src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingStringParser.cs new file mode 100644 index 0000000000..db706e0ba9 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingStringParser.cs @@ -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); + } +} diff --git a/src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingVariables.cs b/src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingVariables.cs new file mode 100644 index 0000000000..6647850ee4 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingVariables.cs @@ -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); + } +} diff --git a/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingAssemblyResolver.cs b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingAssemblyResolver.cs index 35f95ad1e8..2d63c639d1 100644 --- a/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingAssemblyResolver.cs +++ b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingAssemblyResolver.cs @@ -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 { 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 return assemblyReference; } + string ExpandVariables(string assemblyReference) + { + return templatingVariables.Expand(assemblyReference); + } + string ResolveAssemblyFromProject(string assemblyReference) { foreach (ReferenceProjectItem refProjectItem in project.GetItemsOfType(ItemType.Reference)) { diff --git a/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingDirectoryVariable.cs b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingDirectoryVariable.cs new file mode 100644 index 0000000000..ac3428f495 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingDirectoryVariable.cs @@ -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; + } + } +} diff --git a/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingStringParser.cs b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingStringParser.cs new file mode 100644 index 0000000000..efecf4847b --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingStringParser.cs @@ -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); + } + } +} diff --git a/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingVariableLocation.cs b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingVariableLocation.cs new file mode 100644 index 0000000000..3c57284d50 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingVariableLocation.cs @@ -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; + } + } +} diff --git a/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingVariables.cs b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingVariables.cs new file mode 100644 index 0000000000..760da94225 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingVariables.cs @@ -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 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; + } + } +} diff --git a/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingVariablesStringBuilder.cs b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingVariablesStringBuilder.cs new file mode 100644 index 0000000000..b69380a909 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingVariablesStringBuilder.cs @@ -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); + } + } +} diff --git a/src/AddIns/Misc/TextTemplating/Project/TextTemplating.csproj b/src/AddIns/Misc/TextTemplating/Project/TextTemplating.csproj index 0e1e3613dc..b48c0f09ad 100644 --- a/src/AddIns/Misc/TextTemplating/Project/TextTemplating.csproj +++ b/src/AddIns/Misc/TextTemplating/Project/TextTemplating.csproj @@ -60,6 +60,8 @@ + + @@ -68,6 +70,7 @@ + @@ -76,6 +79,10 @@ + + + + diff --git a/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeAssemblyParserService.cs b/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeAssemblyParserService.cs index f90d5743e4..6ab9b11213 100644 --- a/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeAssemblyParserService.cs +++ b/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeAssemblyParserService.cs @@ -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; } diff --git a/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingStringParser.cs b/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingStringParser.cs new file mode 100644 index 0000000000..eab83d1da4 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingStringParser.cs @@ -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 Properties = new Dictionary(); + + 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); + } + } +} diff --git a/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingVariables.cs b/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingVariables.cs new file mode 100644 index 0000000000..6abca76503 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingVariables.cs @@ -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 Variables = new Dictionary(); + + public void AddVariable(string name, string value) + { + Variables.Add(name, value); + } + + public string Expand(string name) + { + foreach (KeyValuePair 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; + } + } +} diff --git a/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingAssemblyResolverTests.cs b/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingAssemblyResolverTests.cs index 00b3c1238a..55901f45a6 100644 --- a/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingAssemblyResolverTests.cs +++ b/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingAssemblyResolverTests.cs @@ -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 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); + } } } diff --git a/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingHostTests.cs b/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingHostTests.cs index 01865570fe..050165667f 100644 --- a/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingHostTests.cs +++ b/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingHostTests.cs @@ -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"; diff --git a/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingVariableLocationTests.cs b/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingVariableLocationTests.cs new file mode 100644 index 0000000000..924eb0adf0 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingVariableLocationTests.cs @@ -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); + } + } +} diff --git a/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingVariablesTests.cs b/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingVariablesTests.cs new file mode 100644 index 0000000000..32fe428b08 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingVariablesTests.cs @@ -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 locations = + variables.GetVariables(null).ToList(); + + Assert.AreEqual(0, locations.Count); + } + + [Test] + public void GetVariableLocations_EmptyStringPassed_ReturnsNoVariables() + { + CreateTextTemplatingVariables(); + List locations = + variables.GetVariables(String.Empty).ToList(); + + Assert.AreEqual(0, locations.Count); + } + + [Test] + public void GetVariableLocations_TextHasNoVariables_ReturnsNoVariables() + { + CreateTextTemplatingVariables(); + List locations = + variables.GetVariables("No Variables").ToList(); + + Assert.AreEqual(0, locations.Count); + } + + [Test] + public void GetVariableLocations_TextHasVariableStartButNoEnd_ReturnsNoVariables() + { + CreateTextTemplatingVariables(); + List locations = + variables.GetVariables("$(No Variables").ToList(); + + Assert.AreEqual(0, locations.Count); + } + + [Test] + public void GetVariableLocations_TwoProperties_ReturnsTwoVariables() + { + CreateTextTemplatingVariables(); + + List 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); + } + } +} diff --git a/src/AddIns/Misc/TextTemplating/Test/TextTemplating.Tests.csproj b/src/AddIns/Misc/TextTemplating/Test/TextTemplating.Tests.csproj index 0a613b236c..dacaa08e62 100644 --- a/src/AddIns/Misc/TextTemplating/Test/TextTemplating.Tests.csproj +++ b/src/AddIns/Misc/TextTemplating/Test/TextTemplating.Tests.csproj @@ -64,6 +64,8 @@ + + @@ -79,6 +81,8 @@ + +