DestructuringTests.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. namespace Jint.Tests.Runtime;
  2. public class DestructuringTests
  3. {
  4. private readonly Engine _engine;
  5. public DestructuringTests()
  6. {
  7. _engine = new Engine()
  8. .SetValue("equal", new Action<object, object>(Assert.Equal));
  9. }
  10. [Fact]
  11. public void WithParameterStrings()
  12. {
  13. const string Script = @"
  14. return function([a, b, c]) {
  15. equal('a', a);
  16. equal('b', b);
  17. return c === void undefined;
  18. }('ab');";
  19. Assert.True(_engine.Evaluate(Script).AsBoolean());
  20. }
  21. [Fact]
  22. public void WithParameterObjectPrimitives()
  23. {
  24. const string Script = @"
  25. return function({toFixed}, {slice}) {
  26. equal(Number.prototype.toFixed, toFixed);
  27. equal(String.prototype.slice, slice);
  28. return true;
  29. }(2,'');";
  30. Assert.True(_engine.Evaluate(Script).AsBoolean());
  31. }
  32. [Fact]
  33. public void WithParameterComputedProperties()
  34. {
  35. const string Script = @"
  36. var qux = 'corge';
  37. return function({ [qux]: grault }) {
  38. equal('garply', grault);
  39. }({ corge: 'garply' });";
  40. _engine.Execute(Script);
  41. }
  42. [Fact]
  43. public void WithParameterFunctionLengthProperty()
  44. {
  45. _engine.Execute("equal(0, ((x = 42, y) => {}).length);");
  46. _engine.Execute("equal(1, ((x, y = 42, z) => {}).length);");
  47. _engine.Execute("equal(1, ((a, b = 39,) => {}).length);");
  48. _engine.Execute("equal(2, function({a, b}, [c, d]){}.length);");
  49. }
  50. [Fact]
  51. public void WithNestedRest()
  52. {
  53. _engine.Execute("return function([x, ...[y, ...z]]) { equal(1, x); equal(2, y); equal('3,4', z + ''); }([1, 2, 3, 4]);");
  54. }
  55. [Fact]
  56. public void EmptyRest()
  57. {
  58. _engine.Execute("function test({ ...props }){}; test({});");
  59. }
  60. }