Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(680)

Unified Diff: test/mjsunit/harmony/typesystem/method-definitions.js

Issue 1841093002: Add optional types to function/method declarations (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@types-1817353007-typ-mod
Patch Set: More tests, disallow optional setter parameter Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/mjsunit/harmony/typesystem/function-declarations.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/harmony/typesystem/method-definitions.js
diff --git a/test/mjsunit/harmony/typesystem/method-definitions.js b/test/mjsunit/harmony/typesystem/method-definitions.js
new file mode 100644
index 0000000000000000000000000000000000000000..4de6df2e8a121bd867432c3750cd803a8d3f099f
--- /dev/null
+++ b/test/mjsunit/harmony/typesystem/method-definitions.js
@@ -0,0 +1,87 @@
+// Copyright 2016 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Flags: --harmony-types
+
+
+load("test/mjsunit/harmony/typesystem/testgen.js");
+
+
+(function TestMethodDefinitions() {
+ // Normal methods.
+ CheckValid("class C { m(a: number, b: number) { return a+b; } }");
+ CheckValid("class C { m(a: number, b?: number) { return a; } }");
+ CheckValid("class C { m(a?: number, b?: number) { return 42; } }");
+ CheckValid("class C { m(a: number, ...rest: number[]) {} }");
+ CheckValid("class C { m(a?: number, ...rest: number[]) {} }");
+ CheckValid("class C { m() : boolean { return true; } }");
+ CheckValid("class C { m(a: number, b: number) : boolean { return true; } }");
+ CheckValid("class C { m(a: number, b?: number) : boolean { return true; } }");
+ CheckValid("class C { m(a?: number, b?: number) : boolean { return true; } }");
+ CheckValid("class C { m(a: number, ...rest: number[]) : void {} }");
+ CheckValid("class C { m(a: number, ...rest: number[]) : void {} }");
+ CheckValid("class C { m([first, ...rest]: number[]) : void {} }");
+ CheckValid("class C { m(a: number = 7, b: number) { return a+b; } }");
+ CheckValid("class C { m(a: number = 1, b?: number) { return a; } }");
+ CheckValid("class C { m(a: number = 2, ...rest: number[]) {} }");
+ CheckValid("class C { m(a: number = 1, b: number) : boolean { return true; } }");
+ CheckValid("class C { m(a: number = 1, b?: number) : boolean { return true; } }");
+ CheckValid("class C { m(a: number = 1, ...rest: number[]) : void {} }");
+ CheckValid("class C { m([first, ...rest]: number[] = [1]) : void {} }");
+ // Type parametric.
+ CheckValid("class C { m <A>(a: A, b: A) { return a+b; } }");
+ CheckValid("class C { m <A extends number>(a: A, b: A) { return a+b; } }");
+ CheckValid("class C { m <A, B>(a: A, b: B) { return a+b; } }");
+ CheckValid("class C { m <A extends number, B extends number>(a: A, b: B) { return a+b; } }");
+ // Constructors.
+ CheckValid("class C { constructor () {} }");
+ CheckValid("class C { constructor (a : number) {} }");
+ CheckValid("class C { constructor (a? : number) {} }");
+ CheckValid("class C { constructor (a : number, b : string) {} }");
+ CheckValid("class C { constructor (a, b? : string) {} }");
+ CheckValid("class C { constructor (a, b? : string, ...rest: any[]) {} }");
+ // Getters.
+ CheckValid("class C { get x () { return 42; } }");
+ CheckValid("class C { get x () : number { return 42; } }");
+ // Setters.
+ CheckValid("class C { set x (a) {} }");
+ CheckValid("class C { set x (a = 42) {} }");
+ CheckValid("class C { set x (a) : void {} }");
+ CheckValid("class C { set x (a = 42) : void {} }");
+ CheckValid("class C { set x (a : number) {} }");
+ CheckValid("class C { set x (a : number = 42) {} }");
+ CheckValid("class C { set x (a : number) : void {} }");
+ CheckValid("class C { set x (a : number = 42) : void {} }");
+ // Invalid constructors.
+ CheckInvalid("class C { constructor (a : number) : boolean {} }");
+ CheckInvalid("class C { constructor <A>(a : A) {} }");
+ // Invalid getters.
+ CheckInvalid("class C { get x (a) { return 42; } }");
+ CheckInvalid("class C { get x (a?) { return 42; } }");
+ CheckInvalid("class C { get x (a) : number { return 42; } }");
+ CheckInvalid("class C { get x (a?) : number { return 42; } }");
+ CheckInvalid("class C { get x (a, b) { return 42; } }");
+ CheckInvalid("class C { get x (a, b) : number { return 42; } }");
+ CheckInvalid("class C { get x (a : number) { return 42; } }");
+ CheckInvalid("class C { get x (a? : number) { return 42; } }");
+ CheckInvalid("class C { get x (a : number) : number { return 42; } }");
+ CheckInvalid("class C { get x (a? : number) : number { return 42; } }");
+ CheckInvalid("class C { get x (...rest) { return 42; } }");
+ CheckInvalid("class C { get x (...rest : number[]) { return 42; } }");
+ CheckInvalid("class C { get x (...rest) : number { return 42; } }");
+ CheckInvalid("class C { get x (...rest : number[]) : number { return 42; } }");
+ CheckInvalid("class C { get x <A>() { return 42; } }");
+ // Invalid setters.
+ CheckInvalid("class C { set x () {} }");
+ CheckInvalid("class C { set x () : void {} }");
+ CheckInvalid("class C { set x (a?) {} }");
+ CheckInvalid("class C { set x (a? : number) : void {} }");
+ CheckInvalid("class C { set x (a? = 42) {} }");
+ CheckInvalid("class C { set x (a? = 42 : number) : void {} }");
+ CheckInvalid("class C { set x (a : number, b : number) {} }");
+ CheckInvalid("class C { set x (a : number, b : number) : void {} }");
+ CheckInvalid("class C { set x (...rest) {} }");
+ CheckInvalid("class C { set x (...rest : string[]) : void {} }");
+ CheckInvalid("class C { set x <A>(a : A) {} }");
+})();
« no previous file with comments | « test/mjsunit/harmony/typesystem/function-declarations.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698