PythonTests.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using NUnit.Framework;
  5. namespace csharp_tests
  6. {
  7. [TestFixture]
  8. [Category("Python")]
  9. public class PythonTests : TestsBase
  10. {
  11. protected override bool generate(string file_path, string expected_exception)
  12. {
  13. ProcessStartInfo start_info = new ProcessStartInfo();
  14. start_info.FileName = "python";
  15. start_info.Arguments = string.Format("../../../python_sccd_compiler/sccdc.py {0} -o {1} -p threads -l C# -v -1", file_path, this.path_generated_code);
  16. start_info.UseShellExecute = false;
  17. start_info.RedirectStandardOutput = true;
  18. //Print any output the compiler gave
  19. using (Process process = Process.Start(start_info))
  20. {
  21. using (StreamReader reader = process.StandardOutput)
  22. {
  23. string result = reader.ReadToEnd();
  24. Console.Write(result);
  25. }
  26. }
  27. //Exception check
  28. //If the test file expects an exception to be thrown by the compiler,
  29. //we just try to compile the model and see if a file got generated.
  30. //If the result file got generated, this means that no exception was thrown
  31. //and thus the test should fail.
  32. bool target_file_exists = File.Exists(this.path_generated_code);
  33. if (expected_exception != null)
  34. {
  35. Assert.AreEqual(false, target_file_exists, "An exception was expected to be thrown by the compiler but the SCCD compiler completed successfully.");
  36. return false; //No target file as expected, we can end the test.
  37. }
  38. Assert.AreEqual(true, target_file_exists, "The SCCD Compiler did not complete compilation. No generated file has been found.");
  39. return true;
  40. }
  41. }
  42. }