mirror of
https://github.com/dart-lang/sdk
synced 2024-09-18 20:51:19 +00:00
912005267d
Change-Id: I46be49b2effec3e38a3dc44cd45cfe736f77fa78 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/182680 Commit-Queue: Sigmund Cherem <sigmund@google.com> Reviewed-by: Joshua Litt <joshualitt@google.com> Reviewed-by: Nicholas Shahan <nshahan@google.com> Reviewed-by: Stephen Adams <sra@google.com>
114 lines
2.3 KiB
Dart
114 lines
2.3 KiB
Dart
// Copyright (c) 2011, 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.
|
|
|
|
// @dart = 2.7
|
|
|
|
// Test the feature where the native string declares the native method's name.
|
|
// #3. The name does not get
|
|
|
|
import 'native_testing.dart';
|
|
|
|
@Native("A")
|
|
class A {
|
|
@JSName('fooA')
|
|
int foo() native;
|
|
}
|
|
|
|
@Native("B")
|
|
class B extends A {
|
|
@JSName('fooB')
|
|
int foo() native;
|
|
int fooA() => 333;
|
|
}
|
|
|
|
class Decoy {
|
|
int fooA() => 666;
|
|
int fooB() => 999;
|
|
}
|
|
|
|
makeA() native;
|
|
makeB() native;
|
|
|
|
void setup() {
|
|
JS('', r"""
|
|
(function(){
|
|
// This code is inside 'setup' and so not accessible from the global scope.
|
|
function inherits(child, parent) {
|
|
if (child.prototype.__proto__) {
|
|
child.prototype.__proto__ = parent.prototype;
|
|
} else {
|
|
function tmp() {};
|
|
tmp.prototype = parent.prototype;
|
|
child.prototype = new tmp();
|
|
child.prototype.constructor = child;
|
|
}
|
|
}
|
|
function A(){}
|
|
A.prototype.fooA = function(){return 100;};
|
|
function B(){}
|
|
inherits(B, A);
|
|
B.prototype.fooB = function(){return 200;};
|
|
|
|
self.makeA = function(){return new A()};
|
|
self.makeB = function(){return new B()};
|
|
|
|
self.nativeConstructor(A);
|
|
self.nativeConstructor(B);
|
|
})()""");
|
|
applyTestExtensions(['A', 'B']);
|
|
}
|
|
|
|
testDynamic() {
|
|
var a = confuse(makeA());
|
|
var b = confuse(makeB());
|
|
var d = confuse(new Decoy());
|
|
|
|
Expect.equals(100, a.foo());
|
|
Expect.equals(200, b.foo());
|
|
Expect.equals(666, d.fooA());
|
|
Expect.equals(999, d.fooB());
|
|
|
|
expectNoSuchMethod(() {
|
|
a.fooA();
|
|
}, 'fooA should be invisible on A');
|
|
Expect.equals(333, b.fooA());
|
|
|
|
expectNoSuchMethod(() {
|
|
a.fooB();
|
|
}, 'fooB should be absent on A');
|
|
expectNoSuchMethod(() {
|
|
b.fooB();
|
|
}, 'fooA should be invisible on B');
|
|
}
|
|
|
|
testTyped() {
|
|
A a = makeA();
|
|
B b = makeB();
|
|
Decoy d = new Decoy();
|
|
|
|
Expect.equals(100, a.foo());
|
|
Expect.equals(200, b.foo());
|
|
Expect.equals(666, d.fooA());
|
|
Expect.equals(999, d.fooB());
|
|
}
|
|
|
|
main() {
|
|
nativeTesting();
|
|
setup();
|
|
|
|
testDynamic();
|
|
testTyped();
|
|
}
|
|
|
|
expectNoSuchMethod(action, note) {
|
|
bool caught = false;
|
|
try {
|
|
action();
|
|
} on NoSuchMethodError catch (ex) {
|
|
caught = true;
|
|
Expect.isTrue(ex is NoSuchMethodError, note);
|
|
}
|
|
Expect.isTrue(caught, note);
|
|
}
|