Skip to content

Commit dc2d4b9

Browse files
committed
added test project for desktop
1 parent b1f36e5 commit dc2d4b9

File tree

7 files changed

+373
-272
lines changed

7 files changed

+373
-272
lines changed

Dotnet.Script.sln

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dotnet.Script.DependencyMod
1414
EndProject
1515
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dotnet.Script.DependencyModel.NuGet", "src\Dotnet.Script.DependencyModel.Nuget\Dotnet.Script.DependencyModel.NuGet.csproj", "{E361528F-178A-4489-AF01-FFD3A7122D99}"
1616
EndProject
17+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dotnet.Script.Desktop.Tests", "src\Dotnet.Script.Desktop.Tests\Dotnet.Script.Desktop.Tests.csproj", "{B0E5959C-6E7F-4023-96BA-5AD4A5E00541}"
18+
EndProject
19+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dotnet.Script.Shared.Tests", "src\Dotnet.Script.Shared.Tests\Dotnet.Script.Shared.Tests.csproj", "{8FFA2816-411E-437C-AB75-FFA546780E7A}"
20+
EndProject
1721
Global
1822
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1923
Debug|Any CPU = Debug|Any CPU
@@ -96,6 +100,30 @@ Global
96100
{E361528F-178A-4489-AF01-FFD3A7122D99}.Release|x64.Build.0 = Release|Any CPU
97101
{E361528F-178A-4489-AF01-FFD3A7122D99}.Release|x86.ActiveCfg = Release|Any CPU
98102
{E361528F-178A-4489-AF01-FFD3A7122D99}.Release|x86.Build.0 = Release|Any CPU
103+
{B0E5959C-6E7F-4023-96BA-5AD4A5E00541}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
104+
{B0E5959C-6E7F-4023-96BA-5AD4A5E00541}.Debug|Any CPU.Build.0 = Debug|Any CPU
105+
{B0E5959C-6E7F-4023-96BA-5AD4A5E00541}.Debug|x64.ActiveCfg = Debug|Any CPU
106+
{B0E5959C-6E7F-4023-96BA-5AD4A5E00541}.Debug|x64.Build.0 = Debug|Any CPU
107+
{B0E5959C-6E7F-4023-96BA-5AD4A5E00541}.Debug|x86.ActiveCfg = Debug|Any CPU
108+
{B0E5959C-6E7F-4023-96BA-5AD4A5E00541}.Debug|x86.Build.0 = Debug|Any CPU
109+
{B0E5959C-6E7F-4023-96BA-5AD4A5E00541}.Release|Any CPU.ActiveCfg = Release|Any CPU
110+
{B0E5959C-6E7F-4023-96BA-5AD4A5E00541}.Release|Any CPU.Build.0 = Release|Any CPU
111+
{B0E5959C-6E7F-4023-96BA-5AD4A5E00541}.Release|x64.ActiveCfg = Release|Any CPU
112+
{B0E5959C-6E7F-4023-96BA-5AD4A5E00541}.Release|x64.Build.0 = Release|Any CPU
113+
{B0E5959C-6E7F-4023-96BA-5AD4A5E00541}.Release|x86.ActiveCfg = Release|Any CPU
114+
{B0E5959C-6E7F-4023-96BA-5AD4A5E00541}.Release|x86.Build.0 = Release|Any CPU
115+
{8FFA2816-411E-437C-AB75-FFA546780E7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
116+
{8FFA2816-411E-437C-AB75-FFA546780E7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
117+
{8FFA2816-411E-437C-AB75-FFA546780E7A}.Debug|x64.ActiveCfg = Debug|Any CPU
118+
{8FFA2816-411E-437C-AB75-FFA546780E7A}.Debug|x64.Build.0 = Debug|Any CPU
119+
{8FFA2816-411E-437C-AB75-FFA546780E7A}.Debug|x86.ActiveCfg = Debug|Any CPU
120+
{8FFA2816-411E-437C-AB75-FFA546780E7A}.Debug|x86.Build.0 = Debug|Any CPU
121+
{8FFA2816-411E-437C-AB75-FFA546780E7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
122+
{8FFA2816-411E-437C-AB75-FFA546780E7A}.Release|Any CPU.Build.0 = Release|Any CPU
123+
{8FFA2816-411E-437C-AB75-FFA546780E7A}.Release|x64.ActiveCfg = Release|Any CPU
124+
{8FFA2816-411E-437C-AB75-FFA546780E7A}.Release|x64.Build.0 = Release|Any CPU
125+
{8FFA2816-411E-437C-AB75-FFA546780E7A}.Release|x86.ActiveCfg = Release|Any CPU
126+
{8FFA2816-411E-437C-AB75-FFA546780E7A}.Release|x86.Build.0 = Release|Any CPU
99127
EndGlobalSection
100128
GlobalSection(SolutionProperties) = preSolution
101129
HideSolutionNode = FALSE
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net461</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
9+
<PackageReference Include="xunit" Version="2.3.1" />
10+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
11+
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\Dotnet.Script.Core\Dotnet.Script.Core.csproj" />
16+
<ProjectReference Include="..\Dotnet.Script.DependencyModel\Dotnet.Script.DependencyModel.csproj" />
17+
<ProjectReference Include="..\Dotnet.Script.Shared.Tests\Dotnet.Script.Shared.Tests.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Xunit;
2+
using Dotnet.Script.DependencyModel.Environment;
3+
using Dotnet.Script.Shared.Tests;
4+
5+
namespace Dotnet.Script.Desktop.Tests
6+
{
7+
[Collection("IntegrationTests")]
8+
public class InteractiveRunnerTests : InteractiveRunnerTestsBase
9+
{
10+
public InteractiveRunnerTests()
11+
{
12+
ScriptEnvironment.Default.OverrideTargetFramework("net461");
13+
}
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
9+
<PackageReference Include="xunit" Version="2.3.1" />
10+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\Dotnet.Script.Core\Dotnet.Script.Core.csproj" />
15+
<ProjectReference Include="..\Dotnet.Script.DependencyModel\Dotnet.Script.DependencyModel.csproj" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
using Dotnet.Script.Core;
2+
using Dotnet.Script.DependencyModel.Runtime;
3+
using Dotnet.Script.DependencyModel.Logging;
4+
using System.Threading.Tasks;
5+
using Xunit;
6+
using System.IO;
7+
using System.Text;
8+
using System;
9+
using Microsoft.CodeAnalysis.Text;
10+
using Dotnet.Script.DependencyModel.Context;
11+
using Dotnet.Script.DependencyModel.Environment;
12+
13+
namespace Dotnet.Script.Shared.Tests
14+
{
15+
public abstract class InteractiveRunnerTestsBase
16+
{
17+
private class InteractiveTestContext
18+
{
19+
public InteractiveTestContext(ScriptConsole console, InteractiveRunner runner)
20+
{
21+
Console = console;
22+
Runner = runner;
23+
}
24+
25+
public ScriptConsole Console { get; }
26+
public InteractiveRunner Runner { get; }
27+
}
28+
29+
private InteractiveTestContext GetRunner(string[] commands)
30+
{
31+
var reader = new StringReader(string.Join(Environment.NewLine, commands));
32+
var writer = new StringWriter();
33+
var error = new StringWriter();
34+
35+
var console = new ScriptConsole(writer, reader, error);
36+
var logger = new ScriptLogger(console.Error, true);
37+
var runtimeDependencyResolver = new RuntimeDependencyResolver(type => ((level, message) =>
38+
{
39+
if (level == LogLevel.Debug)
40+
{
41+
logger.Verbose(message);
42+
}
43+
if (level == LogLevel.Info)
44+
{
45+
logger.Log(message);
46+
}
47+
}));
48+
49+
var compiler = new ScriptCompiler(logger, runtimeDependencyResolver);
50+
var runner = new InteractiveRunner(compiler, logger, console);
51+
return new InteractiveTestContext(console, runner);
52+
}
53+
54+
[Fact]
55+
public async Task SimpleOutput()
56+
{
57+
var commands = new[]
58+
{
59+
"var x = 1;",
60+
"x+x",
61+
"#exit"
62+
};
63+
64+
var ctx = GetRunner(commands);
65+
await ctx.Runner.RunLoop();
66+
67+
var result = ctx.Console.Out.ToString();
68+
Assert.Contains("2", result);
69+
}
70+
71+
[Fact]
72+
public async Task RuntimeException()
73+
{
74+
var commands = new[]
75+
{
76+
"foo",
77+
"#exit"
78+
};
79+
80+
var ctx = GetRunner(commands);
81+
await ctx.Runner.RunLoop();
82+
83+
var result = ctx.Console.Error.ToString();
84+
Assert.Contains("(1,1): error CS0103: The name 'foo' does not exist in the current context", result);
85+
}
86+
87+
[Fact]
88+
public async Task ValueFromSeededFile()
89+
{
90+
var commands = new[]
91+
{
92+
"x+x",
93+
"#exit"
94+
};
95+
96+
var ctx = GetRunner(commands);
97+
await ctx.Runner.RunLoopWithSeed(new ScriptContext(SourceText.From(@"var x = 1;"), Directory.GetCurrentDirectory(), new string[0], scriptMode: ScriptMode.REPL));
98+
99+
var result = ctx.Console.Out.ToString();
100+
Assert.Contains("2", result);
101+
}
102+
103+
[Fact]
104+
public async Task RuntimeExceptionFromSeededFile()
105+
{
106+
var commands = new[]
107+
{
108+
"var x = 1;",
109+
"x+x",
110+
"#exit"
111+
};
112+
113+
var ctx = GetRunner(commands);
114+
await ctx.Runner.RunLoopWithSeed(new ScriptContext(SourceText.From(@"throw new Exception(""die!"");"), Directory.GetCurrentDirectory(), new string[0], scriptMode: ScriptMode.REPL));
115+
116+
var errorResult = ctx.Console.Error.ToString();
117+
var result = ctx.Console.Out.ToString();
118+
Assert.Contains("2", result);
119+
Assert.Contains("die!", errorResult);
120+
}
121+
122+
[Fact]
123+
public async Task Multiline()
124+
{
125+
var commands = new[]
126+
{
127+
"class Foo {",
128+
"}",
129+
"var x = new Foo();",
130+
"x",
131+
"#exit"
132+
};
133+
134+
var ctx = GetRunner(commands);
135+
await ctx.Runner.RunLoop();
136+
137+
var result = ctx.Console.Out.ToString();
138+
Assert.Contains("Submission#0.Foo", result);
139+
}
140+
141+
[Fact]
142+
public async Task ExtensionMethod()
143+
{
144+
var commands = new[]
145+
{
146+
@"var x = ""foo"";",
147+
@"static string SayHi(this string txt) { return $""hi, {txt}""; }",
148+
"x.SayHi()",
149+
"#exit"
150+
};
151+
152+
var ctx = GetRunner(commands);
153+
await ctx.Runner.RunLoop();
154+
155+
var result = ctx.Console.Out.ToString();
156+
Assert.Contains("hi, foo", result);
157+
}
158+
159+
[Fact]
160+
public async Task GlobalsObject()
161+
{
162+
var commands = new[]
163+
{
164+
@"var x = ""foo"";",
165+
"Print(x);",
166+
"#exit"
167+
};
168+
169+
var ctx = GetRunner(commands);
170+
await ctx.Runner.RunLoop();
171+
172+
var result = ctx.Console.Out.ToString();
173+
Assert.Contains("foo", result);
174+
}
175+
176+
[Fact]
177+
public async Task NugetPackageReference()
178+
{
179+
var commands = new[]
180+
{
181+
"var x = 1;",
182+
@"#r ""nuget: Automapper, 6.1.1""",
183+
"using AutoMapper;",
184+
"typeof(MapperConfiguration)",
185+
"#exit"
186+
};
187+
188+
var ctx = GetRunner(commands);
189+
await ctx.Runner.RunLoop();
190+
191+
var result = ctx.Console.Out.ToString();
192+
Assert.Contains("[AutoMapper.MapperConfiguration]", result);
193+
}
194+
195+
[Fact]
196+
public async Task ScriptPackageReference()
197+
{
198+
var commands = new[]
199+
{
200+
"var x = 1;",
201+
@"#load ""nuget: simple-targets-csx, 6.0.0""",
202+
"using static SimpleTargets;",
203+
"typeof(TargetDictionary)",
204+
"#exit"
205+
};
206+
207+
var ctx = GetRunner(commands);
208+
await ctx.Runner.RunLoop();
209+
var result = ctx.Console.Out.ToString();
210+
Assert.Contains("[Submission#1+SimpleTargets+TargetDictionary]", result);
211+
}
212+
213+
[Fact]
214+
public async Task LoadedFile()
215+
{
216+
var pathToFixture = Path.Combine("..", "..", "..", "..", "DotNet.Script.Tests", "TestFixtures", "REPL", "main.csx");
217+
var commands = new[]
218+
{
219+
"var x = 5;",
220+
$@"#load ""{pathToFixture}""",
221+
"x * externalValue",
222+
"#exit"
223+
};
224+
225+
var ctx = GetRunner(commands);
226+
await ctx.Runner.RunLoop();
227+
228+
var result = ctx.Console.Out.ToString();
229+
Assert.Contains("500", result);
230+
}
231+
232+
[Fact]
233+
public async Task ResetCommand()
234+
{
235+
var commands = new[]
236+
{
237+
"var x = 1;",
238+
"x+x",
239+
"#reset",
240+
"x",
241+
"#exit"
242+
};
243+
244+
var ctx = GetRunner(commands);
245+
await ctx.Runner.RunLoop();
246+
247+
var result = ctx.Console.Out.ToString();
248+
Assert.Contains("2", result);
249+
250+
var errResult = ctx.Console.Error.ToString();
251+
Assert.Contains("error CS0103: The name 'x' does not exist in the current context", errResult);
252+
}
253+
254+
[Fact]
255+
public async Task NugetPackageReferenceAsTheFirstLine()
256+
{
257+
var commands = new[]
258+
{
259+
@"#r ""nuget: Automapper, 6.1.1""",
260+
"using AutoMapper;",
261+
"typeof(MapperConfiguration)",
262+
"#exit"
263+
};
264+
265+
var ctx = GetRunner(commands);
266+
await ctx.Runner.RunLoop();
267+
268+
var result = ctx.Console.Out.ToString();
269+
Assert.Contains("[AutoMapper.MapperConfiguration]", result);
270+
}
271+
272+
[Fact]
273+
public async Task ScriptPackageReferenceAsTheFirstLine()
274+
{
275+
var commands = new[]
276+
{
277+
@"#load ""nuget: simple-targets-csx, 6.0.0""",
278+
"using static SimpleTargets;",
279+
"typeof(TargetDictionary)",
280+
"#exit"
281+
};
282+
283+
var ctx = GetRunner(commands);
284+
await ctx.Runner.RunLoop();
285+
var result = ctx.Console.Out.ToString();
286+
Assert.Contains("[Submission#0+SimpleTargets+TargetDictionary]", result);
287+
}
288+
}
289+
}

src/Dotnet.Script.Tests/Dotnet.Script.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<ItemGroup>
1616
<ProjectReference Include="..\Dotnet.Script.Core\Dotnet.Script.Core.csproj" />
1717
<ProjectReference Include="..\Dotnet.Script.DependencyModel\Dotnet.Script.DependencyModel.csproj" />
18+
<ProjectReference Include="..\Dotnet.Script.Shared.Tests\Dotnet.Script.Shared.Tests.csproj" />
1819
<ProjectReference Include="..\Dotnet.Script\Dotnet.Script.csproj" />
1920
</ItemGroup>
2021

0 commit comments

Comments
 (0)