|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Globalization; |
| 6 | +using Microsoft.PowerShell.OutGridView.Models; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Microsoft.PowerShell.ConsoleGuiTools.Tests; |
| 10 | + |
| 11 | +public class DataTableColumnTests |
| 12 | +{ |
| 13 | + [Fact] |
| 14 | + public void FormatValue_NullValue_ReturnsEmptyString() |
| 15 | + { |
| 16 | + var column = new DataTableColumn("Test", "$_.Test"); |
| 17 | + Assert.Equal(string.Empty, column.FormatValue(null)); |
| 18 | + } |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public void FormatValue_WithFormatString_UsesFormatString() |
| 22 | + { |
| 23 | + var column = new DataTableColumn("Test", "$_.Test") { FormatString = "N2" }; |
| 24 | + var result = column.FormatValue(123.456m); |
| 25 | + // Should format as decimal with 2 decimal places |
| 26 | + Assert.Contains("123", result); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void FormatValue_NullFormatString_UsesToString() |
| 31 | + { |
| 32 | + // When FormatString is explicitly null, use plain ToString (no default formatting) |
| 33 | + var column = new DataTableColumn("Id", "$_.Id") { FormatString = null }; |
| 34 | + var result = column.FormatValue(1234); |
| 35 | + // Should NOT have thousands separator — plain ToString |
| 36 | + Assert.Equal("1234", result); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void FormatValue_EmptyFormatString_UsesDefaultTypeFormatting() |
| 41 | + { |
| 42 | + // When FormatString is empty string (not null), use type-based defaults |
| 43 | + var column = new DataTableColumn("Size", "$_.Size") { FormatString = "" }; |
| 44 | + var result = column.FormatValue(1234); |
| 45 | + // int with empty FormatString should use N0 formatting (thousands separator) |
| 46 | + var expected = 1234.ToString("N0", CultureInfo.CurrentCulture); |
| 47 | + Assert.Equal(expected, result); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void FormatValue_DateTime_WithEmptyFormatString_UsesGeneralFormat() |
| 52 | + { |
| 53 | + var column = new DataTableColumn("Date", "$_.Date") { FormatString = "" }; |
| 54 | + var dt = new DateTime(2024, 6, 15, 14, 30, 0); |
| 55 | + var result = column.FormatValue(dt); |
| 56 | + var expected = dt.ToString("G", CultureInfo.CurrentCulture); |
| 57 | + Assert.Equal(expected, result); |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void FormatValue_Decimal_WithEmptyFormatString_UsesN0() |
| 62 | + { |
| 63 | + var column = new DataTableColumn("Amount", "$_.Amount") { FormatString = "" }; |
| 64 | + var result = column.FormatValue(1234567.89m); |
| 65 | + var expected = 1234567.89m.ToString("N0", CultureInfo.CurrentCulture); |
| 66 | + Assert.Equal(expected, result); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public void FormatValue_Double_WithEmptyFormatString_UsesN2() |
| 71 | + { |
| 72 | + var column = new DataTableColumn("Value", "$_.Value") { FormatString = "" }; |
| 73 | + var result = column.FormatValue(3.14159); |
| 74 | + var expected = 3.14159.ToString("N2", CultureInfo.CurrentCulture); |
| 75 | + Assert.Equal(expected, result); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void FormatValue_String_ReturnsStringValue() |
| 80 | + { |
| 81 | + var column = new DataTableColumn("Name", "$_.Name"); |
| 82 | + Assert.Equal("hello", column.FormatValue("hello")); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public void FormatValue_InvalidFormatString_FallsThrough() |
| 87 | + { |
| 88 | + var column = new DataTableColumn("Test", "$_.Test") { FormatString = "ZZZZ_INVALID" }; |
| 89 | + // An invalid format string should not throw — it should fall through |
| 90 | + var result = column.FormatValue(42); |
| 91 | + Assert.NotNull(result); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void Equals_SameLabelAndAccessor_ReturnsTrue() |
| 96 | + { |
| 97 | + var a = new DataTableColumn("Name", "$_.Name"); |
| 98 | + var b = new DataTableColumn("Name", "$_.Name"); |
| 99 | + Assert.True(a.Equals(b)); |
| 100 | + } |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public void Equals_DifferentLabel_ReturnsFalse() |
| 104 | + { |
| 105 | + var a = new DataTableColumn("Name", "$_.Name"); |
| 106 | + var b = new DataTableColumn("Id", "$_.Name"); |
| 107 | + Assert.False(a.Equals(b)); |
| 108 | + } |
| 109 | + |
| 110 | + [Fact] |
| 111 | + public void Equals_DifferentAccessor_ReturnsFalse() |
| 112 | + { |
| 113 | + var a = new DataTableColumn("Name", "$_.Name"); |
| 114 | + var b = new DataTableColumn("Name", "$_.Id"); |
| 115 | + Assert.False(a.Equals(b)); |
| 116 | + } |
| 117 | + |
| 118 | + [Fact] |
| 119 | + public void Equals_Null_ReturnsFalse() |
| 120 | + { |
| 121 | + var a = new DataTableColumn("Name", "$_.Name"); |
| 122 | + Assert.False(a.Equals(null)); |
| 123 | + } |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public void GetHashCode_EqualColumns_SameHashCode() |
| 127 | + { |
| 128 | + var a = new DataTableColumn("Name", "$_.Name"); |
| 129 | + var b = new DataTableColumn("Name", "$_.Name"); |
| 130 | + Assert.Equal(a.GetHashCode(), b.GetHashCode()); |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public void ToString_ReturnsBase64EncodedString() |
| 135 | + { |
| 136 | + var column = new DataTableColumn("Name", "$_.Name"); |
| 137 | + var result = column.ToString(); |
| 138 | + // Should decode to "Name" + "$_.Name" |
| 139 | + var decoded = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(result)); |
| 140 | + Assert.Equal("Name$_.Name", decoded); |
| 141 | + } |
| 142 | + |
| 143 | + [Fact] |
| 144 | + public void Type_ValidStringType_ReturnsCorrectType() |
| 145 | + { |
| 146 | + var column = new DataTableColumn("Test", "$_.Test") |
| 147 | + { |
| 148 | + StringType = typeof(decimal).FullName |
| 149 | + }; |
| 150 | + Assert.Equal(typeof(decimal), column.Type); |
| 151 | + } |
| 152 | + |
| 153 | + [Fact] |
| 154 | + public void Type_NullStringType_ThrowsOnAccess() |
| 155 | + { |
| 156 | + var column = new DataTableColumn("Test", "$_.Test"); |
| 157 | + // StringType is null by default; Type.GetType(null) throws ArgumentNullException |
| 158 | + Assert.Throws<ArgumentNullException>(() => column.Type); |
| 159 | + } |
| 160 | +} |
0 commit comments