|
3 | 3 | using System.Globalization; |
4 | 4 | using System.Linq; |
5 | 5 | using System.Reactive.Subjects; |
| 6 | +using System.Text.RegularExpressions; |
6 | 7 | using Avalonia.Controls.Templates; |
7 | 8 | using Avalonia.Data; |
| 9 | +using Avalonia.Data.Converters; |
8 | 10 | using Avalonia.Threading; |
9 | 11 | using Avalonia.UnitTests; |
10 | 12 | using Xunit; |
@@ -99,6 +101,51 @@ public void NumberFormat_Is_Applied_Immediately() |
99 | 101 | Assert.Equal(value.ToString(newNumberFormat), control.Text); |
100 | 102 | }); |
101 | 103 | } |
| 104 | + |
| 105 | + private class TestNumericUpDownValueConverter(string format) : IValueConverter |
| 106 | + { |
| 107 | + public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) |
| 108 | + { |
| 109 | + var input = value?.ToString() ?? string.Empty; |
| 110 | + if (string.IsNullOrEmpty(input)) |
| 111 | + return 0m; |
| 112 | + var numberPattern = new Regex("[0-9.,]+"); |
| 113 | + var match = numberPattern.Matches(input).FirstOrDefault(); |
| 114 | + if (match == null) |
| 115 | + return 0m; |
| 116 | + |
| 117 | + return decimal.Parse(match.Value, CultureInfo.InvariantCulture); |
| 118 | + } |
| 119 | + |
| 120 | + public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) |
| 121 | + { |
| 122 | + if (value is not decimal inputNumber) |
| 123 | + return null; |
| 124 | + return inputNumber.ToString(format, CultureInfo.InvariantCulture); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public void TextConverter_Is_Applied_Immediately() |
| 130 | + { |
| 131 | + RunTest((control, textbox) => |
| 132 | + { |
| 133 | + const decimal value = 10.11m; |
| 134 | + var initialConverter = new TestNumericUpDownValueConverter("C2"); |
| 135 | + var newConverter = new TestNumericUpDownValueConverter("P2"); |
| 136 | + |
| 137 | + // Establish and verify initial conditions. |
| 138 | + control.Value = value; |
| 139 | + control.TextConverter = initialConverter; |
| 140 | + var oldText = control.Text ?? string.Empty; |
| 141 | + Assert.Equal("¤10.11", oldText); |
| 142 | + |
| 143 | + // Check that NumberFormat is applied. |
| 144 | + control.TextConverter = newConverter; |
| 145 | + var newText = control.Text ?? string.Empty; |
| 146 | + Assert.Equal("1,011.00 %", newText); |
| 147 | + }); |
| 148 | + } |
102 | 149 |
|
103 | 150 | public static IEnumerable<object?[]> Increment_Decrement_TestData() |
104 | 151 | { |
|
0 commit comments