|
| 1 | +using GitVersion.Core.Tests.Helpers; |
| 2 | +using GitVersion.Formatting; |
| 3 | + |
| 4 | +namespace GitVersion.Core.Tests.Formatting; |
| 5 | + |
| 6 | +[TestFixture] |
| 7 | +public class LegacyFormatterProblemTests |
| 8 | +{ |
| 9 | + private TestEnvironment environment; |
| 10 | + |
| 11 | + [SetUp] |
| 12 | + public void Setup() => environment = new TestEnvironment(); |
| 13 | + |
| 14 | + // ========================================== |
| 15 | + // PROBLEM 1: Non-existent properties |
| 16 | + // ========================================== |
| 17 | + |
| 18 | + [Test] |
| 19 | + [Category("Problem1")] |
| 20 | + public void Problem1_MissingProperty_ShouldFailGracefully() |
| 21 | + { |
| 22 | + // Test tries to use {MajorMinorPatch} on SemanticVersion but that property doesn't exist |
| 23 | + var semanticVersion = new SemanticVersion |
| 24 | + { |
| 25 | + Major = 1, |
| 26 | + Minor = 2, |
| 27 | + Patch = 3 |
| 28 | + }; |
| 29 | + |
| 30 | + const string template = "{MajorMinorPatch}"; // This property doesn't exist on SemanticVersion |
| 31 | + |
| 32 | + // Currently this will throw or behave unexpectedly |
| 33 | + // Should either throw meaningful error or handle gracefully |
| 34 | + Assert.Throws<ArgumentException>(() => template.FormatWith(semanticVersion, environment)); |
| 35 | + } |
| 36 | + |
| 37 | + // ========================================== |
| 38 | + // PROBLEM 2: Double negative handling |
| 39 | + // ========================================== |
| 40 | + |
| 41 | + [Test] |
| 42 | + [Category("Problem2")] |
| 43 | + public void Problem2_NegativeValue_ShouldNotDoubleNegative() |
| 44 | + { |
| 45 | + var testObject = new { Value = -5 }; |
| 46 | + const string template = "{Value:positive;negative;zero}"; |
| 47 | + |
| 48 | + // EXPECTED: "negative" (just the literal text from section 2) |
| 49 | + // ACTUAL: "-negative" (the negative sign from -5 plus the literal "negative") |
| 50 | + const string expected = "negative"; |
| 51 | + |
| 52 | + var actual = template.FormatWith(testObject, environment); |
| 53 | + |
| 54 | + // This will currently fail - we get "-negative" instead of "negative" |
| 55 | + actual.ShouldBe(expected, "Negative values should use section text without the negative sign"); |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + [Category("Problem2")] |
| 60 | + public void Problem2_PositiveValue_ShouldFormatCorrectly() |
| 61 | + { |
| 62 | + var testObject = new { Value = 5 }; |
| 63 | + const string template = "{Value:positive;negative;zero}"; |
| 64 | + const string expected = "positive"; |
| 65 | + |
| 66 | + var actual = template.FormatWith(testObject, environment); |
| 67 | + actual.ShouldBe(expected); |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + [Category("Problem2")] |
| 72 | + public void Problem2_ZeroValue_ShouldUseZeroSection() |
| 73 | + { |
| 74 | + var testObject = new { Value = 0 }; |
| 75 | + const string template = "{Value:positive;negative;zero}"; |
| 76 | + const string expected = "zero"; |
| 77 | + |
| 78 | + var actual = template.FormatWith(testObject, environment); |
| 79 | + actual.ShouldBe(expected); |
| 80 | + } |
| 81 | + |
| 82 | + // ========================================== |
| 83 | + // PROBLEM 3: Insufficient formatting logic |
| 84 | + // ========================================== |
| 85 | + |
| 86 | + [Test] |
| 87 | + [Category("Problem3")] |
| 88 | + public void Problem3_NumericFormatting_AllSectionsShouldFormat() |
| 89 | + { |
| 90 | + // Test that numeric formatting works in ALL sections, not just first |
| 91 | + var testObject = new { Value = -42 }; |
| 92 | + const string template = "{Value:0000;0000;0000}"; // All sections should pad with zeros |
| 93 | + |
| 94 | + // EXPECTED: "0042" (absolute value 42, formatted with 0000 in negative section) |
| 95 | + // ACTUAL: "0000" (literal text instead of formatted value) |
| 96 | + const string expected = "0042"; |
| 97 | + |
| 98 | + var actual = template.FormatWith(testObject, environment); |
| 99 | + actual.ShouldBe(expected, "Negative section should format the absolute value, not return literal"); |
| 100 | + } |
| 101 | + |
| 102 | + [Test] |
| 103 | + [Category("Problem3")] |
| 104 | + public void Problem3_FirstSectionWorks_OthersDont() |
| 105 | + { |
| 106 | + // Demonstrate that first section works but others don't |
| 107 | + var positiveObject = new { Value = 42 }; |
| 108 | + var negativeObject = new { Value = -42 }; |
| 109 | + |
| 110 | + const string template = "{Value:0000;WRONG;WRONG}"; |
| 111 | + |
| 112 | + // First section (positive) should work correctly |
| 113 | + var positiveResult = template.FormatWith(positiveObject, environment); |
| 114 | + positiveResult.ShouldBe("0042", "First section should format correctly"); |
| 115 | + |
| 116 | + // Second section (negative) currently broken - returns literal instead of formatting |
| 117 | + var negativeResult = template.FormatWith(negativeObject, environment); |
| 118 | + // This will currently be "WRONG" but should be "0042" (formatted absolute value) |
| 119 | + negativeResult.ShouldNotBe("WRONG", "Negative section should format value, not return literal"); |
| 120 | + } |
| 121 | + |
| 122 | + // ========================================== |
| 123 | + // VERIFY #4654 FIX STILL WORKS |
| 124 | + // ========================================== |
| 125 | + |
| 126 | + [Test] |
| 127 | + [Category("Issue4654")] |
| 128 | + public void Issue4654_LegacySyntax_ShouldStillWork() |
| 129 | + { |
| 130 | + // Verify the original #4654 fix still works |
| 131 | + var testObject = new { CommitsSinceVersionSource = 2 }; |
| 132 | + const string template = "{CommitsSinceVersionSource:0000;;''}"; |
| 133 | + const string expected = "0002"; |
| 134 | + |
| 135 | + var actual = template.FormatWith(testObject, environment); |
| 136 | + actual.ShouldBe(expected, "Issue #4654 fix must be preserved"); |
| 137 | + } |
| 138 | + |
| 139 | + [Test] |
| 140 | + [Category("Issue4654")] |
| 141 | + public void Issue4654_ZeroValue_ShouldUseEmptyString() |
| 142 | + { |
| 143 | + // Zero values should use the third section (empty string) |
| 144 | + var testObject = new { CommitsSinceVersionSource = 0 }; |
| 145 | + const string template = "{CommitsSinceVersionSource:0000;;''}"; |
| 146 | + const string expected = ""; |
| 147 | + |
| 148 | + var actual = template.FormatWith(testObject, environment); |
| 149 | + actual.ShouldBe(expected, "Zero values should use third section (empty)"); |
| 150 | + } |
| 151 | +} |
0 commit comments