Skip to content

Commit 2e3ffd1

Browse files
committed
Unit tests for scriptcs#629 and a few updates
Changed how I initialized the ILineProcessor list. Tests to verify the LineProcessor<T>() function adds the type to the Overrides dictionary when called from ModuleConfiguration. Also found and fixed a problem where ServiceOverrides._this was null when using the non default constructor. Unit test for this is in ModuleConfigurationTests.TheLineProcessorMethod.ShouldReturnTheModuleConfiguration Added RunTimeServicestests for verifying that the default line processors and custom processors get registered when the build container method is called.
1 parent eb06cb3 commit 2e3ffd1

5 files changed

Lines changed: 72 additions & 11 deletions

File tree

src/ScriptCs.Hosting/RuntimeServices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private void RegisterLineProcessors(ContainerBuilder builder)
132132

133133
var processorArray = new[] { loadProcessorType, usingProcessorType, referenceProcessorType }.Union(processorList).ToArray();
134134

135-
builder.RegisterTypes(processorArray.ToArray()).As<ILineProcessor>();
135+
builder.RegisterTypes(processorArray).As<ILineProcessor>();
136136
}
137137

138138
public ScriptServices GetScriptServices()

src/ScriptCs.Hosting/ServiceOverrides.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,21 @@ protected ServiceOverrides()
1919
Overrides[typeof(ILineProcessor)] = new List<Type>();
2020
}
2121

22-
public TConfig ScriptHostFactory<T>() where T : IScriptHostFactory
23-
{
24-
Overrides[typeof (IScriptHostFactory)] = typeof (T);
25-
return _this;
26-
}
27-
2822
protected ServiceOverrides(IDictionary<Type, object> overrides)
23+
: this()
2924
{
30-
Overrides = overrides;
31-
32-
if (!Overrides.ContainsKey(typeof(ILineProcessor)))
25+
if (!overrides.ContainsKey(typeof(ILineProcessor)))
3326
{
34-
Overrides[typeof(ILineProcessor)] = new List<Type>();
27+
overrides[typeof(ILineProcessor)] = Overrides[typeof(ILineProcessor)];
3528
}
29+
30+
Overrides = overrides;
31+
}
32+
33+
public TConfig ScriptHostFactory<T>() where T : IScriptHostFactory
34+
{
35+
Overrides[typeof(IScriptHostFactory)] = typeof(T);
36+
return _this;
3637
}
3738

3839
public TConfig ScriptExecutor<T>() where T : IScriptExecutor
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using ScriptCs.Contracts;
4+
using Should;
5+
using Xunit;
6+
7+
namespace ScriptCs.Hosting.Tests
8+
{
9+
public class ModuleConfigurationTests
10+
{
11+
public class TheLineProcessorMethod
12+
{
13+
[Fact]
14+
public void ShouldAddTheLineProcessorTypeToTheOverridesDictionary()
15+
{
16+
var overrides = new Dictionary<Type, object>();
17+
18+
var moduleConfiguration = new ModuleConfiguration(false, "script1.csx", false, LogLevel.Debug, overrides);
19+
moduleConfiguration.LineProcessor<UsingLineProcessor>();
20+
21+
var processors = overrides[typeof(ILineProcessor)] as List<Type>;
22+
processors.ShouldContain(typeof(UsingLineProcessor));
23+
}
24+
25+
[Fact]
26+
public void ShouldReturnTheModuleConfiguration()
27+
{
28+
var moduleConfiguration = new ModuleConfiguration(false, "script1.csx", false, LogLevel.Debug, new Dictionary<Type, object>());
29+
var config = moduleConfiguration.LineProcessor<UsingLineProcessor>();
30+
config.ShouldImplement<IModuleConfiguration>();
31+
config.ShouldEqual(moduleConfiguration);
32+
}
33+
}
34+
}
35+
}

test/ScriptCs.Hosting.Tests/RuntimeServicesTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using Autofac;
45
using Common.Logging;
56
using Moq;
@@ -132,6 +133,29 @@ public void ShouldRegisterTheDefaultAssemblyResolverIfNoOverride()
132133
_runtimeServices.Container.Resolve<IAssemblyResolver>().ShouldNotBeNull();
133134
}
134135

136+
[Fact]
137+
public void ShouldRegisterTheDefaultLineProcessors()
138+
{
139+
var processors = _runtimeServices.Container.Resolve<IEnumerable<ILineProcessor>>();
140+
processors.ShouldNotBeNull();
141+
processors.Where(p => p is IUsingLineProcessor).ShouldNotBeEmpty();
142+
processors.Where(p => p is IReferenceLineProcessor).ShouldNotBeEmpty();
143+
processors.Where(p => p is ILoadLineProcessor).ShouldNotBeEmpty();
144+
}
145+
146+
[Fact]
147+
public void ShouldRegisterACustomLineProcessor()
148+
{
149+
var mock = new Mock<ILineProcessor>();
150+
var processorList = _overrides[typeof (ILineProcessor)] as List<Type>;
151+
processorList.ShouldNotBeNull();
152+
processorList.Add(mock.Object.GetType());
153+
154+
var processors = _runtimeServices.Container.Resolve<IEnumerable<ILineProcessor>>();
155+
processors.ShouldNotBeNull();
156+
processors.Where(p => p.GetType() == mock.Object.GetType()).ShouldNotBeEmpty();
157+
}
158+
135159
[Fact]
136160
public void ShouldRegisterTheOverriddenScriptHostFactory()
137161
{

test/ScriptCs.Hosting.Tests/ScriptCs.Hosting.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Compile Include="..\ScriptCsAutoDataAttribute.cs">
8585
<Link>ScriptCsAutoDataAttribute.cs</Link>
8686
</Compile>
87+
<Compile Include="ModuleConfigurationTests.cs" />
8788
<Compile Include="ModuleLoaderTests.cs" />
8889
<Compile Include="ObjectSerializerTests.cs" />
8990
<Compile Include="PackageInstallerTests.cs" />

0 commit comments

Comments
 (0)