Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove BindableObject Constraint from .Assign() and .Invoke() #131

Merged
merged 4 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ public void BindDefaultPropertyWithInlineTwoWayParameterizedConvertAndDefaults()
ArgumentNullException.ThrowIfNull(text);
ArgumentNullException.ThrowIfNull(repeat);

return text.Substring(0, text.Length / repeat.Value).Trim('\'');
return text[..(text.Length / repeat.Value)].Trim('\'');
brminnick marked this conversation as resolved.
Show resolved Hide resolved
},
2
);
Expand Down Expand Up @@ -692,20 +692,6 @@ public void BindCommandWithPositionalParameters()
BindingHelpers.AssertBindingExists(textCell, TextCell.CommandParameterProperty, parameterPath, source: parameterSource);
}

[Test]
public void Assign()
{
var createdLabel = new Label().Assign(out Label assignLabel);
Assert.That(ReferenceEquals(createdLabel, assignLabel));
}

[Test]
public void Invoke()
{
var createdLabel = new Label().Invoke(null).Invoke(l => l.Text = nameof(Invoke));
Assert.That(createdLabel.Text, Is.EqualTo(nameof(Invoke)));
}

[Test]
public void SupportDerivedElements()
{
Expand Down Expand Up @@ -744,6 +730,7 @@ public void SupportDerivedElements()
.Assign(out DerivedFromLabel assignDerivedFromLabel));

Assert.IsInstanceOf<DerivedFromTextCell>(new DerivedFromTextCell().BindCommand(nameof(viewModel.Command)));
Assert.IsInstanceOf<DerivedFromLabel>(assignDerivedFromLabel);
}

[TestCase(AppTheme.Light)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using CommunityToolkit.Maui.Markup.UnitTests.Base;
using Microsoft.Maui.Controls;
using NUnit.Framework;

namespace CommunityToolkit.Maui.Markup.UnitTests;

[TestFixture]
class ObjectExtensionsTests : BaseMarkupTestFixture
{
[Test]
public void AssignLabel()
{
var createdLabel = new Label().Assign(out Label assignedLabel);
Assert.That(ReferenceEquals(createdLabel, assignedLabel));
}

[Test]
public void AssignString()
{
string? testString = null;
const string text = "Hello World";

var createdString = text.Invoke(_ => testString = text).Assign(out string assignedString);

Assert.NotNull(testString);
Assert.AreEqual(text, testString);
Assert.AreEqual(text, assignedString);
Assert.AreEqual(text, createdString);
Assert.That(ReferenceEquals(createdString, assignedString));
}

[Test]
public void Invoke()
{
const string text = nameof(Invoke);

var createdLabel = new Label().Invoke(l => l.Text = text);
Assert.That(createdLabel.Text, Is.EqualTo(text));
}

[Test]
public void InvokeNullThrowsArgumentNullException()
{
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.Throws<ArgumentNullException>(() => new Label().Invoke(null));
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
}
}
32 changes: 4 additions & 28 deletions src/CommunityToolkit.Maui.Markup/BindableObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static TBindable Bind<TBindable>(
TargetNullValue = targetNullValue,
FallbackValue = fallbackValue
});

return bindable;
}

Expand All @@ -51,6 +52,7 @@ public static TBindable Bind<TBindable, TSource, TDest>(
TargetNullValue = targetNullValue,
FallbackValue = fallbackValue
});

return bindable;
}

Expand All @@ -76,6 +78,7 @@ public static TBindable Bind<TBindable, TSource, TParam, TDest>(
TargetNullValue = targetNullValue,
FallbackValue = fallbackValue
});

return bindable;
}

Expand Down Expand Up @@ -111,6 +114,7 @@ public static TBindable Bind<TBindable, TSource, TDest>(
TDest? fallbackValue = default) where TBindable : BindableObject
{
var converter = new FuncConverter<TSource, TDest, object>(convert, convertBack);

bindable.Bind(
DefaultBindableProperties.GetDefaultProperty<TBindable>(),
path, mode, converter, null, stringFormat, source, targetNullValue, fallbackValue);
Expand Down Expand Up @@ -164,34 +168,6 @@ public static TBindable BindCommand<TBindable>(
return bindable;
}

/// <summary>
/// Assign TBindable to a variable
/// </summary>
/// <typeparam name="TBindable"></typeparam>
/// <typeparam name="TVariable"></typeparam>
/// <param name="bindable"></param>
/// <param name="variable"></param>
/// <returns>TBindable</returns>
public static TBindable Assign<TBindable, TVariable>(this TBindable bindable, out TVariable variable)
where TBindable : BindableObject, TVariable
{
variable = bindable;
return bindable;
}

/// <summary>
/// Perform an action on a Bindable Object
/// </summary>
/// <typeparam name="TBindable"></typeparam>
/// <param name="bindable"></param>
/// <param name="action"></param>
/// <returns>TBindable</returns>
public static TBindable Invoke<TBindable>(this TBindable bindable, Action<TBindable>? action) where TBindable : BindableObject
{
action?.Invoke(bindable);
return bindable;
}

/// <summary>
/// Sets the property values for <paramref name="light"/> and <paramref name="dark"/> themes respectively.
/// </summary>
Expand Down
37 changes: 37 additions & 0 deletions src/CommunityToolkit.Maui.Markup/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace CommunityToolkit.Maui.Markup;

/// <summary>
/// Extension Methods for Unconstrained Objects
/// </summary>
public static class ObjectExtensions
{
/// <summary>
/// Assign <typeparam name="TAssignable"/> to a variable
/// </summary>
/// <typeparam name="TAssignable"></typeparam>
/// <typeparam name="TVariable"></typeparam>
/// <param name="assignable"></param>
/// <param name="variable"></param>
/// <returns>TBindable</returns>
public static TAssignable Assign<TAssignable, TVariable>(this TAssignable assignable, out TVariable variable)
where TAssignable : TVariable
{
variable = assignable;
return assignable;
}

/// <summary>
/// Perform an action on <typeparam name="TAssignable"/>
/// </summary>
/// <typeparam name="TAssignable"></typeparam>
/// <param name="assignable"></param>
/// <param name="action"></param>
/// <returns>TBindable</returns>
public static TAssignable Invoke<TAssignable>(this TAssignable assignable, Action<TAssignable> action)
{
ArgumentNullException.ThrowIfNull(action);

action(assignable);
return assignable;
}
}