Skip to content

Commit

Permalink
Implement NUnit2045
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCodeTraveler committed Nov 27, 2023
1 parent d58fbc0 commit 2c4a27e
Show file tree
Hide file tree
Showing 13 changed files with 592 additions and 299 deletions.
5 changes: 3 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
CS1598: XML parser could not be loaded. The XML documentation file will not be generated.
CS1658: Identifier expected; 'true' is a keyword
CS1734: XML comment has a paramref tag, but there is no parameter by that name
NUnit2007: Warning NUnit2007 : The actual value should not be a constant - perhaps the actual value and the expected value have switched places -->
<WarningsAsErrors>nullable,CS0419,CS1570,CS1571,CS1572,CS1573,CS1574,CS1580,CS1581,CS1584,CS1589,CS1590,CS1592,CS1598,CS1658,CS1734,NUnit2007</WarningsAsErrors>
NUnit2007: Warning NUnit2007 : The actual value should not be a constant - perhaps the actual value and the expected value have switched places
NUnit2045: Hosting Asserts inside an Assert.Multiple allows detecting more than one failure-->
<WarningsAsErrors>nullable,CS0419,CS1570,CS1571,CS1572,CS1573,CS1574,CS1580,CS1581,CS1584,CS1589,CS1590,CS1592,CS1598,CS1658,CS1734,NUnit2007,NUnit2045</WarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,42 +690,45 @@ public void BindCommandWithPositionalParameters()
[Test]
public void SupportDerivedElements()
{
Assert.That(new DerivedFromLabel()
.Bind(nameof(viewModel.Text))
.Bind(
nameof(viewModel.Text),
convert: (string? text) => $"'{text}'")
.Bind(
nameof(viewModel.Text),
convert: (string? text, int? repeat) =>
{
ArgumentNullException.ThrowIfNull(text);
ArgumentNullException.ThrowIfNull(repeat);

return string.Concat(Enumerable.Repeat($"'{text.Trim('\'')}'", repeat.Value));
})
.Bind(
DerivedFromLabel.TextColorProperty,
nameof(viewModel.TextColor))
.Bind(
DerivedFromLabel.BackgroundColorProperty,
nameof(viewModel.IsRed),
convert: (bool? isRed) => isRed.HasValue && isRed.Value ? Colors.Black : Colors.Transparent)
.Bind(
Label.TextColorProperty,
nameof(viewModel.IsRed),
convert: (bool? isRed, float? alpha) =>
{
ArgumentNullException.ThrowIfNull(alpha);

return (isRed.HasValue && isRed.Value ? Colors.Red : Colors.Green).MultiplyAlpha(alpha.Value);
})
.Invoke(l => l.Text = nameof(SupportDerivedElements))
.Assign(out DerivedFromLabel assignDerivedFromLabel),
Is.InstanceOf<DerivedFromLabel>());

Assert.That(new DerivedFromTextCell().BindCommand(nameof(viewModel.Command)), Is.InstanceOf<DerivedFromTextCell>());
Assert.That(assignDerivedFromLabel, Is.InstanceOf<DerivedFromLabel>());
Assert.Multiple(() =>
{
Assert.That(new DerivedFromLabel()
.Bind(nameof(viewModel.Text))
.Bind(
nameof(viewModel.Text),
convert: (string? text) => $"'{text}'")
.Bind(
nameof(viewModel.Text),
convert: (string? text, int? repeat) =>
{
ArgumentNullException.ThrowIfNull(text);
ArgumentNullException.ThrowIfNull(repeat);

return string.Concat(Enumerable.Repeat($"'{text.Trim('\'')}'", repeat.Value));
})
.Bind(
DerivedFromLabel.TextColorProperty,
nameof(viewModel.TextColor))
.Bind(
DerivedFromLabel.BackgroundColorProperty,
nameof(viewModel.IsRed),
convert: (bool? isRed) => isRed.HasValue && isRed.Value ? Colors.Black : Colors.Transparent)
.Bind(
Label.TextColorProperty,
nameof(viewModel.IsRed),
convert: (bool? isRed, float? alpha) =>
{
ArgumentNullException.ThrowIfNull(alpha);

return (isRed.HasValue && isRed.Value ? Colors.Red : Colors.Green).MultiplyAlpha(alpha.Value);
})
.Invoke(l => l.Text = nameof(SupportDerivedElements))
.Assign(out DerivedFromLabel assignDerivedFromLabel),
Is.InstanceOf<DerivedFromLabel>());

Assert.That(new DerivedFromTextCell().BindCommand(nameof(viewModel.Command)), Is.InstanceOf<DerivedFromTextCell>());
Assert.That(assignDerivedFromLabel, Is.InstanceOf<DerivedFromLabel>());
});
}

