diff options
Diffstat (limited to 'unittests/Format/FormatTestJS.cpp')
| -rw-r--r-- | unittests/Format/FormatTestJS.cpp | 202 | 
1 files changed, 182 insertions, 20 deletions
| diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index f8802b85d606..780b02f746e9 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -81,11 +81,23 @@ TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {                 getGoogleJSStyleWithColumns(20));    verifyFormat("var b = a.map((x) => x + 1);"); +  verifyFormat("return ('aaa') in bbbb;"); +} + +TEST_F(FormatTestJS, UnderstandsAmpAmp) { +  verifyFormat("e && e.SomeFunction();"); +} + +TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) { +  verifyFormat("not.and.or.not_eq = 1;");  }  TEST_F(FormatTestJS, ES6DestructuringAssignment) {    verifyFormat("var [a, b, c] = [1, 2, 3];"); -  verifyFormat("var {a, b} = {a: 1, b: 2};"); +  verifyFormat("var {a, b} = {\n" +               "  a: 1,\n" +               "  b: 2\n" +               "};");  }  TEST_F(FormatTestJS, ContainerLiterals) { @@ -102,29 +114,45 @@ TEST_F(FormatTestJS, ContainerLiterals) {                 "};");    verifyFormat("return {\n"                 "  a: a,\n" -               "  link:\n" -               "      function() {\n" -               "        f();  //\n" -               "      },\n" -               "  link:\n" -               "      function() {\n" -               "        f();  //\n" -               "      }\n" +               "  link: function() {\n" +               "    f();  //\n" +               "  },\n" +               "  link: function() {\n" +               "    f();  //\n" +               "  }\n" +               "};"); +  verifyFormat("var stuff = {\n" +               "  // comment for update\n" +               "  update: false,\n" +               "  // comment for modules\n" +               "  modules: false,\n" +               "  // comment for tasks\n" +               "  tasks: false\n" +               "};"); +  verifyFormat("return {\n" +               "  'finish':\n" +               "      //\n" +               "      a\n" +               "};"); +  verifyFormat("var obj = {\n" +               "  fooooooooo: function(x) {\n" +               "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" +               "  }\n"                 "};");  }  TEST_F(FormatTestJS, SpacesInContainerLiterals) {    verifyFormat("var arr = [1, 2, 3];"); -  verifyFormat("var obj = {a: 1, b: 2, c: 3};"); +  verifyFormat("f({a: 1, b: 2, c: 3});");    verifyFormat("var object_literal_with_long_name = {\n"                 "  a: 'aaaaaaaaaaaaaaaaaa',\n"                 "  b: 'bbbbbbbbbbbbbbbbbb'\n"                 "};"); -  verifyFormat("var obj = {a: 1, b: 2, c: 3};", +  verifyFormat("f({a: 1, b: 2, c: 3});",                 getChromiumStyle(FormatStyle::LK_JavaScript)); -  verifyFormat("someVariable = {'a': [{}]};"); +  verifyFormat("f({'a': [{}]});");  }  TEST_F(FormatTestJS, SingleQuoteStrings) { @@ -138,6 +166,22 @@ TEST_F(FormatTestJS, GoogScopes) {                 "});  // goog.scope");  } +TEST_F(FormatTestJS, GoogModules) { +  verifyFormat("goog.module('this.is.really.absurdly.long');", +               getGoogleJSStyleWithColumns(40)); +  verifyFormat("goog.require('this.is.really.absurdly.long');", +               getGoogleJSStyleWithColumns(40)); +  verifyFormat("goog.provide('this.is.really.absurdly.long');", +               getGoogleJSStyleWithColumns(40)); +  verifyFormat("var long = goog.require('this.is.really.absurdly.long');", +               getGoogleJSStyleWithColumns(40)); + +  // These should be wrapped normally. +  verifyFormat( +      "var MyLongClassName =\n" +      "    goog.module.get('my.long.module.name.followedBy.MyLongClassName');"); +} +  TEST_F(FormatTestJS, FormatsFreestandingFunctions) {    verifyFormat("function outer1(a, b) {\n"                 "  function inner1(a, b) { return a; }\n" @@ -150,8 +194,11 @@ TEST_F(FormatTestJS, FormatsFreestandingFunctions) {  }  TEST_F(FormatTestJS, FunctionLiterals) { +  verifyFormat("doFoo(function() {});");    verifyFormat("doFoo(function() { return 1; });"); -  verifyFormat("var func = function() { return 1; };"); +  verifyFormat("var func = function() {\n" +               "  return 1;\n" +               "};");    verifyFormat("return {\n"                 "  body: {\n"                 "    setAttribute: function(key, val) { this[key] = val; },\n" @@ -159,7 +206,13 @@ TEST_F(FormatTestJS, FunctionLiterals) {                 "    style: {direction: ''}\n"                 "  }\n"                 "};"); -  EXPECT_EQ("abc = xyz ? function() { return 1; } : function() { return -1; };", +  EXPECT_EQ("abc = xyz ?\n" +            "          function() {\n" +            "            return 1;\n" +            "          } :\n" +            "          function() {\n" +            "            return -1;\n" +            "          };",              format("abc=xyz?function(){return 1;}:function(){return -1;};"));    verifyFormat("var closure = goog.bind(\n" @@ -181,13 +234,18 @@ TEST_F(FormatTestJS, FunctionLiterals) {                 "    };\n"                 "  }\n"                 "};"); +  verifyFormat("{\n" +               "  var someVariable = function(x) {\n" +               "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" +               "  };\n" +               "}"); -  verifyFormat("var x = {a: function() { return 1; }};", -               getGoogleJSStyleWithColumns(38)); -  verifyFormat("var x = {\n" +  verifyFormat("f({a: function() { return 1; }});", +               getGoogleJSStyleWithColumns(33)); +  verifyFormat("f({\n"                 "  a: function() { return 1; }\n" -               "};", -               getGoogleJSStyleWithColumns(37)); +               "});", +               getGoogleJSStyleWithColumns(32));    verifyFormat("return {\n"                 "  a: function SomeFunction() {\n" @@ -195,6 +253,74 @@ TEST_F(FormatTestJS, FunctionLiterals) {                 "    return 1;\n"                 "  }\n"                 "};"); +  verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n" +               "    .then(goog.bind(function(aaaaaaaaaaa) {\n" +               "      someFunction();\n" +               "      someFunction();\n" +               "    }, this), aaaaaaaaaaaaaaaaa);"); + +  // FIXME: This is not ideal yet. +  verifyFormat("someFunction(goog.bind(\n" +               "                 function() {\n" +               "                   doSomething();\n" +               "                   doSomething();\n" +               "                 },\n" +               "                 this),\n" +               "             goog.bind(function() {\n" +               "               doSomething();\n" +               "               doSomething();\n" +               "             }, this));"); +} + +TEST_F(FormatTestJS, InliningFunctionLiterals) { +  FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); +  Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; +  verifyFormat("var func = function() {\n" +               "  return 1;\n" +               "};", +               Style); +  verifyFormat("var func = doSomething(function() { return 1; });", Style); +  verifyFormat("var outer = function() {\n" +               "  var inner = function() { return 1; }\n" +               "};", +               Style); +  verifyFormat("function outer1(a, b) {\n" +               "  function inner1(a, b) { return a; }\n" +               "}", +               Style); + +  Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; +  verifyFormat("var func = function() { return 1; };", Style); +  verifyFormat("var func = doSomething(function() { return 1; });", Style); +  verifyFormat( +      "var outer = function() { var inner = function() { return 1; } };", +      Style); +  verifyFormat("function outer1(a, b) {\n" +               "  function inner1(a, b) { return a; }\n" +               "}", +               Style); + +  Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; +  verifyFormat("var func = function() {\n" +               "  return 1;\n" +               "};", +               Style); +  verifyFormat("var func = doSomething(function() {\n" +               "  return 1;\n" +               "});", +               Style); +  verifyFormat("var outer = function() {\n" +               "  var inner = function() {\n" +               "    return 1;\n" +               "  }\n" +               "};", +               Style); +  verifyFormat("function outer1(a, b) {\n" +               "  function inner1(a, b) {\n" +               "    return a;\n" +               "  }\n" +               "}", +               Style);  }  TEST_F(FormatTestJS, MultipleFunctionLiterals) { @@ -228,10 +354,33 @@ TEST_F(FormatTestJS, MultipleFunctionLiterals) {                 "               doFoo();\n"                 "               doBaz();\n"                 "             });\n"); + +  verifyFormat("getSomeLongPromise()\n" +               "    .then(function(value) { body(); })\n" +               "    .thenCatch(function(error) {\n" +               "      body();\n" +               "      body();\n" +               "    });"); +  verifyFormat("getSomeLongPromise()\n" +               "    .then(function(value) {\n" +               "      body();\n" +               "      body();\n" +               "    })\n" +               "    .thenCatch(function(error) {\n" +               "      body();\n" +               "      body();\n" +               "    });"); + +  // FIXME: This is bad, but it used to be formatted correctly by accident. +  verifyFormat("getSomeLongPromise().then(function(value) {\n" +               "  body();\n" +               "}).thenCatch(function(error) { body(); });");  }  TEST_F(FormatTestJS, ReturnStatements) { -  verifyFormat("function() { return [hello, world]; }"); +  verifyFormat("function() {\n" +               "  return [hello, world];\n" +               "}");  }  TEST_F(FormatTestJS, ClosureStyleComments) { @@ -246,6 +395,11 @@ TEST_F(FormatTestJS, TryCatch) {                 "} finally {\n"                 "  h();\n"                 "}"); + +  // But, of course, "catch" is a perfectly fine function name in JavaScript. +  verifyFormat("someObject.catch();"); +  verifyFormat("someObject.new();"); +  verifyFormat("someObject.delete();");  }  TEST_F(FormatTestJS, StringLiteralConcatenation) { @@ -307,6 +461,12 @@ TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {    verifyFormat("var regex = /\\\\/g;");    verifyFormat("var regex = /\\a\\\\/g;");    verifyFormat("var regex = /\a\\//g;"); +  verifyFormat("var regex = /a\\//;\n" +               "var x = 0;"); +  EXPECT_EQ("var regex = /\\/*/;\n" +            "var x = 0;", +            format("var regex = /\\/*/;\n" +                   "var x=0;"));  }  TEST_F(FormatTestJS, RegexLiteralModifiers) { @@ -322,6 +482,8 @@ TEST_F(FormatTestJS, RegexLiteralLength) {    verifyFormat("var regex =\n"                 "    /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",                 getGoogleJSStyleWithColumns(60)); +  verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", +               getGoogleJSStyleWithColumns(50));  }  TEST_F(FormatTestJS, RegexLiteralExamples) { | 
