-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve MultiTypeClassConverter to support null types and multi word …
…enum values
- Loading branch information
1 parent
466902c
commit 5c6507d
Showing
2 changed files
with
71 additions
and
0 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
56 changes: 56 additions & 0 deletions
56
src/UnitTests/JsonConverters/BackgroundTypeConverterTest.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,56 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
using NUnit.Framework; | ||
using NUnit.Framework.Constraints; | ||
using RxTelegram.Bot.Interface.ChatBackground; | ||
using RxTelegram.Bot.Utils.Converter; | ||
|
||
namespace RxTelegram.Bot.UnitTests.JsonConverters; | ||
|
||
[TestFixture] | ||
public class BackgroundTypeConverterTest | ||
{ | ||
private readonly JsonSerializerSettings _jsonSettings = new() | ||
{ | ||
Converters = { new MultiTypeClassConverter() } | ||
}; | ||
|
||
[Test] | ||
[TestCase("chat_theme", typeof(BackgroundTypeChatTheme))] | ||
[TestCase("fill", typeof(BackgroundTypeFill))] | ||
[TestCase("pattern", typeof(BackgroundTypePattern))] | ||
[TestCase("wallpaper", typeof(BackgroundTypeWallpaper))] | ||
public void CanRead(string type, Type expectedType) | ||
{ | ||
var json = $"{{\"type\":\"{type}\"}}"; | ||
var actual = JsonConvert.DeserializeObject<BackgroundType>(json, _jsonSettings); | ||
Assert.That(actual, Is.Not.Null); | ||
Assert.That(actual.GetType(), Is.EqualTo(expectedType)); | ||
} | ||
|
||
[Test] | ||
public void CanReadNull() | ||
{ | ||
const string json = "{}"; | ||
var actual = JsonConvert.DeserializeObject<BackgroundType>(json, _jsonSettings); | ||
Assert.That(actual, Is.Null); | ||
} | ||
|
||
[Test] | ||
[TestCase(typeof(BackgroundFillFreeformGradient), 0)] | ||
[TestCase(typeof(BackgroundFillGradient), 1)] | ||
[TestCase(typeof(BackgroundFillSolid), 2)] | ||
public void CanWrite(Type type, int expectedType) | ||
{ | ||
var instance = Activator.CreateInstance(type); | ||
var json = JsonConvert.SerializeObject(instance, _jsonSettings); | ||
Assert.That(json, new StartsWithConstraint($"{{\"Type\":{expectedType}")); | ||
} | ||
|
||
[Test] | ||
public void CanWriteNull() | ||
{ | ||
var json = JsonConvert.SerializeObject(null, _jsonSettings); | ||
Assert.That(json, Is.EqualTo("null")); | ||
} | ||
} |