Skip to content

Commit

Permalink
Add tests for converts
Browse files Browse the repository at this point in the history
  • Loading branch information
Stam authored and Stam committed Sep 8, 2023
1 parent e25ab05 commit 0a9cb43
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/SoulSplitter/UI/Converters/ColorToBrushConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace SoulSplitter.UI.Converters
{
internal class ColorToBrushConverter : IValueConverter
public class ColorToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// This file is part of the SoulSplitter distribution (https://github.com/FrankvdStam/SoulSplitter).
// Copyright (c) 2022 Frank van der Stam.
// https://github.com/FrankvdStam/SoulSplitter/blob/main/LICENSE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using SoulSplitter.UI.Converters;
using System;
using System.Windows;
using System.Windows.Data;

namespace SoulSplitter.Tests.UI.Converters
{
[TestClass]
public class BoolToVisibilityConverterTests
{
[TestMethod]
public void Convert_Happy_Flow()
{
var converter = new BoolToVisibilityConverter();
Assert.AreEqual(Visibility.Visible, converter.Convert(true));
Assert.AreEqual(Visibility.Collapsed, converter.Convert(false));
}

[TestMethod]
public void Convert_Exception()
{
var converter = new BoolToVisibilityConverter();
Assert.ThrowsException<NotSupportedException>(() => converter.Convert("test"));
}

[TestMethod]
public void ConvertBack_Exception()
{
var converter = new BoolToVisibilityConverter();
Assert.ThrowsException<NotSupportedException>(() => converter.ConvertBack(Visibility.Visible));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// This file is part of the SoulSplitter distribution (https://github.com/FrankvdStam/SoulSplitter).
// Copyright (c) 2022 Frank van der Stam.
// https://github.com/FrankvdStam/SoulSplitter/blob/main/LICENSE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using SoulSplitter.UI.Converters;
using System.Windows;
using System.Windows.Media;

namespace SoulSplitter.Tests.UI.Converters
{
[TestClass]
public class ColorToBrushConverterTests
{
[TestMethod]
public void Convert_Happy_Flow()
{
var converter = new ColorToBrushConverter();
var color = new System.Windows.Media.Color
{
A = 1,
R = 2,
G = 3,
B = 4,
};

var brush = converter.Convert(color);
Assert.IsInstanceOfType<SolidColorBrush>(brush);
Assert.AreEqual("#01020304", ((SolidColorBrush)brush).Color.ToString());
}

[TestMethod]
public void Convert_Exception()
{
var converter = new ColorToBrushConverter();
Assert.ThrowsException<NotSupportedException>(() => converter.Convert("test"));
}

[TestMethod]
public void ConvertBack_Exception()
{
var converter = new BoolToVisibilityConverter();
Assert.ThrowsException<NotSupportedException>(() => converter.ConvertBack(new SolidColorBrush()));
}
}
}
39 changes: 39 additions & 0 deletions tests/SoulSplitter.Tests/UI/Converters/ConverterExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This file is part of the SoulSplitter distribution (https://github.com/FrankvdStam/SoulSplitter).
// Copyright (c) 2022 Frank van der Stam.
// https://github.com/FrankvdStam/SoulSplitter/blob/main/LICENSE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System.Windows.Data;

namespace SoulSplitter.Tests.UI.Converters
{
internal static class ConverterExtensions
{
/// <summary>
/// Calls IValueConverter.Convert, fills in some standard values for all parameters
/// </summary>
public static object Convert(this IValueConverter converter, object value)
{
return converter.Convert(value, typeof(ConverterExtensions), null, Thread.CurrentThread.CurrentCulture);
}

/// <summary>
/// Calls IValueConverter.ConvertBack, fills in some standard values for all parameters
/// </summary>
public static object ConvertBack(this IValueConverter converter, object value)
{
return converter.ConvertBack(value, typeof(ConverterExtensions), null, Thread.CurrentThread.CurrentCulture);
}
}
}

0 comments on commit 0a9cb43

Please sign in to comment.