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

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