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

fix(feeds): Fix possible invalid generated code when using ValueType parameter #729

Merged
merged 1 commit into from
Sep 14, 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 @@ -42,17 +42,21 @@ public override string ToString()
{
if (ParameterType is not null && CanExecute is not null)
{
sb.AppendLine($@"
if (reactive_commandParameter is not {ParameterType} reactive_arguments
|| !({CanExecute("reactive_arguments")}))
sb.AppendLine($@"if (!(reactive_commandParameter is {ParameterType}))
{{
return false;
}}

var reactive_arguments = ({ParameterType}) reactive_commandParameter;
if (!({CanExecute("reactive_arguments")}))
{{
return false;
}}".Align(0));
sb.AppendLine();
}
else if (ParameterType is not null)
{
sb.AppendLine($@"if (reactive_commandParameter is not {ParameterType})
sb.AppendLine($@"if (!(reactive_commandParameter is {ParameterType}))
{{
return false;
}}".Align(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ string GetTypeOrTuple(IEnumerable<CommandParameter> parameters)
{
if (parameters.Count() is 1)
{
if (parameters.First().Symbol.NullableAnnotation is NullableAnnotation.NotAnnotated)
if (parameters.First().Symbol is { Type.IsValueType: false, NullableAnnotation: NullableAnnotation.NotAnnotated })
{
return args => $"{args} is not null";
}
Expand All @@ -177,7 +177,9 @@ string GetTypeOrTuple(IEnumerable<CommandParameter> parameters)
if (parameters.Any(p => p.Symbol.NullableAnnotation is NullableAnnotation.NotAnnotated))
{
return args => parameters
.Select((p, i) => p.Symbol.NullableAnnotation is NullableAnnotation.NotAnnotated ? $"{args}.Item{i} is not null" : null)
.Select((p, i) => p.Symbol is { Type.IsValueType: false, NullableAnnotation: NullableAnnotation.NotAnnotated }
? $"{args}.Item{i} is not null"
: null)
.Where(s => s is not null)
.JoinBy(" && ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,56 @@ public async Task When_OneParameter_Void()
vm.InvokeCount.Should().Be(1);
}

public partial class When_OneValueTypeParameter_Void_ViewModel
{
public int InvokeCount { get; set; }

public DateTimeOffset? LastInvokeParameter { get; set; }

public void MyMethod(DateTimeOffset parameter)
{
LastInvokeParameter = parameter;
InvokeCount++;
}
}

[TestMethod]
public async Task When_OneValueTypeParameter_Void()
{
var vm = new When_OneValueTypeParameter_Void_ViewModel.BindableWhen_OneValueTypeParameter_Void_ViewModel();

vm.MyMethod.Execute(new DateTimeOffset(1983, 9, 9, 15, 00, 00, TimeSpan.FromHours(1)));
await WaitFor(() => vm.InvokeCount == 1);

vm.LastInvokeParameter.Should().Be(new DateTimeOffset(1983, 9, 9, 15, 00, 00, TimeSpan.FromHours(1)));
vm.InvokeCount.Should().Be(1);
}

public partial class When_OneNullableValueTypeParameter_Void_ViewModel
{
public int InvokeCount { get; set; }

public DateTimeOffset? LastInvokeParameter { get; set; }

public void MyMethod(DateTimeOffset? parameter)
{
LastInvokeParameter = parameter;
InvokeCount++;
}
}

[TestMethod]
public async Task When_OneNullableValueTypeParameter_Void()
{
var vm = new When_OneNullableValueTypeParameter_Void_ViewModel.BindableWhen_OneNullableValueTypeParameter_Void_ViewModel();

vm.MyMethod.Execute(new DateTimeOffset(1983, 9, 9, 15, 00, 00, TimeSpan.FromHours(1)));
await WaitFor(() => vm.InvokeCount == 1);

vm.LastInvokeParameter.Should().Be(new DateTimeOffset(1983, 9, 9, 15, 00, 00, TimeSpan.FromHours(1)));
vm.InvokeCount.Should().Be(1);
}

public partial class When_OneParameterAndCT_Void_ViewModel
{
public int InvokeCount { get; set; }
Expand Down