Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing Enum Support to allow setting from numeric or string
- Loading branch information
Showing
7 changed files
with
176 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Prism.Tests.Common.Mocks | ||
{ | ||
internal enum MockEnum | ||
{ | ||
None = 0, | ||
Foo = 1, | ||
Bar = 2, | ||
Fizz = 3, | ||
SomethingElse = 99 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Prism.Common; | ||
|
||
namespace Prism.Tests.Common.Mocks | ||
{ | ||
internal class MockParameters : ParametersBase | ||
{ | ||
public MockParameters() : base() { } | ||
public MockParameters(string query) : base(query) { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
Source/Xamarin/Prism.Forms.Tests/Mvvm/AutoInitializeFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Prism.Common; | ||
using Prism.Forms.Tests.Mvvm.Mocks.ViewModels; | ||
using Prism.Navigation; | ||
using Xunit; | ||
|
||
namespace Prism.Forms.Tests.Mvvm | ||
{ | ||
public class AutoInitializeFixture | ||
{ | ||
[Fact] | ||
public void ThrowsAnExceptionWithoutRequiredParameter() | ||
{ | ||
var vm = new AutoInitializedViewModelMock(); | ||
var parameters = new NavigationParameters("?foo=bar"); | ||
var ex = Record.Exception(() => PageUtilities.Abracadabra(vm, parameters)); | ||
|
||
Assert.NotNull(ex); | ||
Assert.IsType<ArgumentNullException>(ex); | ||
} | ||
|
||
[Fact] | ||
public void NoExceptionWhenTitleIsProvided() | ||
{ | ||
var vm = new AutoInitializedViewModelMock(); | ||
var parameters = new NavigationParameters("?success=true&title=Hello"); | ||
var ex = Record.Exception(() => PageUtilities.Abracadabra(vm, parameters)); | ||
|
||
Assert.Null(ex); | ||
Assert.Equal("Hello", vm.Title); | ||
} | ||
|
||
[Fact] | ||
public void SetsBooleanFromQueryString() | ||
{ | ||
var vm = new AutoInitializedViewModelMock(); | ||
var parameters = new NavigationParameters("?success=true&title=Hello"); | ||
var ex = Record.Exception(() => PageUtilities.Abracadabra(vm, parameters)); | ||
|
||
Assert.Null(ex); | ||
Assert.True(vm.Success); | ||
} | ||
|
||
[Fact] | ||
public void SetsDateTimeFromQueryString() | ||
{ | ||
var vm = new AutoInitializedViewModelMock(); | ||
var parameters = new NavigationParameters("?someDate=July 11, 2019 08:00&title=Hello"); | ||
var ex = Record.Exception(() => PageUtilities.Abracadabra(vm, parameters)); | ||
|
||
Assert.Null(ex); | ||
var expected = new DateTime(2019, 7, 11, 8, 0, 0); | ||
Assert.Equal(expected, vm.SomeDate); | ||
} | ||
|
||
[Theory] | ||
[InlineData("status=OK", MockHttpStatus.OK)] | ||
[InlineData("status=201", MockHttpStatus.Created)] | ||
[InlineData("status=500", (MockHttpStatus)500)] | ||
public void SetsEnumFromQueryString(string queryString, MockHttpStatus status) | ||
{ | ||
var vm = new AutoInitializedViewModelMock(); | ||
var parameters = new NavigationParameters($"?{queryString}&title=Hello"); | ||
var ex = Record.Exception(() => PageUtilities.Abracadabra(vm, parameters)); | ||
|
||
Assert.Null(ex); | ||
Assert.Equal(status, vm.Status); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Source/Xamarin/Prism.Forms.Tests/Mvvm/Mocks/ViewModels/AutoInitializedViewModelMock.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using Prism.AppModel; | ||
|
||
namespace Prism.Forms.Tests.Mvvm.Mocks.ViewModels | ||
{ | ||
public class AutoInitializedViewModelMock : IAutoInitialize | ||
{ | ||
[AutoInitialize(true)] | ||
public string Title { get; set; } | ||
|
||
public bool Success { get; set; } | ||
|
||
public DateTime SomeDate { get; set; } | ||
|
||
public MockHttpStatus Status { get; set; } | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
Source/Xamarin/Prism.Forms.Tests/Mvvm/Mocks/ViewModels/MockHttpStatus.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Prism.Forms.Tests.Mvvm.Mocks.ViewModels | ||
{ | ||
public enum MockHttpStatus | ||
{ | ||
Continue = 100, | ||
OK = 200, | ||
Created = 201, | ||
MultipleChoices = 300 | ||
} | ||
} |
053eb30
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dansiegel how is the conversion supposed to work from string/int values for enums. I don't no if I'm using the wrong method or it shouldn't do what I expect but this seems to error for me:
var parameters = new NavigationParameters() { { "MyEnum", AlertReasonType.Clinical.ToString() } }; parameters.TryGetValue("MyEnum", out AlertReasonType reasonType);