-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1953 from PrismLibrary/enum-autoinitialize
Adding Enum support from QueryStrings
- Loading branch information
Showing
7 changed files
with
230 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Prism.Tests.Common.Mocks; | ||
using Xunit; | ||
|
||
namespace Prism.Tests.Common | ||
{ | ||
public class ParametersFixture | ||
{ | ||
[Fact] | ||
public void TryGetValueOfT() | ||
{ | ||
var parameters = new MockParameters("mock=Foo&mock2=1"); | ||
bool success = false; | ||
MockEnum value = default; | ||
MockEnum value1 = default; | ||
|
||
var ex = Record.Exception(() => success = parameters.TryGetValue<MockEnum>("mock", out value)); | ||
var ex2 = Record.Exception(() => success = parameters.TryGetValue<MockEnum>("mock2", out value1)); | ||
Assert.Null(ex); | ||
Assert.True(success); | ||
Assert.Equal(MockEnum.Foo, value); | ||
Assert.Equal(value, value1); | ||
} | ||
|
||
[Fact] | ||
public void GetValuesOfT() | ||
{ | ||
var parameters = new MockParameters("mock=Foo&mock=2&mock=Fizz"); | ||
|
||
IEnumerable<MockEnum> values = default; | ||
|
||
var ex = Record.Exception(() => values = parameters.GetValues<MockEnum>("mock")); | ||
Assert.Null(ex); | ||
Assert.Equal(3, values.Count()); | ||
Assert.Equal(MockEnum.Foo, values.ElementAt(0)); | ||
Assert.Equal(MockEnum.Bar, values.ElementAt(1)); | ||
Assert.Equal(MockEnum.Fizz, values.ElementAt(2)); | ||
} | ||
|
||
|
||
[Fact] | ||
public void GetValue() | ||
{ | ||
var parameters = new MockParameters("mock=Foo&mock1=2&mock2=Fizz"); | ||
MockEnum value = default; | ||
MockEnum value1 = default; | ||
|
||
var ex = Record.Exception(() => value = parameters.GetValue<MockEnum>("mock")); | ||
var ex2 = Record.Exception(() => value1 = parameters.GetValue<MockEnum>("mock1")); | ||
Assert.Null(ex); | ||
Assert.Equal(MockEnum.Foo, value); | ||
Assert.Equal(MockEnum.Bar, value1); | ||
} | ||
} | ||
} |
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 | ||
} | ||
} |