[TestCase(AppTheme.Light)]
Expand Down
70 changes: 45 additions & 25 deletions src/CommunityToolkit.Maui.Markup.UnitTests/BindingHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,21 @@ internal static void AssertTypedBindingExists<TBindable, TBindingContext, TSourc
TParam? expectedConverterParameter = default) where TBindable : BindableObject
{
var binding = GetTypedBinding(bindable, targetProperty) ?? throw new NullReferenceException();
Assert.That(binding, Is.Not.Null);

Assert.That(binding.Mode, Is.EqualTo(expectedBindingMode));
Assert.Multiple(() =>
{
Assert.That(binding, Is.Not.Null);
Assert.That(binding.Mode, Is.EqualTo(expectedBindingMode));

Assert.That(binding.Converter?.ToString(), Is.EqualTo(expectedConverter?.ToString()));
Assert.That(binding.Converter?.ToString(), Is.EqualTo(expectedConverter?.ToString()));

Assert.That(binding.ConverterParameter, Is.EqualTo(expectedConverterParameter));
Assert.That(binding.ConverterParameter, Is.EqualTo(expectedConverterParameter));

Assert.That(expectedSource, Is.InstanceOf<TBindingContext>());
Assert.That(binding.StringFormat, Is.EqualTo(expectedStringFormat));
Assert.That(binding.TargetNullValue, Is.EqualTo(expectedTargetNullValue));
Assert.That(binding.FallbackValue, Is.EqualTo(expectedFallbackValue));
Assert.That(expectedSource, Is.InstanceOf<TBindingContext>());
Assert.That(binding.StringFormat, Is.EqualTo(expectedStringFormat));
Assert.That(binding.TargetNullValue, Is.EqualTo(expectedTargetNullValue));
Assert.That(binding.FallbackValue, Is.EqualTo(expectedFallbackValue));
});
}

