Skip to content

Commit da8bcdc

Browse files
committed
Support C# format strings in config
1 parent f37d7df commit da8bcdc

23 files changed

+1125
-29
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace GitVersion.Core.Tests.Extensions;
2+
3+
public static class ShouldlyExtensions
4+
{
5+
/// <summary>
6+
/// Asserts that the action throws an exception of type TException
7+
/// with the expected message.
8+
/// </summary>
9+
public static void ShouldThrowWithMessage<TException>(this Action action, string expectedMessage) where TException : Exception
10+
{
11+
var ex = Should.Throw<TException>(action);
12+
ex.Message.ShouldBe(expectedMessage);
13+
}
14+
15+
/// <summary>
16+
/// Asserts that the action throws an exception of type TException,
17+
/// and allows further assertion on the exception instance.
18+
/// </summary>
19+
public static void ShouldThrow<TException>(this Action action, Action<TException> additionalAssertions) where TException : Exception
20+
{
21+
var ex = Should.Throw<TException>(action);
22+
additionalAssertions(ex);
23+
}
24+
}

src/GitVersion.Core.Tests/Extensions/StringFormatWithExtensionTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,29 @@ public void FormatProperty_NullObject_WithFallback_QuotedAndEmpty()
244244
var actual = target.FormatWith(propertyObject, this.environment);
245245
Assert.That(actual, Is.EqualTo(""));
246246
}
247+
248+
[Test]
249+
public void FormatAssemblyInformationalVersionWithSemanticVersionCustomFormattedCommitsSinceVersionSource()
250+
{
251+
var semanticVersion = new SemanticVersion
252+
{
253+
Major = 1,
254+
Minor = 2,
255+
Patch = 3,
256+
PreReleaseTag = new SemanticVersionPreReleaseTag(string.Empty, 9, true),
257+
BuildMetaData = new SemanticVersionBuildMetaData("Branch.main")
258+
{
259+
Branch = "main",
260+
VersionSourceSha = "versionSourceSha",
261+
Sha = "commitSha",
262+
ShortSha = "commitShortSha",
263+
CommitsSinceVersionSource = 42,
264+
CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z")
265+
}
266+
};
267+
const string target = "{Major}.{Minor}.{Patch}-{CommitsSinceVersionSource:0000}";
268+
const string expected = "1.2.3-0042";
269+
var actual = target.FormatWith(semanticVersion, this.environment);
270+
Assert.That(actual, Is.EqualTo(expected));
271+
}
247272
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using GitVersion.Helpers;
2+
3+
namespace GitVersion.Tests.Helpers;
4+
5+
[TestFixture]
6+
public class DateFormatterTests
7+
{
8+
[Test]
9+
public void Priority_ShouldBe2() => new DateFormatter().Priority.ShouldBe(2);
10+
11+
[Test]
12+
public void TryFormat_NullValue_ReturnsFalse()
13+
{
14+
var sut = new DateFormatter();
15+
var result = sut.TryFormat(null, "yyyy-MM-dd", out var formatted);
16+
result.ShouldBeFalse();
17+
formatted.ShouldBeEmpty();
18+
}
19+
20+
[TestCase("2021-01-01", "date:yyyy-MM-dd", "2021-01-01")]
21+
[TestCase("2021-01-01T12:00:00Z", "date:yyyy-MM-ddTHH:mm:ssZ", "2021-01-01T12:00:00Z")]
22+
public void TryFormat_ValidDateFormats_ReturnsExpectedResult(string input, string format, string expected)
23+
{
24+
var date = DateTime.Parse(input);
25+
var sut = new DateFormatter();
26+
var result = sut.TryFormat(date, format, out var formatted);
27+
result.ShouldBeTrue();
28+
formatted.ShouldBe(expected);
29+
}
30+
31+
[Test]
32+
public void TryFormat_UnsupportedFormat_ReturnsFalse()
33+
{
34+
var sut = new DateFormatter();
35+
var result = sut.TryFormat(DateTime.Now, "unsupported", out var formatted);
36+
result.ShouldBeFalse();
37+
formatted.ShouldBeEmpty();
38+
}
39+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using GitVersion.Helpers;
2+
3+
namespace GitVersion.Tests.Helpers;
4+
5+
[TestFixture]
6+
public class FormattableFormatterTests
7+
{
8+
[Test]
9+
public void Priority_ShouldBe2() => new FormattableFormatter().Priority.ShouldBe(2);
10+
11+
[Test]
12+
public void TryFormat_NullValue_ReturnsFalse()
13+
{
14+
var sut = new FormattableFormatter();
15+
var result = sut.TryFormat(null, "G", out var formatted);
16+
result.ShouldBeFalse();
17+
formatted.ShouldBeEmpty();
18+
}
19+
20+
[TestCase(123.456, "F2", "123.46")]
21+
[TestCase(1234.456, "F2", "1234.46")]
22+
public void TryFormat_ValidFormats_ReturnsExpectedResult(object input, string format, string expected)
23+
{
24+
var sut = new FormattableFormatter();
25+
var result = sut.TryFormat(input, format, out var formatted);
26+
result.ShouldBeTrue();
27+
formatted.ShouldBe(expected);
28+
}
29+
30+
[TestCase(123.456, "C", "Format 'C' is not supported in FormattableFormatter")]
31+
[TestCase(123.456, "P", "Format 'P' is not supported in FormattableFormatter")]
32+
[TestCase(1234567890, "N0", "Format 'N0' is not supported in FormattableFormatter")]
33+
[TestCase(1234567890, "Z", "Format 'Z' is not supported in FormattableFormatter")]
34+
public void TryFormat_UnsupportedFormat_ReturnsFalse(object input, string format, string expected)
35+
{
36+
var sut = new FormattableFormatter();
37+
var result = sut.TryFormat(input, format, out var formatted);
38+
result.ShouldBeFalse();
39+
formatted.ShouldBe(expected);
40+
}
41+
}

0 commit comments

Comments
 (0)