|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using NFluent; |
| 5 | +using ReClassNET.Extensions; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace ReClass.NET_Tests.Extensions |
| 9 | +{ |
| 10 | + public class StringExtensionTest |
| 11 | + { |
| 12 | + public static TheoryData<char> GetTestIsPrintableData() => new TheoryData<char> |
| 13 | + { |
| 14 | + '0', '9', ' ', '\t', '\n', 'a', 'A', 'z', 'Z', '-', '_', '°', '^', '"', '\\', '\"', '&', '@', '€', '$', '|', '<', '>', ';', ',', '.', ':', '#', '*', '+', '~', '`', '´', 'ß', '?', '=', '(', ')', '[', ']', '{', '}' |
| 15 | + }; |
| 16 | + |
| 17 | + [Theory] |
| 18 | + [MemberData(nameof(GetTestIsPrintableData))] |
| 19 | + public void TestIsPrintable(char c) |
| 20 | + { |
| 21 | + Check.That(c.IsPrintable()).IsTrue(); |
| 22 | + } |
| 23 | + |
| 24 | + public static TheoryData<char> GetTestIsNotPrintableData() => new TheoryData<char> |
| 25 | + { |
| 26 | + '\u0000','\u0001', '\u0002', '\u009A','\u009B', '\u009C', '\u009D','\u009E', '\u009F' |
| 27 | + }; |
| 28 | + |
| 29 | + [Theory] |
| 30 | + [MemberData(nameof(GetTestIsNotPrintableData))] |
| 31 | + public void TestIsNotPrintable(char c) |
| 32 | + { |
| 33 | + Check.That(c.IsPrintable()).IsFalse(); |
| 34 | + } |
| 35 | + |
| 36 | + public static TheoryData<string, int, string> GetTestLimitLengthData() => new TheoryData<string, int, string> |
| 37 | + { |
| 38 | + { string.Empty, 0, string.Empty }, |
| 39 | + { string.Empty, 1, string.Empty }, |
| 40 | + { "01234", 0, string.Empty }, |
| 41 | + { "01234", 1, "0" }, |
| 42 | + { "01234", 5, "01234" }, |
| 43 | + { "01234", 10, "01234" } |
| 44 | + }; |
| 45 | + |
| 46 | + [Theory] |
| 47 | + [MemberData(nameof(GetTestLimitLengthData))] |
| 48 | + public void TestLimitLength(string sut, int length, string expected) |
| 49 | + { |
| 50 | + Check.That(sut.LimitLength(length)).IsEqualTo(expected); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void TestLimitLengthThrows() |
| 55 | + { |
| 56 | + Check.ThatCode(() => "".LimitLength(-1)).Throws<ArgumentOutOfRangeException>(); |
| 57 | + } |
| 58 | + |
| 59 | + public static TheoryData<IEnumerable<byte>, IEnumerable<char>> GetTestInterpretAsUtf8Data() => new TheoryData<IEnumerable<byte>, IEnumerable<char>> |
| 60 | + { |
| 61 | + { new byte[0], string.Empty }, |
| 62 | + { new [] { (byte)'t', (byte)'e', (byte)'s', (byte)'t' }, "test".ToArray() /* https://github.com/tpierrain/NFluent/issues/299 */ } |
| 63 | + }; |
| 64 | + |
| 65 | + [Theory] |
| 66 | + [MemberData(nameof(GetTestInterpretAsUtf8Data))] |
| 67 | + public void TestInterpretAsUtf8(IEnumerable<byte> sut, IEnumerable<char> expected) |
| 68 | + { |
| 69 | + Check.That(sut.InterpretAsUtf8()).ContainsExactly(expected); |
| 70 | + } |
| 71 | + |
| 72 | + public static TheoryData<IEnumerable<byte>, IEnumerable<char>> GetTestInterpretAsUtf16Data() => new TheoryData<IEnumerable<byte>, IEnumerable<char>> |
| 73 | + { |
| 74 | + { new byte[0], string.Empty }, |
| 75 | + { new [] { (byte)'t', (byte)0, (byte)'e', (byte)0, (byte)'s', (byte)0, (byte)'t', (byte)0 }, "test".ToArray() /* https://github.com/tpierrain/NFluent/issues/299 */ } |
| 76 | + }; |
| 77 | + |
| 78 | + [Theory] |
| 79 | + [MemberData(nameof(GetTestInterpretAsUtf16Data))] |
| 80 | + public void TestInterpretAsUtf16(IEnumerable<byte> sut, IEnumerable<char> expected) |
| 81 | + { |
| 82 | + Check.That(sut.InterpretAsUtf16()).ContainsExactly(expected); |
| 83 | + } |
| 84 | + |
| 85 | + public static TheoryData<IEnumerable<char>, float> GetTestCalculatePrintableDataThresholdData() => new TheoryData<IEnumerable<char>, float> |
| 86 | + { |
| 87 | + { new char[0], 0.0f }, |
| 88 | + { new [] { '\0' }, 0.0f }, |
| 89 | + { new [] { 'a' }, 1.0f }, |
| 90 | + { new [] { '\0', 'a' }, 0.0f }, |
| 91 | + { new [] { 'a', '\0' }, 0.5f }, |
| 92 | + { new [] { '\0', 'a', 'a' }, 0.0f }, |
| 93 | + { new [] { 'a', 'a', '\0' }, 2 / 3.0f }, |
| 94 | + { new [] { 'a', 'a', '\0', '\0' }, 0.5f }, |
| 95 | + { new [] { 'a', 'a', '\0', '\0', '\0' }, 2 / 5.0f } |
| 96 | + }; |
| 97 | + |
| 98 | + [Theory] |
| 99 | + [MemberData(nameof(GetTestCalculatePrintableDataThresholdData))] |
| 100 | + public void TestCalculatePrintableDataThreshold(IEnumerable<char> sut, float expected) |
| 101 | + { |
| 102 | + Check.That(sut.CalculatePrintableDataThreshold()).IsCloseTo(expected, 0.001); |
| 103 | + } |
| 104 | + |
| 105 | + [Theory] |
| 106 | + [InlineData('a')] |
| 107 | + [InlineData('a', 'a')] |
| 108 | + [InlineData('a', 'a', 'f')] |
| 109 | + [InlineData('#', '+', 'r', '?', 'ß', '%', '&', '§', '_', '0', '/', '(', 'ö')] |
| 110 | + public void TestIsPrintableData(params char[] sut) |
| 111 | + { |
| 112 | + Check.That(sut.IsPrintableData()).IsTrue(); |
| 113 | + } |
| 114 | + |
| 115 | + [Theory] |
| 116 | + [InlineData] |
| 117 | + [InlineData('a', '\0')] |
| 118 | + [InlineData('\0', 'a')] |
| 119 | + [InlineData('a', 'a', '\0')] |
| 120 | + [InlineData('a', 'a', 'f', '\0')] |
| 121 | + [InlineData('a', 'a', '\0', 'f')] |
| 122 | + [InlineData('a', '\0', 'a', 'f')] |
| 123 | + [InlineData('\0', 'a', 'a', 'f')] |
| 124 | + public void TestIsNotPrintableData(params char[] sut) |
| 125 | + { |
| 126 | + Check.That(sut.IsPrintableData()).IsFalse(); |
| 127 | + } |
| 128 | + |
| 129 | + [Theory] |
| 130 | + [InlineData('a', 'a', 'f', '\0')] |
| 131 | + [InlineData('1', '2', '3', '4', '5', '6', '7', '8', '\0', '\0')] |
| 132 | + [InlineData('1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', '\0', '\0', '\0', '\0')] |
| 133 | + public void TestIsLikelyPrintableData(params char[] sut) |
| 134 | + { |
| 135 | + Check.That(sut.IsLikelyPrintableData()).IsTrue(); |
| 136 | + } |
| 137 | + |
| 138 | + [Theory] |
| 139 | + [InlineData] |
| 140 | + [InlineData('a', '\0')] |
| 141 | + [InlineData('\0', 'a')] |
| 142 | + [InlineData('a', 'a', '\0')] |
| 143 | + [InlineData('a', 'a', '\0', 'f')] |
| 144 | + [InlineData('a', 'a', '\0', '\0')] |
| 145 | + [InlineData('a', '\0', 'a', 'f')] |
| 146 | + public void TestIsNotLikelyPrintableData(params char[] sut) |
| 147 | + { |
| 148 | + Check.That(sut.IsPrintableData()).IsFalse(); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments