cezarypiatek / RoslynTestKit

A lightweight framework for writing unit tests for Roslyn diagnostic analyzers, code fixes, refactorings and completion providers.
Other
24 stars 7 forks source link

Report not source diagnostics #6

Closed divan-9 closed 4 years ago

divan-9 commented 4 years ago

In case of exception in analyzer it would be reported as AD0001 diagnostic. It has no location in source tree.

Sometimes it can be useful to check absence of exception by unit test

For instance

        [Fact]
        public void DoesNotWarnEmptyArgumentList()
        {
            const string code = @"
                class A
                {
                    void Method()
                    {
                    }
                }
            ";

            this.NoDiagnostic(code, "AD0001");
        }

Honestly I don't really understand why this check exists in original version, and if my solution is totally wrong just let me know.

cezarypiatek commented 4 years ago

Thanks, I wasn't aware of AD0001. Maybe I should add a dedicated assertion that checks if there is no AD0001 or maybe GetDiagnostics should throw an exception in that case? What do you think about it?

divan-9 commented 4 years ago

You right, special exception is better solution

cezarypiatek commented 4 years ago

Do you mean that options with throwing an exception from GetDiagnostics? That would give confidence that no exception is thrown from the analyzer. Let's say your assertion is `NoDiagnostic("XYZ0001") - this assertion is meet even if the analyzer throws an exception but it's rather not expected behavior.

divan-9 commented 4 years ago

I mean the solution with specific assertion like HasNoException(source)

Another option with throwing exception from GetDiagnostic is OK to, but testing this scenario is usualy looks less expressive.

Checking exceptions in unit tests is usually result of working on reported bugs, it's not common for normal TDD cycle, but sometimes it could be helpfull.

I can try to create another pull request in a while