~ruther/NosSmooth

ref: 3b11a9dc11977d6093f31e086d043365df96c526 NosSmooth/Tests/NosSmooth.Packets.Tests/PacketStringEnumeratorTests.cs -rw-r--r-- 7.1 KiB
3b11a9dc — Rutherther feat(game): add smart dialog handling, rejecting conflicting answers to same dialog 2 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
//  PacketStringEnumeratorTests.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 NosSmooth.PacketSerializer.Abstractions;
using NosSmooth.PacketSerializer.Abstractions.Errors;
using Xunit;

namespace NosSmooth.Packets.Tests;

/// <summary>
/// Test for <see cref="PacketStringEnumerator"/>.
/// </summary>
public class PacketStringEnumeratorTests
{
    /// <summary>
    /// Test that array of complex types can be parsed.
    /// </summary>
    [Fact]
    public void EnumeratorListComplexStringGivesCorrectResult()
    {
        var stringEnumerator = new PacketStringEnumerator("in 1 11.12.13|14.15.16|17.18.19");
        var headerTokenResult = stringEnumerator.GetNextToken(out var packetToken);
        Assert.True(headerTokenResult.IsSuccess);
        Assert.False(packetToken.PacketEndReached);
        Assert.NotNull(packetToken.IsLast);
        Assert.NotNull(packetToken.EncounteredUpperLevel);
        Assert.False(packetToken.IsLast);
        Assert.False(packetToken.EncounteredUpperLevel);
        Assert.Matches("in", packetToken.Token.ToString());

        var firstToken = stringEnumerator.GetNextToken(out packetToken);
        Assert.True(firstToken.IsSuccess);
        Assert.False(packetToken.PacketEndReached);
        Assert.NotNull(packetToken.IsLast);
        Assert.NotNull(packetToken.EncounteredUpperLevel);
        Assert.False(packetToken.IsLast);
        Assert.False(packetToken.EncounteredUpperLevel);
        Assert.Matches("1", packetToken.Token.ToString());

        stringEnumerator.PushLevel('|');
        stringEnumerator.PrepareLevel('.');

        for (int i = 0; i < 3; i++)
        {
            stringEnumerator.PushPreparedLevel();

            for (int j = 0; j < 3; j++)
            {
                string currentNum = (j + (i * 3) + 1 + 10).ToString();
                var currentToken = stringEnumerator.GetNextToken(out packetToken);
                Assert.True(currentToken.IsSuccess);
                Assert.False(packetToken.PacketEndReached);
                Assert.NotNull(packetToken.IsLast);
                Assert.NotNull(packetToken.EncounteredUpperLevel);
                if (j == 2 && i == 2)
                {
                    Assert.True(packetToken.EncounteredUpperLevel);
                }
                else
                {
                    Assert.False(packetToken.EncounteredUpperLevel);
                }

                if (j != 2)
                {
                    Assert.False(packetToken.IsLast);
                }
                else
                {
                    Assert.True(packetToken.IsLast);
                }

                Assert.Matches(currentNum, packetToken.Token.ToString());
            }

            Assert.True(stringEnumerator.IsOnLastToken());
            stringEnumerator.PopLevel();
        }

        stringEnumerator.PopLevel();
        Assert.True(stringEnumerator.IsOnLastToken());
    }

    /// <summary>
    /// Test that over reaching the end is not allowed.
    /// </summary>
    [Fact]
    public void EnumeratorDoesNotAllowOvereachingPacketEnd()
    {
        var stringEnumerator = new PacketStringEnumerator("in 1 2 3 4");
        var tokenResult = stringEnumerator.GetNextToken(out _);
        Assert.True(tokenResult.IsSuccess); // in
        tokenResult = stringEnumerator.GetNextToken(out _);
        Assert.True(tokenResult.IsSuccess); // 1
        tokenResult = stringEnumerator.GetNextToken(out _);
        Assert.True(tokenResult.IsSuccess); // 2
        tokenResult = stringEnumerator.GetNextToken(out _);
        Assert.True(tokenResult.IsSuccess); // 3
        tokenResult = stringEnumerator.GetNextToken(out _);
        Assert.True(tokenResult.IsSuccess); // 4

        tokenResult = stringEnumerator.GetNextToken(out _);
        Assert.False(tokenResult.IsSuccess);
        Assert.IsType<PacketEndReachedError>(tokenResult.Error);
    }

