-
Notifications
You must be signed in to change notification settings - Fork 29
#3180. Add instanceof and instanceOfString tests
#3260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A01_t01.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // 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 bool instanceOfString( String constructorName ) | ||
| /// | ||
| /// Whether this [JSAny?] is an `instanceof` the constructor that is defined by | ||
| /// `constructorName`, which is looked up in the `globalContext`. | ||
| /// | ||
| /// If `constructorName` contains '.'s, the name is split into several parts in | ||
| /// order to get the constructor. For example, `library1.JSClass` would involve | ||
| /// fetching `library1` off of the `globalContext`, and then fetching `JSClass` | ||
| /// off of `library1` to get the constructor. | ||
| /// | ||
| /// If `constructorName` is empty or any of the parts or the constructor don't | ||
| /// exist, returns `false`. | ||
| /// | ||
| /// @description Checks that `instanceOfString()` returns `true` if this | ||
| /// `JSAny?` is is an `instanceof` the constructor that is defined by | ||
| /// `constructorName`. | ||
| /// @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 A(id, name) { | ||
| this.id = id; | ||
| this.name = name; | ||
| } | ||
| function B() {} | ||
|
|
||
| globalThis.objA = new A(42, "A form JS"); | ||
| globalThis.objB = new B(); | ||
| '''); | ||
| Expect.isTrue(globalContext["objA"].instanceOfString("A")); | ||
| Expect.isTrue(globalContext["objB"].instanceOfString("B")); | ||
| Expect.isFalse(globalContext["objA"].instanceOfString("B")); | ||
| Expect.isFalse(globalContext["objB"].instanceOfString("A")); | ||
| } | ||
49 changes: 49 additions & 0 deletions
49
LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A01_t02.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 bool instanceOfString( String constructorName ) | ||
| /// | ||
| /// Whether this [JSAny?] is an `instanceof` the constructor that is defined by | ||
| /// `constructorName`, which is looked up in the `globalContext`. | ||
| /// | ||
| /// If `constructorName` contains '.'s, the name is split into several parts in | ||
| /// order to get the constructor. For example, `library1.JSClass` would involve | ||
| /// fetching `library1` off of the `globalContext`, and then fetching `JSClass` | ||
| /// off of `library1` to get the constructor. | ||
| /// | ||
| /// If `constructorName` is empty or any of the parts or the constructor don't | ||
| /// exist, returns `false`. | ||
| /// | ||
| /// @description Checks that `instanceOfString()` returns `true` if this | ||
| /// `JSAny?` is is an `instanceof` the constructor that is defined by | ||
| /// `constructorName`. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../../Utils/expect.dart'; | ||
| import '../js_utils.dart'; | ||
|
|
||
| main() { | ||
| eval(r''' | ||
| class A { | ||
| constructor(id, name) { | ||
| this.id = id; | ||
| this.name = name; | ||
| } | ||
| } | ||
| class B { | ||
| constructor() {} | ||
| } | ||
|
|
||
| globalThis.fnB = B; | ||
| globalThis.fnA = A; | ||
| globalThis.objA = new A(42, "A form JS"); | ||
| globalThis.objB = new B(); | ||
| '''); | ||
| Expect.isTrue(globalContext["objA"].instanceOfString("fnA")); | ||
| Expect.isTrue(globalContext["objB"].instanceOfString("fnB")); | ||
| Expect.isFalse(globalContext["objA"].instanceOfString("B")); | ||
| Expect.isFalse(globalContext["objB"].instanceOfString("A")); | ||
| } |
44 changes: 44 additions & 0 deletions
44
LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A02_t01.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // 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 bool instanceOfString( String constructorName ) | ||
| /// | ||
| /// Whether this [JSAny?] is an `instanceof` the constructor that is defined by | ||
| /// `constructorName`, which is looked up in the `globalContext`. | ||
| /// | ||
| /// If `constructorName` contains '.'s, the name is split into several parts in | ||
| /// order to get the constructor. For example, `library1.JSClass` would involve | ||
| /// fetching `library1` off of the `globalContext`, and then fetching `JSClass` | ||
| /// off of `library1` to get the constructor. | ||
| /// | ||
| /// If `constructorName` is empty or any of the parts or the constructor don't | ||
| /// exist, returns `false`. | ||
| /// | ||
| /// @description Checks that `instanceOfString()` returns `false` if | ||
| /// `constructorName` cannot be found in the `globalContext`. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../../Utils/expect.dart'; | ||
| import '../js_utils.dart'; | ||
|
|
||
| main() { | ||
| eval(r''' | ||
| class A { | ||
| constructor(id, name) { | ||
| this.id = id; | ||
| this.name = name; | ||
| } | ||
| } | ||
| class B { | ||
| constructor() {} | ||
| } | ||
|
|
||
| globalThis.objA = new A(42, "A form JS"); | ||
| globalThis.objB = new B(); | ||
| '''); | ||
| Expect.isFalse(globalContext["objA"].instanceOfString("A")); | ||
| Expect.isFalse(globalContext["objB"].instanceOfString("B")); | ||
| } |
43 changes: 43 additions & 0 deletions
43
LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A03_t01.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // 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 bool instanceOfString( String constructorName ) | ||
| /// | ||
| /// Whether this [JSAny?] is an `instanceof` the constructor that is defined by | ||
| /// `constructorName`, which is looked up in the `globalContext`. | ||
| /// | ||
| /// If `constructorName` contains '.'s, the name is split into several parts in | ||
| /// order to get the constructor. For example, `library1.JSClass` would involve | ||
| /// fetching `library1` off of the `globalContext`, and then fetching `JSClass` | ||
| /// off of `library1` to get the constructor. | ||
| /// | ||
| /// If `constructorName` is empty or any of the parts or the constructor don't | ||
| /// exist, returns `false`. | ||
| /// | ||
| /// @description Checks that if `constructorName` then `instanceOfString` | ||
| /// returns `false`. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../../Utils/expect.dart'; | ||
| import '../js_utils.dart'; | ||
|
|
||
| main() { | ||
| eval(r''' | ||
| class A { | ||
| constructor(id, name) { | ||
| this.id = id; | ||
| this.name = name; | ||
| } | ||
| } | ||
| class B { | ||
| constructor() {} | ||
| } | ||
| globalThis.objA = new A(42, "A form JS"); | ||
| globalThis.objB = new B(); | ||
| '''); | ||
| Expect.isFalse(globalContext["objA"].instanceOfString("")); | ||
| Expect.isFalse(globalContext["objB"].instanceOfString("")); | ||
| } |
43 changes: 43 additions & 0 deletions
43
LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A03_t02.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // 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 bool instanceOfString( String constructorName ) | ||
| /// | ||
| /// Whether this [JSAny?] is an `instanceof` the constructor that is defined by | ||
| /// `constructorName`, which is looked up in the `globalContext`. | ||
| /// | ||
| /// If `constructorName` contains '.'s, the name is split into several parts in | ||
| /// order to get the constructor. For example, `library1.JSClass` would involve | ||
| /// fetching `library1` off of the `globalContext`, and then fetching `JSClass` | ||
| /// off of `library1` to get the constructor. | ||
| /// | ||
| /// If `constructorName` is empty or any of the parts or the constructor don't | ||
| /// exist, returns `false`. | ||
| /// | ||
| /// @description Checks that if any of the parts or the constructor don't exist, | ||
| /// `instanceOfString` returns `false` | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../../Utils/expect.dart'; | ||
| import '../js_utils.dart'; | ||
|
|
||
| main() { | ||
| eval(r''' | ||
| class A { | ||
| constructor(id, name) { | ||
| this.id = id; | ||
| this.name = name; | ||
| } | ||
| } | ||
| class B { | ||
| constructor() {} | ||
| } | ||
| globalThis.objA = new A(42, "A form JS"); | ||
| globalThis.objB = new B(); | ||
| '''); | ||
| Expect.isFalse(globalContext["objA"].instanceOfString("lib1.A")); | ||
| Expect.isFalse(globalContext["objB"].instanceOfString("lib2.B")); | ||
| } |
58 changes: 58 additions & 0 deletions
58
LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A04_t01.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // 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 bool instanceOfString( String constructorName ) | ||
| /// | ||
| /// Whether this [JSAny?] is an `instanceof` the constructor that is defined by | ||
| /// `constructorName`, which is looked up in the `globalContext`. | ||
| /// | ||
| /// If `constructorName` contains '.'s, the name is split into several parts in | ||
| /// order to get the constructor. For example, `library1.JSClass` would involve | ||
| /// fetching `library1` off of the `globalContext`, and then fetching `JSClass` | ||
| /// off of `library1` to get the constructor. | ||
| /// | ||
| /// If `constructorName` is empty or any of the parts or the constructor don't | ||
| /// exist, returns `false`. | ||
| /// | ||
| /// @description Checks that `instanceOfString()` works as expected if this | ||
| /// `JSAny?` is is an `instanceof` the constructor that is defined by | ||
| /// `constructorName`. Test the case when `constructorName` contains '.'. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| // OtherResources=module.js | ||
| import 'dart:async'; | ||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../../Utils/expect.dart'; | ||
| import '../js_utils.dart'; | ||
|
|
||
| final completer = Completer<String>(); | ||
|
|
||
| void complete(String value) { | ||
| completer.complete(value); | ||
| } | ||
|
|
||
| main() { | ||
| globalContext["complete"] = complete.toJS; | ||
| eval(r''' | ||
| var lib1; | ||
| (async () => { | ||
| // This is path to the module on tryjobs. May not work locally. | ||
| lib1 = await import('/root_dart/tests/co19/src/LibTest/js_interop/JSAnyUtilityExtension/module.js'); | ||
| globalThis.objA = new lib1.A(42, "A form JS"); | ||
| globalThis.objB = new lib1.B(); | ||
| console.log('initialized'); | ||
| })().then(function(v) { | ||
| globalThis.complete(""); | ||
| }); | ||
| '''); | ||
| asyncStart(); | ||
| completer.future.then((_) { | ||
| Expect.isTrue(globalContext["objA"].instanceOfString("lib1.A")); | ||
| Expect.isTrue(globalContext["objB"].instanceOfString("lib1.B")); | ||
| Expect.isFalse(globalContext["objA"].instanceOfString("A")); | ||
| Expect.isFalse(globalContext["objB"].instanceOfString("B")); | ||
| asyncEnd(); | ||
| }); | ||
| } |
43 changes: 43 additions & 0 deletions
43
LibTest/js_interop/JSAnyUtilityExtension/instanceof_A01_t01.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // 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 bool instanceof( JSFunction constructor ) | ||
| /// | ||
| /// Whether this `JSAny?` is an instance of `constructor`. | ||
| /// | ||
| /// @description Checks that `instanceof()` returns `true` if this `JSAny?` is | ||
| /// an instance of `constructor` and `false` otherwise. | ||
| /// @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 A(id, name) { | ||
| this.id = id; | ||
| this.name = name; | ||
| } | ||
| function B() {} | ||
|
|
||
| globalThis.fnB = B; | ||
| globalThis.fnA = A; | ||
| globalThis.objA = new A(42, "A form JS"); | ||
| globalThis.objB = new B(); | ||
| '''); | ||
| Expect.isTrue( | ||
| globalContext["objA"].instanceof(globalContext["fnA"] as JSFunction), | ||
| ); | ||
| Expect.isTrue( | ||
| globalContext["objB"].instanceof(globalContext["fnB"] as JSFunction), | ||
| ); | ||
| Expect.isFalse( | ||
| globalContext["objA"].instanceof(globalContext["fnB"] as JSFunction), | ||
| ); | ||
| Expect.isFalse( | ||
| globalContext["objF"].instanceof(globalContext["fnA"] as JSFunction), | ||
| ); | ||
| } |
47 changes: 47 additions & 0 deletions
47
LibTest/js_interop/JSAnyUtilityExtension/instanceof_A01_t02.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // 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 bool instanceof( JSFunction constructor ) | ||
| /// | ||
| /// Whether this `JSAny?` is an instance of `constructor`. | ||
| /// | ||
| /// @description Checks that `instanceof()` returns `true` if this `JSAny?` is | ||
| /// an instance of `constructor` and `false` otherwise. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../../Utils/expect.dart'; | ||
| import '../js_utils.dart'; | ||
|
|
||
| main() { | ||
| eval(r''' | ||
| class A { | ||
| constructor(id, name) { | ||
| this.id = id; | ||
| this.name = name; | ||
| } | ||
| } | ||
| class B { | ||
| constructor() {} | ||
| } | ||
|
|
||
| globalThis.fnB = B; | ||
| globalThis.fnA = A; | ||
| globalThis.objA = new A(42, "A form JS"); | ||
| globalThis.objB = new B(); | ||
| '''); | ||
| Expect.isTrue( | ||
| globalContext["objA"].instanceof(globalContext["fnA"] as JSFunction), | ||
| ); | ||
| Expect.isTrue( | ||
| globalContext["objB"].instanceof(globalContext["fnB"] as JSFunction), | ||
| ); | ||
| Expect.isFalse( | ||
| globalContext["objA"].instanceof(globalContext["fnB"] as JSFunction), | ||
| ); | ||
| Expect.isFalse( | ||
| globalContext["objF"].instanceof(globalContext["fnA"] as JSFunction), | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export function A(id, name) { | ||
| this.id = id; | ||
| this.name = name; | ||
| } | ||
|
|
||
| export function B() {} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.