using System.Globalization; using System.Windows; using System.Windows.Data; using System.Windows.Media; using CursorLang.Models; using CursorLang.Views; namespace CursorLang.Tests.Views; /// /// The binding converters: they decide what the settings window shows and what /// it keeps out of sight. /// public sealed class ConvertersTests { private static readonly CultureInfo Culture = CultureInfo.InvariantCulture; [Fact] public void A_match_with_the_single_listed_value_shows_the_element() { var converter = new EnumToVisibilityConverter(); Assert.Equal( Visibility.Visible, converter.Convert(PopupPlacementMode.AtCursor, typeof(Visibility), "AtCursor", Culture)); } // The anchor settings suit two placement modes at once [Fact] public void A_match_with_one_of_the_listed_values_shows_the_element() { var converter = new EnumToVisibilityConverter(); Assert.Equal( Visibility.Visible, converter.Convert(PopupPlacementMode.AtCaret, typeof(Visibility), "AtCursor, AtCaret", Culture)); } [Fact] public void No_match_hides_the_element() { var converter = new EnumToVisibilityConverter(); Assert.Equal( Visibility.Collapsed, converter.Convert(PopupPlacementMode.FixedPoint, typeof(Visibility), "AtCursor, AtCaret", Culture)); } [Theory] [InlineData(null, "AtCursor")] [InlineData(PopupPlacementMode.AtCursor, null)] [InlineData(PopupPlacementMode.AtCursor, "")] [InlineData(PopupPlacementMode.AtCursor, ",,")] [InlineData(null, null)] public void Without_a_value_or_without_a_list_the_element_is_hidden(object? value, string? parameter) { var converter = new EnumToVisibilityConverter(); Assert.Equal(Visibility.Collapsed, converter.Convert(value, typeof(Visibility), parameter, Culture)); } [Fact] public void Extra_spaces_in_the_list_do_not_get_in_the_way() { var converter = new EnumToVisibilityConverter(); Assert.Equal( Visibility.Visible, converter.Convert(ScreenPosition.Center, typeof(Visibility), " Top , Center ", Culture)); } [Fact] public void Visibility_does_not_convert_back() { var converter = new EnumToVisibilityConverter(); Assert.Equal( Binding.DoNothing, converter.ConvertBack(Visibility.Visible, typeof(PopupPlacementMode), "AtCursor", Culture)); } // The alpha is not shown: transparency is a setting of its own [Fact] public void A_colour_is_shown_with_six_digits() { var converter = new ColorToHexConverter(); Assert.Equal("#0A1B2C", converter.Convert(Color.FromRgb(0x0A, 0x1B, 0x2C), typeof(string), null, Culture)); } [Fact] public void A_semi_transparent_colour_is_shown_without_its_alpha() { var converter = new ColorToHexConverter(); Assert.Equal( "#102030", converter.Convert(Color.FromArgb(0x80, 0x10, 0x20, 0x30), typeof(string), null, Culture)); } [Theory] [InlineData(null)] [InlineData("not a colour")] [InlineData(42)] public void Anything_that_is_not_a_colour_shows_as_an_empty_string(object? value) { var converter = new ColorToHexConverter(); Assert.Equal(string.Empty, converter.Convert(value, typeof(string), null, Culture)); } [Fact] public void The_colour_notation_does_not_convert_back() { var converter = new ColorToHexConverter(); Assert.Equal(Binding.DoNothing, converter.ConvertBack("#102030", typeof(Color), null, Culture)); } [Fact] public void A_colour_turns_into_a_brush() { var converter = new ColorToBrushConverter(); Color color = Color.FromRgb(0x10, 0x20, 0x30); var brush = Assert.IsType(converter.Convert(color, typeof(Brush), null, Culture)); Assert.Equal(color, brush.Color); } [Theory] [InlineData(null)] [InlineData("not a colour")] public void Anything_that_is_not_a_colour_turns_into_a_transparent_brush(object? value) { var converter = new ColorToBrushConverter(); Assert.Same(Brushes.Transparent, converter.Convert(value, typeof(Brush), null, Culture)); } // Picking a swatch in the list sends the colour back into the settings [Fact] public void A_brush_converts_back_into_a_colour() { var converter = new ColorToBrushConverter(); Color color = Color.FromRgb(0x10, 0x20, 0x30); Assert.Equal(color, converter.ConvertBack(new SolidColorBrush(color), typeof(Color), null, Culture)); } [Theory] [InlineData(null)] [InlineData("not a brush")] public void Anything_that_is_not_a_brush_does_not_convert_back(object? value) { var converter = new ColorToBrushConverter(); Assert.Equal(Binding.DoNothing, converter.ConvertBack(value, typeof(Color), null, Culture)); } [Fact] public void The_converters_are_fit_for_bindings() { Assert.IsAssignableFrom(new EnumToVisibilityConverter()); Assert.IsAssignableFrom(new ColorToHexConverter()); Assert.IsAssignableFrom(new ColorToBrushConverter()); } }