Skip to content

Commit

Permalink
Add NumberToColorConverter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Coding-Enthusiast committed Feb 12, 2025
1 parent 7575987 commit e5a8366
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
47 changes: 47 additions & 0 deletions Tests/MVVM/Converters/NumberToColorConverterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// WatchOnlyBitcoinWallet Tests
// Copyright (c) 2016 Coding Enthusiast
// Distributed under the MIT software license, see the accompanying
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.

using Avalonia.Media;
using System.Globalization;
using WatchOnlyBitcoinWallet.MVVM.Converters;

namespace Tests.MVVM.Converters
{
public class NumberToColorConverterTests
{
public static TheoryData<decimal, IImmutableSolidColorBrush> GetConvertCases()
{
TheoryData<decimal, IImmutableSolidColorBrush> result = new()
{
{ 0, Brushes.Transparent },
{ 0.01m, Brushes.LightGreen },
{ -0.002m, Brushes.Orange },
};

return result;
}
[Theory]
[MemberData(nameof(GetConvertCases))]
public void ConvertTest(decimal value, IImmutableSolidColorBrush expected)
{
NumberToColorConverter converter = new();
object actual = converter.Convert(value, typeof(decimal), null, CultureInfo.InvariantCulture);
Assert.Equal(expected, actual);
}

[Fact]
public void ExceptionTests()
{
NumberToColorConverter converter = new();
Type t = typeof(decimal);
CultureInfo ci = CultureInfo.InvariantCulture;

Assert.Throws<NotImplementedException>(() => converter.Convert(null, t, null, ci));
Assert.Throws<NotImplementedException>(() => converter.Convert("foo", t, null, ci));
Assert.Throws<NotImplementedException>(() => converter.Convert(123, t, null, ci));
Assert.Throws<NotImplementedException>(() => converter.ConvertBack(null, t, null, ci));
}
}
}
33 changes: 17 additions & 16 deletions WatchOnlyBitcoinWallet/MVVM/Converters/NumberToColorConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,31 @@ namespace WatchOnlyBitcoinWallet.MVVM.Converters
{
/// <summary>
/// Converts a decimal number to color.
/// </para> positive : green
/// </para> negative : red
/// </para> zero : null
/// <para/> positive : green
/// <para/> negative : red
/// <para/> zero : transparent
/// </summary>
public class NumberToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
public object Convert(object? value, Type targetType, object? parameter, System.Globalization.CultureInfo culture)
{
decimal number = (decimal)value;
if (number == 0)
if (value is decimal d)
{
return null;
}
else if (number >= 0)
{
return Brushes.LightGreen;
}
else
{
return Brushes.Orange;
switch (d)
{
case 0:
return Brushes.Transparent;
case > 0:
return Brushes.LightGreen;
case < 0:
return Brushes.Orange;
}
}

throw new NotImplementedException();
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
public object ConvertBack(object? value, Type targetType, object? parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
Expand Down

0 comments on commit e5a8366

Please sign in to comment.