-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScriptRunnerTests.cs
140 lines (122 loc) · 5.1 KB
/
ScriptRunnerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
namespace CSharpInteractive.Tests;
using System.Diagnostics.CodeAnalysis;
[SuppressMessage("Performance", "CA1861:Avoid constant arrays as arguments")]
[SuppressMessage("Usage", "xUnit1042:The member referenced by the MemberData attribute returns untyped data rows")]
public class ScriptRunnerTests
{
private readonly Mock<ICommandSource> _commandSource;
private readonly Mock<ICommandsRunner> _commandsRunner;
private readonly Mock<IStatistics> _statistics;
private readonly Mock<IPresenter<Summary>> _summaryPresenter;
private readonly Mock<ILog<ScriptRunner>> _log;
public ScriptRunnerTests()
{
_log = new Mock<ILog<ScriptRunner>>();
var commands = Mock.Of<IEnumerable<ICommand>>();
_commandSource = new Mock<ICommandSource>();
_commandSource.Setup(i => i.GetCommands()).Returns(commands);
_commandsRunner = new Mock<ICommandsRunner>();
_statistics = new Mock<IStatistics>();
_summaryPresenter = new Mock<IPresenter<Summary>>();
}
[Theory]
[MemberData(nameof(Data))]
internal void ShouldRun(CommandResult[] results, string[] errors, string[] warnings, int expectedExitCode)
{
// Given
var runner = CreateInstance();
_commandsRunner.Setup(i => i.Run(It.IsAny<IEnumerable<ICommand>>())).Returns(results);
_statistics.SetupGet(i => i.Items).Returns(errors.Select(i => new StatisticsItem(StatisticsType.Error, [new Text(i)])).Concat(warnings.Select(i => new StatisticsItem(StatisticsType.Warning, [new Text(i)]))).ToArray);
// When
var actualExitCode = runner.Run();
// Then
actualExitCode.ShouldBe(expectedExitCode);
}
public static IEnumerable<object?[]> Data => new List<object?[]>
{
// Success
new object[]
{
new CommandResult[] {new(new CodeCommand(), true), new(new CodeCommand(), true), new(new ScriptCommand(string.Empty, string.Empty), true)},
Array.Empty<string>(),
Array.Empty<string>(),
0
},
// Success
new object[]
{
new CommandResult[] {new(new CodeCommand(), true), new(new CodeCommand(), true), new(new ScriptCommand(string.Empty, string.Empty), true, 33)},
Array.Empty<string>(),
Array.Empty<string>(),
33
},
new object[]
{
new CommandResult[] {new(new CodeCommand(), null), new(new CodeCommand(), null), new(new ScriptCommand(string.Empty, string.Empty), null)},
Array.Empty<string>(),
Array.Empty<string>(),
0
},
// Warnings
new object[]
{
new CommandResult[] {new(new CodeCommand(), null), new(new CodeCommand(), null), new(new ScriptCommand(string.Empty, string.Empty), true)},
Array.Empty<string>(),
new[] {"warn"},
0
},
// Errors
new object[]
{
new CommandResult[] {new(new CodeCommand(), null), new(new CodeCommand(), null), new(new ScriptCommand(string.Empty, string.Empty), true)},
new[] {"err"},
new[] {"warn"},
1
},
new object[]
{
new CommandResult[] {new(new CodeCommand(), null), new(new CodeCommand(), null), new(new ScriptCommand(string.Empty, string.Empty), true, 44)},
new[] {"err"},
new[] {"warn"},
44
},
// Failed
new object[]
{
new CommandResult[] {new(new CodeCommand(), null), new(new CodeCommand(), null), new(new ScriptCommand(string.Empty, string.Empty), false)},
Array.Empty<string>(),
Array.Empty<string>(),
1
},
new object[]
{
new CommandResult[] {new(new CodeCommand(), null), new(new CodeCommand(), null), new(new ScriptCommand(string.Empty, string.Empty), false, 55)},
Array.Empty<string>(),
Array.Empty<string>(),
1
}
};
[Fact]
[SuppressMessage("Performance", "CA1806:Do not ignore method results")]
public void ShouldShowErrorWhenScriptIsUncompleted()
{
// Given
var runner = CreateInstance();
//_log.Setup(i => i.Error(ErrorId.UncompletedScript, It.IsAny<string>()));
_commandSource.Setup(i => i.GetCommands()).Returns([new ScriptCommand(string.Empty, string.Empty), new CodeCommand()]);
_statistics.Setup(i => i.Items).Returns(new List<StatisticsItem>());
// ReSharper disable once ReturnValueOfPureMethodIsNotUsed
_commandsRunner.Setup(i => i.Run(It.IsAny<IEnumerable<ICommand>>())).Callback<IEnumerable<ICommand>>(i => i.Count()).Returns([new CommandResult(new ScriptCommand(string.Empty, string.Empty), true)]);
// When
runner.Run();
// Then
_log.Verify(i => i.Error(ErrorId.UncompletedScript, It.IsAny<Text[]>()));
}
private ScriptRunner CreateInstance() =>
new(
_log.Object,
_commandSource.Object,
_commandsRunner.Object,
_statistics.Object,
_summaryPresenter.Object);
}