Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Signed-off-by: issacto <[email protected]>
  • Loading branch information
issacto committed Aug 9, 2023
1 parent dd85b22 commit 5e3b174
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.openmainframeproject.cobolcheck.features.interpreter;

import java.util.ArrayList;
import java.util.List;

public class Block {

private List<String> lines;

public Block(){
lines = new ArrayList<>();
}

void addLine(String line){
lines.add(line);
}

List<String> getLines(){
return lines;
}

void removeLines(){
lines.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class InterpreterController {
private boolean insideSectionOrParagraphMockBody;
private TreeMap<Integer,String> currentDataStructure;
private final String stubTag;
private MockedSectionOrPargraph mockedSection;
private Block block;

public InterpreterController(BufferedReader sourceReader) {
if (sourceReader == null) {
Expand All @@ -40,7 +40,7 @@ public InterpreterController(BufferedReader sourceReader) {
tokenExtractor = new StringTokenizerExtractor();
currentDataStructure = new TreeMap<>();
stubTag = Config.getStubTag();
mockedSection = new MockedSectionOrPargraph();
block = new Block();
}

//Getters for lists of specific source lines
Expand Down Expand Up @@ -507,27 +507,27 @@ private void resetPossibleMockValues(){
possibleMockType = null;
}

public List<String> getSectionLines(){
return mockedSection.getSectionLines();
public List<String> getBlockLines(){
return block.getLines();
}

public void removeSectionLines(){
mockedSection.removeSectionLines();
public void removeBlockLines(){
block.removeLines();
}

public void addMockedSectionLine(){
public void addBlockLine(){
if(Interpreter.shouldLineBeStubbed(reader.getCurrentLine(), reader.getState()))
mockedSection.addSectionLine(StringHelper.stubLine(reader.getCurrentLine().getUnNumberedString(), stubTag));
else mockedSection.addSectionLine(reader.getCurrentLine().getUnNumberedString());
block.addLine(StringHelper.stubLine(reader.getCurrentLine().getUnNumberedString(), stubTag));
else block.addLine(reader.getCurrentLine().getUnNumberedString());
}

public void addMockedSectionLine(String line){
mockedSection.addSectionLine(line);
public void addBlockLine(String line){
block.addLine(line);
}

public void addMockedSectionLines(List<String> lines){
public void addBlockLines(List<String> lines){
for (String line : lines){
mockedSection.addSectionLine(line);
block.addLine(line);
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static void addStartAndEndTags(List<String> lines){
lines.add(getInjectEndTagComment());
}

static List<String> generateWhenOtherMockLines(String identifier, String type, List<String> commentLines, List<String> bodyLines){
static List<String> generateWhenOtherLines(String identifier, String type, List<String> commentLines, List<String> bodyLines){
List<String> lines = new ArrayList<>();
if(type.equals(Constants.SECTION_TOKEN))
lines.add(String.format(WHEN_OTHER_SECTION_HEADER_FORMAT, identifier));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,4 @@ private String getArgumentText(){
return combinedArgs.substring(0, combinedArgs.length() - 2);
}

public void addLinesWithoutMoving(List<String> lines) {
this.lines.addAll(lines);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,36 +156,6 @@ private List<String> generateParagraphsForMock(Mock mock, boolean withComment){
body.addAll(mock.getLines());
return CobolGenerator.generateParagraphLines(mock.getGeneratedMockIdentifier(), comments, body);
}

/**Generates the lines for Paragraphs based on mocks,
* for each mock in a given list.
* @param mocks - The mocks to generate Paragraphs for
* @return The generated lines
*/
List<String> generateWhenOtherMock(Mock mock, boolean withComments){
List<String> lines = new ArrayList<>();
lines.addAll(CobolGenerator.generateCommentBlock("Paragraphs called when mocking"));
lines.addAll(generateParagraphsForWhenOtherMock(mock, withComments));
lines.add("");
return lines;
}

String generateWhenOtherMockPerformCall(Mock mock){
return String.format(performFormat, mock.getGeneratedMockIdentifier());
}

private List<String> generateParagraphsForWhenOtherMock(Mock mock, boolean withComment){
List<String> comments = new ArrayList<>();
if (withComment){
for (String line : mock.getCommentText()){
comments.add(StringHelper.commentOutLine(line));
}
}
List<String> body = new ArrayList<>();
body.addAll(mock.getLines());
return CobolGenerator.generateWhenOtherMockLines(mock.getGeneratedMockIdentifier(),mock.getType(), comments, body);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class TestSuiteParser {
private String currentTestCaseName = Constants.EMPTY_STRING;
private int testCaseNumber = 0;
private boolean expectNumericCompare;
private int whenOtherNumber=0;

// Lines inserted into the test program
private static final String COBOL_PERFORM_INITIALIZE = " PERFORM %sINITIALIZE";
Expand Down Expand Up @@ -1160,14 +1161,14 @@ public String getCurrentFieldName() {
return currentFieldName;
}

public Mock getWhenOtherMock(String type, List<String> lines, String itdentifier, boolean withComments){
mockNumber += 1;
Mock mock = new Mock(currentTestSuiteName ,currentTestCaseName, testSuiteNumber, testCaseNumber,mockNumber);
mock.addLinesWithoutMoving(lines);
mock.setScope(MockScope.Local);
mock.setType(type);
mock.setIdentifier(itdentifier);
return mock;
public WhenOther getWhenOtherBlock(String type, List<String> lines, String itdentifier, boolean withComments){
WhenOther whenOther = new WhenOther(currentTestSuiteName ,currentTestCaseName, testSuiteNumber, testCaseNumber,whenOtherNumber);
whenOther.addLines(lines);
whenOther.setScope(MockScope.Local);
whenOther.setType(type);
whenOther.setIdentifier(itdentifier);
whenOtherNumber += 1;
return whenOther;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class TestSuiteParserController {
private BeforeAfterRepo beforeAfterRepo;
private MockGenerator mockGenerator;
private BufferedReader testSuiteReader;
private WhenOtherGenerator whenOtherGenerator;

private TestSuiteErrorLog testSuiteErrorLog;

Expand Down Expand Up @@ -54,6 +55,7 @@ public TestSuiteParserController(String testFileNames) {
testSuiteParser = new TestSuiteParser(new KeywordExtractor(), mockRepository, beforeAfterRepo, testSuiteErrorLog);
mockGenerator = new MockGenerator();
testCodePrefix = Config.getTestCodePrefix();
whenOtherGenerator = new WhenOtherGenerator();
}

//Used for testing only
Expand Down Expand Up @@ -314,17 +316,15 @@ public void prepareNextParse() {
Config.setDecimalPointIsCommaFromFile();
}

public Mock getWhenOtherMock(String type, List<String> lines, String identifier, boolean withComments){
// Local scope
return testSuiteParser.getWhenOtherMock(type,lines, identifier, withComments);
}

public String generateWhenOtherMockPerformCall(Mock mock){
return mockGenerator.generateWhenOtherMockPerformCall(mock);
}

public List<String> generateWhenOtherMock(Mock mock, boolean withComments){
return mockGenerator.generateWhenOtherMock(mock, withComments);
public List<String> generateWhenOtherBlock(String type, List<String> blocklines, String sourceLine, String identifier, boolean withComments) throws IOException{
List<String> lines = new ArrayList<>();
WhenOther whenOther = testSuiteParser.getWhenOtherBlock(type, blocklines, identifier, true);
lines.add(whenOtherGenerator.generateWhenOtherCall(whenOther));
lines.addAll(this.getEndEvaluateLine());
lines.add(sourceLine);
lines.add("");
lines.addAll(whenOtherGenerator.generateWhenOther(whenOther, withComments));
return lines;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.openmainframeproject.cobolcheck.features.testSuiteParser;


import java.util.ArrayList;
import java.util.List;

import org.openmainframeproject.cobolcheck.services.Config;
import org.openmainframeproject.cobolcheck.services.StringHelper;

public class WhenOther {

private String identifier;
private String type;
private List<String> lines;
private MockScope scope;
private String testSuiteName;
private String testCaseName;
private int testSuiteNumber;
private int testCaseNumber;
private int mockNumber;


public WhenOther(String testSuiteName, String testCaseName, int testSuiteNumber, int testCaseNumber, int mockNumber) {
this.testSuiteName = testSuiteName;
this.testCaseName = testCaseName;
this.testSuiteNumber = testSuiteNumber;
this.testCaseNumber = testCaseNumber;
this.mockNumber = mockNumber;
lines = new ArrayList<>();
}

public String getGeneratedWhenOtherIdentifier(){
return Config.getTestCodePrefix() + getGeneratedWhenOtherIdentifierRoot();
}

public String getGeneratedWhenOtherIdentifierRoot(){
return testSuiteNumber + "-" + testCaseNumber + "-" + mockNumber + "-WO";
}

public void addLines(List<String> lines) {
this.lines.addAll(lines);
}

public void setScope(MockScope scope) {
this.scope = scope;
}

public void setType(String type) {
this.type = type;
}

public String getType() {
return this.type;
}

public List<String> getLines() {
return this.lines;
}

public void setIdentifier(String identifier) {
this.identifier = identifier;
}


public List<String> getCommentText(){
List<String> newLines = new ArrayList<>();
newLines.add(" *****************************************************************");
newLines.add( "When other block of: " + type + ": " + identifier);
newLines.add("In testsuite: " + testSuiteName);
if (scope == MockScope.Local){
newLines.add("In testcase: " + testCaseName);
}
newLines.add(" *****************************************************************");
return newLines;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.openmainframeproject.cobolcheck.features.testSuiteParser;

import java.util.ArrayList;
import java.util.List;

import org.openmainframeproject.cobolcheck.services.StringHelper;

public class WhenOtherGenerator {
private final String performFormat = " PERFORM %s";


public String generateWhenOtherCall(WhenOther whenOther) {
return String.format(performFormat, whenOther.getGeneratedWhenOtherIdentifier());
}

List<String> generateWhenOther(WhenOther whenOther, boolean withComments){
List<String> lines = new ArrayList<>();
lines.addAll(CobolGenerator.generateCommentBlock("WhenOther block called when mocking"));
lines.addAll(generateParagraphsForWhenOther(whenOther, withComments));
lines.add("");
return lines;
}

private List<String> generateParagraphsForWhenOther(WhenOther whenOther, boolean withComments){
List<String> comments = new ArrayList<>();
if (withComments){
for (String line : whenOther.getCommentText()){
comments.add(StringHelper.commentOutLine(line));
}
}
List<String> body = new ArrayList<>();
body.addAll(whenOther.getLines());
return CobolGenerator.generateWhenOtherLines(whenOther.getGeneratedWhenOtherIdentifier(), whenOther.getType(), comments, body);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ private String tryInsertEndEvaluateAtMockedCompomentEnd(String sourceLine) throw
if (interpreter.isInsideSectionOrParagraphMockBody()){
if (interpreter.isCurrentLineEndingSectionOrParagraph()){
if (interpreter.canWriteEndEvaluateBeforeCurrentLine()){
writeWhenOtherMockedSection(sourceLine);
interpreter.removeSectionLines();
writeWhenOtherBlock(sourceLine);
interpreter.removeBlockLines();
interpreter.setInsideSectionOrParagraphMockBody(false);
return "";
}
Expand Down Expand Up @@ -206,9 +206,9 @@ private void processingAfterEchoingSourceLineToOutput() throws IOException {
List<String> arguments = interpreter.getPossibleMockArgs();
if (testSuiteParserController.mockExistsFor(identifier, type, arguments)){
if(interpreter.isInsideSectionOrParagraphMockBody()){
interpreter.addMockedSectionLines(testSuiteParserController.generateMockPerformCalls(identifier, type, arguments));
interpreter.addBlockLines(testSuiteParserController.generateMockPerformCalls(identifier, type, arguments));
if (type.equals(Constants.CALL_TOKEN)){
interpreter.addMockedSectionLine(" CONTINUE");
interpreter.addBlockLine(" CONTINUE");
}
}else writerController.writeLines(testSuiteParserController.generateMockPerformCalls(identifier, type, arguments));
if (type.equals(Constants.SECTION_TOKEN) || type.equals(Constants.PARAGRAPH_TOKEN)){
Expand All @@ -226,19 +226,14 @@ private void closeReadersAndWriters(String programName) {
writerController.closeWriter(programName);
}

private void writeWhenOtherMockedSection(String sourceLine) throws IOException{
Mock mock = testSuiteParserController.getWhenOtherMock(currentMockType, interpreter.getSectionLines(), currentIdentifier, true);
writerController.writeLine(testSuiteParserController.generateWhenOtherMockPerformCall(mock));
writerController.writeLines(testSuiteParserController.getEndEvaluateLine());
writerController.writeLine(sourceLine);
writerController.writeLine("");
writerController.writeLines(testSuiteParserController.generateWhenOtherMock(mock,true));
private void writeWhenOtherBlock(String sourceLine) throws IOException{
writerController.writeLines(testSuiteParserController.generateWhenOtherBlock(currentMockType, interpreter.getBlockLines(), sourceLine, currentIdentifier, true));
}

private void echoingSourceLineToOutput(String sourceLine){
try{
if(interpreter.isInsideSectionOrParagraphMockBody()){
interpreter.addMockedSectionLine();
interpreter.addBlockLine();
}
sourceLine = tryInsertEndEvaluateAtMockedCompomentEnd(sourceLine);
if(!interpreter.isInsideSectionOrParagraphMockBody()){
Expand Down

0 comments on commit 5e3b174

Please sign in to comment.