Skip to content

Commit 353c207

Browse files
committed
Implemented analyzers for flubu execute methods.
1 parent b820256 commit 353c207

9 files changed

Lines changed: 1275 additions & 1 deletion
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using FlubuCore.Analyzers.Tests.Scripts;
2+
using Microsoft.CodeAnalysis;
3+
using Microsoft.CodeAnalysis.Diagnostics;
4+
using TestHelper;
5+
using Xunit;
6+
7+
namespace FlubuCore.Analyzers.Tests
8+
{
9+
public class ExecuteInAddTaskAnalyzerUnitTests : CodeFixVerifier
10+
{
11+
[Fact]
12+
public void CorrectAddCoreTask_NoDiagnostic()
13+
{
14+
VerifyCSharpDiagnostic(ExecuteInAddTaskAnalyzerScripts.CorrectAddCoreTaskScript);
15+
}
16+
17+
[Fact]
18+
public void ExecuteInAddCoreTask_ReportsDiagnostic()
19+
{
20+
var expected = new DiagnosticResult
21+
{
22+
Id = "FlubuCore_Execute_001",
23+
Message = "Do not call 'Execute' inside 'AddCoreTask'. The target executes the task automatically. Remove the Execute call.",
24+
Severity = DiagnosticSeverity.Error,
25+
Locations =
26+
new[]
27+
{
28+
new DiagnosticResultLocation("Test0.cs", 30, 47)
29+
}
30+
};
31+
32+
VerifyCSharpDiagnostic(ExecuteInAddTaskAnalyzerScripts.ExecuteInAddCoreTaskScript, expected);
33+
}
34+
35+
[Fact]
36+
public void ExecuteInAddTask_ReportsDiagnostic()
37+
{
38+
var expected = new DiagnosticResult
39+
{
40+
Id = "FlubuCore_Execute_001",
41+
Message = "Do not call 'Execute' inside 'AddTask'. The target executes the task automatically. Remove the Execute call.",
42+
Severity = DiagnosticSeverity.Error,
43+
Locations =
44+
new[]
45+
{
46+
new DiagnosticResultLocation("Test0.cs", 31, 43)
47+
}
48+
};
49+
50+
VerifyCSharpDiagnostic(ExecuteInAddTaskAnalyzerScripts.ExecuteInAddTaskScript, expected);
51+
}
52+
53+
[Fact]
54+
public void ExecuteInAddCoreTaskAsync_ReportsDiagnostic()
55+
{
56+
var expected = new DiagnosticResult
57+
{
58+
Id = "FlubuCore_Execute_001",
59+
Message = "Do not call 'Execute' inside 'AddCoreTaskAsync'. The target executes the task automatically. Remove the Execute call.",
60+
Severity = DiagnosticSeverity.Error,
61+
Locations =
62+
new[]
63+
{
64+
new DiagnosticResultLocation("Test0.cs", 30, 52)
65+
}
66+
};
67+
68+
VerifyCSharpDiagnostic(ExecuteInAddTaskAnalyzerScripts.ExecuteInAddCoreTaskAsyncScript, expected);
69+
}
70+
71+
[Fact]
72+
public void ExecuteInAddTaskAsync_ReportsDiagnostic()
73+
{
74+
var expected = new DiagnosticResult
75+
{
76+
Id = "FlubuCore_Execute_001",
77+
Message = "Do not call 'Execute' inside 'AddTaskAsync'. The target executes the task automatically. Remove the Execute call.",
78+
Severity = DiagnosticSeverity.Error,
79+
Locations =
80+
new[]
81+
{
82+
new DiagnosticResultLocation("Test0.cs", 30, 48)
83+
}
84+
};
85+
86+
VerifyCSharpDiagnostic(ExecuteInAddTaskAnalyzerScripts.ExecuteInAddTaskAsyncScript, expected);
87+
}
88+
89+
[Fact]
90+
public void ExecuteAsyncInAddCoreTask_ReportsDiagnostic()
91+
{
92+
var expected = new DiagnosticResult
93+
{
94+
Id = "FlubuCore_Execute_001",
95+
Message = "Do not call 'ExecuteAsync' inside 'AddCoreTask'. The target executes the task automatically. Remove the ExecuteAsync call.",
96+
Severity = DiagnosticSeverity.Error,
97+
Locations =
98+
new[]
99+
{
100+
new DiagnosticResultLocation("Test0.cs", 31, 47)
101+
}
102+
};
103+
104+
VerifyCSharpDiagnostic(ExecuteInAddTaskAnalyzerScripts.ExecuteAsyncInAddCoreTaskScript, expected);
105+
}
106+
107+
[Fact]
108+
public void ExecuteInDoMethod_NoDiagnostic()
109+
{
110+
VerifyCSharpDiagnostic(ExecuteInAddTaskAnalyzerScripts.ExecuteInDoMethodScript);
111+
}
112+
113+
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
114+
{
115+
return new ExecuteInAddTaskAnalyzer();
116+
}
117+
}
118+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using FlubuCore.Analyzers.Tests.Scripts;
2+
using Microsoft.CodeAnalysis;
3+
using Microsoft.CodeAnalysis.Diagnostics;
4+
using TestHelper;
5+
using Xunit;
6+
7+
namespace FlubuCore.Analyzers.Tests
8+
{
9+
public class MissingExecuteAnalyzerUnitTests : CodeFixVerifier
10+
{
11+
[Fact]
12+
public void CorrectExecuteInChain_NoDiagnostic()
13+
{
14+
VerifyCSharpDiagnostic(MissingExecuteAnalyzerScripts.CorrectExecuteInChainScript);
15+
}
16+
17+
[Fact]
18+
public void CorrectExecuteOnVariable_NoDiagnostic()
19+
{
20+
VerifyCSharpDiagnostic(MissingExecuteAnalyzerScripts.CorrectExecuteOnVariableScript);
21+
}
22+
23+
[Fact]
24+
public void CorrectExecuteAfterFluentOnVariable_NoDiagnostic()
25+
{
26+
VerifyCSharpDiagnostic(MissingExecuteAnalyzerScripts.CorrectExecuteAfterFluentOnVariableScript);
27+
}
28+
29+
[Fact]
30+
public void CorrectReassignedVariable_NoDiagnostic()
31+
{
32+
VerifyCSharpDiagnostic(MissingExecuteAnalyzerScripts.CorrectReassignedVariableScript);
33+
}
34+
35+
[Fact]
36+
public void MissingExecuteInChain_ReportsDiagnostic()
37+
{
38+
var expected = new DiagnosticResult
39+
{
40+
Id = "FlubuCore_Execute_002",
41+
Message = "Task created via 'RunProgramTask' is never executed. Call Execute(context) or ExecuteAsync(context) on the task.",
42+
Severity = DiagnosticSeverity.Warning,
43+
Locations =
44+
new[]
45+
{
46+
new DiagnosticResultLocation("Test0.cs", 28, 13)
47+
}
48+
};
49+
50+
VerifyCSharpDiagnostic(MissingExecuteAnalyzerScripts.MissingExecuteInChainScript, expected);
51+
}
52+
53+
[Fact]
54+
public void MissingExecuteOnVariable_ReportsDiagnostic()
55+
{
56+
var expected = new DiagnosticResult
57+
{
58+
Id = "FlubuCore_Execute_002",
59+
Message = "Task created via 'RunProgramTask' is never executed. Call Execute(context) or ExecuteAsync(context) on the task.",
60+
Severity = DiagnosticSeverity.Warning,
61+
Locations =
62+
new[]
63+
{
64+
new DiagnosticResultLocation("Test0.cs", 28, 24)
65+
}
66+
};
67+
68+
VerifyCSharpDiagnostic(MissingExecuteAnalyzerScripts.MissingExecuteOnVariableScript, expected);
69+
}
70+
71+
[Fact]
72+
public void MissingExecuteDiscarded_ReportsDiagnostic()
73+
{
74+
var expected = new DiagnosticResult
75+
{
76+
Id = "FlubuCore_Execute_002",
77+
Message = "Task created via 'Build' is never executed. Call Execute(context) or ExecuteAsync(context) on the task.",
78+
Severity = DiagnosticSeverity.Warning,
79+
Locations =
80+
new[]
81+
{
82+
new DiagnosticResultLocation("Test0.cs", 27, 13)
83+
}
84+
};
85+
86+
VerifyCSharpDiagnostic(MissingExecuteAnalyzerScripts.MissingExecuteDiscardedScript, expected);
87+
}
88+
89+
[Fact]
90+
public void MissingExecuteCoreTasks_ReportsDiagnostic()
91+
{
92+
var expected = new DiagnosticResult
93+
{
94+
Id = "FlubuCore_Execute_002",
95+
Message = "Task created via 'Build' is never executed. Call Execute(context) or ExecuteAsync(context) on the task.",
96+
Severity = DiagnosticSeverity.Warning,
97+
Locations =
98+
new[]
99+
{
100+
new DiagnosticResultLocation("Test0.cs", 28, 13)
101+
}
102+
};
103+
104+
VerifyCSharpDiagnostic(MissingExecuteAnalyzerScripts.MissingExecuteCoreTasksScript, expected);
105+
}
106+
107+
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
108+
{
109+
return new MissingExecuteAnalyzer();
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)