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 f04e9788b5ab9201d9c7f3bf4848d110d92167fc Author: Josh Tynjala <[email protected]> AuthorDate: Thu Oct 27 15:48:30 2022 -0700 linter: more tests --- .../royale/linter/rules/TestNoAnyTypeRule.java | 109 +++++++++++++++++++++ .../linter/rules/TestNoBooleanEqualityRule.java | 98 ++++++++++++++++++ .../rules/TestNoConstructorDispatchEventRule.java | 59 +++++++++++ .../rules/TestNoConstructorReturnTypeRule.java | 59 +++++++++++ .../rules/TestNoDuplicateObjectKeysRule.java | 59 +++++++++++ .../linter/rules/TestNoDynamicClassRule.java | 59 +++++++++++ .../linter/rules/TestNoIfBooleanLiteralRule.java | 96 ++++++++++++++++++ .../linter/rules/TestNoLeadingZeroesRule.java | 71 ++++++++++++++ .../royale/linter/rules/TestNoSparseArrayRule.java | 71 ++++++++++++++ .../linter/rules/TestNoStringEventNameRule.java | 73 ++++++++++++++ .../linter/rules/TestNoThisInClosureRule.java | 59 +++++++++++ 11 files changed, 813 insertions(+) diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestNoAnyTypeRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestNoAnyTypeRule.java new file mode 100644 index 000000000..e14d7da05 --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestNoAnyTypeRule.java @@ -0,0 +1,109 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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 TestNoAnyTypeRule { + @Test + public void testVariableWithoutAnyType() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoAnyTypeRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "var a:Object;", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testVariableWithAnyType() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoAnyTypeRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "var a:*;", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoAnyTypeRule.NoAnyTypeOnVariableLinterProblem); + } + + @Test + public void testParameterWithoutAnyType() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoAnyTypeRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "function myFunction(a:Object):void {}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testParameterWithAnyType() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoAnyTypeRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "function myFunction(a:*):void {}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoAnyTypeRule.NoAnyTypeOnParameterLinterProblem); + } + + @Test + public void testReturnWithoutAnyType() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoAnyTypeRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "function myFunction():Object{return null;}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testReturnWithAnyType() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoAnyTypeRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "function myFunction():*{return null;}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoAnyTypeRule.NoAnyTypeReturnLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestNoBooleanEqualityRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestNoBooleanEqualityRule.java new file mode 100644 index 000000000..0f610f49c --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestNoBooleanEqualityRule.java @@ -0,0 +1,98 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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 TestNoBooleanEqualityRule { + @Test + public void testWithoutBooleanEquality() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoBooleanEqualityRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "if(a){}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testWithLooseEqualsTrue() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoBooleanEqualityRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "if(a==true){}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoBooleanEqualityRule.NoBooleanEqualityLinterProblem); + } + + @Test + public void testWithLooseNotEqualsTrue() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoBooleanEqualityRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "if(a!=true){}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoBooleanEqualityRule.NoBooleanEqualityLinterProblem); + } + + @Test + public void testWithLooseEqualsFalse() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoBooleanEqualityRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "if(a==false){}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoBooleanEqualityRule.NoBooleanEqualityLinterProblem); + } + + @Test + public void testWithLooseNotEqualsFalse() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoBooleanEqualityRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "if(a!=false){}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoBooleanEqualityRule.NoBooleanEqualityLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestNoConstructorDispatchEventRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestNoConstructorDispatchEventRule.java new file mode 100644 index 000000000..55b2bf06d --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestNoConstructorDispatchEventRule.java @@ -0,0 +1,59 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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 TestNoConstructorDispatchEventRule { + @Test + public void testWithoutDispatchEvent() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoConstructorDispatchEventRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "class MyClass{function MyClass(){}}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testWithDispatchEvent() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoConstructorDispatchEventRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "class MyClass{function MyClass():void{dispatchEvent(new Event(\"change\"));}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoConstructorDispatchEventRule.NoConstructorDispatchEventLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestNoConstructorReturnTypeRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestNoConstructorReturnTypeRule.java new file mode 100644 index 000000000..233d3e0a1 --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestNoConstructorReturnTypeRule.java @@ -0,0 +1,59 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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 TestNoConstructorReturnTypeRule { + @Test + public void testWithoutReturnType() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoConstructorReturnTypeRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "class MyClass{function MyClass(){}}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testWithReturnType() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoConstructorReturnTypeRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "class MyClass{function MyClass():void{}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoConstructorReturnTypeRule.NoConstructorReturnTypeLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestNoDuplicateObjectKeysRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestNoDuplicateObjectKeysRule.java new file mode 100644 index 000000000..12e0e0945 --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestNoDuplicateObjectKeysRule.java @@ -0,0 +1,59 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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 TestNoDuplicateObjectKeysRule { + @Test + public void testWithoutDuplicates() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoDuplicateObjectKeysRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "var a:Object = {a: 1, b: 2};", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testWithDuplicates() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoDuplicateObjectKeysRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "var a:Object = {a: 1, b: 2, a: 3};", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoDuplicateObjectKeysRule.NoDuplicateObjectKeysLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestNoDynamicClassRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestNoDynamicClassRule.java new file mode 100644 index 000000000..a14ee0455 --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestNoDynamicClassRule.java @@ -0,0 +1,59 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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 TestNoDynamicClassRule { + @Test + public void testWithoutDynamic() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoDynamicClassRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "class MyClass {}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testWithDynamic() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoDynamicClassRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "dynamic class MyClass {}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoDynamicClassRule.NoDynamicClassLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestNoIfBooleanLiteralRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestNoIfBooleanLiteralRule.java new file mode 100644 index 000000000..08b545c52 --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestNoIfBooleanLiteralRule.java @@ -0,0 +1,96 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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 TestNoIfBooleanLiteralRule { + @Test + public void testIfIdentifier() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoIfBooleanLiteralRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "if(a){}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testIfUndefined() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoIfBooleanLiteralRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "if(undefined){}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testIfNull() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoIfBooleanLiteralRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "if(null){}", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testIfTrue() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoIfBooleanLiteralRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "if(true){}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoIfBooleanLiteralRule.NoIfBooleanLiteralLinterProblem); + } + + @Test + public void testIfFalse() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoIfBooleanLiteralRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "if(false){}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoIfBooleanLiteralRule.NoIfBooleanLiteralLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestNoLeadingZeroesRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestNoLeadingZeroesRule.java new file mode 100644 index 000000000..dca8c46d4 --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestNoLeadingZeroesRule.java @@ -0,0 +1,71 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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 TestNoLeadingZeroesRule { + @Test + public void testNoLeadingZeroes() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoLeadingZeroesRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "var a:Number = 123.4", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testLeadingZeroes() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoLeadingZeroesRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "var a:Number = 0123", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoLeadingZeroesRule.NoLeadingZeroesLinterProblem); + } + + @Test + public void testHexadecimal() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoLeadingZeroesRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "var a:Number = 0x1234", problems); + assertEquals(0, problems.size()); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestNoSparseArrayRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestNoSparseArrayRule.java new file mode 100644 index 000000000..8b8701246 --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestNoSparseArrayRule.java @@ -0,0 +1,71 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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 TestNoSparseArrayRule { + @Test + public void testNonSparseArray() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoSparseArrayRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "var a:Array = [a, b, c]", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testEmptyArray() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoSparseArrayRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "var a:Array = []", problems); + assertEquals(0, problems.size()); + } + + @Test + public void testSparseArray() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoSparseArrayRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "var a:Array = [a,,c]", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoSparseArrayRule.NoSparseArrayLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestNoStringEventNameRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestNoStringEventNameRule.java new file mode 100644 index 000000000..a47f2eedc --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestNoStringEventNameRule.java @@ -0,0 +1,73 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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 TestNoStringEventNameRule { + @Test + public void testAddEventListenerStringEventName() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoStringEventNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "addEventListener(\"change\", listener)", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoStringEventNameRule.NoStringEventNameLinterProblem); + } + + @Test + public void testRemoveEventListenerStringEventName() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoStringEventNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "removeEventListener(\"change\", listener)", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoStringEventNameRule.NoStringEventNameLinterProblem); + } + + @Test + public void testHasEventListenerStringEventName() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoStringEventNameRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "hasEventListener(\"change\", listener)", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoStringEventNameRule.NoStringEventNameLinterProblem); + } +} \ No newline at end of file diff --git a/linter/src/test/java/org/apache/royale/linter/rules/TestNoThisInClosureRule.java b/linter/src/test/java/org/apache/royale/linter/rules/TestNoThisInClosureRule.java new file mode 100644 index 000000000..58a8d926c --- /dev/null +++ b/linter/src/test/java/org/apache/royale/linter/rules/TestNoThisInClosureRule.java @@ -0,0 +1,59 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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 TestNoThisInClosureRule { + @Test + public void testThisInClosure() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoThisInClosureRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "function myFunction():void{function myClosure():void{this;}}", problems); + assertEquals(1, problems.size()); + assertTrue(problems.get(0) instanceof NoThisInClosureRule.NoThisInClosureLinterProblem); + } + + @Test + public void testThisInMethod() { + List<LinterRule> rules = new ArrayList<LinterRule>(); + rules.add(new NoThisInClosureRule()); + LinterSettings settings = new LinterSettings(); + settings.rules = rules; + ASLinter linter = new ASLinter(settings); + List<ICompilerProblem> problems = new ArrayList<ICompilerProblem>(); + linter.lint("file.as", "class MyClass{function myMethod():void{this;}}", problems); + assertEquals(0, problems.size()); + } +} \ No newline at end of file
