Expand tests

This commit is contained in:
Andy Hanson 2016-05-23 06:06:01 -07:00
parent d3b4c6a93e
commit 17009e41d4
5 changed files with 162 additions and 33 deletions

View file

@ -0,0 +1,50 @@
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/readonlyConstructorAssignment.ts(13,9): error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property.
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/readonlyConstructorAssignment.ts(33,7): error TS2415: Class 'E' incorrectly extends base class 'D'.
Property 'x' is private in type 'D' but not in type 'E'.
==== tests/cases/conformance/classes/constructorDeclarations/constructorParameters/readonlyConstructorAssignment.ts (2 errors) ====
// Tests that readonly parameter properties behave like regular readonly properties
class A {
constructor(readonly x: number) {
this.x = 0;
}
}
class B extends A {
constructor(x: number) {
super(x);
// Fails, x is readonly
this.x = 1;
~~~~~~
!!! error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property.
}
}
class C extends A {
// This is the usual behavior of readonly properties:
// if one is redeclared in a base class, then it can be assigned to.
constructor(readonly x: number) {
super(x);
this.x = 1;
}
}
class D {
constructor(private readonly x: number) {
this.x = 0;
}
}
// Fails, can't redeclare readonly property
class E extends D {
~
!!! error TS2415: Class 'E' incorrectly extends base class 'D'.
!!! error TS2415: Property 'x' is private in type 'D' but not in type 'E'.
constructor(readonly x: number) {
super(x);
this.x = 1;
}
}

View file

@ -1,16 +1,92 @@
//// [readonlyConstructorAssignment.ts]
// Tests that readonly parameter properties behave like regular readonly properties
class A {
constructor(readonly x: number) {
this.x = 7;
this.x = 0;
}
}
class B extends A {
constructor(x: number) {
super(x);
// Fails, x is readonly
this.x = 1;
}
}
class C extends A {
// This is the usual behavior of readonly properties:
// if one is redeclared in a base class, then it can be assigned to.
constructor(readonly x: number) {
super(x);
this.x = 1;
}
}
class D {
constructor(private readonly x: number) {
this.x = 0;
}
}
// Fails, can't redeclare readonly property
class E extends D {
constructor(readonly x: number) {
super(x);
this.x = 1;
}
}
//// [readonlyConstructorAssignment.js]
// Tests that readonly parameter properties behave like regular readonly properties
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var A = (function () {
function A(x) {
this.x = x;
this.x = 7;
this.x = 0;
}
return A;
}());
var B = (function (_super) {
__extends(B, _super);
function B(x) {
_super.call(this, x);
// Fails, x is readonly
this.x = 1;
}
return B;
}(A));
var C = (function (_super) {
__extends(C, _super);
// This is the usual behavior of readonly properties:
// if one is redeclared in a base class, then it can be assigned to.
function C(x) {
_super.call(this, x);
this.x = x;
this.x = 1;
}
return C;
}(A));
var D = (function () {
function D(x) {
this.x = x;
this.x = 0;
}
return D;
}());
// Fails, can't redeclare readonly property
var E = (function (_super) {
__extends(E, _super);
function E(x) {
_super.call(this, x);
this.x = x;
this.x = 1;
}
return E;
}(D));

View file

@ -1,14 +0,0 @@
=== tests/cases/conformance/classes/constructorDeclarations/constructorParameters/readonlyConstructorAssignment.ts ===
class A {
>A : Symbol(A, Decl(readonlyConstructorAssignment.ts, 0, 0))
constructor(readonly x: number) {
>x : Symbol(A.x, Decl(readonlyConstructorAssignment.ts, 1, 16))
this.x = 7;
>this.x : Symbol(A.x, Decl(readonlyConstructorAssignment.ts, 1, 16))
>this : Symbol(A, Decl(readonlyConstructorAssignment.ts, 0, 0))
>x : Symbol(A.x, Decl(readonlyConstructorAssignment.ts, 1, 16))
}
}

View file

@ -1,16 +0,0 @@
=== tests/cases/conformance/classes/constructorDeclarations/constructorParameters/readonlyConstructorAssignment.ts ===
class A {
>A : A
constructor(readonly x: number) {
>x : number
this.x = 7;
>this.x = 7 : number
>this.x : number
>this : this
>x : number
>7 : number
}
}

View file

@ -1,5 +1,38 @@
// Tests that readonly parameter properties behave like regular readonly properties
class A {
constructor(readonly x: number) {
this.x = 7;
this.x = 0;
}
}
class B extends A {
constructor(x: number) {
super(x);
// Fails, x is readonly
this.x = 1;
}
}
class C extends A {
// This is the usual behavior of readonly properties:
// if one is redeclared in a base class, then it can be assigned to.
constructor(readonly x: number) {
super(x);
this.x = 1;
}
}
class D {
constructor(private readonly x: number) {
this.x = 0;
}
}
// Fails, can't redeclare readonly property
class E extends D {
constructor(readonly x: number) {
super(x);
this.x = 1;
}
}