Skip to content

Commit 8a16bcb

Browse files
committed
Adding a unit test for FormatWith method
1 parent d0f4994 commit 8a16bcb

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/GitVersionCore.Tests/GitVersionCore.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
<Compile Include="Helpers\PathHelper.cs" />
144144
<Compile Include="IntegrationTests\MasterScenarios.cs" />
145145
<Compile Include="SemanticVersionTests.cs" />
146+
<Compile Include="StringFormatWithExtensionTests.cs" />
146147
<Compile Include="TestableVersionVariables.cs" />
147148
<Compile Include="TestEffectiveConfiguration.cs" />
148149
<Compile Include="TestFileSystem.cs" />
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
3+
using GitVersion;
4+
using NUnit.Framework;
5+
6+
namespace GitVersionCore.Tests
7+
{
8+
[TestFixture]
9+
10+
public class StringFormatWithExtensionTests
11+
{
12+
[Test]
13+
public void FormatWith_NoTokens()
14+
{
15+
var propertyObject = new { };
16+
var target = "Some String without tokens";
17+
var expected = target;
18+
var actual = target.FormatWith(propertyObject);
19+
Assert.AreEqual(expected, actual);
20+
}
21+
22+
[Test]
23+
public void FormatWith_SingleSimpleToken()
24+
{
25+
var propertyObject = new { SomeProperty = "SomeValue" };
26+
var target = "{SomeProperty}";
27+
var expected = "SomeValue";
28+
var actual = target.FormatWith(propertyObject);
29+
Assert.AreEqual(expected, actual);
30+
}
31+
32+
[Test]
33+
public void FormatWith_MultipleTokensAndVerbatimText()
34+
{
35+
var propertyObject = new { SomeProperty = "SomeValue", AnotherProperty = "Other Value" };
36+
var target = "{SomeProperty} some text {AnotherProperty}";
37+
var expected = "SomeValue some text Other Value";
38+
var actual = target.FormatWith(propertyObject);
39+
Assert.AreEqual(expected, actual);
40+
}
41+
42+
[Test]
43+
public void FormatWith_EnvVarToken()
44+
{
45+
Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR", "Env Var Value");
46+
var propertyObject = new { };
47+
var target = "{$GIT_VERSION_TEST_VAR}";
48+
var expected = "Env Var Value";
49+
var actual = target.FormatWith(propertyObject);
50+
Assert.AreEqual(expected, actual);
51+
}
52+
53+
[Test]
54+
public void FormatWith_EnvVarTokenWithFallback()
55+
{
56+
Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR", "Env Var Value");
57+
var propertyObject = new { };
58+
var target = "{$GIT_VERSION_TEST_VAR??fallback}";
59+
var expected = "Env Var Value";
60+
var actual = target.FormatWith(propertyObject);
61+
Assert.AreEqual(expected, actual);
62+
}
63+
64+
[Test]
65+
public void FormatWith_UnsetEnvVarTokenWithFallback()
66+
{
67+
Environment.SetEnvironmentVariable("GIT_VERSION_UNSET_TEST_VAR", null);
68+
var propertyObject = new { };
69+
var target = "{$GIT_VERSION_UNSET_TEST_VAR??fallback}";
70+
var expected = "fallback";
71+
var actual = target.FormatWith(propertyObject);
72+
Assert.AreEqual(expected, actual);
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)