~ruther/NosSmooth

ref: 55a85a5afc3b997bf24c0ce9fc87ee8fea715d68 NosSmooth/Tests/NosSmooth.Packets.Tests/Converters/Basic/StringConverterTests.cs -rw-r--r-- 1.9 KiB
55a85a5a — František Boháček feat: add raid type skeleton 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
//  StringConverterTests.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using NosSmooth.Packets.Extensions;
using NosSmooth.PacketSerializer.Abstractions;
using Xunit;

namespace NosSmooth.Packets.Tests.Converters.Basic;

/// <summary>
/// Tests for <see cref="StringConverter"/>.
/// </summary>
public class StringConverterTests
{
    private readonly IStringSerializer _stringSerializer;

    /// <summary>
    /// Initializes a new instance of the <see cref="StringConverterTests"/> class.
    /// </summary>
    public StringConverterTests()
    {
        var provider = new ServiceCollection()
            .AddPacketSerialization()
            .BuildServiceProvider();

        _stringSerializer = provider.GetRequiredService<IStringSerializer>();
    }

    /// <summary>
    /// Tests that the serializer serializes null as -.
    /// </summary>
    [Fact]
    public void TestsTreatsNullAsMinus()
    {
        string? test = null;
        var stringBuilder = new PacketStringBuilder();
        var serializeResult = _stringSerializer.Serialize(test, stringBuilder);
        Assert.True(serializeResult.IsSuccess, !serializeResult.IsSuccess ? serializeResult.Error.Message : string.Empty);
        Assert.Equal("-", stringBuilder.ToString());
    }

    /// <summary>
    /// Tests that the serializer serializes null as -.
    /// </summary>
    [Fact]
    public void TestsTreatsMinusAsNull()
    {
        var deserialize = "-";
        var stringEnumerator = new PacketStringEnumerator(deserialize);
        var deserializeResult = _stringSerializer.Deserialize<string?>(ref stringEnumerator);
        Assert.True(deserializeResult.IsSuccess, !deserializeResult.IsSuccess ? deserializeResult.Error.Message : string.Empty);
        Assert.Null(deserializeResult.Entity);
    }
}
Do not follow this link