forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplTests.cs
More file actions
257 lines (216 loc) · 9.11 KB
/
ReplTests.cs
File metadata and controls
257 lines (216 loc) · 9.11 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Logging;
using Moq;
using ScriptCs.Contracts;
using Should;
using Xunit;
namespace ScriptCs.Tests
{
public class ReplTests
{
public class Mocks
{
public Mocks()
{
FileSystem = new Mock<IFileSystem>();
ScriptEngine = new Mock<IScriptEngine>();
Logger = new Mock<ILog>();
Console = new Mock<IConsole>();
ScriptPack = new Mock<IScriptPack>();
FilePreProcessor = new Mock<IFilePreProcessor>();
}
public Mock<IFileSystem> FileSystem { get; private set; }
public Mock<IScriptEngine> ScriptEngine { get; private set; }
public Mock<ILog> Logger { get; private set; }
public Mock<IConsole> Console { get; private set; }
public Mock<IScriptPack> ScriptPack { get; private set; }
public Mock<IFilePreProcessor> FilePreProcessor { get; private set; }
}
public static Repl GetRepl(Mocks mocks)
{
return new Repl(mocks.FileSystem.Object, mocks.ScriptEngine.Object, mocks.Logger.Object, mocks.Console.Object, mocks.FilePreProcessor.Object);
}
public class TheConstructor
{
[Fact]
public void ShouldProperlyInitializeMembers()
{
var mocks = new Mocks();
var repl = GetRepl(mocks);
repl.FileSystem.ShouldEqual(mocks.FileSystem.Object);
repl.ScriptEngine.ShouldEqual(mocks.ScriptEngine.Object);
repl.Logger.ShouldEqual(mocks.Logger.Object);
repl.Console.ShouldEqual(mocks.Console.Object);
}
}
public class TheInitializeMethod
{
private Mocks _mocks;
private Repl _repl;
public TheInitializeMethod()
{
_mocks = new Mocks();
_repl = GetRepl(_mocks);
_mocks.FileSystem.Setup(x => x.CurrentDirectory).Returns(@"c:\");
var paths = new[] { @"c:\path" };
_repl.Initialize(paths, new[] { _mocks.ScriptPack.Object });
}
[Fact]
public void PopulatesReferences()
{
foreach (var reference in Repl.DefaultReferences)
{
_repl.References.ShouldContain(reference);
}
_repl.References.ShouldContain(@"c:\path");
}
[Fact]
public void SetsTheScriptEngineBaseDirectory()
{
_mocks.ScriptEngine.VerifySet(x => x.BaseDirectory = @"c:\bin");
}
[Fact]
public void CreatesTheScriptPackSession()
{
_repl.ScriptPackSession.ShouldNotBeNull();
}
[Fact]
public void InitializesScriptPacks()
{
_mocks.ScriptPack.Verify(x => x.Initialize(It.IsAny<IScriptPackSession>()));
}
}
public class TheTerminateMethod
{
private Mocks _mocks;
private Repl _repl;
public TheTerminateMethod()
{
_mocks = new Mocks();
_repl = GetRepl(_mocks);
_mocks.FileSystem.Setup(x => x.CurrentDirectory).Returns(@"c:\");
_repl.Initialize(new List<string>(), new[] { _mocks.ScriptPack.Object });
_repl.Terminate();
}
[Fact]
public void TerminatesScriptPacks()
{
_mocks.ScriptPack.Verify(x => x.Terminate());
}
[Fact]
public void ExitsTheConsole()
{
_mocks.Console.Verify(x => x.Exit());
}
}
public class TheExecuteMethod
{
private Mocks _mocks;
private Repl _repl;
public TheExecuteMethod()
{
_mocks = new Mocks();
_repl = GetRepl(_mocks);
_repl.Console.ForegroundColor = ConsoleColor.White;
_repl.Execute("foo");
}
[Fact]
public void SetsTheForegroundColorToCyan()
{
_mocks.Console.VerifySet(x => x.ForegroundColor = ConsoleColor.Cyan, Times.Once());
}
[Fact]
public void ResetsColorAfterExecutingScript()
{
_mocks.Console.Verify(x => x.ResetColor());
}
[Fact]
public void CallsExecuteOnTheScriptEngine()
{
_mocks.ScriptEngine.Verify(x => x.Execute("foo", new string[0], _repl.References, Repl.DefaultNamespaces, It.IsAny<ScriptPackSession>()));
}
[Fact]
public void CatchesExceptionsAndWritesThemInRed()
{
_mocks.ScriptEngine.Setup(
x => x.Execute(It.IsAny<string>(), It.IsAny<string[]>(), It.IsAny<IEnumerable<string>>(), It.IsAny<IEnumerable<string>>(), It.IsAny<ScriptPackSession>()))
.Throws<ArgumentException>();
_repl.Execute("foo");
_mocks.Console.VerifySet(x => x.ForegroundColor = ConsoleColor.Red);
_mocks.Console.Verify(x => x.WriteLine(It.IsAny<string>()));
}
[Fact]
public void SetsTheForegroundColorBackToTheDefault()
{
_mocks.Console.VerifySet(x => x.ForegroundColor = ConsoleColor.White);
}
[Fact]
public void ShouldProcessFileIfLineIsALoad()
{
var mocks = new Mocks();
mocks.FileSystem.Setup(x => x.FileExists("file.csx")).Returns(true);
_repl = GetRepl(mocks);
_repl.Execute("#load \"file.csx\"");
mocks.FilePreProcessor.Verify(i => i.ProcessFile(It.Is<string>(x => x == "file.csx")), Times.Once());
}
[Fact]
public void ShouldExecuteLoadedFileIfLineIsALoad()
{
var mocks = new Mocks();
mocks.FilePreProcessor.Setup(x => x.ProcessFile(It.IsAny<string>()))
.Returns(new FilePreProcessorResult());
mocks.FileSystem.Setup(x => x.FileExists("file.csx")).Returns(true);
_repl = GetRepl(mocks);
_repl.Execute("#load \"file.csx\"");
mocks.ScriptEngine.Verify(i => i.Execute(It.IsAny<string>(), It.IsAny<string[]>(), It.IsAny<IEnumerable<string>>(), It.IsAny<IEnumerable<string>>(), It.IsAny<ScriptPackSession>()), Times.Once());
}
[Fact]
public void ShouldNotExecuteLoadedFileIfFileDoesNotExist()
{
var mocks = new Mocks();
mocks.FileSystem.Setup(x => x.FileExists("file.csx")).Returns(false);
_repl = GetRepl(mocks);
_repl.Execute("#load \"file.csx\"");
mocks.ScriptEngine.Verify(i => i.Execute(It.IsAny<string>(), It.IsAny<string[]>(), It.IsAny<IEnumerable<string>>(), It.IsAny<IEnumerable<string>>(), It.IsAny<ScriptPackSession>()), Times.Never());
}
[Fact]
public void ShouldReferenceAssemblyIfLineIsAReference()
{
var mocks = new Mocks();
mocks.FileSystem.Setup(i => i.CurrentDirectory).Returns("C:/");
mocks.FileSystem.Setup(x => x.FileExists("my.dll")).Returns(true);
_repl = GetRepl(mocks);
_repl.Initialize(Enumerable.Empty<string>(), Enumerable.Empty<IScriptPack>());
_repl.Execute("#r \"my.dll\"");
//default references = 6, + 1 we just added
_repl.References.Count().ShouldEqual(7);
}
[Fact]
public void ShouldNotReferenceAssemblyIfFileDoesNotExist()
{
var mocks = new Mocks();
mocks.FileSystem.Setup(i => i.CurrentDirectory).Returns("C:/");
mocks.FileSystem.Setup(x => x.FileExists("my.dll")).Returns(false);
_repl = GetRepl(mocks);
_repl.Initialize(Enumerable.Empty<string>(), Enumerable.Empty<IScriptPack>());
_repl.Execute("#r \"my.dll\"");
//default references = 6
_repl.References.Count().ShouldEqual(6);
}
[Fact]
public void ShouldNotExecuteAnythingIfLineIsAReference()
{
var mocks = new Mocks();
mocks.FileSystem.Setup(i => i.CurrentDirectory).Returns("C:/");
_repl = GetRepl(mocks);
_repl.Initialize(Enumerable.Empty<string>(), Enumerable.Empty<IScriptPack>());
_repl.Execute("#r \"my.dll\"");
mocks.ScriptEngine.Verify(i => i.Execute(It.IsAny<string>(), It.IsAny<string[]>(), It.IsAny<IEnumerable<string>>(), It.IsAny<IEnumerable<string>>(), It.IsAny<ScriptPackSession>()), Times.Never());
}
}
}
}