mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 01:30:32 +00:00
Initial batch of private name tests.
Tests for using a private class from another library exposed via a public typedef name. Change-Id: I1b40958d73bc7b4c16bd013eca721b52ee4d4503 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/193642 Reviewed-by: Erik Ernst <eernst@google.com> Commit-Queue: Erik Ernst <eernst@google.com>
This commit is contained in:
parent
628e630ca2
commit
4162c560f0
|
@ -0,0 +1,45 @@
|
|||
// Copyright (c) 2021, 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.
|
||||
|
||||
// SharedOptions=--enable-experiment=nonfunction-type-aliases
|
||||
|
||||
// Test that private names exported via public typedefs allow extension.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
import "private_name_library.dart";
|
||||
|
||||
class Derived extends PublicClass {
|
||||
Derived() : super(0);
|
||||
}
|
||||
|
||||
class AlsoDerived extends AlsoPublicClass {
|
||||
AlsoDerived() : super.named(0);
|
||||
int instanceMethod() => 0;
|
||||
int _privateInstanceMethod() => 0;
|
||||
}
|
||||
|
||||
/// Test that inherited methods work correctly.
|
||||
void test1() {
|
||||
{
|
||||
PublicClass p = Derived();
|
||||
Expect.equals(3, p.instanceMethod());
|
||||
Expect.throwsNoSuchMethodError(() => (p as dynamic)._privateInstanceMethod());
|
||||
}
|
||||
}
|
||||
|
||||
/// Test that inherited methods work correctly.
|
||||
void test2() {
|
||||
{
|
||||
var p = AlsoDerived();
|
||||
Expect.equals(0, p.instanceMethod());
|
||||
Expect.equals(0, p._privateInstanceMethod());
|
||||
Expect.equals(0, (p as dynamic)._privateInstanceMethod());
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
test1();
|
||||
test2();
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
// Copyright (c) 2021, 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.
|
||||
|
||||
// Shared code for tests that private names exported publicly via a typedef work
|
||||
// as expected.
|
||||
|
||||
library private;
|
||||
|
||||
class _PrivateClass {
|
||||
int x;
|
||||
_PrivateClass(this.x);
|
||||
_PrivateClass.named(this.x);
|
||||
static int staticMethod() => 3;
|
||||
static int _privateStaticMethod() => 3;
|
||||
int instanceMethod() => 3;
|
||||
int _privateInstanceMethod() => 3;
|
||||
}
|
||||
|
||||
typedef PublicClass = _PrivateClass;
|
||||
PublicClass mkPublicClass() => PublicClass(0);
|
||||
|
||||
typedef _PrivateTypeDef = _PrivateClass;
|
||||
typedef AlsoPublicClass = _PrivateTypeDef;
|
||||
AlsoPublicClass mkAlsoPublicClass() => AlsoPublicClass(0);
|
||||
|
||||
class _PrivateGenericClass<T> {
|
||||
static int staticMethod() => 3;
|
||||
}
|
||||
typedef PublicGenericClass<T> = _PrivateGenericClass<T>;
|
||||
PublicGenericClass<T> mkPublicGenericClass<T>() => PublicGenericClass();
|
||||
typedef PublicGenericClassOfInt = _PrivateGenericClass<int>;
|
|
@ -0,0 +1,32 @@
|
|||
// Copyright (c) 2021, 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.
|
||||
|
||||
// SharedOptions=--enable-experiment=nonfunction-type-aliases
|
||||
|
||||
// Test that private names exported via public typedefs don't give access to
|
||||
// private static methods.
|
||||
|
||||
import "private_name_library.dart";
|
||||
|
||||
/// Test that accessing private static methods is not accidentally enabled.
|
||||
void test1() {
|
||||
{
|
||||
PublicClass._privateStaticMethod();
|
||||
// ^^^^^^^^^^^^^^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_METHOD
|
||||
// [cfe] Method not found: '_PrivateClass._privateStaticMethod'.
|
||||
AlsoPublicClass._privateStaticMethod();
|
||||
// ^^^^^^^^^^^^^^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_METHOD
|
||||
// [cfe] Method not found: '_PrivateClass._privateStaticMethod'.
|
||||
PublicGenericClassOfInt._privateStaticMethod();
|
||||
// ^^^^^^^^^^^^^^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_METHOD
|
||||
// [cfe] Method not found: '_PrivateGenericClass._privateStaticMethod'.
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
test1();
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Copyright (c) 2021, 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.
|
||||
|
||||
// SharedOptions=--enable-experiment=nonfunction-type-aliases
|
||||
|
||||
// Test that private names exported via public typedefs work correctly for
|
||||
// accessing static methods.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
import "../../static_type_helper.dart";
|
||||
import "private_name_library.dart";
|
||||
|
||||
/// Test that each public typedef can be used to access static methods.
|
||||
void test1() {
|
||||
{
|
||||
Expect.equals(3, PublicClass.staticMethod());
|
||||
PublicClass.staticMethod().expectStaticType<Exactly<int>>();
|
||||
|
||||
Expect.equals(3, AlsoPublicClass.staticMethod());
|
||||
AlsoPublicClass.staticMethod().expectStaticType<Exactly<int>>();
|
||||
|
||||
Expect.equals(3, PublicGenericClassOfInt.staticMethod());
|
||||
PublicGenericClassOfInt.staticMethod().expectStaticType<Exactly<int>>();
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
test1();
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
// Copyright (c) 2021, 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.
|
||||
|
||||
// SharedOptions=--enable-experiment=nonfunction-type-aliases
|
||||
|
||||
// Test that private names exported via public typedefs work correctly as
|
||||
// types.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
import "private_name_library.dart";
|
||||
import "private_name_library.dart" as prefixed;
|
||||
|
||||
/// Test that each public typedef is usable as a type, and that the types
|
||||
/// behave as expected.
|
||||
void test1() {
|
||||
{
|
||||
PublicClass p0 = mkPublicClass();
|
||||
AlsoPublicClass p1 = mkAlsoPublicClass();
|
||||
// Test that equivalent private types are still equivalent
|
||||
p0 = p1;
|
||||
p1 = p0;
|
||||
}
|
||||
|
||||
{
|
||||
PublicGenericClass<int> p0 = mkPublicGenericClass();
|
||||
PublicGenericClassOfInt p1 = mkPublicGenericClass();
|
||||
// Test that equivalent private types are still equivalent
|
||||
p0 = p1;
|
||||
p1 = p0;
|
||||
|
||||
// Test that inference works on private generic names.
|
||||
Type capture<T>(PublicGenericClass<T> a) => T;
|
||||
Expect.equals(int, capture(p1));
|
||||
}
|
||||
}
|
||||
|
||||
/// Test that each public typedef is usable as a type when the types are
|
||||
/// imported with a prefix, and that the types behave as expected.
|
||||
void test2() {
|
||||
{
|
||||
prefixed.PublicClass p0 = prefixed.mkPublicClass();
|
||||
prefixed.AlsoPublicClass p1 = prefixed.mkAlsoPublicClass();
|
||||
// Test that equivalent private types are still equivalent
|
||||
p0 = p1;
|
||||
p1 = p0;
|
||||
}
|
||||
|
||||
{
|
||||
prefixed.PublicGenericClass<int> p0 = prefixed.mkPublicGenericClass();
|
||||
prefixed.PublicGenericClassOfInt p1 = prefixed.mkPublicGenericClass();
|
||||
// Test that equivalent private types are still equivalent
|
||||
p0 = p1;
|
||||
p1 = p0;
|
||||
|
||||
// Test that inference works on private generic names.
|
||||
Type capture<T>(prefixed.PublicGenericClass<T> a) => T;
|
||||
Expect.equals(capture(p1), int);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
test1();
|
||||
test2();
|
||||
}
|
Loading…
Reference in a new issue