diff --git a/LibTest/js_interop/JSAnyOperatorExtension/add_t01.dart b/LibTest/js_interop/JSAnyOperatorExtension/add_t01.dart index 9585352df9..c8883baf71 100644 --- a/LibTest/js_interop/JSAnyOperatorExtension/add_t01.dart +++ b/LibTest/js_interop/JSAnyOperatorExtension/add_t01.dart @@ -6,37 +6,49 @@ /// The result of `this + any` in JavaScript. /// /// @description Check that `add` returns result of `this + any` in JavaScript. -/// Test an empty JS object as `this`. /// @author sgrekhov22@gmail.com import 'dart:js_interop'; import 'dart:js_interop_unsafe'; -import '../../../Utils/expect.dart'; import '../js_utils.dart'; -main() { +testAdd(JSAny? underTest) { eval(r''' - var object = {}; - var one = object + 1; // String "[object Object]1" - var string = object + "s"; // String "[object Object]s" - var a1 = object + []; // String "[object Object]" - var a2 = object + [1, 2]; // String "[object Object]1,2" - var o1 = object + {}; // String "[object Object][object Object]" - var o2 = object + {"a": "b"}; // String "[object Object][object Object]" - var n = object + null; // String "[object Object]null" - var b1 = object + true; // String "[object Object]true" - var b2 = object + false; // String "[object Object]false" + var addNum = underTest + 2; + var addString = underTest + "text"; + var addNull = underTest + null; + var addNaN = underTest + (0 / 0); + var addArray = underTest + [1, 2]; + var addTrue = underTest + true; '''); - JSObject o = JSObject(); - Expect.equals(globalContext["one"], o.add(1.toJS)); - Expect.equals(globalContext["string"], o.add("s".toJS)); - Expect.equals(globalContext["a1"], o.add([].jsify())); - Expect.equals(globalContext["a2"], o.add([1, 2].jsify())); - Expect.equals(globalContext["o1"], o.add(JSObject())); - Expect.equals(globalContext["o2"], o.add({"a": "b"}.jsify())); - Expect.equals(globalContext["n"], o.add(null.jsify())); - Expect.equals(globalContext["n"], o.add(null)); - Expect.equals(globalContext["b1"], o.add(true.toJS)); - Expect.equals(globalContext["b2"], o.add(false.toJS)); + jsExpectEquals(globalContext["addNum"], underTest.add(2.toJS)); + jsExpectEquals(globalContext["addString"], underTest.add("text".toJS)); + jsExpectEquals(globalContext["addNull"], underTest.add(null)); + jsExpectEquals(globalContext["addNaN"], underTest.add((0 / 0).toJS)); + jsExpectEquals(globalContext["addArray"], underTest.add([1, 2].jsify())); + jsExpectEquals(globalContext["addTrue"], underTest.add(true.toJS)); +} + +main() { + eval("globalThis.underTest = 42;"); + testAdd(42.toJS); + + eval("globalThis.underTest = 'Some text';"); + testAdd('Some text'.toJS); + + eval("globalThis.underTest = null;"); + testAdd(null); + + eval("globalThis.underTest = 0 / 0;"); + testAdd((0 / 0).jsify()); + + eval("globalThis.underTest = [];"); + testAdd([].jsify()); + + eval("globalThis.underTest = false;"); + testAdd(false.toJS); + + eval("globalThis.underTest = {};"); + testAdd(JSObject()); } diff --git a/LibTest/js_interop/JSAnyOperatorExtension/add_t02.dart b/LibTest/js_interop/JSAnyOperatorExtension/add_t02.dart deleted file mode 100644 index e09ec65f9c..0000000000 --- a/LibTest/js_interop/JSAnyOperatorExtension/add_t02.dart +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -/// @assertion JSAny add( JSAny? any ) -/// The result of `this + any` in JavaScript. -/// -/// @description Check that `add` returns result of `this + any` in JavaScript. -/// Test a number as `this`. -/// @author sgrekhov22@gmail.com - -import 'dart:js_interop'; -import 'dart:js_interop_unsafe'; -import '../../../Utils/expect.dart'; -import '../js_utils.dart'; - -test(JSAny o) { - eval("var o = $o;"); - eval(r''' - // If o == 42, then: - var one = o + 1; // 43 - var string = o + "s"; // String "42s" - var a1 = o + []; // String "42" - var a2 = o + [1, 2]; // String "421,2" - var o1 = o + {}; // String "42[object Object]" - var o2 = o + {"a": "b"}; // String "42[object Object]" - var n = o + null; // 42 - var b1 = o + true; // 43 - var b2 = o + false; // 42 - '''); - - Expect.equals(globalContext["one"], o.add(1.toJS)); - Expect.equals(globalContext["string"], o.add("s".toJS)); - Expect.equals(globalContext["a1"], o.add([].jsify())); - Expect.equals(globalContext["a2"], o.add([1, 2].jsify())); - Expect.equals(globalContext["o1"], o.add(JSObject())); - Expect.equals(globalContext["o1"], o.add({"a": "b"}.jsify())); - Expect.equals(globalContext["n"], o.add(null.jsify())); - Expect.equals(globalContext["n"], o.add(null)); - Expect.equals(globalContext["b1"], o.add(true.toJS)); - Expect.equals(globalContext["b2"], o.add(false.toJS)); -} - -main() { - test(0.toJS); - test((-1).toJS); - test(42.toJS); - test(3.14.toJS); -} diff --git a/LibTest/js_interop/JSAnyOperatorExtension/add_t03.dart b/LibTest/js_interop/JSAnyOperatorExtension/add_t03.dart deleted file mode 100644 index 56f4e60d07..0000000000 --- a/LibTest/js_interop/JSAnyOperatorExtension/add_t03.dart +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -/// @assertion JSAny add( JSAny? any ) -/// The result of `this + any` in JavaScript. -/// -/// @description Check that `add` returns result of `this + any` in JavaScript. -/// Test a string as `this`. -/// @author sgrekhov22@gmail.com - -import 'dart:js_interop'; -import 'dart:js_interop_unsafe'; -import '../../../Utils/expect.dart'; -import '../js_utils.dart'; - -main() { - eval(r''' - var object = 's'; - var one = object + 1; // String "s1" - var string = object + "s"; // String "ss" - var a1 = object + []; // String "s" - var a2 = object + [1, 2]; // String "s1,2" - var o1 = object + {}; // String "s[object Object]" - var o2 = object + {"a": "b"}; // String "s[object Object]" - var n = object + null; // String "snull" - var b1 = object + true; // String "strue" - var b2 = object + false; // String "sfalse" - '''); - - final s = "s".toJS; - Expect.equals(globalContext["one"], s.add(1.toJS)); - Expect.equals(globalContext["string"], s.add("s".toJS)); - Expect.equals(globalContext["a1"], s.add([].jsify())); - Expect.equals(globalContext["a2"], s.add([1, 2].jsify())); - Expect.equals(globalContext["o1"], s.add(JSObject())); - Expect.equals(globalContext["o2"], s.add({"a": "b"}.jsify())); - Expect.equals(globalContext["n"], s.add(null.jsify())); - Expect.equals(globalContext["n"], s.add(null)); - Expect.equals(globalContext["b1"], s.add(true.toJS)); - Expect.equals(globalContext["b2"], s.add(false.toJS)); -} diff --git a/LibTest/js_interop/JSAnyOperatorExtension/add_t04.dart b/LibTest/js_interop/JSAnyOperatorExtension/add_t04.dart deleted file mode 100644 index 80fe028c10..0000000000 --- a/LibTest/js_interop/JSAnyOperatorExtension/add_t04.dart +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -/// @assertion JSAny add( JSAny? any ) -/// The result of `this + any` in JavaScript. -/// -/// @description Check that `add` returns result of `this + any` in JavaScript. -/// Test a `bool` as `this`. -/// @author sgrekhov22@gmail.com - -import 'dart:js_interop'; -import 'dart:js_interop_unsafe'; -import '../../../Utils/expect.dart'; -import '../js_utils.dart'; - -test(JSAny b) { - eval("var b = $b;"); - eval(r''' - // If b == true, then: - var one = b + 1; // 2 - var string = b + "s"; // String "trues" - var a1 = b + []; // String "true" - var a2 = b + [1, 2]; // String "true1,2" - var o1 = b + {}; // String "true[object Object]" - var o2 = b + {"a": "b"}; // String "true[object Object]" - var n = b + null; // 1 - var b1 = b + true; // 2 - var b2 = b + false; // 1 - '''); - - Expect.equals(globalContext["one"], b.add(1.toJS)); - Expect.equals(globalContext["string"], b.add("s".toJS)); - Expect.equals(globalContext["a1"], b.add([].jsify())); - Expect.equals(globalContext["a2"], b.add([1, 2].jsify())); - Expect.equals(globalContext["o1"], b.add(JSObject())); - Expect.equals(globalContext["o2"], b.add({"a": "b"}.jsify())); - Expect.equals(globalContext["n"], b.add(null.jsify())); - Expect.equals(globalContext["n"], b.add(null)); - Expect.equals(globalContext["b1"], b.add(true.toJS)); - Expect.equals(globalContext["b2"], b.add(false.toJS)); -} - -main() { - test(true.toJS); - test(false.toJS); -} diff --git a/LibTest/js_interop/JSAnyOperatorExtension/add_t05.dart b/LibTest/js_interop/JSAnyOperatorExtension/add_t05.dart deleted file mode 100644 index 4f576016f2..0000000000 --- a/LibTest/js_interop/JSAnyOperatorExtension/add_t05.dart +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -/// @assertion JSAny add( JSAny? any ) -/// The result of `this + any` in JavaScript. -/// -/// @description Check that `add` returns result of `this + any` in JavaScript. -/// Test `null` as `this`. -/// @author sgrekhov22@gmail.com - -import 'dart:js_interop'; -import 'dart:js_interop_unsafe'; -import '../../../Utils/expect.dart'; -import '../js_utils.dart'; - -test(JSAny? n) { - eval(r''' - var object = null; - var one = object + 1; // 1 - var string = object + "s"; // String "nulls" - var a1 = object + []; // String "null" - var a2 = object + [1, 2]; // String "null1,2" - var o1 = object + {}; // String "null[object Object]" - var o2 = object + {"a": "b"}; // String "null[object Object]" - var n = object + null; // 0 - var b1 = object + true; // 1 - var b2 = object + false; // 0 - '''); - - Expect.equals(globalContext["one"], n.add(1.toJS)); - Expect.equals(globalContext["string"], n.add("s".toJS)); - Expect.equals(globalContext["a1"], n.add([].jsify())); - Expect.equals(globalContext["a2"], n.add([1, 2].jsify())); - Expect.equals(globalContext["o1"], n.add(JSObject())); - Expect.equals(globalContext["o2"], n.add({"a": "b"}.jsify())); - Expect.equals(globalContext["n"], n.add(null.jsify())); - Expect.equals(globalContext["n"], n.add(null)); - Expect.equals(globalContext["b1"], n.add(true.toJS)); - Expect.equals(globalContext["b2"], n.add(false.toJS)); -} - -main() { - test(null.jsify()); - test(null); -} diff --git a/LibTest/js_interop/JSAnyOperatorExtension/add_t06.dart b/LibTest/js_interop/JSAnyOperatorExtension/add_t06.dart deleted file mode 100644 index c18909522d..0000000000 --- a/LibTest/js_interop/JSAnyOperatorExtension/add_t06.dart +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -/// @assertion JSAny add( JSAny? any ) -/// The result of `this + any` in JavaScript. -/// -/// @description Check that `add` returns result of `this + any` in JavaScript. -/// Test an array as `this`. -/// @author sgrekhov22@gmail.com - -import 'dart:js_interop'; -import 'dart:js_interop_unsafe'; -import '../../../Utils/expect.dart'; -import '../js_utils.dart'; - -test(JSAny? a, String s) { - eval("var object = $s;"); - eval(r''' - // If object == [1,2] then: - var one = object + 1; // String "1,21" - var string = object + "s"; // String "1,2s" - var a1 = object + []; // String "1,2" - var a2 = object + [1, 2]; // String "1,21,2" - var o1 = object + {}; // String "1,2[object Object]" - var o2 = object + {"a": "b"}; // String "1,2[object Object]" - var n = object + null; // String "1,2null" - var b1 = object + true; // String "1,2true" - var b2 = object + false; // String "1,2false" - '''); - - Expect.equals(globalContext["one"], a.add(1.toJS)); - Expect.equals(globalContext["string"], a.add("s".toJS)); - Expect.equals(globalContext["a1"], a.add([].jsify())); - Expect.equals(globalContext["a2"], a.add([1, 2].jsify())); - Expect.equals(globalContext["o1"], a.add(JSObject())); - Expect.equals(globalContext["o2"], a.add({"a": "b"}.jsify())); - Expect.equals(globalContext["n"], a.add(null.jsify())); - Expect.equals(globalContext["n"], a.add(null)); - Expect.equals(globalContext["b1"], a.add(true.toJS)); - Expect.equals(globalContext["b2"], a.add(false.toJS)); -} - -main() { - test([].jsify(), "[]"); - test([1, 2].jsify(), "[1, 2]"); -} diff --git a/LibTest/js_interop/JSAnyOperatorExtension/add_t07.dart b/LibTest/js_interop/JSAnyOperatorExtension/add_t07.dart deleted file mode 100644 index b0dbbe5a7f..0000000000 --- a/LibTest/js_interop/JSAnyOperatorExtension/add_t07.dart +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -/// @assertion JSAny add( JSAny? any ) -/// The result of `this + any` in JavaScript. -/// -/// @description Check that `add` returns result of `this + any` in JavaScript. -/// Test a function as `this`. -/// @author sgrekhov22@gmail.com - -import 'dart:js_interop'; -import 'dart:js_interop_unsafe'; -import '../../../Utils/expect.dart'; -import '../js_utils.dart'; - -main() { - eval(r''' - function foo() {} - - var one = foo + 1; // String "function foo() {}1" - var string = foo + "s"; // String "function foo() {}s" - var a1 = foo + [1, 2]; // String "function foo() {}1,2" - var o1 = foo + {}; // String "function foo() {}[object Object]" - var o2 = foo + {"a": "b"}; // String "function foo() {}[object Object]" - var n = foo + null; // String "function foo() {}null" - var b1 = foo + true; // String "function foo() {}true" - var b2 = foo + false; // String "function foo() {}false" - '''); - - var f = globalContext["foo"]; - - Expect.equals(globalContext["one"], f.add(1.toJS)); - Expect.equals(globalContext["string"], f.add("s".toJS)); - Expect.equals(globalContext["a1"], f.add([1, 2].jsify())); - Expect.equals(globalContext["o1"], f.add(JSObject())); - Expect.equals(globalContext["o2"], f.add({"a": "b"}.jsify())); - Expect.equals(globalContext["n"], f.add(null.jsify())); - Expect.equals(globalContext["n"], f.add(null)); - Expect.equals(globalContext["b1"], f.add(true.toJS)); - Expect.equals(globalContext["b2"], f.add(false.toJS)); -} diff --git a/LibTest/js_interop/JSAnyOperatorExtension/add_t08.dart b/LibTest/js_interop/JSAnyOperatorExtension/add_t08.dart deleted file mode 100644 index 276553f64d..0000000000 --- a/LibTest/js_interop/JSAnyOperatorExtension/add_t08.dart +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -/// @assertion JSAny add( JSAny? any ) -/// The result of `this + any` in JavaScript. -/// -/// @description Check that `add` returns result of `this + any` in JavaScript. -/// Test a function as `any`. -/// @author sgrekhov22@gmail.com - -import 'dart:js_interop'; -import 'dart:js_interop_unsafe'; -import '../../../Utils/expect.dart'; -import '../js_utils.dart'; - -main() { - eval(r''' - function foo() {} - - var one = 1 + foo; // String "1function foo() {}" - var string = "s" + foo; // String "sfunction foo() {}" - var a1 = [1, 2] + foo; // String "1,2function foo() {}" - var o1 = {} + foo; // String "[object Object]function foo() {}" - var o2 = {"a": "b"} + foo; // String "[object Object]function foo() {}" - var n = null + foo; // String "nullfunction foo() {}null" - var b1 = true + foo; // String "truefunction foo() {}" - var b2 = false + foo; // String "falsefunction foo() {}" - '''); - - var f = globalContext["foo"]; - - Expect.equals(globalContext["one"], 1.toJS.add(f)); - Expect.equals(globalContext["string"], "s".toJS.add(f)); - Expect.equals(globalContext["a1"], [1, 2].jsify().add(f)); - Expect.equals(globalContext["o1"], JSObject().add(f)); - Expect.equals(globalContext["o2"], {"a": "b"}.jsify().add(f)); - Expect.equals(globalContext["n"], null.jsify().add(f)); - Expect.equals(globalContext["n"], null.add(f)); - Expect.equals(globalContext["b1"], true.toJS.add(f)); - Expect.equals(globalContext["b2"], false.toJS.add(f)); -} diff --git a/LibTest/js_interop/JSAnyOperatorExtension/and_t01.dart b/LibTest/js_interop/JSAnyOperatorExtension/and_t01.dart new file mode 100644 index 0000000000..00296c418d --- /dev/null +++ b/LibTest/js_interop/JSAnyOperatorExtension/and_t01.dart @@ -0,0 +1,49 @@ +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion JSAny and( JSAny? any ) +/// The result of `this && any` in JavaScript. +/// +/// @description Check that `and` returns result of `this && any` in JavaScript. +/// Test 'truthy' first operand. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; +import '../js_utils.dart'; + +testAnd(JSAny? underTest) { + eval(r''' + var andNum = underTest && 2; + var andString = underTest && "text"; + var andNull = underTest && null; + var andNaN = underTest && (0 / 0); + var andArray = underTest && [1, 2]; + var andTrue = underTest && true; + '''); + + jsExpectEquals(globalContext["andNum"], underTest.and(2.toJS)); + jsExpectEquals(globalContext["andString"], underTest.and("text".toJS)); + jsExpectEquals(globalContext["andNull"], underTest.and(null)); + jsExpectEquals(globalContext["andNaN"], underTest.and((0 / 0).toJS)); + jsExpectArrayEquals(globalContext["andArray"], underTest.and([1, 2].jsify())); + jsExpectEquals(globalContext["andTrue"], underTest.and(true.toJS)); +} + +main() { + eval("globalThis.underTest = 42;"); + testAnd(42.toJS); + + eval("globalThis.underTest = 'Some text';"); + testAnd('Some text'.toJS); + + eval("globalThis.underTest = [];"); + testAnd([].jsify()); + + eval("globalThis.underTest = true;"); + testAnd(true.toJS); + + eval("globalThis.underTest = {};"); + testAnd(JSObject()); +} diff --git a/LibTest/js_interop/JSAnyOperatorExtension/and_t02.dart b/LibTest/js_interop/JSAnyOperatorExtension/and_t02.dart new file mode 100644 index 0000000000..66c028c350 --- /dev/null +++ b/LibTest/js_interop/JSAnyOperatorExtension/and_t02.dart @@ -0,0 +1,49 @@ +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion JSAny and( JSAny? any ) +/// The result of `this && any` in JavaScript. +/// +/// @description Check that `and` returns result of `this && any` in JavaScript. +/// Test not 'truthy' first operand. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; +import '../js_utils.dart'; + +testAnd(JSAny? underTest) { + eval(r''' + var andNum = underTest && 2; + var andString = underTest && "text"; + var andNull = underTest && null; + var andNaN = underTest && (0 / 0); + var andArray = underTest && [1, 2]; + var andTrue = underTest && true; + '''); + + jsExpectEquals(globalContext["andNum"], underTest.and(2.toJS)); + jsExpectEquals(globalContext["andString"], underTest.and("text".toJS)); + jsExpectEquals(globalContext["andNull"], underTest.and(null)); + jsExpectEquals(globalContext["andNaN"], underTest.and((0 / 0).toJS)); + jsExpectEquals(globalContext["andArray"], underTest.and([1, 2].jsify())); + jsExpectEquals(globalContext["andTrue"], underTest.and(true.toJS)); +} + +main() { + eval("globalThis.underTest = 0;"); + testAnd(0.toJS); + + eval("globalThis.underTest = '';"); + testAnd(''.toJS); + + eval("globalThis.underTest = null;"); + testAnd(null); + + eval("globalThis.underTest = 0 / 0;"); + testAnd((0 / 0).jsify()); + + eval("globalThis.underTest = false;"); + testAnd(false.toJS); +} diff --git a/LibTest/js_interop/JSAnyOperatorExtension/divide_t01.dart b/LibTest/js_interop/JSAnyOperatorExtension/divide_t01.dart new file mode 100644 index 0000000000..5212e3eddb --- /dev/null +++ b/LibTest/js_interop/JSAnyOperatorExtension/divide_t01.dart @@ -0,0 +1,55 @@ +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion JSAny divide( JSAny? any ) +/// The result of `this / any` in JavaScript. +/// +/// @description Check that `divide` returns result of `this / any` in +/// JavaScript. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; +import '../js_utils.dart'; + +testDivide(JSAny? underTest) { + eval(r''' + var divNum = underTest / 2; + var divString = underTest / "text"; + var divNull = underTest / null; + var divNaN = underTest / (0 / 0); + var divArray = underTest / [1, 2]; + var divTrue = underTest / true; + '''); + + jsExpectEquals(globalContext["divNum"], underTest.divide(2.toJS)); + jsExpectEquals(globalContext["divString"], underTest.divide("text".toJS)); + jsExpectEquals(globalContext["divNull"], underTest.divide(null)); + jsExpectEquals(globalContext["divNaN"], underTest.divide((0 / 0).toJS)); + jsExpectEquals(globalContext["divArray"], underTest.divide([1, 2].jsify())); + jsExpectEquals(globalContext["divTrue"], underTest.divide(true.toJS)); +} + +main() { + eval("globalThis.underTest = 42;"); + testDivide(42.toJS); + + eval("globalThis.underTest = 'Some text';"); + testDivide('Some text'.toJS); + + eval("globalThis.underTest = null;"); + testDivide(null); + + eval("globalThis.underTest = 0 / 0;"); + testDivide((0 / 0).jsify()); + + eval("globalThis.underTest = [0];"); + testDivide([].jsify()); + + eval("globalThis.underTest = false;"); + testDivide(false.toJS); + + eval("globalThis.underTest = {};"); + testDivide(JSObject()); +} diff --git a/LibTest/js_interop/JSAnyOperatorExtension/equals_t01.dart b/LibTest/js_interop/JSAnyOperatorExtension/equals_t01.dart new file mode 100644 index 0000000000..173e7b1869 --- /dev/null +++ b/LibTest/js_interop/JSAnyOperatorExtension/equals_t01.dart @@ -0,0 +1,63 @@ +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion JSBoolean equals( JSAny? any ) +/// The result of `this == any` in JavaScript. +/// +/// @description Check that `equals` returns result of `this == any` in +/// JavaScript. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; +import '../js_utils.dart'; + +testEquals(JSAny? underTest) { + eval(r''' + var eqNum = underTest == 2; + var eqString = underTest == "text"; + var eqNull = underTest == null; + var eqUndefined = underTest == undefined; + var eqNaN = underTest == (0 / 0); + var eqArray = underTest == [1, 2]; + var eqTrue = underTest == true; + '''); + + jsExpectEquals(globalContext["eqNum"], underTest.equals(2.toJS)); + jsExpectEquals(globalContext["eqString"], underTest.equals("text".toJS)); + jsExpectEquals(globalContext["eqNull"], underTest.equals(null)); + jsExpectEquals(globalContext["eqUndefined"], underTest.equals(null)); + jsExpectEquals(globalContext["eqNaN"], underTest.equals((0 / 0).toJS)); + jsExpectEquals(globalContext["eqArray"], underTest.equals([1, 2].jsify())); + jsExpectEquals(globalContext["eqTrue"], underTest.equals(true.toJS)); +} + +main() { + eval("globalThis.underTest = 2;"); + testEquals(2.toJS); + + eval("globalThis.underTest = 42;"); + testEquals(42.toJS); + + eval("globalThis.underTest = 'Some text';"); + testEquals('Some text'.toJS); + + eval("globalThis.underTest = null;"); + testEquals(null); + + eval("globalThis.underTest = undefined;"); + testEquals(null); + + eval("globalThis.underTest = 0 / 0;"); + testEquals((0 / 0).jsify()); + + eval("globalThis.underTest = [0];"); + testEquals([].jsify()); + + eval("globalThis.underTest = false;"); + testEquals(false.toJS); + + eval("globalThis.underTest = {};"); + testEquals(JSObject()); +} diff --git a/LibTest/js_interop/JSAnyOperatorExtension/exponentiate_t01.dart b/LibTest/js_interop/JSAnyOperatorExtension/exponentiate_t01.dart new file mode 100644 index 0000000000..133efb4256 --- /dev/null +++ b/LibTest/js_interop/JSAnyOperatorExtension/exponentiate_t01.dart @@ -0,0 +1,61 @@ +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion JSAny exponentiate( JSAny? any ) +/// The result of `this ** any` in JavaScript. +/// +/// @description Check that `exponentiate` returns result of `this ** any` in +/// JavaScript. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; +import '../js_utils.dart'; + +testExponentiate(JSAny? underTest) { + eval(r''' + var expNum = underTest ** 2; + var expString = underTest ** "text"; + var expNull = underTest ** null; + var expNaN = underTest ** (0 / 0); + var expArray = underTest ** [1, 2]; + var expTrue = underTest ** true; + '''); + + jsExpectEquals(globalContext["expNum"], underTest.exponentiate(2.toJS)); + jsExpectEquals( + globalContext["expString"], + underTest.exponentiate("text".toJS), + ); + jsExpectEquals(globalContext["expNull"], underTest.exponentiate(null)); + jsExpectEquals(globalContext["expNaN"], underTest.exponentiate((0 / 0).toJS)); + jsExpectEquals( + globalContext["expArray"], + underTest.exponentiate([1, 2].jsify()), + ); + jsExpectEquals(globalContext["expTrue"], underTest.exponentiate(true.toJS)); +} + +main() { + eval("globalThis.underTest = 42;"); + testExponentiate(42.toJS); + + eval("globalThis.underTest = 'Some text';"); + testExponentiate('Some text'.toJS); + + eval("globalThis.underTest = null;"); + testExponentiate(null); + + eval("globalThis.underTest = 0 / 0;"); + testExponentiate((0 / 0).jsify()); + + eval("globalThis.underTest = [0];"); + testExponentiate([].jsify()); + + eval("globalThis.underTest = false;"); + testExponentiate(false.toJS); + + eval("globalThis.underTest = {};"); + testExponentiate(JSObject()); +} diff --git a/LibTest/js_interop/js_utils.dart b/LibTest/js_interop/js_utils.dart index a25d7ebb02..61b2086278 100644 --- a/LibTest/js_interop/js_utils.dart +++ b/LibTest/js_interop/js_utils.dart @@ -6,6 +6,23 @@ /// @author sgrekhov22@gmail.com import 'dart:js_interop'; +import '../../Utils/expect.dart'; @JS() external void eval(String code); + +void jsExpectEquals(JSAny? expected, JSAny? actual) { + if (isJS) { + Expect.equals(expected, actual); + return; + } + if (isWasm) { + Expect.equals(expected.dartify(), actual.dartify()); + return; + } + throw Exception("Only dart2js and dart2wasm compilers supported"); +} + +void jsExpectArrayEquals(JSAny? expected, JSAny? actual) { + Expect.listEquals(expected.dartify(), actual.dartify()); +} diff --git a/Utils/expect_common.dart b/Utils/expect_common.dart index 45f17f87fd..fee560580c 100644 --- a/Utils/expect_common.dart +++ b/Utils/expect_common.dart @@ -400,8 +400,17 @@ final bool assertStatementsEnabled = (() { return result; })(); -/// Is true iff js compiler is used -final bool isJS = identical(1.0, 1); +/// Is true iff ddc compiler is used +const bool isDDC = const bool.fromEnvironment('dart.library._ddc_only'); + +/// Is true iff dart2js compiler is used +const bool isDart2JS = const bool.fromEnvironment('dart.tool.dart2js'); + +/// Is true iff JS compiler is used +const bool isJS = isDart2JS || isDDC; + +/// Is true iff dart2wasm compiler is used +const bool isWasm = bool.fromEnvironment('dart.tool.dart2wasm'); /// Checks that objects are identical at the compile time class CheckIdentical {