|
| 1 | +using GitVersion.Core.Tests.Extensions; |
| 2 | +using GitVersion.Helpers; |
| 3 | + |
| 4 | +namespace GitVersion.Tests.Helpers; |
| 5 | + |
| 6 | +[TestFixture] |
| 7 | +public partial class InputSanitizerTests |
| 8 | +{ |
| 9 | + [TestFixture] |
| 10 | + public class SanitizeFormatTests : InputSanitizerTests |
| 11 | + { |
| 12 | + [Test] |
| 13 | + public void SanitizeFormat_WithValidFormat_ReturnsInput() |
| 14 | + { |
| 15 | + var sut = new InputSanitizer(); |
| 16 | + const string validFormat = "yyyy-MM-dd"; |
| 17 | + sut.SanitizeFormat(validFormat).ShouldBe(validFormat); |
| 18 | + } |
| 19 | + |
| 20 | + [TestCase("")] |
| 21 | + [TestCase(" ")] |
| 22 | + [TestCase("\t")] |
| 23 | + public void SanitizeFormat_WithEmptyOrWhitespace_ThrowsFormatException(string invalidFormat) |
| 24 | + { |
| 25 | + var sut = new InputSanitizer(); |
| 26 | + Action act = () => sut.SanitizeFormat(invalidFormat); |
| 27 | + act.ShouldThrowWithMessage<FormatException>("Format string cannot be empty."); |
| 28 | + } |
| 29 | + |
| 30 | + [Test] |
| 31 | + public void SanitizeFormat_WithTooLongFormat_ThrowsFormatException() |
| 32 | + { |
| 33 | + var sut = new InputSanitizer(); |
| 34 | + var longFormat = new string('x', 51); |
| 35 | + Action act = () => sut.SanitizeFormat(longFormat); |
| 36 | + act.ShouldThrowWithMessage<FormatException>("Format string too long: 'xxxxxxxxxxxxxxxxxxxx...'"); |
| 37 | + } |
| 38 | + |
| 39 | + [Test] |
| 40 | + public void SanitizeFormat_WithMaxValidLength_ReturnsInput() |
| 41 | + { |
| 42 | + var sut = new InputSanitizer(); |
| 43 | + var maxLengthFormat = new string('x', 50); |
| 44 | + sut.SanitizeFormat(maxLengthFormat).ShouldBe(maxLengthFormat); |
| 45 | + } |
| 46 | + |
| 47 | + [TestCase("\r", TestName = "SanitizeFormat_ControlChar_CR")] |
| 48 | + [TestCase("\n", TestName = "SanitizeFormat_ControlChar_LF")] |
| 49 | + [TestCase("\0", TestName = "SanitizeFormat_ControlChar_Null")] |
| 50 | + [TestCase("\x01", TestName = "SanitizeFormat_ControlChar_0x01")] |
| 51 | + [TestCase("\x1F", TestName = "SanitizeFormat_ControlChar_0x1F")] |
| 52 | + public void SanitizeFormat_WithControlCharacters_ThrowsFormatException(string controlChar) |
| 53 | + { |
| 54 | + var sut = new InputSanitizer(); |
| 55 | + var formatWithControl = $"valid{controlChar}format"; |
| 56 | + Action act = () => sut.SanitizeFormat(formatWithControl); |
| 57 | + act.ShouldThrowWithMessage<FormatException>("Format string contains invalid control characters"); |
| 58 | + } |
| 59 | + |
| 60 | + [Test] |
| 61 | + public void SanitizeFormat_WithTabCharacter_ReturnsInput() |
| 62 | + { |
| 63 | + var sut = new InputSanitizer(); |
| 64 | + const string formatWithTab = "format\twith\ttab"; |
| 65 | + sut.SanitizeFormat(formatWithTab).ShouldBe(formatWithTab); |
| 66 | + } |
| 67 | + |
| 68 | + [TestCase("yyyy-MM-dd")] |
| 69 | + [TestCase("HH:mm:ss")] |
| 70 | + [TestCase("0.00")] |
| 71 | + [TestCase("C2")] |
| 72 | + [TestCase("X8")] |
| 73 | + [TestCase("format with spaces")] |
| 74 | + [TestCase("format-with-dashes")] |
| 75 | + [TestCase("format_with_underscores")] |
| 76 | + public void SanitizeFormat_WithValidFormats_ReturnsInput(string validFormat) |
| 77 | + { |
| 78 | + var sut = new InputSanitizer(); |
| 79 | + sut.SanitizeFormat(validFormat).ShouldBe(validFormat); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments