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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. var isRunning = _interactiveService.StartAsync(_workingDir, default).Result;
  23. isRunning.Should().BeTrue();
  24. }
  25. public void Dispose()
  26. {
  27. _interactiveService.Dispose();
  28. }
  29. [Fact]
  30. public async Task ItRunCSharpCodeSnippetTestsAsync()
  31. {
  32. // test code snippet
  33. var hello_world = @"
  34. Console.WriteLine(""hello world"");
  35. ";
  36. await this.TestCSharpCodeSnippet(_interactiveService, hello_world, "hello world");
  37. await this.TestCSharpCodeSnippet(
  38. _interactiveService,
  39. code: @"
  40. Console.WriteLine(""hello world""
  41. ",
  42. expectedOutput: "Error: (2,32): error CS1026: ) expected");
  43. await this.TestCSharpCodeSnippet(
  44. service: _interactiveService,
  45. code: "throw new Exception();",
  46. expectedOutput: "Error: System.Exception: Exception of type 'System.Exception' was thrown");
  47. }
  48. [Fact]
  49. public async Task ItRunPowershellScriptTestsAsync()
  50. {
  51. // test power shell
  52. var ps = @"Write-Output ""hello world""";
  53. await this.TestPowershellCodeSnippet(_interactiveService, ps, "hello world");
  54. }
  55. private async Task TestPowershellCodeSnippet(InteractiveService service, string code, string expectedOutput)
  56. {
  57. var result = await service.SubmitPowershellCodeAsync(code, CancellationToken.None);
  58. result.Should().StartWith(expectedOutput);
  59. }
  60. private async Task TestCSharpCodeSnippet(InteractiveService service, string code, string expectedOutput)
  61. {
  62. var result = await service.SubmitCSharpCodeAsync(code, CancellationToken.None);
  63. result.Should().StartWith(expectedOutput);
  64. }
  65. }