mirror of https://github.com/Squidex/squidex.git
5 changed files with 107 additions and 1 deletions
@ -0,0 +1,42 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Json.Newtonsoft; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Contents.Json |
|||
{ |
|||
public sealed class StatusConverter : JsonConverter, ISupportedTypes |
|||
{ |
|||
public IEnumerable<Type> SupportedTypes |
|||
{ |
|||
get { yield return typeof(Status2); } |
|||
} |
|||
|
|||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
|||
{ |
|||
writer.WriteValue(value.ToString()); |
|||
} |
|||
|
|||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) |
|||
{ |
|||
if (reader.TokenType != JsonToken.String) |
|||
{ |
|||
throw new JsonException($"Expected String, but got {reader.TokenType}."); |
|||
} |
|||
|
|||
return new Status2(reader.Value.ToString()); |
|||
} |
|||
|
|||
public override bool CanConvert(Type objectType) |
|||
{ |
|||
return objectType == typeof(Status2); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,60 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Squidex.Domain.Apps.Core.Contents; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Model.Contents |
|||
{ |
|||
public class StatusTests |
|||
{ |
|||
[Fact] |
|||
public void Should_initialize_status_from_string() |
|||
{ |
|||
var result = new Status2("Draft"); |
|||
|
|||
Assert.Equal("Draft", result.Name); |
|||
Assert.Equal("Draft", result.ToString()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_provide_published_status() |
|||
{ |
|||
var result = Status2.Published; |
|||
|
|||
Assert.Equal("Published", result.Name); |
|||
Assert.Equal("Published", result.ToString()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_make_correct_equal_comparisons() |
|||
{ |
|||
var status_1_a = new Status2("Draft"); |
|||
var status_1_b = new Status2("Draft"); |
|||
|
|||
var status2_a = new Status2("Published"); |
|||
|
|||
Assert.Equal(status_1_a, status_1_b); |
|||
Assert.Equal(status_1_a.GetHashCode(), status_1_b.GetHashCode()); |
|||
Assert.True(status_1_a.Equals((object)status_1_b)); |
|||
|
|||
Assert.NotEqual(status_1_a, status2_a); |
|||
Assert.NotEqual(status_1_a.GetHashCode(), status2_a.GetHashCode()); |
|||
Assert.False(status_1_a.Equals((object)status2_a)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_serialize_and_deserialize() |
|||
{ |
|||
var status = new Status2("Draft"); |
|||
|
|||
var serialized = status.SerializeAndDeserialize(); |
|||
|
|||
Assert.Equal(status, serialized); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue