Skip to content

Commit 86e0b15

Browse files
combine tests
1 parent 3ada040 commit 86e0b15

37 files changed

+414
-368
lines changed

Machine.Specifications.Runner.VisualStudio.sln

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26228.4
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29209.62
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{405D88EB-F3ED-4B4C-B758-4EA98692F96F}"
77
ProjectSection(SolutionItems) = preProject
@@ -15,8 +15,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Machine.Specifications.Runn
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Machine.Specifications.Runner.VisualStudio.Specs", "src\Machine.Specifications.Runner.VisualStudio.Specs\Machine.Specifications.Runner.VisualStudio.Specs.csproj", "{28A8C30C-7C6C-402B-86E2-D54A20748564}"
1717
EndProject
18-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleSpecs", "src\SampleSpecs\SampleSpecs.csproj", "{64E9F680-210D-4A69-BF87-13BA3E0FD3B7}"
19-
EndProject
2018
Global
2119
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2220
Debug|Any CPU = Debug|Any CPU
@@ -31,10 +29,6 @@ Global
3129
{28A8C30C-7C6C-402B-86E2-D54A20748564}.Debug|Any CPU.Build.0 = Debug|Any CPU
3230
{28A8C30C-7C6C-402B-86E2-D54A20748564}.Release|Any CPU.ActiveCfg = Release|Any CPU
3331
{28A8C30C-7C6C-402B-86E2-D54A20748564}.Release|Any CPU.Build.0 = Release|Any CPU
34-
{64E9F680-210D-4A69-BF87-13BA3E0FD3B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35-
{64E9F680-210D-4A69-BF87-13BA3E0FD3B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
36-
{64E9F680-210D-4A69-BF87-13BA3E0FD3B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
37-
{64E9F680-210D-4A69-BF87-13BA3E0FD3B7}.Release|Any CPU.Build.0 = Release|Any CPU
3832
EndGlobalSection
3933
GlobalSection(SolutionProperties) = preSolution
4034
HideSolutionNode = FALSE
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.CodeDom.Compiler;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Reflection;
7+
using Machine.Specifications;
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CSharp;
10+
11+
namespace Machine.VSTestAdapter.Specs
12+
{
13+
public class CompileContext : IDisposable
14+
{
15+
private readonly string _directory = Path.GetDirectoryName(typeof(CompileContext).Assembly.Location);
16+
17+
public string Compile(string code)
18+
{
19+
var filename = Path.Combine(_directory, Guid.NewGuid() + ".dll");
20+
21+
#if NETCOREAPP
22+
var result = Microsoft.CodeAnalysis.CSharp.CSharpCompilation.Create(Path.GetFileName(filename))
23+
.WithOptions(new Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
24+
.AddReferences(
25+
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
26+
MetadataReference.CreateFromFile(Assembly.Load("netstandard, Version=2.0.0.0").Location),
27+
MetadataReference.CreateFromFile(Assembly.Load("System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a").Location),
28+
MetadataReference.CreateFromFile(typeof(Establish).Assembly.Location),
29+
MetadataReference.CreateFromFile(typeof(Console).Assembly.Location),
30+
MetadataReference.CreateFromFile(typeof(ShouldExtensionMethods).Assembly.Location))
31+
.AddSyntaxTrees(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(code))
32+
.Emit(filename);
33+
34+
if (!result.Success)
35+
throw new InvalidOperationException();
36+
#else
37+
var parameters = new CompilerParameters
38+
{
39+
OutputAssembly = filename
40+
};
41+
42+
parameters.ReferencedAssemblies.AddRange(new []
43+
{
44+
"Machine.Specifications.dll",
45+
"Machine.Specifications.Should.dll"
46+
});
47+
48+
var options = new Dictionary<string, string> {{"CompilerVersion", "v4.0"}};
49+
50+
var provider = new CSharpCodeProvider(options);
51+
var results = provider.CompileAssemblyFromSource(parameters, code);
52+
53+
if (results.Errors.Count > 0)
54+
throw new InvalidOperationException();
55+
#endif
56+
return filename;
57+
}
58+
59+
public void Dispose()
60+
{
61+
var files = Directory.GetFiles(_directory, "*.dll")
62+
.Where(x => Guid.TryParse(Path.GetFileNameWithoutExtension(x), out _));
63+
64+
foreach (var file in files)
65+
{
66+
SafeDelete(file);
67+
}
68+
}
69+
70+
private void SafeDelete(string filename)
71+
{
72+
try
73+
{
74+
File.Delete(filename);
75+
}
76+
catch
77+
{
78+
}
79+
}
80+
}
81+
}
Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Linq;
32
using Machine.Fakes;
43
using Machine.Specifications;
54
using Machine.VSTestAdapter.Configuration;
@@ -21,34 +20,36 @@ public class When_adapter_runs_tests : WithFakes
2120
</MSpec>
2221
</RunSettings>";
2322

24-
static MSpecTestAdapter Adapter;
23+
static MSpecTestAdapter adapter;
2524

26-
Establish establish = () => {
27-
The<IRunSettings>().WhenToldTo(runSettings => runSettings.SettingsXml).Return(ConfigurationXml);
28-
The<IRunContext>().WhenToldTo(context => context.RunSettings).Return(The<IRunSettings>());
29-
30-
Adapter = new MSpecTestAdapter(An<ISpecificationDiscoverer>(), The<ISpecificationExecutor>());
31-
};
32-
33-
34-
Because of = () => {
35-
Adapter.RunTests(new[] { "dll" }, The<IRunContext>(), An<IFrameworkHandle>());
36-
};
25+
Establish establish = () =>
26+
{
27+
The<IRunSettings>()
28+
.WhenToldTo(runSettings => runSettings.SettingsXml)
29+
.Return(ConfigurationXml);
3730

31+
The<IRunContext>()
32+
.WhenToldTo(context => context.RunSettings)
33+
.Return(The<IRunSettings>());
3834

39-
It should_pick_up_DisableFullTestNameInIDE = () => {
40-
The<ISpecificationExecutor>().WasToldTo(d => d.RunAssembly("dll",
41-
Param<Settings>.Matches(s => s.DisableFullTestNameInIDE == true),
42-
Param<Uri>.IsAnything,
43-
Param<IFrameworkHandle>.IsAnything));
35+
adapter = new MSpecTestAdapter(An<ISpecificationDiscoverer>(), The<ISpecificationExecutor>());
4436
};
4537

46-
It should_pick_up_DisableFullTestNameInOutput = () => {
47-
The<ISpecificationExecutor>().WasToldTo(d => d.RunAssembly("dll",
48-
Param<Settings>.Matches(s => s.DisableFullTestNameInOutput == true),
49-
Param<Uri>.IsAnything,
50-
Param<IFrameworkHandle>.IsAnything));
51-
};
52-
38+
Because of = () =>
39+
adapter.RunTests(new[] { "dll" }, The<IRunContext>(), An<IFrameworkHandle>());
40+
41+
It should_pick_up_DisableFullTestNameInIDE = () =>
42+
The<ISpecificationExecutor>()
43+
.WasToldTo(d => d.RunAssembly("dll",
44+
Param<Settings>.Matches(s => s.DisableFullTestNameInIDE),
45+
Param<Uri>.IsAnything,
46+
Param<IFrameworkHandle>.IsAnything));
47+
48+
It should_pick_up_DisableFullTestNameInOutput = () =>
49+
The<ISpecificationExecutor>()
50+
.WasToldTo(d => d.RunAssembly("dll",
51+
Param<Settings>.Matches(s => s.DisableFullTestNameInOutput),
52+
Param<Uri>.IsAnything,
53+
Param<IFrameworkHandle>.IsAnything));
5354
}
5455
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Linq;
3-
using Machine.Specifications;
1+
using Machine.Specifications;
42
using Machine.VSTestAdapter.Configuration;
53

64
namespace Machine.VSTestAdapter.Specs.Configuration
@@ -11,13 +9,10 @@ public class When_parsing_configuration_and_mspec_section_is_missing
119
static Settings Settings;
1210
static string ConfigurationXml = "<RunSettings></RunSettings>";
1311

14-
Because of = () => {
12+
Because of = () =>
1513
Settings = Settings.Parse(ConfigurationXml);
16-
};
1714

18-
It should_default_to_DisplayFullTestName_off = () => {
15+
It should_default_to_DisplayFullTestName_off = () =>
1916
Settings.DisableFullTestNameInOutput.ShouldBeFalse();
20-
};
21-
2217
}
2318
}

src/Machine.Specifications.Runner.VisualStudio.Specs/Discovery/BuiltIn/When_discovering_behaviors.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66

77
namespace Machine.VSTestAdapter.Specs.Discovery.BuiltIn
88
{
9-
10-
11-
129
public class When_discovering_specs_using_behaviors : With_DiscoverySetup<BuiltInSpecificationDiscoverer>
1310
{
1411
It should_pick_up_the_behavior = () => {
@@ -29,4 +26,4 @@ public class When_discovering_specs_using_behaviors : With_DiscoverySetup<BuiltI
2926
discoveredSpec.BehaviorFieldType.ShouldEqual("SampleSpecs.SampleBehavior");
3027
};
3128
}
32-
}
29+
}

src/Machine.Specifications.Runner.VisualStudio.Specs/Discovery/BuiltIn/When_discovering_specs.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace Machine.VSTestAdapter.Specs.Discovery.BuiltIn
88
{
9-
109
public class When_discovering_specs : With_DiscoverySetup<BuiltInSpecificationDiscoverer>
1110
{
1211
It should_find_spec = () => {
@@ -36,4 +35,4 @@ public class When_discovering_specs : With_DiscoverySetup<BuiltInSpecificationDi
3635
discoveredSpec.CodeFilePath.ShouldBeNull();
3736
};
3837
}
39-
}
38+
}

src/Machine.Specifications.Runner.VisualStudio.Specs/Discovery/When_DisableFullTestNameInIDE_is_off.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,35 @@
44
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
55
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
66
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
7-
using SampleSpecs;
87
using System.Reflection;
8+
using Machine.VSTestAdapter.Specs.Fixtures;
99

1010
namespace Machine.VSTestAdapter.Specs.Discovery
1111
{
1212
public class When_DisableFullTestNameInIDE_is_off : WithFakes
1313
{
14+
static CompileContext compiler;
15+
static Assembly assembly;
1416

15-
Because of = () => {
16-
The<MSpecTestAdapter>().DiscoverTests(new[] { typeof(When_something).GetTypeInfo().Assembly.Location },
17-
An<IDiscoveryContext>(),
18-
An<IMessageLogger>(),
19-
The<ITestCaseDiscoverySink>());
17+
Establish context = () =>
18+
{
19+
compiler = new CompileContext();
20+
21+
var assemblyPath = compiler.Compile(SampleFixture.Code);
22+
assembly = Assembly.LoadFile(assemblyPath);
2023
};
2124

25+
Because of = () =>
26+
The<MSpecTestAdapter>()
27+
.DiscoverTests(new[] {assembly.GetType("SampleSpecs.When_something").GetTypeInfo().Assembly.Location},
28+
An<IDiscoveryContext>(),
29+
An<IMessageLogger>(),
30+
The<ITestCaseDiscoverySink>());
31+
2232

23-
It should_use_full_type_and_field_name_for_display_name = () => {
33+
It should_use_full_type_and_field_name_for_display_name = () =>
2434
The<ITestCaseDiscoverySink>()
2535
.WasToldTo(d => d.SendTestCase(Param<TestCase>.Matches(t => t.DisplayName.Equals("When something it should pass", StringComparison.Ordinal))))
2636
.OnlyOnce();
27-
};
2837
}
2938
}

src/Machine.Specifications.Runner.VisualStudio.Specs/Discovery/When_DisableFullTestNameInIDE_is_on.cs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
using System;
2-
using System.Linq;
32
using Machine.Fakes;
43
using Machine.Specifications;
54
using Machine.VSTestAdapter.Helpers;
65
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
76
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
87
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
9-
using SampleSpecs;
108
using System.Reflection;
9+
using Machine.VSTestAdapter.Specs.Fixtures;
1110

1211
namespace Machine.VSTestAdapter.Specs.Discovery
1312
{
1413
public class When_DisableFullTestNameInIDE_is_on : WithFakes
1514
{
16-
static string ConfigurationXml = @"<RunSettings>
15+
const string ConfigurationXml = @"<RunSettings>
1716
<RunConfiguration>
1817
<MaxCpuCount>0</MaxCpuCount>
1918
</RunConfiguration>
@@ -22,26 +21,43 @@ public class When_DisableFullTestNameInIDE_is_on : WithFakes
2221
</MSpec>
2322
</RunSettings>";
2423

25-
Establish establish = () => {
26-
The<IRunSettings>().WhenToldTo(runSettings => runSettings.SettingsXml).Return(ConfigurationXml);
27-
The<IDiscoveryContext>().WhenToldTo(context => context.RunSettings).Return(The<IRunSettings>());
24+
static CompileContext compiler;
25+
static Assembly assembly;
26+
27+
Establish context = () =>
28+
{
29+
compiler = new CompileContext();
30+
31+
var assemblyPath = compiler.Compile(SampleFixture.Code);
32+
assembly = Assembly.LoadFile(assemblyPath);
2833
};
2934

35+
Establish establish = () =>
36+
{
37+
The<IRunSettings>()
38+
.WhenToldTo(runSettings => runSettings.SettingsXml)
39+
.Return(ConfigurationXml);
3040

31-
Because of = () => {
32-
The<MSpecTestAdapter>().DiscoverTests(new[] { typeof(StandardSpec).GetTypeInfo().Assembly.Location },
33-
The<IDiscoveryContext>(),
34-
An<IMessageLogger>(),
35-
The<ITestCaseDiscoverySink>());
41+
The<IDiscoveryContext>()
42+
.WhenToldTo(context => context.RunSettings)
43+
.Return(The<IRunSettings>());
3644
};
3745

46+
Because of = () =>
47+
The<MSpecTestAdapter>().DiscoverTests(new[] {assembly.GetType("SampleSpecs.StandardSpec").GetTypeInfo().Assembly.Location},
48+
The<IDiscoveryContext>(),
49+
An<IMessageLogger>(),
50+
The<ITestCaseDiscoverySink>());
51+
3852

39-
It should_use_test_name_as_display_name = () => {
53+
It should_use_test_name_as_display_name = () =>
54+
{
4055
var specId = new VisualStudioTestIdentifier("SampleSpecs.StandardSpec", "should_pass");
4156

4257
The<ITestCaseDiscoverySink>()
43-
.WasToldTo(d => d.SendTestCase(Param<TestCase>.Matches(t => t.ToVisualStudioTestIdentifier().Equals(specId) &&
44-
t.DisplayName.Equals("should pass", StringComparison.Ordinal))))
58+
.WasToldTo(d => d.SendTestCase(Param<TestCase>.Matches(t =>
59+
t.ToVisualStudioTestIdentifier().Equals(specId) &&
60+
t.DisplayName.Equals("should pass", StringComparison.Ordinal))))
4561
.OnlyOnce();
4662
};
4763
}

0 commit comments

Comments
 (0)