UuidTests.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using Jint.Tests.Runtime.Domain;
  2. namespace Jint.Tests.Runtime;
  3. public class UuidTests : IDisposable
  4. {
  5. private readonly Engine _engine;
  6. public UuidTests()
  7. {
  8. _engine = new Engine(o => o.AddObjectConverter(new UuidConverter()))
  9. .SetValue("copy", new Func<Guid, Guid>(v => new Guid(v.ToByteArray())))
  10. ;
  11. UuidConstructor.CreateUuidConstructor(_engine);
  12. }
  13. void IDisposable.Dispose()
  14. {
  15. }
  16. private object RunTest(string source)
  17. {
  18. return _engine.Evaluate(source).ToObject();
  19. }
  20. [Fact]
  21. public void Empty()
  22. {
  23. Assert.Equal(Guid.Empty, RunTest($"Uuid.parse('{Guid.Empty}')"));
  24. Assert.Equal(Guid.Empty, RunTest($"Uuid.Empty"));
  25. }
  26. [Fact]
  27. public void Random()
  28. {
  29. var actual = RunTest($"new Uuid()");
  30. Assert.NotEqual(Guid.Empty, actual);
  31. Assert.IsType<Guid>(actual);
  32. }
  33. [Fact]
  34. public void Copy()
  35. {
  36. _engine.Evaluate("const g = new Uuid();");
  37. Assert.Equal(_engine.Evaluate("copy(g).toString()").AsString(), _engine.Evaluate("g.toString()").AsString());
  38. }
  39. }