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.6 kB

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