EcmaTest.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using Xunit;
  5. namespace Jint.Tests
  6. {
  7. public class EcmaTest
  8. {
  9. private static string _lastError;
  10. protected Action<string> Error = s => { _lastError = s; };
  11. protected string basePath;
  12. public EcmaTest()
  13. {
  14. var assemblyPath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
  15. var assemblyDirectory = new FileInfo(assemblyPath).Directory;
  16. basePath = assemblyDirectory.Parent.Parent.FullName;
  17. }
  18. protected void RunTest(string sourceFilename, bool negative)
  19. {
  20. _lastError = null;
  21. var fullName = Path.Combine(basePath, sourceFilename);
  22. if (!File.Exists(fullName))
  23. {
  24. throw new ArgumentException("Could not find source file: " + fullName);
  25. }
  26. string code = File.ReadAllText(fullName);
  27. var engine = new Engine(cfg => cfg
  28. .WithDelegate("$ERROR", Error)
  29. );
  30. if (negative)
  31. {
  32. try
  33. {
  34. engine.Execute(code);
  35. Assert.NotNull(_lastError);
  36. Assert.False(true);
  37. }
  38. catch
  39. {
  40. }
  41. }
  42. else
  43. {
  44. Assert.DoesNotThrow(() => engine.Execute(code));
  45. Assert.Null(_lastError);
  46. }
  47. }
  48. }
  49. }