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

Hotfix/1.5.1 #270

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Catel.Analyzers.Tests/CTL0008/CTL0008CodeFixProviderFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,39 @@ public Program(object arg)

Solution.Verify<ArgumentsAnalyzer>(analyzer => RoslynAssert.CodeFix(Fixer, ExpectedDiagnostic.Create("CS0103"), before, after));
}

[TestCase]
public void InvalidCode_NameOf()
{
var before = @"
namespace ConsoleApp1
{
using Catel;

internal class Program
{
public Program(object arg)
{
↓Argument.IsNotNull(nameof(arg), arg);
}
}
}";
var after = @"
namespace ConsoleApp1
{
using Catel;

internal class Program
{
public Program(object arg)
{
ArgumentNullException.ThrowIfNull(arg);
}
}
}";

Solution.Verify<ArgumentsAnalyzer>(analyzer => RoslynAssert.CodeFix(analyzer, Fixer, before, after));
}
}
}
}
71 changes: 65 additions & 6 deletions src/Catel.Analyzers.Tests/CTL0011/CTL0011CodeFixProviderFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ public void InvalidCode_Default()
var before = @"
namespace ConsoleApp1
{
using Catel;
using Catel.Logging;

internal class Program
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();

public Program()
{

Expand All @@ -33,6 +38,7 @@ public async Task MakeError()
var after = @"
namespace ConsoleApp1
{
using Catel;
using Catel.Logging;

internal class Program
Expand All @@ -46,7 +52,7 @@ public Program()

public async Task MakeError()
{
throw Log.ErrorAndCreateException<InvalidOperationException>(""Some invalid operation"", new Exception(""This is error!""));
throw Log.ErrorAndCreateException(message => new InvalidOperationException(message, new Exception(""This is error!"")), ""Some invalid operation"");
}
}
}";
Expand All @@ -60,18 +66,22 @@ public void InvalidCode_NestedClass()
var before =
@"namespace ConsoleApp1
{
using Catel;
using Catel.Logging;
using System.Threading;
using System.Reflection;

internal class Program
{
{
public Program()
{

}

internal class NestedProgram
{
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();

public async Task MakeError()
{
↓throw new InvalidOperationException(""Some invalid operation"");
Expand All @@ -82,19 +92,20 @@ public async Task MakeError()
var after =
@"namespace ConsoleApp1
{
using Catel;
using Catel.Logging;
using System.Threading;
using System.Reflection;
using Catel.Logging;

internal class Program
{
{
public Program()
{

}

internal class NestedProgram
{
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();

public async Task MakeError()
Expand All @@ -107,6 +118,54 @@ public async Task MakeError()

Solution.Verify<ExceptionsAnalyzer>(analyzer => RoslynAssert.CodeFix(analyzer, Fixer, before, after));
}

[TestCase]
public void InvalidCode_Arguments_NameofExpression_StringLiteral()
{
var before = @"
namespace ConsoleApp1
{
using Catel.Logging;

internal class Program
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();

public Program()
{

}

public async Task MakeError()
{
↓throw new ArgumentOutOfRangeException(nameof(mode), $""The suspension mode '{Enum<SuspensionMode>.ToString(mode)}' is unhandled."");
}
}
}";
var after = @"
namespace ConsoleApp1
{
using Catel.Logging;

internal class Program
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();

public Program()
{

}

public async Task MakeError()
{
throw Log.ErrorAndCreateException(message => new ArgumentOutOfRangeException(nameof(mode), message), $""The suspension mode '{Enum<SuspensionMode>.ToString(mode)}' is unhandled."");
}
}
}";

Solution.Verify<ExceptionsAnalyzer>(analyzer => RoslynAssert.CodeFix(analyzer, Fixer, before, after));
}

}
}
}
46 changes: 44 additions & 2 deletions src/Catel.Analyzers.Tests/CTL0011/CTL0011DiagnosticFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ public void InvalidCode_Exception_Thrown()
{
var before = @"
namespace ConsoleApp1
{
{
using Catel;
using Catel.Logging;

internal class Program
{
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();

public Program()
{

Expand All @@ -36,14 +41,45 @@ public async Task MakeError()

public class Reposts_NoDiagnostic
{
[TestCase]
public void ValidCode_NoClassLogger()
{
var before = @"
namespace ConsoleApp1
{
using Catel;
using Catel.Logging;

internal class Program
{
public Program()
{

}

public async Task MakeError()
{
throw new InvalidOperationException();
}
}
}";
Solution.Verify<ExceptionsAnalyzer>(analyzer => RoslynAssert.NoAnalyzerDiagnostics(analyzer, Descriptors.CTL0011_ProvideCatelLogOnThrowingException, before));

}

[TestCase]
public void ValidCode_NoMessageProvided()
{
var before = @"
namespace ConsoleApp1
{
using Catel;
using Catel.Logging;

internal class Program
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();

public Program()
{

Expand All @@ -65,7 +101,9 @@ public void ValidCode_Log_ErrorAndCreateException()
var before = @"
namespace ConsoleApp1
{
using Catel;
using Catel.Logging;

internal class Program
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();
Expand All @@ -91,7 +129,9 @@ public void ValidCode_Manually_Created_Exception_NotThrown()
var before = @"
namespace ConsoleApp1
{
using Catel;
using Catel.Logging;

internal class Program
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();
Expand Down Expand Up @@ -123,7 +163,9 @@ public void ValidCode_Exception_Created_AsCallback()
var before = @"
namespace ConsoleApp1
{
using Catel;
using Catel.Logging;

internal class Program
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();
Expand Down
4 changes: 3 additions & 1 deletion src/Catel.Analyzers.Tests/Helpers/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ public static void GlobalSetup()
var debugMetadataReferences = MetadataReferences.CreateFromAssembly(typeof(System.Diagnostics.Debug).Assembly)
.WithAliases(new[] { "global", "System" });
var transitiveMetadataReferences = MetadataReferences.Transitive(typeof(ValidCodeWithAllAnalyzers).Assembly);
var catelMetadataReferences = MetadataReferences.CreateFromAssembly(typeof(Catel.Logging.Log).Assembly);

var allMetadata = transitiveMetadataReferences.Append(debugMetadataReferences)
.Append(systemMetadataReferences);
.Append(systemMetadataReferences)
.Append(catelMetadataReferences);

Settings.Default = Settings.Default.WithAllowedCompilerDiagnostics(AllowedCompilerDiagnostics.WarningsAndErrors)
.WithMetadataReferences(refs => refs.Concat(allMetadata));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ private static string[] GetArgumentNames(InvocationExpressionSyntax invocationEx

var argumentNames = argumentList.Arguments.Select(x =>
{
if (x.DescendantNodes().FirstOrDefault(x => x.IsKind(SyntaxKind.IdentifierName)) is IdentifierNameSyntax identifierNameSyntax)
var argumentSyntax = x;
if (argumentSyntax.Expression is InvocationExpressionSyntax childInvocation)
{
return GetArgumentNames(childInvocation).FirstOrDefault() ?? string.Empty;
}

if (argumentSyntax.DescendantNodes().FirstOrDefault(x => x.IsKind(SyntaxKind.IdentifierName)) is IdentifierNameSyntax identifierNameSyntax)
{
return identifierNameSyntax.Identifier.ValueText;
}
Expand Down
Loading