Skip to content

Add Mockito library #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions com.ca.lsp.cobol/lsp-core-cobol-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@
<groupId>net.jodah</groupId>
<artifactId>concurrentunit</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Broadcom.
* Copyright (c) 2020 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
Expand All @@ -21,6 +21,8 @@
import com.ca.lsp.core.cobol.semantics.CobolCleanExtraLanguageTest;
import com.ca.lsp.core.cobol.semantics.CobolVariableCheckTest;
import com.ca.lsp.core.cobol.semantics.CobolVariableContextTest;
import com.ca.lsp.core.cobol.visitor.LevenshteinDistanceTest;
import com.ca.lsp.core.cobol.visitor.VariableDefinitionTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
Expand All @@ -36,5 +38,7 @@
CobolVariableContextTest.class,
CobolVariableCheckTest.class,
CobolCleanExtraLanguageTest.class,
LevenshteinDistanceTest.class,
VariableDefinitionTest.class,
})
public class CobolTestSuite {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2020 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
*/

package com.ca.lsp.core.cobol.utils;

import lombok.AllArgsConstructor;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.TokenSource;
import org.antlr.v4.runtime.WritableToken;

@AllArgsConstructor
public class CustomToken implements WritableToken {

protected int line;
protected int charPositionInLine = -1;
protected String text;
protected int start;
protected int stop;

@Override
public void setText(String s) {}

@Override
public void setType(int i) {}

@Override
public void setLine(int i) {}

@Override
public void setCharPositionInLine(int i) {}

@Override
public void setChannel(int i) {}

@Override
public void setTokenIndex(int i) {}

@Override
public String getText() {
return this.text;
}

@Override
public int getType() {
return 0;
}

@Override
public int getLine() {
return this.line;
}

@Override
public int getCharPositionInLine() {
return this.charPositionInLine;
}

@Override
public int getChannel() {
return 0;
}

@Override
public int getTokenIndex() {
return 0;
}

@Override
public int getStartIndex() {
return this.start;
}

@Override
public int getStopIndex() {
return this.stop;
}

@Override
public TokenSource getTokenSource() {
return null;
}

@Override
public CharStream getInputStream() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2020 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
*
*/

package com.ca.lsp.core.cobol.visitor;

import com.broadcom.lsp.domain.cobol.model.Position;
import com.ca.lsp.core.cobol.model.SyntaxError;
import com.ca.lsp.core.cobol.parser.CobolParser;
import com.ca.lsp.core.cobol.parser.listener.SemanticListener;
import com.ca.lsp.core.cobol.utils.CustomToken;
import org.antlr.v4.runtime.Token;
import org.junit.Test;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

public class LevenshteinDistanceTest {
private CobolVisitor visitor = new CobolVisitor();
List<SyntaxError> errors = new CopyOnWriteArrayList<>();
final String WRONG_TOKEN = "MOVES";

@Test
public void testDistance() {
errors.add(new SyntaxError(new Position("", 1, 1, 1, 1, 1), null, 2, "", 2));
visitor.setSemanticErrors(new SemanticListener(errors));

CobolParser.StatementContext node = mock(CobolParser.StatementContext.class);
when(node.getStart()).thenReturn(createNewToken(WRONG_TOKEN));

visitor.visitStatement(node);

assertEquals(2, errors.size());
assertEquals("Misspelled word, maybe you want to put MOVE", errors.get(1).getSuggestion());
}

private Token createNewToken(String text) {
return new CustomToken(10, 10, text, 10, 10);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2020 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
*
*/

package com.ca.lsp.core.cobol.visitor;

import com.broadcom.lsp.domain.cobol.model.Position;
import com.ca.lsp.core.cobol.model.SyntaxError;
import com.ca.lsp.core.cobol.model.Variable;
import com.ca.lsp.core.cobol.parser.CobolParser;
import com.ca.lsp.core.cobol.parser.listener.SemanticListener;
import com.ca.lsp.core.cobol.semantics.SemanticContext;
import com.ca.lsp.core.cobol.utils.CustomToken;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class VariableDefinitionTest {
private CobolVisitor visitor = new CobolVisitor();
List<SyntaxError> errors = new CopyOnWriteArrayList<>();
final SemanticContext semanticContext = new SemanticContext(new ArrayList<>());
final String VARNAME = "custom";
final String VARNAME2 = "notCustom";
final String LEVEL_NUMBER = "05";
final Variable VARIABLE = new Variable(LEVEL_NUMBER, VARNAME2);
final Position VARIABLE_POSITION = new Position("", 1, 1, 1, 1, 1);

@Test
public void testCheckIfVariableNotDefined() {
semanticContext.getVariables().define(VARIABLE, VARIABLE_POSITION);
CustomToken token = createNewToken(VARNAME);

mockMethod(VARNAME, token);

assertEquals(1, errors.size());
assertEquals("Invalid definition for: " + VARNAME.toUpperCase(), errors.get(0).getSuggestion());
}

@Test
public void checkIfVariableDefined() {
semanticContext.getVariables().define(VARIABLE, VARIABLE_POSITION);
CustomToken token = createNewToken(VARNAME2);

mockMethod(VARNAME2, token);
assertEquals(
semanticContext
.getVariables()
.getDefinitions()
.get(VARNAME2.toUpperCase())
.iterator()
.next(),
VARIABLE_POSITION);
assertEquals(0, errors.size());
}

private void mockMethod(String variableName, CustomToken token) {
CobolParser.QualifiedDataNameFormat1Context node =
mock(CobolParser.QualifiedDataNameFormat1Context.class);
CobolParser.DataNameContext nodeData = mock(CobolParser.DataNameContext.class);

when(node.dataName()).thenReturn(nodeData);
when(nodeData.getStart()).thenReturn(token);
when(nodeData.getText()).thenReturn(variableName);
when(node.getStart()).thenReturn(token);

visitor.setSemanticContext(semanticContext);
visitor.setSemanticErrors(new SemanticListener(errors));
visitor.visitQualifiedDataNameFormat1(node);
}

private CustomToken createNewToken(String text) {
return new CustomToken(10, 10, text, 10, 10);
}
}
6 changes: 6 additions & 0 deletions com.ca.lsp.cobol/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@
<artifactId>guava</artifactId>
<version>28.1-jre</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/net.jodah/concurrentunit -->
<dependency>
<groupId>net.jodah</groupId>
Expand Down