    /// <summary>
    /// Test that over reaching the end of a list is not allowed.
    /// </summary>
    [Fact]
    public void EnumeratorDoesNotAllowOvereachingListComplexTypeEnd()
    {
        var stringEnumerator = new PacketStringEnumerator("in 1|2.2|3.3|4.4|5");
        var tokenResult = stringEnumerator.GetNextToken(out _);
        Assert.True(tokenResult.IsSuccess); // in

        stringEnumerator.PushLevel('.');
        stringEnumerator.PushLevel('|');

        tokenResult = stringEnumerator.GetNextToken(out _);
        Assert.True(tokenResult.IsSuccess); // 1

        tokenResult = stringEnumerator.GetNextToken(out var packetToken);
        Assert.True(tokenResult.IsSuccess); // 2
        Assert.True(packetToken.IsLast);

        tokenResult = stringEnumerator.GetNextToken(out _);
        Assert.False(tokenResult.IsSuccess);
        Assert.IsType<PacketEndReachedError>(tokenResult.Error);
        Assert.True(((PacketEndReachedError)tokenResult.Error!).LevelEnd);
    }

    /// <summary>
    /// Test that over reaching the length of a list is not allowed.
    /// </summary>
    [Fact]
    public void EnumeratorDoesNotAllowOvereachingListLength()
    {
        var stringEnumerator = new PacketStringEnumerator("in 1|2.2|3.3|4.4|5");
        var tokenResult = stringEnumerator.GetNextToken(out _);
        Assert.True(tokenResult.IsSuccess); // in

        stringEnumerator.PushLevel('.', 2);
        stringEnumerator.PushLevel('|');

        // first item
        tokenResult = stringEnumerator.GetNextToken(out _);
        Assert.True(tokenResult.IsSuccess);

        tokenResult = stringEnumerator.GetNextToken(out var packetToken);
        Assert.True(tokenResult.IsSuccess);
        Assert.True(packetToken.IsLast);

        stringEnumerator.PopLevel();

        // second item
        stringEnumerator.PushLevel('|');
        tokenResult = stringEnumerator.GetNextToken(out _);
        Assert.True(tokenResult.IsSuccess);

        stringEnumerator.GetNextToken(out packetToken);
        Assert.True(tokenResult.IsSuccess);
        Assert.True(packetToken.IsLast);

        stringEnumerator.PopLevel();

        // cannot reach third item
        Assert.True(stringEnumerator.IsOnLastToken());
        stringEnumerator.PushLevel('|');
        tokenResult = stringEnumerator.GetNextToken(out _);
        Assert.False(tokenResult.IsSuccess);
        Assert.IsType<PacketEndReachedError>(tokenResult.Error);
        Assert.True(((PacketEndReachedError)tokenResult.Error!).LevelEnd);
    }

    /// <summary>
    /// Test that EncounteredUpperLevel is returned if appropriate.
    /// </summary>
    [Fact]
    public void EnumeratorReturnsEncounteredUpperLevel()
    {
        var stringEnumerator = new PacketStringEnumerator("in 1|2 1");
        var tokenResult = stringEnumerator.GetNextToken(out _);
        Assert.True(tokenResult.IsSuccess); // in

        stringEnumerator.PushLevel('.');
        stringEnumerator.PushLevel('|');

        tokenResult = stringEnumerator.GetNextToken(out _);
        Assert.True(tokenResult.IsSuccess);

        tokenResult = stringEnumerator.GetNextToken(out var packetToken);
        Assert.True(tokenResult.IsSuccess);
        Assert.True(packetToken.IsLast);
        Assert.True(packetToken.EncounteredUpperLevel);
    }
}
Do not follow this link