You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DotnetInteractiveServiceTest.cs 2.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // DotnetInteractiveServiceTest.cs
  3. using FluentAssertions;
  4. using Xunit;
  5. using Xunit.Abstractions;
  6. namespace AutoGen.DotnetInteractive.Tests;
  7. [Collection("Sequential")]
  8. public class DotnetInteractiveServiceTest : IDisposable
  9. {
  10. private ITestOutputHelper _output;
  11. private InteractiveService _interactiveService;
  12. private string _workingDir;
  13. public DotnetInteractiveServiceTest(ITestOutputHelper output)
  14. {
  15. _output = output;
  16. _workingDir = Path.Combine(Path.GetTempPath(), "test", Path.GetRandomFileName());
  17. if (!Directory.Exists(_workingDir))
  18. {
  19. Directory.CreateDirectory(_workingDir);
  20. }
  21. _interactiveService = new InteractiveService(_workingDir);
  22. _interactiveService.StartAsync(_workingDir, default).Wait();
  23. }
  24. public void Dispose()
  25. {
  26. _interactiveService.Dispose();
  27. }
  28. [Fact]
  29. public async Task ItRunCSharpCodeSnippetTestsAsync()
  30. {
  31. var cts = new CancellationTokenSource();
  32. var isRunning = await _interactiveService.StartAsync(_workingDir, cts.Token);
  33. isRunning.Should().BeTrue();
  34. _interactiveService.IsRunning().Should().BeTrue();
  35. // test code snippet
  36. var hello_world = @"
  37. Console.WriteLine(""hello world"");
  38. ";
  39. await this.TestCSharpCodeSnippet(_interactiveService, hello_world, "hello world");
  40. await this.TestCSharpCodeSnippet(
  41. _interactiveService,
  42. code: @"
  43. Console.WriteLine(""hello world""
  44. ",
  45. expectedOutput: "Error: (2,32): error CS1026: ) expected");
  46. await this.TestCSharpCodeSnippet(
  47. service: _interactiveService,
  48. code: "throw new Exception();",
  49. expectedOutput: "Error: System.Exception: Exception of type 'System.Exception' was thrown");
  50. }
  51. [Fact]
  52. public async Task ItRunPowershellScriptTestsAsync()
  53. {
  54. // test power shell
  55. var ps = @"Write-Output ""hello world""";
  56. await this.TestPowershellCodeSnippet(_interactiveService, ps, "hello world");
  57. }
  58. private async Task TestPowershellCodeSnippet(InteractiveService service, string code, string expectedOutput)
  59. {
  60. var result = await service.SubmitPowershellCodeAsync(code, CancellationToken.None);
  61. result.Should().StartWith(expectedOutput);
  62. }
  63. private async Task TestCSharpCodeSnippet(InteractiveService service, string code, string expectedOutput)
  64. {
  65. var result = await service.SubmitCSharpCodeAsync(code, CancellationToken.None);
  66. result.Should().StartWith(expectedOutput);
  67. }
  68. }