aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/ErrorCodeTest.cs
blob: 258ebf4e8e4ff0e84bde0e161f8105a951824df5 (plain)
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
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Timeline.Models.Http;
using Xunit;
using Xunit.Abstractions;

namespace Timeline.Tests
{
    public class ErrorCodeTest
    {
        private readonly ITestOutputHelper _output;

        public ErrorCodeTest(ITestOutputHelper output)
        {
            _output = output;
        }

        [Fact]
        public void ShouldWork()
        {
            var errorCodes = new Dictionary<int, string>();

            void RecursiveCheckErrorCode(Type type)
            {
                foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
                    .Where(fi => fi.IsLiteral && !fi.IsInitOnly && fi.FieldType == typeof(int)))
                {
                    var name = type.FullName + "." + field.Name;
                    var value = (int)field.GetRawConstantValue();
                    _output.WriteLine($"Find error code {name} , value is {value}.");

                    value.Should().BeInRange(1000_0000, 9999_9999, "Error code should have exactly 8 digits.");

                    errorCodes.Should().NotContainKey(value,
                        "identical error codes are found and conflict paths are {0} and {1}",
                        name, errorCodes.GetValueOrDefault(value));

                    errorCodes.Add(value, name);
                }

                foreach (var nestedType in type.GetNestedTypes())
                {
                    RecursiveCheckErrorCode(nestedType);
                }
            }

            RecursiveCheckErrorCode(typeof(ErrorCodes));
        }
    }
}