blob: b473097a4004453cb3db49008f5214550d8d337a [file] [log] [blame]
////////////////////////////////////////////////////////////////////////////////
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////
package org.apache.royale.formatter;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
// for member functions of classes, see TestMethodDeclaration
public class TestFunctionDeclaration extends BaseFormatterTests {
@Test
public void testPlaceOpenBraceOnNewLineWithEmptyBlock() {
FORMATTER formatter = new FORMATTER();
formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
formatter.placeOpenBraceOnNewLine = true;
formatter.insertSpaces = false;
String result = formatter.formatActionScriptText(
// @formatter:off
"function myFunction() {\n" +
"}",
// @formatter:on
problems
);
assertEquals(
// @formatter:off
"function myFunction()\n" +
"{\n" +
"}",
// @formatter:on
result);
}
@Test
public void testDisablePlaceOpenBraceOnNewLineWithEmptyBlock() {
FORMATTER formatter = new FORMATTER();
formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
formatter.placeOpenBraceOnNewLine = false;
formatter.insertSpaces = false;
String result = formatter.formatActionScriptText(
// @formatter:off
"function myFunction()\n" +
"{\n" +
"}",
// @formatter:on
problems
);
assertEquals(
// @formatter:off
"function myFunction() {\n" +
"}",
// @formatter:on
result);
}
@Test
public void testPlaceOpenBraceOnNewLineWithStatement() {
FORMATTER formatter = new FORMATTER();
formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
formatter.placeOpenBraceOnNewLine = true;
formatter.insertSpaces = false;
String result = formatter.formatActionScriptText(
// @formatter:off
"function myFunction() {\n" +
"\tstatement;\n" +
"}",
// @formatter:on
problems
);
assertEquals(
// @formatter:off
"function myFunction()\n" +
"{\n" +
"\tstatement;\n" +
"}",
// @formatter:on
result);
}
@Test
public void testDisablePlaceOpenBraceOnNewLineWithStatement() {
FORMATTER formatter = new FORMATTER();
formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
formatter.placeOpenBraceOnNewLine = false;
formatter.insertSpaces = false;
String result = formatter.formatActionScriptText(
// @formatter:off
"function myFunction()\n" +
"{\n" +
"\tstatement;\n" +
"}",
// @formatter:on
problems
);
assertEquals(
// @formatter:off
"function myFunction() {\n" +
"\tstatement;\n" +
"}",
// @formatter:on
result);
}
@Test
public void testReturnType() {
FORMATTER formatter = new FORMATTER();
formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
formatter.placeOpenBraceOnNewLine = true;
formatter.insertSpaces = false;
String result = formatter.formatActionScriptText(
// @formatter:off
"function myFunction():void\n" +
"{\n" +
"\tstatement;\n" +
"}",
// @formatter:on
problems
);
assertEquals(
// @formatter:off
"function myFunction():void\n" +
"{\n" +
"\tstatement;\n" +
"}",
// @formatter:on
result);
}
@Test
public void testParameter() {
FORMATTER formatter = new FORMATTER();
formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
formatter.placeOpenBraceOnNewLine = true;
formatter.insertSpaces = false;
String result = formatter.formatActionScriptText(
// @formatter:off
"function myFunction(p1:String)\n" +
"{\n" +
"\tstatement;\n" +
"}",
// @formatter:on
problems
);
assertEquals(
// @formatter:off
"function myFunction(p1:String)\n" +
"{\n" +
"\tstatement;\n" +
"}",
// @formatter:on
result);
}
@Test
public void testParameters() {
FORMATTER formatter = new FORMATTER();
formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
formatter.placeOpenBraceOnNewLine = true;
formatter.insertSpaces = false;
String result = formatter.formatActionScriptText(
// @formatter:off
"function myFunction(p1:String, p2:Number)\n" +
"{\n" +
"\tstatement;\n" +
"}",
// @formatter:on
problems
);
assertEquals(
// @formatter:off
"function myFunction(p1:String, p2:Number)\n" +
"{\n" +
"\tstatement;\n" +
"}",
// @formatter:on
result);
}
@Test
public void testNewLinesAmongParameters() {
FORMATTER formatter = new FORMATTER();
formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
formatter.placeOpenBraceOnNewLine = true;
formatter.insertSpaces = false;
String result = formatter.formatActionScriptText(
// @formatter:off
"function myFunction(p1:String,\n" +
"p2:Number):void\n" +
"{\n" +
"\tstatement;\n" +
"\tstatement;\n" +
"}",
// @formatter:on
problems
);
assertEquals(
// @formatter:off
"function myFunction(p1:String,\n" +
"\t\tp2:Number):void\n" +
"{\n" +
"\tstatement;\n" +
"\tstatement;\n" +
"}",
// @formatter:on
result);
}
@Test
public void testParametersWithRest() {
FORMATTER formatter = new FORMATTER();
formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
formatter.placeOpenBraceOnNewLine = true;
formatter.insertSpaces = false;
String result = formatter.formatActionScriptText(
// @formatter:off
"function myFunction(p1:String, ...rest)\n" +
"{\n" +
"\tstatement;\n" +
"}",
// @formatter:on
problems
);
assertEquals(
// @formatter:off
"function myFunction(p1:String, ...rest)\n" +
"{\n" +
"\tstatement;\n" +
"}",
// @formatter:on
result);
}
@Test
public void testParametersRestOnly() {
FORMATTER formatter = new FORMATTER();
formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
formatter.placeOpenBraceOnNewLine = true;
formatter.insertSpaces = false;
String result = formatter.formatActionScriptText(
// @formatter:off
"function myFunction(...rest)\n" +
"{\n" +
"\tstatement;\n" +
"}",
// @formatter:on
problems
);
assertEquals(
// @formatter:off
"function myFunction(...rest)\n" +
"{\n" +
"\tstatement;\n" +
"}",
// @formatter:on
result);
}
@Test
public void testWrappedInParentheses() {
FORMATTER formatter = new FORMATTER();
formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
formatter.placeOpenBraceOnNewLine = true;
formatter.insertSpaces = false;
String result = formatter.formatActionScriptText(
// @formatter:off
"(function myFunction(...rest)\n" +
"{\n" +
"\tstatement;\n" +
"});",
// @formatter:on
problems
);
assertEquals(
// @formatter:off
"(function myFunction(...rest)\n" +
"\t{\n" +
"\t\tstatement;\n" +
"\t});",
// @formatter:on
result);
}
@Test
public void testAsArgument() {
FORMATTER formatter = new FORMATTER();
formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
formatter.placeOpenBraceOnNewLine = true;
formatter.insertSpaces = false;
String result = formatter.formatActionScriptText(
// @formatter:off
"identifier(function myFunction(...rest)\n" +
"{\n" +
"\tstatement;\n" +
"}, 123.4);",
// @formatter:on
problems
);
assertEquals(
// @formatter:off
"identifier(function myFunction(...rest)\n" +
"\t{\n" +
"\t\tstatement;\n" +
"\t}, 123.4);",
// @formatter:on
result);
}
@Test
public void testCollapseEmptyBlock() {
FORMATTER formatter = new FORMATTER();
formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
formatter.placeOpenBraceOnNewLine = false;
formatter.collapseEmptyBlocks = true;
String result = formatter.formatActionScriptText(
// @formatter:off
"function myFunction() {\n" +
"}",
// @formatter:on
problems
);
assertEquals(
// @formatter:off
"function myFunction() {}",
// @formatter:on
result);
}
}