-
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.
support Trello's named bottom position
- Loading branch information
Showing
6 changed files
with
167 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
|
||
namespace GoldenSyrupGames.T2MD | ||
{ | ||
/// <summary> | ||
/// Trello sometimes has e.g. pos as "bottom" instead of 123.45 or "123.45", so handle that. | ||
/// </summary> | ||
public class TrelloDoubleJsonConverter : JsonConverter<Double> | ||
{ | ||
public override Double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == JsonTokenType.String) | ||
{ | ||
string? stringValue = reader.GetString(); | ||
// handle the weird values | ||
if (stringValue == "bottom") | ||
{ | ||
return double.MaxValue - 1; | ||
} | ||
else if (stringValue == "top") | ||
{ | ||
return 0.0; | ||
} | ||
// standard double as string | ||
else if (double.TryParse(stringValue, out double value)) | ||
{ | ||
return value; | ||
} | ||
else | ||
{ | ||
throw new System.Text.Json.JsonException($"Couldn't parse the string `{stringValue}` as a double or as one of Trello's positions"); | ||
} | ||
} | ||
// double without string | ||
else if (reader.TokenType == JsonTokenType.Number) | ||
{ | ||
return reader.GetDouble(); | ||
} | ||
// else | ||
throw new System.Text.Json.JsonException(); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, Double doubleValue, JsonSerializerOptions options) | ||
{ | ||
writer.WriteNumberValue(doubleValue); | ||
} | ||
} | ||
} |
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,70 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json; | ||
|
||
namespace GoldenSyrupGames.T2MD.Tests | ||
{ | ||
[TestClass] | ||
public class TrelloDoubleJsonConverterTests | ||
{ | ||
private JsonSerializerOptions _jsonDeserializeOptions = new JsonSerializerOptions | ||
{ | ||
PropertyNameCaseInsensitive = true, | ||
NumberHandling = JsonNumberHandling.AllowReadingFromString, | ||
Converters = | ||
{ | ||
new TrelloDoubleJsonConverter() | ||
} | ||
}; | ||
|
||
// runs before each test | ||
[TestInitialize] | ||
public void Initialize() | ||
{ | ||
} | ||
|
||
// ensure the default number handling still works (double) | ||
[TestMethod] | ||
public void Read_JsonDouble_ReturnsDouble() | ||
{ | ||
double number = 65535.0; | ||
var trelloCardJson = $"{{\"pos\": {number}}}"; | ||
var trelloCard = JsonSerializer.Deserialize<TrelloCardModel>(trelloCardJson, _jsonDeserializeOptions); | ||
|
||
Assert.AreEqual(number, trelloCard.Pos); | ||
} | ||
|
||
// ensure the default number handling still works (int) | ||
[TestMethod] | ||
public void Read_JsonInt_ReturnsDouble() | ||
{ | ||
int number = 65535; | ||
var trelloCardJson = $"{{\"pos\": {number}}}"; | ||
var trelloCard = JsonSerializer.Deserialize<TrelloCardModel>(trelloCardJson, _jsonDeserializeOptions); | ||
|
||
Assert.AreEqual(number, trelloCard.Pos); | ||
} | ||
|
||
// ensure converting from string works | ||
[TestMethod] | ||
public void Read_JsonNumberInString_ReturnsDouble() | ||
{ | ||
double number = 65535.0; | ||
var trelloCardJson = $"{{\"pos\": \"{number}\"}}"; | ||
var trelloCard = JsonSerializer.Deserialize<TrelloCardModel>(trelloCardJson, _jsonDeserializeOptions); | ||
|
||
Assert.AreEqual(number, trelloCard.Pos); | ||
} | ||
|
||
// ensure converting Trello's custom positions works | ||
[TestMethod] | ||
public void Read_Bottom_ReturnsDouble() | ||
{ | ||
var trelloCardJson = "{\"pos\": \"bottom\"}"; | ||
var trelloCard = JsonSerializer.Deserialize<TrelloCardModel>(trelloCardJson, _jsonDeserializeOptions); | ||
|
||
Assert.IsInstanceOfType(trelloCard.Pos, typeof(double)); | ||
} | ||
|
||
} | ||
} |
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,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" /> | ||
<PackageReference Include="coverlet.collector" Version="1.3.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\T2MDCli\T2MDCli.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |