Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Dena.CodeAnalysis.Testing can read metadata. #10

Merged
merged 5 commits into from
Jan 30, 2024

Conversation

get-me-power
Copy link
Member

@get-me-power get-me-power commented Jan 24, 2024


Contribution License Agreement

motivation

Until now, when including external libraries in code, it was necessary to prepare dummy implementations each time.
This PR will solve those problems.

before

  • fakes
using System.Runtime.CompilerServices;

namespace Cysharp.Threading.Tasks
{
    [AsyncMethodBuilder(typeof(CompilerServices.AsyncUniTaskMethodBuilder))]
    public struct UniTask
    {
    }

    [AsyncMethodBuilder(typeof(CompilerServices.AsyncUniTaskMethodBuilder))]
    public struct UniTaskVoid
    {
    }
}

namespace Cysharp.Threading.Tasks.CompilerServices
{
    public struct AsyncUniTaskMethodBuilder
    {
    }
}

namespace NotSystem.Threading.Tasks
{
    [AsyncMethodBuilder(typeof(CompilerServices.AsyncUniTaskMethodBuilder))]
    public struct Task
    {
    }

    public class CompilerServices
    {
        public class AsyncUniTaskMethodBuilder
        {
        }
    }
}
  • testcode
// Copyright (c) 2020-2023 DeNA Co., Ltd.
// This software is released under the MIT License.

using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dena.CodeAnalysis.CSharp.Testing;
using NUnit.Framework;

namespace BanAsyncTaskAnalyzer.Test;

/// <summary>
/// This test is an examples of using the Dena.CodeAnalysis.Testing test helper library.
/// <see href="https://github.com/DeNA/Dena.CodeAnalysis.Testing"/>
/// </summary>
[TestFixture]
public class BanAsyncTaskAnalyzerTest
{    [Test]
    public async Task asyncメソッド_戻り値がUniTask_何もレポートされない()
    {
        var analyzer = new BanAsyncTaskAnalyzer();
        var source = ReadCodes("UseUniTaskCase.txt", "Fakes.cs"); //  ←Need Fake
        var diagnostics = await DiagnosticAnalyzerRunner.Run(analyzer, source);

        var actual = diagnostics
            .Where(x => x.Id != "CS1591") // Ignore "Missing XML comment for publicly visible type or member"
            .Where(x => x.Id != "CS8019") // Ignore "Unnecessary using directive"
            .Where(x => x.Id !=
                        "CS1998") // Ignore "This async method lacks 'await' operators and will run synchronously."
            .ToArray();

        DiagnosticsAssert.IsEmpty(actual);
    }
}

after

// Copyright (c) 2020-2023 DeNA Co., Ltd.
// This software is released under the MIT License.

using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dena.CodeAnalysis.CSharp.Testing;
using Cysharp.Threading.Tasks
using NUnit.Framework;

namespace BanAsyncTaskAnalyzer.Test;

/// <summary>
/// This test is an examples of using the Dena.CodeAnalysis.Testing test helper library.
/// <see href="https://github.com/DeNA/Dena.CodeAnalysis.Testing"/>
/// </summary>
[TestFixture]
public class BanAsyncTaskAnalyzerTest
{    [Test]
    public async Task asyncメソッド_戻り値がUniTask_何もレポートされない()
    {
        var analyzer = new BanAsyncTaskAnalyzer();
        var source = ReadCodes("UseUniTaskCase.txt"); // 
        var diagnostics = await DiagnosticAnalyzerRunner.Run(analyzer, typeof(UniTask) ,source); // ← no need fake

        var actual = diagnostics
            .Where(x => x.Id != "CS1591") // Ignore "Missing XML comment for publicly visible type or member"
            .Where(x => x.Id != "CS8019") // Ignore "Unnecessary using directive"
            .Where(x => x.Id !=
                        "CS1998") // Ignore "This async method lacks 'await' operators and will run synchronously."
            .ToArray();

        DiagnosticsAssert.IsEmpty(actual);
    }
}

diff --git a/tests/Dena.CodeAnalysis.Testing.Tests/AnalyzerRunnerTests.cs b/tests/Dena.CodeAnalysis.Testing.Tests/AnalyzerRunnerTests.cs
index b1b8f4e..fd4e358 100644
--- a/tests/Dena.CodeAnalysis.Testing.Tests/AnalyzerRunnerTests.cs
+++ b/tests/Dena.CodeAnalysis.Testing.Tests/AnalyzerRunnerTests.cs
@@ -1,4 +1,5 @@
 using System.Threading.Tasks;
+using Cysharp.Threading.Tasks;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using MSTestAssert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;

@@ -25,12 +26,24 @@ namespace Dena.CodeAnalysis.CSharp.Testing
             var anyAnalyzer = new NullAnalyzer();
             var diagnostics = await DiagnosticAnalyzerRunner.Run(
                 anyAnalyzer,
-                ExampleCode.DiagnosticsFreeClassLibrary
+                codes: ExampleCode.DiagnosticsFreeClassLibrary
             );

             MSTestAssert.AreEqual(0, diagnostics.Length, DiagnosticsFormatter.Format(diagnostics));
         }

+        [TestMethod]
+        public async Task WhenGivenUniTaskImport_ItShouldReturnNoDiagnostics()
+        {
+            var anyAnalyzer = new NullAnalyzer();
+            var diagnostics = await DiagnosticAnalyzerRunner.Run(
+                anyAnalyzer,
+                new[] { typeof(UniTask) },
+                ExampleCode.UniTaskImport
+            );
+
+            MSTestAssert.AreEqual(1, diagnostics.Length, DiagnosticsFormatter.Format(diagnostics));
+        }

         [TestMethod]
         public async Task WhenGivenContainingASyntaxError_ItShouldReturnSeveralDiagnostics()
@@ -38,7 +51,7 @@ namespace Dena.CodeAnalysis.CSharp.Testing
             var anyAnalyzer = new NullAnalyzer();
             var diagnostics = await DiagnosticAnalyzerRunner.Run(
                 anyAnalyzer,
-                ExampleCode.ContainingSyntaxError
+                codes: ExampleCode.ContainingSyntaxError
             );

             MSTestAssert.AreNotEqual(0, diagnostics.Length);
@@ -50,7 +63,7 @@ namespace Dena.CodeAnalysis.CSharp.Testing
         {
             var spyAnalyzer = new SpyAnalyzer();

-            await DiagnosticAnalyzerRunner.Run(spyAnalyzer, ExampleCode.DiagnosticsFreeClassLibrary);
+            await DiagnosticAnalyzerRunner.Run(spyAnalyzer, codes: ExampleCode.DiagnosticsFreeClassLibrary);

             MSTestAssert.IsTrue(spyAnalyzer.IsInitialized);
         }
@get-me-power get-me-power changed the title update: test project version Feature: Dena.CodeAnalysis.Testing can read metadata. Jan 24, 2024
@get-me-power get-me-power requested a review from Kuniwak January 24, 2024 14:48
@get-me-power get-me-power marked this pull request as ready for review January 24, 2024 14:56
@Kuniwak
Copy link
Member

Kuniwak commented Jan 30, 2024

@get-me-power Thank you for the contribution.

@Kuniwak Kuniwak merged commit b7e63e6 into master Jan 30, 2024
1 check passed
@Kuniwak Kuniwak deleted the feature/extraMetaData branch January 30, 2024 09:08
@get-me-power get-me-power self-assigned this Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants