This is an automated email from the ASF dual-hosted git repository. joshtynjala pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/royale-compiler.git
commit b4672772792f68ba2eb72404909c68f64e24a5e2 Author: Josh Tynjala <[email protected]> AuthorDate: Thu Oct 27 13:41:09 2022 -0700 linter: start adding some tests --- linter/build.xml | 2 +- linter/src/test/build.xml | 93 +++++++++ .../royale/linter/rules/TestClassNameRule.java | 111 +++++++++++ .../royale/linter/rules/TestConstantNameRule.java | 85 ++++++++ .../royale/linter/rules/TestFieldNameRule.java | 123 ++++++++++++ .../royale/linter/rules/TestFunctionNameRule.java | 221 +++++++++++++++++++++ .../royale/linter/rules/TestInterfaceNameRule.java | 111 +++++++++++ .../rules/TestLocalVarAndParameterNameRule.java | 201 +++++++++++++++++++ .../apache/royale/linter/rules/TestMXMLIDRule.java | 124 ++++++++++++ .../royale/linter/rules/TestPackageNameRule.java | 148 ++++++++++++++ 10 files changed, 1218 insertions(+), 1 deletion(-) diff --git a/linter/build.xml b/linter/build.xml index 3ccd6b9a1..5ed64452f 100644 --- a/linter/build.xml +++ b/linter/build.xml @@ -108,7 +108,7 @@ </target> <target name="main" depends="jar"> - <!-- <antcall target="test"/> --> + <antcall target="test"/> <antcall target="jar-test"/> </target> diff --git a/linter/src/test/build.xml b/linter/src/test/build.xml new file mode 100644 index 000000000..43acdec94 --- /dev/null +++ b/linter/src/test/build.xml @@ -0,0 +1,93 @@ +<?xml version="1.0" ?> + +<!-- + + 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. + +--> + +<project name="linter.tests" default="main" basedir="."> + + <pathconvert property="linter.tests" dirsep="/"> + <path location="${basedir}"/> + </pathconvert> + + <property environment="env"/> + <property file="unittest.properties" /> + <property name="test.timeout" value="3000000" /> + <property name="maxmem" value="512" /> + + <condition property="sdk" value="${ROYALE_HOME}" else="${env.ROYALE_HOME}"> + <isset property="ROYALE_HOME" /> + </condition> + + <property name="linter" value="${linter.tests}/../.."/> + <property name="royale" value="${linter}/../compiler"/> + + <target name="jar.tests"> + <java jar="${royale}/lib/linter.jar" fork="true"/> + </target> + + <target name="compile.tests"> + <echo>linter is ${linter} royale is ${royale}</echo> + <delete dir="${linter}/target/test-classes"/> + <mkdir dir="${linter}/target/test-classes"/> + <javac debug="${javac.debug}" deprecation="${javac.deprecation}" destdir="${linter}/target/test-classes" includeAntRuntime="true"> + <src path="${linter.tests}/java"/> + <compilerarg value="-Xlint:all,-path"/> + <classpath> + <pathelement location="${linter}/../compiler-test-utils/target/classes"/> + <fileset dir="${royale}/lib" includes="**/*.jar"/> + </classpath> + </javac> + </target> + + <target name="tests" depends="compile.tests"> + <mkdir dir="${linter}/target/junit-reports"/> + <mkdir dir="${linter}/target/junit-temp"/> + <junit dir="${linter}" + fork="yes" forkMode="perBatch" maxmemory="${maxmem}m" timeout="${test.timeout}" + printsummary="true" showoutput="true" + haltonerror="true" haltonfailure="true" + failureproperty="tests.unit.failed"> + <jvmarg value="-Duser.language=en"/> + <jvmarg value="-Duser.country=US"/> + <classpath> + <pathelement location="${linter}/target/classes"/> + <pathelement location="${linter}/target/test-classes"/> + <pathelement location="${linter}/../compiler-test-utils/target/classes"/> + <fileset dir="${royale}/lib" includes="**/*.jar"/> + </classpath> + <batchtest todir="${linter}/target/junit-reports"> + <fileset dir="${linter}/target/test-classes"> + <include name="**/Test*.class"/> + </fileset> + </batchtest> + <formatter type="xml"/> + </junit> + </target> + + <target name="main" depends="jar.tests, tests"/> + + <target name="clean"> + <delete dir="${linter}/target/junit-reports"/> + <delete dir="${linter}/target/junit-temp"/> + </target> + + <target name="wipe" depends="clean"> + </target> + +</project> diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestClassNameRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestClassNameRule.java new file mode 100644 index 000000000..0674b97c5 --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestClassNameRule.java @@ -0,0 +1,111 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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.linter.rules; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.royale.compiler.problems.ICompilerProblem; +import org.apache.royale.linter.ASLinter; +import org.apache.royale.linter.LinterRule; +import org.apache.royale.linter.LinterSettings; +import org.junit.Test; + +public class TestClassNameRule { + @Test + public void testClassNameValid() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new ClassNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{}}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testClassNameStartsWithLowerCase() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new ClassNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class myClass{}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof ClassNameRule.ClassNameLinterProblem); + } + + @Test + public void testClassNameStartsWith$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new ClassNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class $MyClass{}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof ClassNameRule.ClassNameLinterProblem); + } + + @Test + public void testClassNameStartsWith_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new ClassNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class _MyClass{}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof ClassNameRule.ClassNameLinterProblem); + } + + @Test + public void testClassNameContains$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new ClassNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class My$Class{}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof ClassNameRule.ClassNameLinterProblem); + } + + @Test + public void testClassNameContains_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new ClassNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class My_Class{}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof ClassNameRule.ClassNameLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestConstantNameRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestConstantNameRule.java new file mode 100644 index 000000000..3f9adc1da --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestConstantNameRule.java @@ -0,0 +1,85 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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.linter.rules; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.royale.compiler.problems.ICompilerProblem; +import org.apache.royale.linter.ASLinter; +import org.apache.royale.linter.LinterRule; +import org.apache.royale.linter.LinterSettings; +import org.junit.Test; + +public class TestConstantNameRule { + @Test + public void testConstantNameValid() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new ConstantNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{const MY_CONSTANT:String;}}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testConstantNameContainsLowerCase() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new ConstantNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{const mY_CONSTANT:String;}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof ConstantNameRule.ConstantNameLinterProblem); + } + + @Test + public void testConstantNameStartsWith$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new ConstantNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{const $MY_CONSTANT:String;}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof ConstantNameRule.ConstantNameLinterProblem); + } + + @Test + public void testConstantNameStartsWith_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new ConstantNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{const _MY_CONSTANT:String;}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof ConstantNameRule.ConstantNameLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestFieldNameRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestFieldNameRule.java new file mode 100644 index 000000000..261d86aa8 --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestFieldNameRule.java @@ -0,0 +1,123 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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.linter.rules; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.royale.compiler.problems.ICompilerProblem; +import org.apache.royale.linter.ASLinter; +import org.apache.royale.linter.LinterRule; +import org.apache.royale.linter.LinterSettings; +import org.junit.Test; + +public class TestFieldNameRule { + @Test + public void testFieldNameValid() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FieldNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{var myField:String;}}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testFieldNameStartsWithUpperCase() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FieldNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{var MyField:String;}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FieldNameRule.FieldNameLinterProblem); + } + + @Test + public void testFieldNameStartsWith$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FieldNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{var $myField:String;}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FieldNameRule.FieldNameLinterProblem); + } + + @Test + public void testFieldNameStartsWith_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FieldNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{var _myField:String;}}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testFieldNameStartsWith_AndUpperCase() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FieldNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{var _MyField:String;}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FieldNameRule.FieldNameLinterProblem); + } + + @Test + public void testFieldNameContains$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FieldNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{var my$Field:String;}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FieldNameRule.FieldNameLinterProblem); + } + + @Test + public void testFieldNameContains_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FieldNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{var my_Field:String;}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FieldNameRule.FieldNameLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestFunctionNameRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestFunctionNameRule.java new file mode 100644 index 000000000..d3f261c5d --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestFunctionNameRule.java @@ -0,0 +1,221 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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.linter.rules; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.royale.compiler.problems.ICompilerProblem; +import org.apache.royale.linter.ASLinter; +import org.apache.royale.linter.LinterRule; +import org.apache.royale.linter.LinterSettings; +import org.junit.Test; + +public class TestFunctionNameRule { + @Test + public void testMethodNameValid() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FunctionNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myMethod():void{}}}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testMethodNameStartsWithUpperCase() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FunctionNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function MyMethod():void{}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FunctionNameRule.FunctionNameLinterProblem); + } + + @Test + public void testMethodNameStartsWith$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FunctionNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function $myMethod():void{}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FunctionNameRule.FunctionNameLinterProblem); + } + + @Test + public void testMethodNameStartsWith_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FunctionNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function _myMethod():void{}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FunctionNameRule.FunctionNameLinterProblem); + } + + @Test + public void testMethodNameStartsWith_AndUpperCase() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FunctionNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function _MyMethod():void{}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FunctionNameRule.FunctionNameLinterProblem); + } + + @Test + public void testMethodNameContains$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FunctionNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function my$Method():void{}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FunctionNameRule.FunctionNameLinterProblem); + } + + @Test + public void testMethodNameContains_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FunctionNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function my_Method():void{}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FunctionNameRule.FunctionNameLinterProblem); + } + + @Test + public void testLocalFunctionNameValid() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FunctionNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myMethod():void{function myFunction():void{}}}}", + problems); + assertEquals(0, problems.size()); + } + + @Test + public void testLocalFunctionNameStartsWithUpperCase() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FunctionNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myMethod():void{function MyFunction():void{}}}}", + problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FunctionNameRule.FunctionNameLinterProblem); + } + + @Test + public void testLocalFunctionNameStartsWith$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FunctionNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myMethod():void{function $myFunction():void{}}}}", + problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FunctionNameRule.FunctionNameLinterProblem); + } + + @Test + public void testLocalFunctionNameStartsWith_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FunctionNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myMethod():void{function _myFunction():void{}}}}", + problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FunctionNameRule.FunctionNameLinterProblem); + } + + @Test + public void testLocalFunctionNameStartsWith_AndUpperCase() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FunctionNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myMethod():void{function _MyFunction():void{}}}}", + problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FunctionNameRule.FunctionNameLinterProblem); + } + + @Test + public void testLocalFunctionNameContains$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FunctionNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myMethod():void{function my$Function():void{}}}}", + problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FunctionNameRule.FunctionNameLinterProblem); + } + + @Test + public void testLocalFunctionNameContains_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new FunctionNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myMethod():void{function my_Function():void{}}}}", + problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof FunctionNameRule.FunctionNameLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestInterfaceNameRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestInterfaceNameRule.java new file mode 100644 index 000000000..3b4a66ecb --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestInterfaceNameRule.java @@ -0,0 +1,111 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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.linter.rules; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.royale.compiler.problems.ICompilerProblem; +import org.apache.royale.linter.ASLinter; +import org.apache.royale.linter.LinterRule; +import org.apache.royale.linter.LinterSettings; +import org.junit.Test; + +public class TestInterfaceNameRule { + @Test + public void testInterfaceNameValid() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new InterfaceNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {interface IMyInterface{}}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testInterfaceNameStartsWithLowerCase() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new InterfaceNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {interface iMyInterface{}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof InterfaceNameRule.InterfaceNameLinterProblem); + } + + @Test + public void testInterfaceNameStartsWithLowerCaseAfterI() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new InterfaceNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {interface ImyInterface{}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof InterfaceNameRule.InterfaceNameLinterProblem); + } + + @Test + public void testInterfaceNameStartsWithoutI() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new InterfaceNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {interface MyInterface{}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof InterfaceNameRule.InterfaceNameLinterProblem); + } + + @Test + public void testInterfaceNameContains$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new InterfaceNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {interface IMy$Interface{}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof InterfaceNameRule.InterfaceNameLinterProblem); + } + + @Test + public void testInterfaceNameContains_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new InterfaceNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {interface IMy_Interface{}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof InterfaceNameRule.InterfaceNameLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestLocalVarAndParameterNameRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestLocalVarAndParameterNameRule.java new file mode 100644 index 000000000..5eee108be --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestLocalVarAndParameterNameRule.java @@ -0,0 +1,201 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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.linter.rules; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.royale.compiler.problems.ICompilerProblem; +import org.apache.royale.linter.ASLinter; +import org.apache.royale.linter.LinterRule; +import org.apache.royale.linter.LinterSettings; +import org.junit.Test; + +public class TestLocalVarAndParameterNameRule { + @Test + public void testLocalVarNameValid() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new LocalVarAndParameterNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myFunction():void{var myVar:String;}}}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testLocalVarNameStartsWithUpperCase() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new LocalVarAndParameterNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myFunction():void{var MyVar:String;}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof LocalVarAndParameterNameRule.LocalVarNameLinterProblem); + } + + @Test + public void testLocalVarNameStartsWith$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new LocalVarAndParameterNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myFunction():void{var $myVar:String;}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof LocalVarAndParameterNameRule.LocalVarNameLinterProblem); + } + + @Test + public void testLocalVarNameStartsWith_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new LocalVarAndParameterNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myFunction():void{var _myVar:String;}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof LocalVarAndParameterNameRule.LocalVarNameLinterProblem); + } + + @Test + public void testLocalVarNameStartsWith_AndUpperCase() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new LocalVarAndParameterNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myFunction():void{var _MyVar:String;}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof LocalVarAndParameterNameRule.LocalVarNameLinterProblem); + } + + @Test + public void testLocalVarNameContains$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new LocalVarAndParameterNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myFunction():void{var my$Var:String;}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof LocalVarAndParameterNameRule.LocalVarNameLinterProblem); + } + + @Test + public void testLocalVarNameContains_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new LocalVarAndParameterNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myFunction():void{var my_Var:String;}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof LocalVarAndParameterNameRule.LocalVarNameLinterProblem); + } + + @Test + public void testParameterNameValid() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new LocalVarAndParameterNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myFunction(myParam:String):void{}}}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testParameterNameStartsWithUpperCase() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new LocalVarAndParameterNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myFunction(MyParam:String):void{}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof LocalVarAndParameterNameRule.ParameterNameLinterProblem); + } + + @Test + public void testParameterNameStartsWith$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new LocalVarAndParameterNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myFunction($myParam:String):void{}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof LocalVarAndParameterNameRule.ParameterNameLinterProblem); + } + + @Test + public void testParameterNameStartsWith_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new LocalVarAndParameterNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myFunction(_myParam:String):void{}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof LocalVarAndParameterNameRule.ParameterNameLinterProblem); + } + + @Test + public void testParameterNameContains$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new LocalVarAndParameterNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myFunction(my$Param:String):void{}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof LocalVarAndParameterNameRule.ParameterNameLinterProblem); + } + + @Test + public void testParameterNameContains_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new LocalVarAndParameterNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {class MyClass{function myFunction(my_Param:String):void{}}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof LocalVarAndParameterNameRule.ParameterNameLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestMXMLIDRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestMXMLIDRule.java new file mode 100644 index 000000000..796efe6e4 --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestMXMLIDRule.java @@ -0,0 +1,124 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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.linter.rules; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.royale.compiler.problems.ICompilerProblem; +import org.apache.royale.linter.LinterRule; +import org.apache.royale.linter.LinterSettings; +import org.apache.royale.linter.MXMLLinter; +import org.junit.Test; + +public class TestMXMLIDRule { + @Test + public void testMXMLIDValid() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new MXMLIDRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + MXMLLinter linter = new MXMLLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.mxml", "<fx:Object><fx:Number id=\"myMxmlId\">123.4</fx:Number></fx:Object>", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testMXMLIDEmpty() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new MXMLIDRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + MXMLLinter linter = new MXMLLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.mxml", "<fx:Object><fx:Number id=\"\">123.4</fx:Number></fx:Object>", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof MXMLIDRule.MXMLIDLinterProblem); + } + + @Test + public void testMXMLIDStartsWithUpperCase() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new MXMLIDRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + MXMLLinter linter = new MXMLLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.mxml", "<fx:Object><fx:Number id=\"MyMxmlId\">123.4</fx:Number></fx:Object>", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof MXMLIDRule.MXMLIDLinterProblem); + } + + @Test + public void testMXMLIDStartsWith$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new MXMLIDRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + MXMLLinter linter = new MXMLLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.mxml", "<fx:Object><fx:Number id=\"$myMxmlId\">123.4</fx:Number></fx:Object>", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof MXMLIDRule.MXMLIDLinterProblem); + } + + @Test + public void testMXMLIDStartsWith_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new MXMLIDRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + MXMLLinter linter = new MXMLLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.mxml", "<fx:Object><fx:Number id=\"_myMxmlId\">123.4</fx:Number></fx:Object>", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof MXMLIDRule.MXMLIDLinterProblem); + } + + @Test + public void testMXMLIDContains$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new MXMLIDRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + MXMLLinter linter = new MXMLLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.mxml", "<fx:Object><fx:Number id=\"my$MxmlId\">123.4</fx:Number></fx:Object>", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof MXMLIDRule.MXMLIDLinterProblem); + } + + @Test + public void testMXMLIDContains_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new MXMLIDRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + MXMLLinter linter = new MXMLLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.mxml", "<fx:Object><fx:Number id=\"my_MxmlId\">123.4</fx:Number></fx:Object>", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof MXMLIDRule.MXMLIDLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestPackageNameRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestPackageNameRule.java new file mode 100644 index 000000000..8011505e3 --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestPackageNameRule.java @@ -0,0 +1,148 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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.linter.rules; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.royale.compiler.problems.ICompilerProblem; +import org.apache.royale.linter.ASLinter; +import org.apache.royale.linter.LinterRule; +import org.apache.royale.linter.LinterSettings; +import org.junit.Test; + +public class TestPackageNameRule { + @Test + public void testPackageNameValid() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new PackageNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package myPackage {}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testEmptyPackageNameValid() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new PackageNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package {}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testMultiPackageNameValid() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new PackageNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package com.myPackage {}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testPackageNameStartsWithUpperCase() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new PackageNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package MyPackage {}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof PackageNameRule.PackageNameLinterProblem); + } + + @Test + public void testPackageNameMultiStartsWithUpperCase() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new PackageNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package com.MyPackage {}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof PackageNameRule.PackageNameLinterProblem); + } + + @Test + public void testPackageNameStartsWith$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new PackageNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package $myPackage {}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof PackageNameRule.PackageNameLinterProblem); + } + + @Test + public void testPackageNameStartsWith_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new PackageNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package _myPackage {}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof PackageNameRule.PackageNameLinterProblem); + } + + @Test + public void testPackageNameContains$() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new PackageNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package my$Package {}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof PackageNameRule.PackageNameLinterProblem); + } + + @Test + public void testPackageNameContains_() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new PackageNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "package my_Package {}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof PackageNameRule.PackageNameLinterProblem); + } +} \ No newline at end of file
