dart-sdk/tests/lib/js/call_utils.dart
Nicholas Shahan 8ca97716cc [dart2js,ddc] Add package:js .call() tests
Some expectations in these tests do not match the language 
specification or are undefined.

These tests should help:
 - avoid regressions in the existing behavior
 - highlight incremental improvements towards implementing the 
   desired behavior (with corresponding changes to the expectations)
 - identify the differences between the JavaScript compilers

Change-Id: Icaa7371b3cf8c4221e4348176f712b3d03196720
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/359245
Commit-Queue: Nicholas Shahan <nshahan@google.com>
Reviewed-by: Srujan Gaddam <srujzs@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
2024-04-02 00:41:23 +00:00

57 lines
1.4 KiB
Dart

// Copyright (c) 2024, 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.
import 'package:js/js.dart';
const dart2js = const bool.fromEnvironment('dart.library._dart2js_only');
@pragma('dart2js:noInline')
@pragma('dart2js:assumeDynamic')
dynamic confuse(dynamic x) => x;
@JS()
external dynamic eval(String script);
void injectJS() {
eval(
'''
self.jsFunction = function(s) {
if (this == null) {
throw "`this` is null or undefined";
}
if (typeof s != 'string') {
throw "`s` is not a string";
}
return s.at(0);
};
self.jsObject = { call: function(s) {
if (this == null) {
throw "`this` is null or undefined";
}
if (typeof s != 'string') {
throw "`s` is not a string";
}
return s.at(0);
} };
self.NamedClass = class NamedClass {
call(s) {
if (this == null) {
throw "`this` is null or undefined";
}
if (typeof s != 'string') {
throw "`s` is not a string";
}
return s.at(0);
}
}
self.jsClass = new NamedClass();
''',
);
}
bool jsThisIsNullCheck(e) =>
e.toString().contains('`this` is null or undefined');
bool jsArgIsNotStringCheck(e) => e.toString().contains('`s` is not a string');