13 changed files with 213 additions and 16 deletions
@ -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 ITextTemplatingEnvironment |
||||||
|
{ |
||||||
|
string ExpandEnvironmentVariables(string name); |
||||||
|
} |
||||||
|
} |
||||||
@ -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 ITextTemplatingPathResolver |
||||||
|
{ |
||||||
|
string ResolvePath(string path); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,15 @@ |
|||||||
|
// 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 TextTemplatingEnvironment : ITextTemplatingEnvironment |
||||||
|
{ |
||||||
|
public string ExpandEnvironmentVariables(string name) |
||||||
|
{ |
||||||
|
return Environment.ExpandEnvironmentVariables(name); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,32 @@ |
|||||||
|
// 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 TextTemplatingPathResolver : ITextTemplatingPathResolver |
||||||
|
{ |
||||||
|
ITextTemplatingVariables templatingVariables; |
||||||
|
ITextTemplatingEnvironment environment; |
||||||
|
|
||||||
|
public TextTemplatingPathResolver() |
||||||
|
: this(new TextTemplatingVariables(), new TextTemplatingEnvironment()) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
public TextTemplatingPathResolver( |
||||||
|
ITextTemplatingVariables templatingVariables, |
||||||
|
ITextTemplatingEnvironment environment) |
||||||
|
{ |
||||||
|
this.templatingVariables = templatingVariables; |
||||||
|
this.environment = environment; |
||||||
|
} |
||||||
|
|
||||||
|
public string ResolvePath(string path) |
||||||
|
{ |
||||||
|
path = environment.ExpandEnvironmentVariables(path); |
||||||
|
return templatingVariables.Expand(path); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,29 @@ |
|||||||
|
// 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 FakeTextTemplatingEnvironment : ITextTemplatingEnvironment |
||||||
|
{ |
||||||
|
public Dictionary<string, string> Variables = new Dictionary<string, string>(); |
||||||
|
|
||||||
|
public string ExpandEnvironmentVariables(string name) |
||||||
|
{ |
||||||
|
string value = null; |
||||||
|
if (Variables.TryGetValue(name, out value)) { |
||||||
|
return value; |
||||||
|
} |
||||||
|
return name; |
||||||
|
} |
||||||
|
|
||||||
|
public void AddVariable(string name, string value) |
||||||
|
{ |
||||||
|
name = "%" + name + "%"; |
||||||
|
Variables.Add(name, value); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,28 @@ |
|||||||
|
// 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 FakeTextTemplatingPathResolver : ITextTemplatingPathResolver |
||||||
|
{ |
||||||
|
public Dictionary<string, string> Paths = new Dictionary<string, string>(); |
||||||
|
|
||||||
|
public string ResolvePath(string path) |
||||||
|
{ |
||||||
|
string resolvedPath = null; |
||||||
|
if (Paths.TryGetValue(path, out resolvedPath)) { |
||||||
|
return resolvedPath; |
||||||
|
} |
||||||
|
return path; |
||||||
|
} |
||||||
|
|
||||||
|
public void AddPath(string path, string resolvedPath) |
||||||
|
{ |
||||||
|
Paths.Add(path, resolvedPath); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,57 @@ |
|||||||
|
// 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; |
||||||
|
using TextTemplating.Tests.Helpers; |
||||||
|
|
||||||
|
namespace TextTemplating.Tests |
||||||
|
{ |
||||||
|
[TestFixture] |
||||||
|
public class TextTemplatingPathResolverTests |
||||||
|
{ |
||||||
|
TextTemplatingPathResolver pathResolver; |
||||||
|
FakeTextTemplatingVariables fakeTemplatingVariables; |
||||||
|
FakeTextTemplatingEnvironment fakeEnvironment; |
||||||
|
|
||||||
|
void CreatePathResolver() |
||||||
|
{ |
||||||
|
fakeTemplatingVariables = new FakeTextTemplatingVariables(); |
||||||
|
fakeEnvironment = new FakeTextTemplatingEnvironment(); |
||||||
|
pathResolver = new TextTemplatingPathResolver(fakeTemplatingVariables, fakeEnvironment); |
||||||
|
} |
||||||
|
|
||||||
|
void AddEnvironmentVariable(string name, string value) |
||||||
|
{ |
||||||
|
fakeEnvironment.AddVariable(name, value); |
||||||
|
} |
||||||
|
|
||||||
|
void AddTemplateVariable(string name, string value) |
||||||
|
{ |
||||||
|
fakeTemplatingVariables.AddVariable(name, value); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void ResolvePath_EnvironmentVariableInPath_ReturnsPathWithEnvironmentVariableExpanded() |
||||||
|
{ |
||||||
|
CreatePathResolver(); |
||||||
|
AddEnvironmentVariable("windir", @"c:\windows"); |
||||||
|
|
||||||
|
string path = pathResolver.ResolvePath("%windir%"); |
||||||
|
|
||||||
|
Assert.AreEqual(@"c:\windows", path); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void ResolvePath_TemplateVariableInPath_ReturnsPathWithTemplateVariableExpanded() |
||||||
|
{ |
||||||
|
CreatePathResolver(); |
||||||
|
AddTemplateVariable("SolutionDir", @"d:\projects\MyApp\"); |
||||||
|
|
||||||
|
string path = pathResolver.ResolvePath("$(SolutionDir)"); |
||||||
|
|
||||||
|
Assert.AreEqual(@"d:\projects\MyApp\", path); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue