1
using System;
2
using Libplanet.Analyzers.Tests.Helpers;
3
using Libplanet.Analyzers.Tests.Verifiers;
4
using Microsoft.CodeAnalysis;
5
using Microsoft.CodeAnalysis.Diagnostics;
6
using Xunit;
7

8
namespace Libplanet.Analyzers.Tests
9
{
10
    public class ActionAnalyzerTest : DiagnosticVerifier
11
    {
12
        [Fact]
13
        public void EmptyCode()
14 1
        {
15 1
            VerifyDiagnostic(" ");
16 1
        }
17

18
        [Fact]
19
        public void LA1001_SystemRandomBreaksActionDeterminism()
20 1
        {
21 1
            var test = @"
22 1
                using System;
23 1
                using Bencodex;
24 1
                using Libplanet.Action;
25 1
                namespace SampleGame {
26 1
                    public class SampleAction : IAction {
27 1
                        IValue PlainValue => default(Null);
28 1
                        public void LoadPlainValue(IValue plainValue) { }
29 1
                        public IAccountStateDelta Execute(IActionContext context) {
30 1
                            new Random().Next();
31 1
                            return context.PreviousStates;
32 1
                        }
33 1
                    }
34 1
                }
35 1
            ";
36 1
            var expected = new DiagnosticResult
37 1
            {
38 1
                Id = "LAA1001",
39 1
                Message = "The System.Random makes an IAction indeterministic; " +
40 1
                    "use IActionContext.Random property instead.",
41 1
                Severity = DiagnosticSeverity.Warning,
42 1
                Locations = new[] { new DiagnosticResultLocation(10, 29) },
43 1
            };
44

45 1
            VerifyDiagnostic(test, expected);
46 1
        }
47

48
        protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() =>
49 1
            new ActionAnalyzer();
50
    }
51
}

Read our documentation on viewing source code .

Loading