internal static void AssertBindingExists<TDest, TParam>(
Expand All @@ -112,9 +115,13 @@ internal static void AssertBindingExists<TDest, TParam>(
Action<IValueConverter>? assertConvert = null)
{
var binding = GetBinding(bindable, targetProperty) ?? throw new NullReferenceException();
Assert.That(binding, Is.Not.Null);
Assert.That(binding.Path, Is.EqualTo(path));
Assert.That(binding.Mode, Is.EqualTo(mode));

Assert.Multiple(() =>
{
Assert.That(binding, Is.Not.Null);
Assert.That(binding.Path, Is.EqualTo(path));
Assert.That(binding.Mode, Is.EqualTo(mode));
});

if (assertConverterInstanceIsAnyNotNull)
{
Expand All @@ -125,11 +132,14 @@ internal static void AssertBindingExists<TDest, TParam>(
Assert.That(binding.Converter, Is.EqualTo(converter));
}

Assert.That(binding.ConverterParameter, Is.EqualTo(converterParameter));
Assert.That(binding.StringFormat, Is.EqualTo(stringFormat));
Assert.That(binding.Source, Is.EqualTo(source));
Assert.That(binding.TargetNullValue, Is.EqualTo(targetNullValue));
Assert.That(binding.FallbackValue, Is.EqualTo(fallbackValue));
Assert.Multiple(() =>
{
Assert.That(binding.ConverterParameter, Is.EqualTo(converterParameter));
Assert.That(binding.StringFormat, Is.EqualTo(stringFormat));
Assert.That(binding.Source, Is.EqualTo(source));
Assert.That(binding.TargetNullValue, Is.EqualTo(targetNullValue));
Assert.That(binding.FallbackValue, Is.EqualTo(fallbackValue));
});

assertConvert?.Invoke(binding.Converter);
}
Expand Down Expand Up @@ -163,9 +173,13 @@ internal static void AssertBindingExists<TDest, TParam>(
Action<IMultiValueConverter>? assertConvert = null)
{
var binding = GetMultiBinding(bindable, targetProperty) ?? throw new NullReferenceException();
Assert.That(binding, Is.Not.Null);
Assert.That(binding.Bindings.SequenceEqual(bindings), Is.True);
Assert.That(binding.Mode, Is.EqualTo(mode));

Assert.Multiple(() =>
{
Assert.That(binding, Is.Not.Null);
Assert.That(binding.Bindings.SequenceEqual(bindings), Is.True);
Assert.That(binding.Mode, Is.EqualTo(mode));
});

if (assertConverterInstanceIsAnyNotNull)
{
Expand All @@ -176,10 +190,13 @@ internal static void AssertBindingExists<TDest, TParam>(
Assert.That(binding.Converter, Is.EqualTo(converter));
}

Assert.That(binding.ConverterParameter, Is.EqualTo(converterParameter));
Assert.That(binding.StringFormat, Is.EqualTo(stringFormat));
Assert.That(binding.TargetNullValue, Is.EqualTo(targetNullValue));
Assert.That(binding.FallbackValue, Is.EqualTo(fallbackValue));
Assert.Multiple(() =>
{
Assert.That(binding.ConverterParameter, Is.EqualTo(converterParameter));
Assert.That(binding.StringFormat, Is.EqualTo(stringFormat));
Assert.That(binding.TargetNullValue, Is.EqualTo(targetNullValue));
Assert.That(binding.FallbackValue, Is.EqualTo(fallbackValue));
});

assertConvert?.Invoke(binding.Converter);
}
Expand Down Expand Up @@ -217,8 +234,11 @@ internal static void AssertBindingExists<TDest, TParam>(

internal static IValueConverter AssertConvert<TValue, TConvertedValue>(this IValueConverter converter, TValue value, object? parameter, TConvertedValue expectedConvertedValue, bool twoWay = false, bool backOnly = false, CultureInfo? culture = null)
{
Assert.That(converter.Convert(value, typeof(object), parameter, culture ?? CultureInfo.InvariantCulture), Is.EqualTo(backOnly ? default : expectedConvertedValue));
Assert.That(converter.ConvertBack(expectedConvertedValue, typeof(object), parameter, culture ?? CultureInfo.InvariantCulture), Is.EqualTo(twoWay || backOnly ? value : default(TValue)));
Assert.Multiple(() =>
{
Assert.That(converter.Convert(value, typeof(object), parameter, culture ?? CultureInfo.InvariantCulture), Is.EqualTo(backOnly ? default : expectedConvertedValue));
Assert.That(converter.ConvertBack(expectedConvertedValue, typeof(object), parameter, culture ?? CultureInfo.InvariantCulture), Is.EqualTo(twoWay || backOnly ? value : default(TValue)));
});
return converter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ static Label AssertDynamicResources()
{
var label = new Label { Resources = new ResourceDictionary { { "TextKey", "TextValue" }, { "ColorKey", Colors.Green } } };

Assert.That(label.Text, Is.EqualTo(Label.TextProperty.DefaultValue));
Assert.That(label.TextColor, Is.EqualTo(Label.TextColorProperty.DefaultValue));
Assert.Multiple(() =>
{
Assert.That(label.Text, Is.EqualTo(Label.TextProperty.DefaultValue));
Assert.That(label.TextColor, Is.EqualTo(Label.TextColorProperty.DefaultValue));
});

label.DynamicResources((Label.TextProperty, "TextKey"),
(Label.TextColorProperty, "ColorKey"));

Assert.That(label.Text, Is.EqualTo("TextValue"));
Assert.That(label.TextColor, Is.EqualTo(Colors.Green));
Assert.Multiple(() =>
{
Assert.That(label.Text, Is.EqualTo("TextValue"));
Assert.That(label.TextColor, Is.EqualTo(Colors.Green));
});

return label;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ public void RemoveDynamicResources()
label.Resources["TextKey"] = "ChangedTextValue";
label.Resources["ColorKey"] = Colors.Green;

Assert.That(label.Text, Is.EqualTo("TextValue"));
Assert.That(label.TextColor, Is.EqualTo(Colors.Green));
Assert.Multiple(() =>
{
Assert.That(label.Text, Is.EqualTo("TextValue"));
Assert.That(label.TextColor, Is.EqualTo(Colors.Green));
});
}

[Test]
Expand All @@ -41,9 +44,12 @@ public void EffectsMultiple()
NullEffect effect1 = new NullEffect(), effect2 = new NullEffect();
Bindable.Effects(effect1, effect2);

Assert.That(Bindable.Effects.Count, Is.EqualTo(2));
Assert.That(Bindable.Effects.Contains(effect1));
Assert.That(Bindable.Effects.Contains(effect2));
Assert.Multiple(() =>
{
Assert.That(Bindable.Effects.Count, Is.EqualTo(2));
Assert.That(Bindable.Effects.Contains(effect1));
Assert.That(Bindable.Effects.Contains(effect2));
});
}

[Test]
Expand Down Expand Up @@ -86,14 +92,20 @@ static Label AssertDynamicResources()
{
var label = new Label { Resources = new ResourceDictionary { { "TextKey", "TextValue" }, { "ColorKey", Colors.Green } } };

Assert.That(label.Text, Is.EqualTo(Label.TextProperty.DefaultValue));
Assert.That(label.TextColor, Is.EqualTo(Label.TextColorProperty.DefaultValue));
Assert.Multiple(() =>
{
Assert.That(label.Text, Is.EqualTo(Label.TextProperty.DefaultValue));
Assert.That(label.TextColor, Is.EqualTo(Label.TextColorProperty.DefaultValue));
});

label.DynamicResources((Label.TextProperty, "TextKey"),
(Label.TextColorProperty, "ColorKey"));

Assert.That(label.Text, Is.EqualTo("TextValue"));
Assert.That(label.TextColor, Is.EqualTo(Colors.Green));
Assert.Multiple(() =>
{
Assert.That(label.Text, Is.EqualTo("TextValue"));
Assert.That(label.TextColor, Is.EqualTo(Colors.Green));
});

return label;
}
Expand Down
Loading

0 comments on commit 2c4a27e

Please sign in to comment.