This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 634
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Normalize behavior of parsing nullable booleans
- Loading branch information
Showing
3 changed files
with
94 additions
and
17 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using NUnit.Framework; | ||
|
||
namespace ServiceStack.Text.Tests.Issues | ||
{ | ||
public class NullableIssues | ||
{ | ||
public class NBoolTest | ||
{ | ||
public bool? IsOk {get; set;} | ||
|
||
protected bool Equals(NBoolTest other) => IsOk == other.IsOk; | ||
public override bool Equals(object obj) | ||
{ | ||
if (ReferenceEquals(null, obj)) return false; | ||
if (ReferenceEquals(this, obj)) return true; | ||
if (obj.GetType() != this.GetType()) return false; | ||
return Equals((NBoolTest) obj); | ||
} | ||
public override int GetHashCode() => IsOk.GetHashCode(); | ||
} | ||
|
||
[Test] | ||
public void Does_deserialize_nullable_bools() | ||
{ | ||
Assert.That("{\"IsOk\": true}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = true })); | ||
Assert.That("{\"IsOk\": false}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = false })); | ||
Assert.That("{\"IsOk\": \"true\"}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = true })); | ||
Assert.That("{\"IsOk\": \"false\"}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = false })); | ||
Assert.That("{\"IsOk\": \"True\"}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = true })); | ||
Assert.That("{\"IsOk\": \"False\"}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = false })); | ||
|
||
Assert.That("{\"IsOk\": null}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = null })); | ||
} | ||
|
||
[Test] | ||
public void Does_deserialize_nullable_bools_conventions() | ||
{ | ||
Assert.That("{\"IsOk\": \"t\"}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = true })); | ||
Assert.That("{\"IsOk\": \"f\"}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = false })); | ||
Assert.That("{\"IsOk\": \"Y\"}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = true })); | ||
Assert.That("{\"IsOk\": \"N\"}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = false })); | ||
Assert.That("{\"IsOk\": \"on\"}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = true })); | ||
Assert.That("{\"IsOk\": \"off\"}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = false })); | ||
} | ||
|
||
[Test] | ||
public void Deserialize_nullable_bools_results_in_error() | ||
{ | ||
Assert.That("{\"IsOk\": \"tt\"}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = null })); | ||
Assert.That("{\"IsOk\": \"fu\"}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = null })); | ||
Assert.That("{\"IsOk\": \"eee\"}".FromJson<NBoolTest>(), Is.EqualTo(new NBoolTest { IsOk = null })); | ||
} | ||
} | ||
} |