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

Side by Side Diff: test/cctest/test-parsing.cc

Issue 1309813007: [es6] implement destructuring assignment (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Remove facilities for rewriting the expression multiple ways Created 5 years 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 unified diff | Download patch
OLDNEW
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 1482 matching lines...) Expand 10 before | Expand all | Expand 10 after
1493 1493
1494 1494
1495 enum ParserFlag { 1495 enum ParserFlag {
1496 kAllowLazy, 1496 kAllowLazy,
1497 kAllowNatives, 1497 kAllowNatives,
1498 kAllowHarmonyDefaultParameters, 1498 kAllowHarmonyDefaultParameters,
1499 kAllowHarmonyRestParameters, 1499 kAllowHarmonyRestParameters,
1500 kAllowHarmonySloppy, 1500 kAllowHarmonySloppy,
1501 kAllowHarmonySloppyLet, 1501 kAllowHarmonySloppyLet,
1502 kAllowHarmonyDestructuring, 1502 kAllowHarmonyDestructuring,
1503 kAllowHarmonyDestructuringAssignment,
1503 kAllowHarmonyNewTarget, 1504 kAllowHarmonyNewTarget,
1504 kAllowStrongMode, 1505 kAllowStrongMode,
1505 kNoLegacyConst 1506 kNoLegacyConst
1506 }; 1507 };
1507 1508
1508 1509
1509 enum ParserSyncTestResult { 1510 enum ParserSyncTestResult {
1510 kSuccessOrError, 1511 kSuccessOrError,
1511 kSuccess, 1512 kSuccess,
1512 kError 1513 kError
1513 }; 1514 };
1514 1515
1515 template <typename Traits> 1516 template <typename Traits>
1516 void SetParserFlags(i::ParserBase<Traits>* parser, 1517 void SetParserFlags(i::ParserBase<Traits>* parser,
1517 i::EnumSet<ParserFlag> flags) { 1518 i::EnumSet<ParserFlag> flags) {
1518 parser->set_allow_lazy(flags.Contains(kAllowLazy)); 1519 parser->set_allow_lazy(flags.Contains(kAllowLazy));
1519 parser->set_allow_natives(flags.Contains(kAllowNatives)); 1520 parser->set_allow_natives(flags.Contains(kAllowNatives));
1520 parser->set_allow_harmony_default_parameters( 1521 parser->set_allow_harmony_default_parameters(
1521 flags.Contains(kAllowHarmonyDefaultParameters)); 1522 flags.Contains(kAllowHarmonyDefaultParameters));
1522 parser->set_allow_harmony_rest_parameters( 1523 parser->set_allow_harmony_rest_parameters(
1523 flags.Contains(kAllowHarmonyRestParameters)); 1524 flags.Contains(kAllowHarmonyRestParameters));
1524 parser->set_allow_harmony_sloppy(flags.Contains(kAllowHarmonySloppy)); 1525 parser->set_allow_harmony_sloppy(flags.Contains(kAllowHarmonySloppy));
1525 parser->set_allow_harmony_sloppy_let(flags.Contains(kAllowHarmonySloppyLet)); 1526 parser->set_allow_harmony_sloppy_let(flags.Contains(kAllowHarmonySloppyLet));
1526 parser->set_allow_harmony_destructuring_bind( 1527 parser->set_allow_harmony_destructuring_bind(
1527 flags.Contains(kAllowHarmonyDestructuring)); 1528 flags.Contains(kAllowHarmonyDestructuring));
1529 parser->set_allow_harmony_destructuring_assignment(
1530 flags.Contains(kAllowHarmonyDestructuringAssignment));
1528 parser->set_allow_strong_mode(flags.Contains(kAllowStrongMode)); 1531 parser->set_allow_strong_mode(flags.Contains(kAllowStrongMode));
1529 parser->set_allow_legacy_const(!flags.Contains(kNoLegacyConst)); 1532 parser->set_allow_legacy_const(!flags.Contains(kNoLegacyConst));
1530 } 1533 }
1531 1534
1532 1535
1533 void TestParserSyncWithFlags(i::Handle<i::String> source, 1536 void TestParserSyncWithFlags(i::Handle<i::String> source,
1534 i::EnumSet<ParserFlag> flags, 1537 i::EnumSet<ParserFlag> flags,
1535 ParserSyncTestResult result, 1538 ParserSyncTestResult result,
1536 bool is_module = false) { 1539 bool is_module = false) {
1537 i::Isolate* isolate = CcTest::i_isolate(); 1540 i::Isolate* isolate = CcTest::i_isolate();
(...skipping 5267 matching lines...) Expand 10 before | Expand all | Expand 10 after
6805 NULL}; 6808 NULL};
6806 // clang-format on 6809 // clang-format on
6807 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring, 6810 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring,
6808 kAllowHarmonySloppyLet}; 6811 kAllowHarmonySloppyLet};
6809 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags, 6812 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
6810 arraysize(always_flags)); 6813 arraysize(always_flags));
6811 } 6814 }
6812 } 6815 }
6813 6816
6814 6817
6818 TEST(DestructuringAssignmentPositiveTests) {
6819 const char* context_data[][2] = {
6820 {"'use strict'; let x, y, z; (", " = {});"},
6821 {"var x, y, z; (", " = {});"},
6822 {"'use strict'; let x, y, z; for (x in ", " = {});"},
6823 {"'use strict'; let x, y, z; for (x of ", " = {});"},
6824 {"var x, y, z; for (x in ", " = {});"},
6825 {"var x, y, z; for (x of ", " = {});"},
6826 {NULL, NULL}};
6827
6828 // clang-format off
6829 const char* data[] = {
6830 "x",
6831
6832 "{ x : y }",
6833 "{ x : foo().y }",
6834 "{ x : foo()[y] }",
6835 "{ x : y.z }",
6836 "{ x : y[z] }",
6837 "{ x : { y } }",
6838 "{ x : { foo: y } }",
6839 "{ x : { foo: foo().y } }",
6840 "{ x : { foo: foo()[y] } }",
6841 "{ x : { foo: y.z } }",
6842 "{ x : { foo: y[z] } }",
6843 "{ x : [ y ] }",
6844 "{ x : [ foo().y ] }",
6845 "{ x : [ foo()[y] ] }",
6846 "{ x : [ y.z ] }",
6847 "{ x : [ y[z] ] }",
6848
6849 "{ x : y = 10 }",
6850 "{ x : foo().y = 10 }",
6851 "{ x : foo()[y] = 10 }",
6852 "{ x : y.z = 10 }",
6853 "{ x : y[z] = 10 }",
6854 "{ x : { y = 10 } = {} }",
6855 "{ x : { foo: y = 10 } = {} }",
6856 "{ x : { foo: foo().y = 10 } = {} }",
6857 "{ x : { foo: foo()[y] = 10 } = {} }",
6858 "{ x : { foo: y.z = 10 } = {} }",
6859 "{ x : { foo: y[z] = 10 } = {} }",
6860 "{ x : [ y = 10 ] = {} }",
6861 "{ x : [ foo().y = 10 ] = {} }",
6862 "{ x : [ foo()[y] = 10 ] = {} }",
6863 "{ x : [ y.z = 10 ] = {} }",
6864 "{ x : [ y[z] = 10 ] = {} }",
6865
6866 "[ x ]",
6867 "[ foo().x ]",
6868 "[ foo()[x] ]",
6869 "[ x.y ]",
6870 "[ x[y] ]",
6871 "[ { x } ]",
6872 "[ { x : y } ]",
6873 "[ { x : foo().y } ]",
6874 "[ { x : foo()[y] } ]",
6875 "[ { x : x.y } ]",
6876 "[ { x : x[y] } ]",
6877 "[ [ x ] ]",
6878 "[ [ foo().x ] ]",
6879 "[ [ foo()[x] ] ]",
6880 "[ [ x.y ] ]",
6881 "[ [ x[y] ] ]",
6882
6883 "[ x = 10 ]",
6884 "[ foo().x = 10 ]",
6885 "[ foo()[x] = 10 ]",
6886 "[ x.y = 10 ]",
6887 "[ x[y] = 10 ]",
6888 "[ { x = 10 } = {} ]",
6889 "[ { x : y = 10 } = {} ]",
6890 "[ { x : foo().y = 10 } = {} ]",
6891 "[ { x : foo()[y] = 10 } = {} ]",
6892 "[ { x : x.y = 10 } = {} ]",
6893 "[ { x : x[y] = 10 } = {} ]",
6894 "[ [ x = 10 ] = {} ]",
6895 "[ [ foo().x = 10 ] = {} ]",
6896 "[ [ foo()[x] = 10 ] = {} ]",
6897 "[ [ x.y = 10 ] = {} ]",
6898 "[ [ x[y] = 10 ] = {} ]",
6899
6900 "{ x : y }",
6901 "{ x : y = 1 }",
6902 "{ x }",
6903 "{ x, y, z }",
6904 "{ x = 1, y: z, z: y }",
6905 "{x = 42, y = 15}",
6906 "[x]",
6907 "[x = 1]",
6908 "[x,y,z]",
6909 "[x, y = 42, z]",
6910 "{ x : x, y : y }",
6911 "{ x : x = 1, y : y }",
6912 "{ x : x, y : y = 42 }",
6913 "[]",
6914 "{}",
6915 "[{x:x, y:y}, [,x,z,]]",
6916 "[{x:x = 1, y:y = 2}, [z = 3, z = 4, z = 5]]",
6917 "[x,,y]",
6918 "[(x),,(y)]",
6919 "[(x)]",
6920 "{42 : x}",
6921 "{42 : x = 42}",
6922 "{42e-2 : x}",
6923 "{42e-2 : x = 42}",
6924 "{'hi' : x}",
6925 "{'hi' : x = 42}",
6926 "{var: x}",
6927 "{var: x = 42}",
6928 "{var: (x) = 42}",
6929 "{[x] : z}",
6930 "{[1+1] : z}",
6931 "{[1+1] : (z)}",
6932 "{[foo()] : z}",
6933 "{[foo()] : (z)}",
6934 "{[foo()] : foo().bar}",
6935 "{[foo()] : foo()['bar']}",
6936 "{[foo()] : this.bar}",
6937 "{[foo()] : this['bar']}",
6938 "{[foo()] : 'foo'.bar}",
6939 "{[foo()] : 'foo'['bar']}",
6940 "[...x]",
6941 "[x,y,...z]",
6942 "[x,,...z]",
6943 "{ x: y } = z",
6944 "[x, y] = z",
6945 "{ x: y } = { z }",
6946 "[x, y] = { z }",
6947 "{ x: y } = [ z ]",
6948 "[x, y] = [ z ]",
6949 "[((x, y) => z).x]",
6950 "{x: ((y, z) => z).x}",
6951 "[((x, y) => z)['x']]",
6952 "{x: ((y, z) => z)['x']}",
6953
6954 "{x: { y = 10 } }",
6955 "[(({ x } = { x: 1 }) => x).a]",
6956 NULL};
6957 // clang-format on
6958 static const ParserFlag always_flags[] = {
6959 kAllowHarmonyDestructuringAssignment, kAllowHarmonyDestructuring,
6960 kAllowHarmonyDefaultParameters};
6961 RunParserSyncTest(context_data, data, kSuccess, NULL, 0, always_flags,
6962 arraysize(always_flags));
6963
6964 const char* empty_context_data[][2] = {
6965 {"'use strict';", ""}, {"", ""}, {NULL, NULL}};
6966
6967 // CoverInitializedName ambiguity handling in various contexts
6968 const char* ambiguity_data[] = {
6969 "var foo = { x = 10 } = {};",
6970 "var foo = { q } = { x = 10 } = {};",
6971 "var foo; foo = { x = 10 } = {};",
6972 "var foo; foo = { q } = { x = 10 } = {};",
6973 "var x; ({ x = 10 } = {});",
6974 "var q, x; ({ q } = { x = 10 } = {});",
6975 "var x; [{ x = 10 } = {}]",
6976 "var x; (true ? { x = true } = {} : { x = false } = {})",
6977 "var q, x; (q, { x = 10 } = {});",
6978 "var { x = 10 } = { x = 20 } = {};",
6979 "var { x = 10 } = (o = { x = 20 } = {});",
6980 "var x; (({ x = 10 } = { x = 20 } = {}) => x)({})",
6981 NULL,
6982 };
6983 RunParserSyncTest(empty_context_data, ambiguity_data, kSuccess, NULL, 0,
6984 always_flags, arraysize(always_flags));
6985 }
6986
6987
6988 TEST(DestructuringAssignmentNegativeTests) {
6989 const char* context_data[][2] = {
6990 {"'use strict'; let x, y, z; (", " = {});"},
6991 {"var x, y, z; (", " = {});"},
6992 {"'use strict'; let x, y, z; for (x in ", " = {});"},
6993 {"'use strict'; let x, y, z; for (x of ", " = {});"},
6994 {"var x, y, z; for (x in ", " = {});"},
6995 {"var x, y, z; for (x of ", " = {});"},
6996 {NULL, NULL}};
6997
6998 // clang-format off
6999 const char* data[] = {
7000 "{ x : ++y }",
7001 "{ x : y * 2 }",
7002 "{ ...x }",
7003 "{ get x() {} }",
7004 "{ set x() {} }",
7005 "{ x: y() }",
7006 "{ this }",
7007 "{ x: this }",
7008 "{ x: this = 1 }",
7009 "{ super }",
7010 "{ x: super }",
7011 "{ x: super = 1 }",
7012 "{ new.target }",
7013 "{ x: new.target }",
7014 "{ x: new.target = 1 }",
7015 "[x--]",
7016 "[--x = 1]",
7017 "[x()]",
7018 "[this]",
7019 "[this = 1]",
7020 "[new.target]",
7021 "[new.target = 1]",
7022 "[super]",
7023 "[super = 1]",
7024 "[function f() {}]",
7025 "[50]",
7026 "[(50)]",
7027 "[(function() {})]",
7028 "[(foo())]",
7029 "{ x: 50 }",
7030 "{ x: (50) }",
7031 "['str']",
7032 "{ x: 'str' }",
7033 "{ x: ('str') }",
7034 "{ x: (foo()) }",
7035 "{ x: (function() {}) }",
7036 "{ x: y } = 'str'",
7037 "[x, y] = 'str'",
7038 "[(x,y) => z]",
7039 "{x: (y) => z}",
7040 "[x, ...y, z]",
7041 "[...x,]",
7042 "[x, y, ...z = 1]",
7043 "[...z = 1]",
7044 NULL};
7045 // clang-format on
7046 static const ParserFlag always_flags[] = {
7047 kAllowHarmonyDestructuringAssignment, kAllowHarmonyDestructuring,
7048 kAllowHarmonyDefaultParameters};
7049 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
7050 arraysize(always_flags));
7051
7052 const char* empty_context_data[][2] = {
7053 {"'use strict';", ""}, {"", ""}, {NULL, NULL}};
7054
7055 // CoverInitializedName ambiguity handling in various contexts
7056 const char* ambiguity_data[] = {
7057 "var foo = { x = 10 };",
7058 "var foo = { q } = { x = 10 };",
7059 "var foo; foo = { x = 10 };",
7060 "var foo; foo = { q } = { x = 10 };",
7061 "var x; ({ x = 10 });",
7062 "var q, x; ({ q } = { x = 10 });",
7063 "var x; [{ x = 10 }]",
7064 "var x; (true ? { x = true } : { x = false })",
7065 "var q, x; (q, { x = 10 });",
7066 "var { x = 10 } = { x = 20 };",
7067 "var { x = 10 } = (o = { x = 20 });",
7068 "var x; (({ x = 10 } = { x = 20 }) => x)({})",
7069 NULL,
7070 };
7071 RunParserSyncTest(empty_context_data, ambiguity_data, kError, NULL, 0,
7072 always_flags, arraysize(always_flags));
7073 }
7074
7075
6815 TEST(DestructuringDisallowPatternsInForVarIn) { 7076 TEST(DestructuringDisallowPatternsInForVarIn) {
6816 i::FLAG_harmony_destructuring_bind = true; 7077 i::FLAG_harmony_destructuring_bind = true;
6817 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring}; 7078 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring};
6818 const char* context_data[][2] = { 7079 const char* context_data[][2] = {
6819 {"", ""}, {"function f() {", "}"}, {NULL, NULL}}; 7080 {"", ""}, {"function f() {", "}"}, {NULL, NULL}};
6820 // clang-format off 7081 // clang-format off
6821 const char* error_data[] = { 7082 const char* error_data[] = {
6822 "for (let x = {} in null);", 7083 "for (let x = {} in null);",
6823 "for (let x = {} of null);", 7084 "for (let x = {} of null);",
6824 NULL}; 7085 NULL};
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
6998 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring, 7259 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring,
6999 kAllowHarmonyDefaultParameters, 7260 kAllowHarmonyDefaultParameters,
7000 kAllowStrongMode}; 7261 kAllowStrongMode};
7001 7262
7002 RunParserSyncTest(sloppy_function_context_data, parameter_data, kSuccess, 7263 RunParserSyncTest(sloppy_function_context_data, parameter_data, kSuccess,
7003 NULL, 0, always_flags, arraysize(always_flags)); 7264 NULL, 0, always_flags, arraysize(always_flags));
7004 RunParserSyncTest(sloppy_function_context_data, destructuring_assignment_data, 7265 RunParserSyncTest(sloppy_function_context_data, destructuring_assignment_data,
7005 kSuccess, NULL, 0, always_flags, arraysize(always_flags)); 7266 kSuccess, NULL, 0, always_flags, arraysize(always_flags));
7006 RunParserSyncTest(sloppy_arrow_context_data, parameter_data, kSuccess, NULL, 7267 RunParserSyncTest(sloppy_arrow_context_data, parameter_data, kSuccess, NULL,
7007 0, always_flags, arraysize(always_flags)); 7268 0, always_flags, arraysize(always_flags));
7008 // TODO(wingo): Will change to kSuccess when destructuring assignment lands.
7009 RunParserSyncTest(sloppy_arrow_context_data, destructuring_assignment_data, 7269 RunParserSyncTest(sloppy_arrow_context_data, destructuring_assignment_data,
7010 kError, NULL, 0, always_flags, arraysize(always_flags)); 7270 kSuccess, NULL, 0, always_flags, arraysize(always_flags));
7011 7271
7012 RunParserSyncTest(strict_function_context_data, parameter_data, kError, NULL, 7272 RunParserSyncTest(strict_function_context_data, parameter_data, kError, NULL,
7013 0, always_flags, arraysize(always_flags)); 7273 0, always_flags, arraysize(always_flags));
7014 RunParserSyncTest(strict_function_context_data, destructuring_assignment_data, 7274 RunParserSyncTest(strict_function_context_data, destructuring_assignment_data,
7015 kError, NULL, 0, always_flags, arraysize(always_flags)); 7275 kError, NULL, 0, always_flags, arraysize(always_flags));
7016 RunParserSyncTest(strict_arrow_context_data, parameter_data, kError, NULL, 0, 7276 RunParserSyncTest(strict_arrow_context_data, parameter_data, kError, NULL, 0,
7017 always_flags, arraysize(always_flags)); 7277 always_flags, arraysize(always_flags));
7018 RunParserSyncTest(strict_arrow_context_data, destructuring_assignment_data, 7278 RunParserSyncTest(strict_arrow_context_data, destructuring_assignment_data,
7019 kError, NULL, 0, always_flags, arraysize(always_flags)); 7279 kError, NULL, 0, always_flags, arraysize(always_flags));
7020 7280
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after
7483 "var publ\\u0069c = 1;", 7743 "var publ\\u0069c = 1;",
7484 "var { publ\\u0069c } = {};", 7744 "var { publ\\u0069c } = {};",
7485 NULL}; 7745 NULL};
7486 RunParserSyncTest(sloppy_context_data, valid_data, kSuccess, NULL, 0, 7746 RunParserSyncTest(sloppy_context_data, valid_data, kSuccess, NULL, 0,
7487 always_flags, arraysize(always_flags)); 7747 always_flags, arraysize(always_flags));
7488 RunParserSyncTest(strict_context_data, valid_data, kError, NULL, 0, 7748 RunParserSyncTest(strict_context_data, valid_data, kError, NULL, 0,
7489 always_flags, arraysize(always_flags)); 7749 always_flags, arraysize(always_flags));
7490 RunModuleParserSyncTest(strict_context_data, valid_data, kError, NULL, 0, 7750 RunModuleParserSyncTest(strict_context_data, valid_data, kError, NULL, 0,
7491 always_flags, arraysize(always_flags)); 7751 always_flags, arraysize(always_flags));
7492 } 7752 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698