OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 7870 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
7881 static const ParserFlag always_flags[] = {kAllowTypes}; | 7881 static const ParserFlag always_flags[] = {kAllowTypes}; |
7882 RunParserSyncTest(untyped_context_data, correct_data, kError, NULL, 0, | 7882 RunParserSyncTest(untyped_context_data, correct_data, kError, NULL, 0, |
7883 always_flags, arraysize(always_flags)); | 7883 always_flags, arraysize(always_flags)); |
7884 RunParserSyncTest(typed_context_data, correct_data, kSuccess, NULL, 0, | 7884 RunParserSyncTest(typed_context_data, correct_data, kSuccess, NULL, 0, |
7885 always_flags, arraysize(always_flags)); | 7885 always_flags, arraysize(always_flags)); |
7886 RunParserSyncTest(untyped_context_data, error_data, kError, NULL, 0, | 7886 RunParserSyncTest(untyped_context_data, error_data, kError, NULL, 0, |
7887 always_flags, arraysize(always_flags)); | 7887 always_flags, arraysize(always_flags)); |
7888 RunParserSyncTest(typed_context_data, error_data, kError, NULL, 0, | 7888 RunParserSyncTest(typed_context_data, error_data, kError, NULL, 0, |
7889 always_flags, arraysize(always_flags)); | 7889 always_flags, arraysize(always_flags)); |
7890 } | 7890 } |
7891 | |
7892 TEST(TypedModeAmbientDeclarations) { | |
7893 const char* untyped_context_data[][2] = {{"", ""}, {NULL, NULL}}; | |
7894 const char* typed_context_data[][2] = {{"'use types'; ", ""}, {NULL, NULL}}; | |
7895 const char* typed_inner_context_data[][2] = { | |
7896 {"'use types'; {", "}"}, | |
7897 {"'use types'; try {", "} catch(x) {}"}, | |
7898 // TODO(nikolaos): Leaving this out, for now, there's an unrelated bug. | |
7899 // {"'use types'; function f() {", "}"}, | |
7900 {"'use types'; for(", " of []) {}"}, | |
7901 {NULL, NULL} | |
7902 }; | |
7903 | |
7904 const char* correct_data[] = { | |
7905 #define VALID(decl) \ | |
7906 "declare var " decl, \ | |
7907 "declare var " decl "; var " decl | |
7908 VALID("x"), | |
7909 VALID("x: number"), | |
7910 VALID("x, y"), | |
7911 VALID("x: number, y: string"), | |
7912 #undef VALID | |
7913 #define VALID(decl, full) \ | |
7914 "declare var " decl, \ | |
7915 "declare var " decl "; var " full | |
7916 VALID("[x, y]", "[x, y] = [42, 'hello']"), | |
7917 VALID("[x, y] : [number, string]", "[x, y] = [42, 'hello']"), | |
7918 VALID("{a: x, b: y}", "{a: x, b: y} = {a: 42, b: 'hello'}"), | |
7919 VALID("{a: x, b: y} : {a: number, b: string}", | |
7920 "{a: x, b: y} = {a: 42, b: 'hello'}"), | |
7921 #undef VALID | |
7922 #define VALID(decl) \ | |
7923 "declare " decl, \ | |
7924 "declare " decl "; " decl " {}" | |
7925 VALID("function f()"), | |
7926 VALID("function f() : number"), | |
7927 VALID("function f(x: number)"), | |
7928 VALID("function f(x: number) : number"), | |
7929 VALID("function f<A>()"), | |
7930 VALID("function f<A>() : A"), | |
7931 VALID("function f<A>(x: A)"), | |
7932 VALID("function f<A, B>(x: A) : B"), | |
7933 VALID("function* f()"), | |
7934 VALID("function* f() : number"), | |
7935 VALID("function* f(x: number)"), | |
7936 VALID("function* f(x: number) : number"), | |
7937 VALID("function* f<A>()"), | |
7938 VALID("function* f<A>() : A"), | |
7939 VALID("function* f<A>(x: A)"), | |
7940 VALID("function* f<A, B>(x: A) : B"), | |
7941 #undef VALID | |
7942 #define VALID(members) \ | |
7943 "declare class C {" members "}", \ | |
7944 "class B {}; declare class C extends B {" members "}", \ | |
7945 "declare class C implements I {" members "}", \ | |
7946 "declare class C implements I, J {" members "}", \ | |
7947 "class B {}; declare class C extends B implements I {" members "}", \ | |
7948 "class B {}; declare class C extends B implements I, J {" \ | |
7949 members "}", \ | |
7950 "declare class C<A, B> {" members "}", \ | |
7951 "class D {}; declare class C<A, B> extends D {" members "}", \ | |
7952 "declare class C<A, B> implements I<A, B> {" members "}", \ | |
7953 "class D {}; declare class C<A, B> extends D implements I<A, B> {" \ | |
7954 members "}" | |
7955 VALID("constructor (x: number, y: boolean);" | |
7956 "x;" | |
7957 "y: boolean;" | |
7958 "'one': number;" | |
7959 "'2': boolean;" | |
7960 "3: string;" | |
7961 "f1 () : number;" | |
7962 "f2 (a: number[]) : number;" | |
7963 "f3 (a: number[], b: number) : number;" | |
7964 "f4 <A, B>(a: A, b: B) : [A, B];" | |
7965 "[x: string];" | |
7966 "[x: string] : number;" | |
7967 "[x: number];" | |
7968 "[x: number] : boolean;"), | |
7969 VALID("static x;" | |
7970 "static y: boolean;" | |
7971 "static 'one': number;" | |
7972 "static '2': boolean;" | |
7973 "static 3: string;" | |
7974 "static f1 () : number;" | |
7975 "static f2 (a: number[]) : number;" | |
7976 "static f3 (a: number[], b: number) : number;" | |
7977 "static f4 <A, B>(a: A, b: B) : [A, B];"), | |
7978 #undef VALID | |
7979 NULL | |
7980 }; | |
7981 | |
7982 const char* error_data[] = { | |
7983 "declare var x : ()", | |
7984 "declare var x = 42", | |
7985 "declare x: number = 42", | |
7986 "declare x = 42, y = 'hello'", | |
7987 "declare x: number = 42, y: string = 'hello'", | |
7988 #define INVALID(decl) "declare " decl " {}" | |
7989 INVALID("function f()"), | |
7990 INVALID("function f() : number"), | |
7991 INVALID("function f(x: number)"), | |
7992 INVALID("function f(x: number) : number"), | |
7993 INVALID("function f<A>()"), | |
7994 INVALID("function f<A>() : A"), | |
7995 INVALID("function f<A>(x: A)"), | |
7996 INVALID("function f<A, B>(x: A) : B"), | |
7997 INVALID("function* f()"), | |
7998 INVALID("function* f() : number"), | |
7999 INVALID("function* f(x: number)"), | |
8000 INVALID("function* f(x: number) : number"), | |
8001 INVALID("function* f<A>()"), | |
8002 INVALID("function* f<A>() : A"), | |
8003 INVALID("function* f<A>(x: A)"), | |
8004 INVALID("function* f<A, B>(x: A) : B"), | |
8005 #undef INVALID | |
8006 "declare function f() : ()", | |
8007 "declare function f(x? = 42) : ()", | |
8008 "declare function f<>()", | |
8009 "declare function ()", | |
8010 "declare function () : number", | |
8011 "declare function (x: number)", | |
8012 "declare function (x: number) : number", | |
8013 "declare function <A>()", | |
8014 "declare function <A>() : A", | |
8015 "declare function <A>(x: A)", | |
8016 "declare function <A, B>(x: A) : B", | |
8017 #define INVALID1(member) \ | |
8018 "declare class C {" member "}", \ | |
8019 "class B {}; declare class C extends B {" member "}", \ | |
8020 "declare class C implements I {" member "}", \ | |
8021 "declare class C implements I, J {" member "}", \ | |
8022 "class B {}; declare class C extends B implements I {" member "}", \ | |
8023 "class B {}; declare class C extends B implements I, J {" \ | |
8024 member "}", \ | |
8025 "declare class C<A, B> {" member "}", \ | |
8026 "class D {}; declare class C<A, B> extends D {" member "}", \ | |
8027 "declare class C<A, B> implements I<A, B> {" member "}", \ | |
8028 "class D {}; declare class C<A, B> extends D implements I<A, B> {" \ | |
8029 member "}" | |
8030 #define INVALID2(member, suffix) \ | |
8031 INVALID1(member), \ | |
8032 INVALID1(member " " suffix) | |
8033 INVALID2("constructor (a : number) : boolean", "{}"), | |
8034 INVALID2("constructor <A>(a : A)", "{}"), | |
8035 INVALID1("x = 42"), | |
8036 INVALID1("x : number = 42"), | |
8037 INVALID1("'four': number = 4"), | |
8038 INVALID1("'5': boolean = false"), | |
8039 INVALID1("6: string[] = [...['six', 'six', 'and', 'six']]"), | |
8040 INVALID2("get x ()", "{ return 42; }"), | |
8041 INVALID2("get x () : number", "{ return 42; }"), | |
8042 INVALID2("set x (a)", "{}"), | |
8043 INVALID2("set x (a) : void", "{}"), | |
8044 INVALID2("set x (a : number)", "{}"), | |
8045 INVALID2("set x (a : number) : void", "{}"), | |
8046 INVALID2("get x (a)", "{ return 42; }"), | |
8047 INVALID2("get x (a) : number", "{ return 42; }"), | |
8048 INVALID2("get x (a, b)", "{ return 42; }"), | |
8049 INVALID2("get x (a, b) : number", "{ return 42; }"), | |
8050 INVALID2("get x (a : number)", "{ return 42; }"), | |
8051 INVALID2("get x (a : number) : number", "{ return 42; }"), | |
8052 INVALID2("get x <A>()", "{ return 42; }"), | |
8053 INVALID2("set x ()", "{}"), | |
8054 INVALID2("set x () : void", "{}"), | |
8055 INVALID2("set x (a : number, b : number)", "{}"), | |
8056 INVALID2("set x (a : number, b : number) : void", "{}"), | |
8057 INVALID2("set x (...rest)", "{}"), | |
8058 INVALID2("set x (...rest : string[]) : void", "{}"), | |
8059 INVALID2("set x <A>(a : A)", "{}"), | |
8060 #undef INVALID1 | |
8061 #undef INVALID2 | |
8062 "declare type N = number", | |
8063 "declare interface I {}", | |
8064 "declare x = 42", | |
8065 "declare {}", | |
8066 "declare while (true) {}", | |
8067 NULL | |
8068 }; | |
8069 | |
8070 static const ParserFlag always_flags[] = {kAllowTypes}; | |
8071 RunParserSyncTest(untyped_context_data, correct_data, kError, NULL, 0, | |
8072 always_flags, arraysize(always_flags)); | |
8073 RunParserSyncTest(typed_context_data, correct_data, kSuccess, NULL, 0, | |
8074 always_flags, arraysize(always_flags)); | |
8075 RunParserSyncTest(typed_inner_context_data, correct_data, kError, NULL, 0, | |
8076 always_flags, arraysize(always_flags)); | |
8077 RunParserSyncTest(untyped_context_data, error_data, kError, NULL, 0, | |
8078 always_flags, arraysize(always_flags)); | |
8079 RunParserSyncTest(typed_context_data, error_data, kError, NULL, 0, | |
8080 always_flags, arraysize(always_flags)); | |
8081 RunParserSyncTest(typed_inner_context_data, error_data, kError, NULL, 0, | |
8082 always_flags, arraysize(always_flags)); | |
8083 } | |
8084 | |
8085 TEST(TypedModeAmbientExportDeclarations) { | |
8086 const char* untyped_context_data[][2] = {{"", ""}, {NULL, NULL}}; | |
8087 const char* typed_context_data[][2] = {{"'use types'; ", ""}, {NULL, NULL}}; | |
8088 | |
8089 const char* correct_data[] = { | |
8090 // export var | |
8091 "export declare var x", | |
8092 "export declare var x : number", | |
8093 "export declare var x, y", | |
8094 "export declare var x: number, y: string", | |
8095 "export declare var [x, y]", | |
8096 "export declare var [x, y] : [number, string]", | |
8097 "export declare var {a: x, b: y}", | |
8098 "export declare var {a: x, b: y} : {a: number, b: string}", | |
8099 // export function | |
8100 "export declare function f()", | |
8101 "export declare function f() : number", | |
8102 "export declare function f(x: number)", | |
8103 "export declare function f(x: number) : number", | |
8104 "export declare function f<A>()", | |
8105 "export declare function f<A>() : A", | |
8106 "export declare function f<A>(x: A)", | |
8107 "export declare function f<A, B>(x: A) : B", | |
8108 "export declare function* f()", | |
8109 "export declare function* f() : number", | |
8110 "export declare function* f(x: number)", | |
8111 "export declare function* f(x: number) : number", | |
8112 "export declare function* f<A>()", | |
8113 "export declare function* f<A>() : A", | |
8114 "export declare function* f<A>(x: A)", | |
8115 "export declare function* f<A, B>(x: A) : B", | |
8116 // export class | |
8117 #define VALID(members) \ | |
8118 "export declare class C {" members "}", \ | |
8119 "class B {}; export declare class C extends B {" members "}", \ | |
8120 "export declare class C implements I {" members "}", \ | |
8121 "export declare class C implements I, J {" members "}", \ | |
8122 "class B {}; export declare class C extends B implements I {" \ | |
8123 members "}", \ | |
8124 "class B {}; export declare class C extends B implements I, J {" \ | |
8125 members "}", \ | |
8126 "export declare class C<A, B> {" members "}", \ | |
8127 "class D {}; export declare class C<A, B> extends D {" members "}", \ | |
8128 "export declare class C<A, B> implements I<A, B> {" members "}", \ | |
8129 "class D {}; export declare class C<A, B> extends D " \ | |
8130 "implements I<A, B> {" members "}" | |
8131 VALID("constructor (x: number, y: boolean);" | |
8132 "x;" | |
8133 "y: boolean;" | |
8134 "'one': number;" | |
8135 "'2': boolean;" | |
8136 "3: string;" | |
8137 "f1 () : number;" | |
8138 "f2 (a: number[]) : number;" | |
8139 "f3 (a: number[], b: number) : number;" | |
8140 "f4 <A, B>(a: A, b: B) : [A, B];" | |
8141 "[x: string];" | |
8142 "[x: string] : number;" | |
8143 "[x: number];" | |
8144 "[x: number] : boolean;"), | |
8145 VALID("static x;" | |
8146 "static y: boolean;" | |
8147 "static 'one': number;" | |
8148 "static '2': boolean;" | |
8149 "static 3: string;" | |
8150 "static f1 () : number;" | |
8151 "static f2 (a: number[]) : number;" | |
8152 "static f3 (a: number[], b: number) : number;" | |
8153 "static f4 <A, B>(a: A, b: B) : [A, B];"), | |
8154 #undef VALID | |
8155 // export default function | |
8156 "export default declare function f()", | |
rossberg
2016/04/20 15:16:35
Sigh, this syntax is such a mess...
nickie
2016/04/21 13:57:10
Acknowledged. :-)
| |
8157 "export default declare function f() : number", | |
8158 "export default declare function f(x: number)", | |
8159 "export default declare function f(x: number) : boolean", | |
8160 "export default declare function f<A>()", | |
8161 "export default declare function f<A, B>(x: A) : B", | |
8162 "export default declare function ()", | |
8163 "export default declare function () : number", | |
8164 "export default declare function (x: number)", | |
8165 "export default declare function (x: number) : number", | |
8166 "export default declare function <A>()", | |
8167 "export default declare function <A, B>(x: A) : B", | |
8168 "export default declare function* f()", | |
8169 "export default declare function* f() : number", | |
8170 "export default declare function* f(x: number)", | |
8171 "export default declare function* f(x: number) : boolean", | |
8172 "export default declare function* f<A>()", | |
8173 "export default declare function* f<A, B>(x: A) : B", | |
8174 "export default declare function* ()", | |
8175 "export default declare function* () : number", | |
8176 "export default declare function* (x: number)", | |
8177 "export default declare function* (x: number) : number", | |
8178 "export default declare function* <A>()", | |
8179 "export default declare function* <A, B>(x: A) : B", | |
8180 // export default class | |
8181 "export default declare class C implements I { x: number }", | |
8182 NULL | |
8183 }; | |
8184 | |
8185 const char* error_data[] = { | |
8186 // export var | |
8187 "export declare var x : ()", | |
8188 "export declare var x = 42", | |
8189 "export declare x: number = 42", | |
8190 "export declare x = 42, y = 'hello'", | |
8191 "export declare x: number = 42, y: string = 'hello'", | |
8192 // export function | |
8193 #define INVALID(decl) "export declare " decl " {}" | |
8194 INVALID("function f()"), | |
8195 INVALID("function f() : number"), | |
8196 INVALID("function f(x: number)"), | |
8197 INVALID("function f(x: number) : number"), | |
8198 INVALID("function f<A>()"), | |
8199 INVALID("function f<A>() : A"), | |
8200 INVALID("function f<A>(x: A)"), | |
8201 INVALID("function f<A, B>(x: A) : B"), | |
8202 INVALID("function* f()"), | |
8203 INVALID("function* f() : number"), | |
8204 INVALID("function* f(x: number)"), | |
8205 INVALID("function* f(x: number) : number"), | |
8206 INVALID("function* f<A>()"), | |
8207 INVALID("function* f<A>() : A"), | |
8208 INVALID("function* f<A>(x: A)"), | |
8209 INVALID("function* f<A, B>(x: A) : B"), | |
8210 #undef INVALID | |
8211 "export declare function f() : ()", | |
8212 "export declare function f(x? = 42) : ()", | |
8213 "export declare function f<>()", | |
8214 "export declare function ()", | |
8215 "export declare function () : number", | |
8216 "export declare function (x: number)", | |
8217 "export declare function (x: number) : number", | |
8218 "export declare function <A>()", | |
8219 "export declare function <A>() : A", | |
8220 "export declare function <A>(x: A)", | |
8221 "export declare function <A, B>(x: A) : B", | |
8222 // export class | |
8223 #define INVALID1(member) \ | |
8224 "export declare class C {" member "}", \ | |
8225 "class B {}; export declare class C extends B {" member "}", \ | |
8226 "export declare class C implements I {" member "}", \ | |
8227 "export declare class C implements I, J {" member "}", \ | |
8228 "class B {}; export declare class C extends B implements I {" \ | |
8229 member "}", \ | |
8230 "class B {}; export declare class C extends B implements I, J {" \ | |
8231 member "}", \ | |
8232 "export declare class C<A, B> {" member "}", \ | |
8233 "class D {}; export declare class C<A, B> extends D {" member "}", \ | |
8234 "export declare class C<A, B> implements I<A, B> {" member "}", \ | |
8235 "class D {}; export declare class C<A, B> extends D " \ | |
8236 "implements I<A, B> {" member "}" | |
8237 #define INVALID2(member, suffix) \ | |
8238 INVALID1(member), \ | |
8239 INVALID1(member " " suffix) | |
8240 INVALID2("constructor (a : number) : boolean", "{}"), | |
8241 INVALID2("constructor <A>(a : A)", "{}"), | |
8242 INVALID1("x = 42"), | |
8243 INVALID1("x : number = 42"), | |
8244 INVALID1("'four': number = 4"), | |
8245 INVALID1("'5': boolean = false"), | |
8246 INVALID1("6: string[] = [...['six', 'six', 'and', 'six']]"), | |
8247 INVALID2("get x ()", "{ return 42; }"), | |
8248 INVALID2("get x () : number", "{ return 42; }"), | |
8249 INVALID2("set x (a)", "{}"), | |
8250 INVALID2("set x (a) : void", "{}"), | |
8251 INVALID2("set x (a : number)", "{}"), | |
8252 INVALID2("set x (a : number) : void", "{}"), | |
8253 INVALID2("get x (a)", "{ return 42; }"), | |
8254 INVALID2("get x (a) : number", "{ return 42; }"), | |
8255 INVALID2("get x (a, b)", "{ return 42; }"), | |
8256 INVALID2("get x (a, b) : number", "{ return 42; }"), | |
8257 INVALID2("get x (a : number)", "{ return 42; }"), | |
8258 INVALID2("get x (a : number) : number", "{ return 42; }"), | |
8259 INVALID2("get x <A>()", "{ return 42; }"), | |
8260 INVALID2("set x ()", "{}"), | |
8261 INVALID2("set x () : void", "{}"), | |
8262 INVALID2("set x (a : number, b : number)", "{}"), | |
8263 INVALID2("set x (a : number, b : number) : void", "{}"), | |
8264 INVALID2("set x (...rest)", "{}"), | |
8265 INVALID2("set x (...rest : string[]) : void", "{}"), | |
8266 INVALID2("set x <A>(a : A)", "{}"), | |
8267 #undef INVALID1 | |
8268 #undef INVALID2 | |
8269 // export other ambients | |
8270 "export declare type N = number", | |
8271 "export declare interface I {}", | |
8272 // export invalid ambients | |
8273 "export declare x = 42", | |
8274 "export declare {}", | |
8275 "export declare while (true) {}", | |
8276 // export other forms | |
8277 "export declare * from 'somewhere'", | |
8278 "export declare {}", | |
8279 "export declare { something }", | |
8280 "export declare { something as different }", | |
8281 "export declare {} from 'somewhere'", | |
8282 "export declare { something } from 'somewhere'", | |
8283 "export declare { something as different } from 'somewhere'", | |
8284 "export declare default function f()", | |
8285 // export default var | |
8286 "export default declare var x", | |
8287 "export default declare var x : number", | |
8288 "export default declare var x = 42", | |
8289 "export default declare x: number = 42", | |
8290 // export default function | |
8291 "export default declare function f() {}", | |
8292 "export default declare function f(x: number) : boolean {}", | |
8293 "export default declare function f<A, B>(x: A) : B {}", | |
8294 "export default declare function f<>()", | |
8295 "export default declare function () {}", | |
8296 "export default declare function () : number {}", | |
8297 "export default declare function (x: number) {}", | |
8298 "export default declare function (x: number) : number {}", | |
8299 "export default declare function* f() {}", | |
8300 "export default declare function* f(x: number) : boolean {}", | |
8301 "export default declare function* f<A, B>(x: A) : B {}", | |
8302 "export default declare function* f<>()", | |
8303 "export default declare function* () {}", | |
8304 "export default declare function* () : number {}", | |
8305 "export default declare function* (x: number) {}", | |
8306 "export default declare function* (x: number) : number {}", | |
8307 // export default class | |
8308 "export default declare class C { x: number = 42 }", | |
8309 "export default declare class C { get x() : number }", | |
8310 // export default ... | |
8311 "export default declare type N = number", | |
8312 "export default declare interface I {}", | |
8313 "export default declare x = 42", | |
8314 "export default declare {}", | |
8315 "export default declare while (true) {}", | |
8316 NULL | |
8317 }; | |
8318 | |
8319 static const ParserFlag always_flags[] = {kAllowTypes}; | |
8320 RunModuleParserSyncTest(untyped_context_data, correct_data, kError, NULL, 0, | |
8321 always_flags, arraysize(always_flags)); | |
8322 RunModuleParserSyncTest(typed_context_data, correct_data, kSuccess, NULL, 0, | |
8323 always_flags, arraysize(always_flags)); | |
8324 RunModuleParserSyncTest(untyped_context_data, error_data, kError, NULL, 0, | |
8325 always_flags, arraysize(always_flags)); | |
8326 RunModuleParserSyncTest(typed_context_data, error_data, kError, NULL, 0, | |
8327 always_flags, arraysize(always_flags)); | |
8328 } | |
OLD | NEW |