From 17009e41d4eb84fe319b52c6925ae845888e2ba4 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 23 May 2016 06:06:01 -0700 Subject: [PATCH] Expand tests --- .../readonlyConstructorAssignment.errors.txt | 50 ++++++++++++ .../readonlyConstructorAssignment.js | 80 ++++++++++++++++++- .../readonlyConstructorAssignment.symbols | 14 ---- .../readonlyConstructorAssignment.types | 16 ---- .../readonlyConstructorAssignment.ts | 35 +++++++- 5 files changed, 162 insertions(+), 33 deletions(-) create mode 100644 tests/baselines/reference/readonlyConstructorAssignment.errors.txt delete mode 100644 tests/baselines/reference/readonlyConstructorAssignment.symbols delete mode 100644 tests/baselines/reference/readonlyConstructorAssignment.types diff --git a/tests/baselines/reference/readonlyConstructorAssignment.errors.txt b/tests/baselines/reference/readonlyConstructorAssignment.errors.txt new file mode 100644 index 00000000000..e764fe21eda --- /dev/null +++ b/tests/baselines/reference/readonlyConstructorAssignment.errors.txt @@ -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; + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/readonlyConstructorAssignment.js b/tests/baselines/reference/readonlyConstructorAssignment.js index 3d00f829a78..40c292e4ce8 100644 --- a/tests/baselines/reference/readonlyConstructorAssignment.js +++ b/tests/baselines/reference/readonlyConstructorAssignment.js @@ -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)); diff --git a/tests/baselines/reference/readonlyConstructorAssignment.symbols b/tests/baselines/reference/readonlyConstructorAssignment.symbols deleted file mode 100644 index 594f9e02b69..00000000000 --- a/tests/baselines/reference/readonlyConstructorAssignment.symbols +++ /dev/null @@ -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)) - } -} - diff --git a/tests/baselines/reference/readonlyConstructorAssignment.types b/tests/baselines/reference/readonlyConstructorAssignment.types deleted file mode 100644 index 10f1cd20470..00000000000 --- a/tests/baselines/reference/readonlyConstructorAssignment.types +++ /dev/null @@ -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 - } -} - diff --git a/tests/cases/conformance/classes/constructorDeclarations/constructorParameters/readonlyConstructorAssignment.ts b/tests/cases/conformance/classes/constructorDeclarations/constructorParameters/readonlyConstructorAssignment.ts index a81bf69249c..aeecf085f2f 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/constructorParameters/readonlyConstructorAssignment.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/constructorParameters/readonlyConstructorAssignment.ts @@ -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; } }