TextTests.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. using Jint.Runtime.Interop;
  2. using Jint.Tests.Runtime.Domain;
  3. namespace Jint.Tests.Runtime;
  4. public sealed class TextTests
  5. {
  6. private readonly Engine _engine;
  7. public TextTests()
  8. {
  9. _engine = new Engine()
  10. .SetValue("log", new Action<object>(Console.WriteLine))
  11. .SetValue("assert", new Action<bool>(Assert.True))
  12. .SetValue("equal", new Action<object, object>(Assert.Equal));
  13. _engine
  14. .SetValue("TextDecoder", TypeReference.CreateTypeReference<TextDecoder>(_engine))
  15. ;
  16. }
  17. private object RunTest(string source)
  18. {
  19. return _engine.Evaluate(source).ToObject();
  20. }
  21. [Fact]
  22. public void CanDecode()
  23. {
  24. Assert.Equal("", RunTest($"new TextDecoder().decode()"));
  25. Assert.Equal("foo", RunTest($"new TextDecoder().decode(new Uint8Array([102,111,111]))"));
  26. Assert.Equal("foo", RunTest($"new TextDecoder().decode(new Uint8Array([102,111,111]).buffer)"));
  27. Assert.Equal("foo", RunTest($"new TextDecoder().decode(new DataView(new Uint8Array([0,102,111,111,0]).buffer,1,3))"));
  28. }
  29. }