Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #671 from Shan1024/update-tests-temp
Browse files Browse the repository at this point in the history
Update tests
  • Loading branch information
sameerajayasoma authored Oct 22, 2017
2 parents e8f6ea5 + 32c9c1a commit f7e8636
Show file tree
Hide file tree
Showing 21 changed files with 939 additions and 556 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,33 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public abstract class BallerinaCompletionTestBase extends BallerinaCodeInsightFixtureTestCase {

static final List<String> FILE_LEVEL_KEYWORDS = Arrays.asList("public", "package", "import", "const",
"service", "function", "connector", "struct", "typemapper", "annotation", "enum");
static final List<String> DATA_TYPES = Arrays.asList("boolean", "int", "float", "string", "blob");
static final List<String> REFERENCE_TYPES = Arrays.asList("message", "map", "xml", "json", "datatable");
static final List<String> XMLNS_TYPE = Collections.singletonList("xmlns");
static final List<String> OTHER_TYPES = Arrays.asList("any", "type", "var");
static final List<String> COMMON_KEYWORDS = Arrays.asList("if", "else", "fork", "join", "timeout",
"worker", "transform", "transaction", "failed", "aborted", "committed", "abort", "try", "catch", "finally",
"iterate", "while", "next", "break", "throw");
static final List<String> VALUE_KEYWORDS = Arrays.asList("true", "false", "null");
static final List<String> FUNCTION_LEVEL_KEYWORDS = Collections.singletonList("return");

void doTestFile(String... expectedLookups) {
String testName = getTestName(false);
myFixture.configureByFile(testName + ".bal");
myFixture.complete(CompletionType.BASIC, 1);
List<String> lookupElementStrings = myFixture.getLookupElementStrings();
assertNotNull(lookupElementStrings);
assertSameElements(lookupElementStrings, expectedLookups);
}

void doTest(String fileContent, String... expectedLookups) {
if (fileContent != null) {
myFixture.configureByText("test.bal", fileContent);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.ballerinalang.completion;

import java.util.LinkedList;
import java.util.List;

public class BallerinaConstantCompletionTest extends BallerinaCompletionTestBase {

/**
* Test constants.
*/
public void testConstTypes() {
doTest("const <caret>", DATA_TYPES.toArray(new String[DATA_TYPES.size()]));
}

public void testConstIdentifier() {
doTest("const boolean <caret>");
}

public void testConstValues() {
doTest("const string NAME = <caret>", "true", "false", "null");
}

public void testConstantAnnotation() {
doCheckResult("test.bal", "@<caret> const string S=\"\";", null, '@');
}

public void testConstantAnnotationWithImports() {
myFixture.addFileToProject("org/test/file.bal", "string s = \"\";");
doCheckResult("test.bal", "import org.test; <caret> const string S=\"\";", null, '@', "test");
}

public void testConstantAnnotationWithImportsNoAnnotationDefinitions() {
myFixture.addFileToProject("org/test/file.bal", "function test(){}");
doCheckResult("test.bal", "import org.test; @test<caret> const string S=\"\";", null, ':');
}

public void testConstantAnnotationWithImportsWithNoMatchingAnnotationDefinitions() {
myFixture.addFileToProject("org/test/file.bal", "public annotation TEST attach service {}");
doCheckResult("test.bal", "import org.test; @test:<caret> const string S=\"\";", null, null);
}

public void testConstantAnnotationWithImportsWithMatchingAnnotationDefinitions() {
myFixture.addFileToProject("org/test/file.bal", "package org.test; public annotation TEST attach const {} " +
"annotation TEST2 attach resource {}");
doCheckResult("test.bal", "import org.test; @test:<caret> const string S=\"\";", null, null, "TEST");
}

public void testConstantAnnotationWithImportsWithMatchingAnnotationDefinitionsAutoCompletion() {
myFixture.addFileToProject("org/test/file.bal", "package org.test; public annotation TEST attach const {}");
doCheckResult("test.bal", "import org.test; @test:T<caret> const string S=\"\";",
"import org.test; @test:TEST {} const string S=\"\";", null);
}

public void testConstantAnnotationInCurrentPackageSameFile() {
doCheckResult("test.bal", "annotation TEST attach const {} <caret> const string S=\"\";", null, '@',
"TEST");
}

public void testConstantAnnotationInCurrentPackageSameFileAutoComplete() {
doCheckResult("test.bal", "annotation TEST attach const {} @T<caret> const string S=\"\";",
"annotation TEST attach const {} @TEST {} const string S=\"\";", null);
}

public void testConstantAnnotationInCurrentPackageDifferentFile() {
myFixture.addFileToProject("file.bal", "annotation TEST attach const {}");
doCheckResult("test.bal", "<caret> const string S=\"\";", null, '@', "TEST");
}

public void testConstantAnnotationInCurrentPackageDifferentFileAutoComplete() {
myFixture.addFileToProject("file.bal", "annotation TEST attach function {}");
doCheckResult("test.bal", "@T<caret> function A(){}", "@TEST {} function A(){}", null);
}

public void testConstantAnnotationInCurrentPackageDifferentFileHasMoreDefinitionsAfter() {
myFixture.addFileToProject("file.bal", "annotation TEST attach const {}");
doCheckResult("test.bal", "<caret> const string S=\"\"; service R{}", null, '@', "TEST");
}

public void testConstantAnnotationInCurrentPackageSameFileHasMoreDefinitionsAfter() {
doCheckResult("test.bal", "annotation TEST attach const {} <caret> const string S=\"\"; service R{}", null, '@',
"TEST");
}

public void testConstantTypesBeforeGlobalVarDef() {
doTest("const <caret>\n string test =\"\";", DATA_TYPES.toArray(new String[DATA_TYPES.size()]));
}

public void testConstantInSamePackageSameFile() {
List<String> expectedLookups = new LinkedList<>();
expectedLookups.addAll(DATA_TYPES);
expectedLookups.addAll(OTHER_TYPES);
expectedLookups.addAll(XMLNS_TYPE);
expectedLookups.addAll(REFERENCE_TYPES);
expectedLookups.addAll(COMMON_KEYWORDS);
expectedLookups.addAll(FUNCTION_LEVEL_KEYWORDS);
expectedLookups.add("S");
expectedLookups.add("F");
doTest("string S=\"\"; function F(){ <caret> }", expectedLookups.toArray(new String[expectedLookups.size()]));
}

public void testConstantInSamePackageDifferentFile() {
myFixture.addFileToProject("file.bal", "const string S=\"\";");
List<String> expectedLookups = new LinkedList<>();
expectedLookups.addAll(DATA_TYPES);
expectedLookups.addAll(OTHER_TYPES);
expectedLookups.addAll(XMLNS_TYPE);
expectedLookups.addAll(REFERENCE_TYPES);
expectedLookups.addAll(COMMON_KEYWORDS);
expectedLookups.addAll(FUNCTION_LEVEL_KEYWORDS);
expectedLookups.add("S");
expectedLookups.add("F");
doTest("function F(){ <caret> }", expectedLookups.toArray(new String[expectedLookups.size()]));
}

public void testConstantInDifferentPackage() {
myFixture.addFileToProject("org/test/file.bal", "public const string S=\"\";");
doTest("import org.test; function F(){ test:<caret> }", "S");
}

public void testConstantInDifferentPackageImportedAsAlias() {
myFixture.addFileToProject("org/test/file.bal", "cont string S=\"\";");
doTest("import org.test as utils; function F(){ utils:<caret> }", "S");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.ballerinalang.completion;

import java.util.LinkedList;
import java.util.List;

public class BallerinaFileLevelCompletionTest extends BallerinaCompletionTestBase {

/**
* Test file level lookups.
*/
public void testEmptyFile() {
List<String> expectedLookups = new LinkedList<>();
expectedLookups.addAll(FILE_LEVEL_KEYWORDS);
expectedLookups.addAll(OTHER_TYPES);
expectedLookups.addAll(XMLNS_TYPE);
expectedLookups.addAll(DATA_TYPES);
expectedLookups.addAll(REFERENCE_TYPES);
doTest("<caret>", expectedLookups.toArray(new String[expectedLookups.size()]));
}

public void testEmptyFilePackageKeyword() {
doTest("p<caret>", "public", "import", "package", "typemapper", "map", "type");
}

public void testEmptyFileImportKeyword() {
doTest("i<caret>", "public", "annotation", "function", "import", "service", "int", "string");
}

public void testEmptyFileWithSpaceBeforeCaret() {
List<String> expectedLookups = new LinkedList<>();
expectedLookups.addAll(FILE_LEVEL_KEYWORDS);
expectedLookups.addAll(OTHER_TYPES);
expectedLookups.addAll(XMLNS_TYPE);
expectedLookups.addAll(DATA_TYPES);
expectedLookups.addAll(REFERENCE_TYPES);
doTest("\n<caret>", expectedLookups.toArray(new String[expectedLookups.size()]));
}

public void testEmptyFileWithSpaceBeforeCaretPackageKeyword() {
doTest("\np<caret>", "public", "import", "package", "typemapper", "map", "type");
}

public void testEmptyFileWithSpaceBeforeCaretImportKeyword() {
doTest("\ni<caret>", "public", "annotation", "function", "import", "service", "int", "string");
}

public void testEmptyFileWithSpaceAfterCaret() {
List<String> expectedLookups = new LinkedList<>();
expectedLookups.addAll(FILE_LEVEL_KEYWORDS);
expectedLookups.addAll(OTHER_TYPES);
expectedLookups.addAll(XMLNS_TYPE);
expectedLookups.addAll(DATA_TYPES);
expectedLookups.addAll(REFERENCE_TYPES);
doTest("<caret>\n", expectedLookups.toArray(new String[expectedLookups.size()]));
}

public void testEmptyFileWithSpaceAfterCaretPackageKeyword() {
doTest("p<caret>\n", "public", "import", "package", "typemapper", "map", "type");
}

public void testEmptyFileWithSpaceAfterCaretImportKeyword() {
doTest("i<caret>\n", "public", "annotation", "function", "import", "service", "int", "string");
}

public void testEmptyFileWithSpaces() {
List<String> expectedLookups = new LinkedList<>();
expectedLookups.addAll(FILE_LEVEL_KEYWORDS);
expectedLookups.addAll(OTHER_TYPES);
expectedLookups.addAll(XMLNS_TYPE);
expectedLookups.addAll(DATA_TYPES);
expectedLookups.addAll(REFERENCE_TYPES);
doTest("\n<caret>\n", expectedLookups.toArray(new String[expectedLookups.size()]));
}

public void testEmptyFileWithSpacesPackageKeyword() {
doTest("\np<caret>\n", "public", "import", "package", "typemapper", "map", "type");
}

public void testEmptyFileWithSpacesImportKeyword() {
doTest("\ni<caret>\n", "public", "annotation", "function", "import", "service", "int", "string");
}

public void testImportAfterPackage() {
List<String> expectedLookups = new LinkedList<>();
expectedLookups.addAll(OTHER_TYPES);
expectedLookups.addAll(XMLNS_TYPE);
expectedLookups.addAll(DATA_TYPES);
expectedLookups.addAll(REFERENCE_TYPES);
expectedLookups.add("public");
expectedLookups.add("import");
expectedLookups.add("const");
expectedLookups.add("service");
expectedLookups.add("function");
expectedLookups.add("connector");
expectedLookups.add("struct");
expectedLookups.add("typemapper");
expectedLookups.add("annotation");
expectedLookups.add("enum");
doTest("package test; \n<caret>\n", expectedLookups.toArray(new String[expectedLookups.size()]));
}

public void testImportAfterPackagePartialIdentifier() {
doTest("package test; \ni<caret>\n", "public", "annotation", "function", "import", "service", "int", "string");
}

public void testImportAfterPackageBeforeFunction() {
List<String> expectedLookups = new LinkedList<>();
expectedLookups.addAll(OTHER_TYPES);
expectedLookups.addAll(XMLNS_TYPE);
expectedLookups.addAll(DATA_TYPES);
expectedLookups.addAll(REFERENCE_TYPES);
expectedLookups.add("public");
expectedLookups.add("import");
expectedLookups.add("const");
expectedLookups.add("service");
expectedLookups.add("function");
expectedLookups.add("connector");
expectedLookups.add("struct");
expectedLookups.add("typemapper");
expectedLookups.add("annotation");
expectedLookups.add("enum");
doTest("package test; \n<caret>\nfunction A(){}", expectedLookups.toArray(new String[expectedLookups.size()]));
}

public void testImportAfterPackageBeforeFunctionPartialIdentifier() {
doTest("package test; \ni<caret>\nfunction A(){}", "public", "annotation", "function", "import", "service",
"int", "string");
}

public void testPackageBeforeImport() {
List<String> expectedLookups = new LinkedList<>();
expectedLookups.addAll(FILE_LEVEL_KEYWORDS);
expectedLookups.addAll(OTHER_TYPES);
expectedLookups.addAll(XMLNS_TYPE);
expectedLookups.addAll(DATA_TYPES);
expectedLookups.addAll(REFERENCE_TYPES);
doTest("<caret>\nimport test; \nfunction A(){}", expectedLookups.toArray(new String[expectedLookups.size()]));
}

public void testPackageBeforeImportPartialIdentifier() {
doTest("p<caret>\nimport test; \nfunction A(){}", "public", "import", "package", "typemapper", "map", "type");
}

public void testImportBeforeImport() {
List<String> expectedLookups = new LinkedList<>();
expectedLookups.addAll(FILE_LEVEL_KEYWORDS);
expectedLookups.addAll(OTHER_TYPES);
expectedLookups.addAll(XMLNS_TYPE);
expectedLookups.addAll(DATA_TYPES);
expectedLookups.addAll(REFERENCE_TYPES);
doTest("<caret>\nimport test; \nfunction A(){}", expectedLookups.toArray(new String[expectedLookups.size()]));
}

public void testImportBeforeImportPartialIdentifier() {
doTest("i<caret>\nimport test; \nfunction A(){}", "public", "annotation", "function", "import", "service",
"int", "string");
}

public void testImportAfterImport() {
List<String> expectedLookups = new LinkedList<>();
expectedLookups.addAll(OTHER_TYPES);
expectedLookups.addAll(XMLNS_TYPE);
expectedLookups.addAll(DATA_TYPES);
expectedLookups.addAll(REFERENCE_TYPES);
expectedLookups.add("public");
expectedLookups.add("import");
expectedLookups.add("const");
expectedLookups.add("service");
expectedLookups.add("function");
expectedLookups.add("connector");
expectedLookups.add("struct");
expectedLookups.add("typemapper");
expectedLookups.add("annotation");
expectedLookups.add("enum");
expectedLookups.add("test");
myFixture.addFileToProject("test/file.bal", "string s = \"\";");
doTest("import test; \n<caret> \nfunction A(){}", expectedLookups.toArray(new String[expectedLookups.size()]));
}

public void testImportAfterImportPartialIdentifier() {
doTest("import test; \ni<caret> \nfunction A(){}", "public", "annotation", "function", "import", "service",
"int", "string");
}
}
Loading

0 comments on commit f7e8636

Please sign in to comment.