Skip to content
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

test: migrate PrinterTest JUnit 5 #4499

Merged
merged 1 commit into from
Jan 16, 2022
Merged
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
80 changes: 40 additions & 40 deletions src/test/java/spoon/test/prettyprinter/PrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@
*/
package spoon.test.prettyprinter;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static spoon.testing.utils.ModelUtils.canBeBuilt;

import org.apache.commons.io.FileUtils;
import org.junit.Test;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import spoon.Launcher;
import spoon.compiler.SpoonResourceHelper;
import spoon.reflect.code.CtBinaryOperator;
Expand All @@ -33,24 +38,19 @@
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.DefaultJavaPrettyPrinter;
import spoon.reflect.visitor.DefaultTokenWriter;
import spoon.reflect.visitor.ElementPrinterHelper;
import spoon.reflect.visitor.PrettyPrinter;
import spoon.reflect.visitor.PrinterHelper;
import spoon.reflect.visitor.TokenWriter;
import spoon.reflect.visitor.DefaultTokenWriter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.prettyprinter.testclasses.MissingVariableDeclaration;
import spoon.testing.utils.ModelUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static spoon.testing.utils.ModelUtils.canBeBuilt;

public class PrinterTest {

Expand Down Expand Up @@ -87,15 +87,15 @@ public void testChangeAutoImportModeWorks() {
printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
String result = printer.getResult();

assertTrue("The result should not contain imports: "+result, !result.contains("import java.util.List;"));
assertTrue(!result.contains("import java.util.List;"), "The result should not contain imports: " + result);

// recreating an auto-immport printer
spoon.getEnvironment().setAutoImports(true);
printer = spoon.createPrettyPrinter();

printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
result = printer.getResult();
assertTrue("The result should now contain imports: "+result, result.contains("import java.util.List;"));
assertTrue(result.contains("import java.util.List;"), "The result should now contain imports: " + result);
}

@Test
Expand All @@ -112,8 +112,8 @@ public void testFQNModeWriteFQNConstructorInCtVisitor() {
printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
String result = printer.getResult();

assertTrue("The result should contain FQN for constructor: "+result, result.contains("new spoon.support.visitor.replace.ReplacementVisitor("));
assertTrue("The result should not contain reduced constructors: "+result, !result.contains("new ReplacementVisitor("));
assertTrue(result.contains("new spoon.support.visitor.replace.ReplacementVisitor("), "The result should contain FQN for constructor: " + result);
assertTrue(!result.contains("new ReplacementVisitor("), "The result should not contain reduced constructors: " + result);
}

@Test
Expand All @@ -130,11 +130,11 @@ public void testAutoimportModeDontImportUselessStatic() {
printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
String result = printer.getResult();

assertTrue("The result should not contain import static: ", !result.contains("import static spoon.test.prettyprinter.testclasses.sub.Constants.READY"));
assertTrue("The result should contain import type: ", result.contains("import spoon.test.prettyprinter.testclasses.sub.Constants"));
assertTrue("The result should contain import static assertTrue: ", result.contains("import static org.junit.Assert.assertTrue;"));
assertTrue("The result should contain assertTrue(...): ", result.contains("assertTrue(\"blabla\".equals(\"toto\"));"));
assertTrue("The result should use System.out.println(Constants.READY): "+result, result.contains("System.out.println(Constants.READY);"));
assertTrue(!result.contains("import static spoon.test.prettyprinter.testclasses.sub.Constants.READY"), "The result should not contain import static: ");
assertTrue(result.contains("import spoon.test.prettyprinter.testclasses.sub.Constants"), "The result should contain import type: ");
assertTrue(result.contains("import static org.junit.Assert.assertTrue;"), "The result should contain import static assertTrue: ");
assertTrue(result.contains("assertTrue(\"blabla\".equals(\"toto\"));"), "The result should contain assertTrue(...): ");
assertTrue(result.contains("System.out.println(Constants.READY);"), "The result should use System.out.println(Constants.READY): " + result);
}

@Test
Expand All @@ -152,9 +152,9 @@ public void testAutoimportModeDontImportUselessStaticNoClassPath() {
printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
String result = printer.getResult();

assertTrue("The result should contain import java.util.ArrayList: ", result.contains("import java.util.ArrayList;"));
assertTrue("The result should contain import java.util.List: ", result.contains("import java.util.List;"));
assertTrue("The result should contain import static org.Bar.m: ", result.contains("import static org.Bar.m;"));
assertTrue(result.contains("import java.util.ArrayList;"), "The result should contain import java.util.ArrayList: ");
assertTrue(result.contains("import java.util.List;"), "The result should contain import java.util.List: ");
assertTrue(result.contains("import static org.Bar.m;"), "The result should contain import static org.Bar.m: ");
}

@Test
Expand All @@ -172,9 +172,9 @@ public void testUnresolvedImportStaticNoClassPath() {
printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
String result = printer.getResult();

assertTrue("The result should contain import java.util.ArrayList: ", result.contains("import java.util.ArrayList;"));
assertTrue("The result should contain import java.util.List: ", result.contains("import java.util.List;"));
assertTrue("The result should contain import static org.Bar.m: ", result.contains("import static org.Bar.*;"));
assertTrue(result.contains("import java.util.ArrayList;"), "The result should contain import java.util.ArrayList: ");
assertTrue(result.contains("import java.util.List;"), "The result should contain import java.util.List: ");
assertTrue(result.contains("import static org.Bar.*;"), "The result should contain import static org.Bar.m: ");
}

@Test
Expand All @@ -192,8 +192,8 @@ public void testUnresolvedNoClassPath() {
printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
String result = printer.getResult();

assertTrue("The result should contain import org.Bar: ", result.contains("import org.Bar;"));
assertTrue("The result should contain import org.foo.*: ", result.contains("import org.foo.*;"));
assertTrue(result.contains("import org.Bar;"), "The result should contain import org.Bar: ");
assertTrue(result.contains("import org.foo.*;"), "The result should contain import org.foo.*: ");
}

@Test
Expand All @@ -212,7 +212,7 @@ public void testRuleCanBeBuild() {
printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
String result = printer.getResult();

assertTrue("The result should contain direct this accessor for field: "+result, !result.contains("Rule.Phoneme.this.phonemeText"));
assertTrue(!result.contains("Rule.Phoneme.this.phonemeText"), "The result should contain direct this accessor for field: " + result);
canBeBuilt(output, 7);
}

Expand Down Expand Up @@ -251,7 +251,7 @@ public void testJDTBatchCompilerCanBeBuild() {
printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
String result = printer.getResult();

assertTrue("The result should contain direct this accessor for field: "+result, !result.contains("Rule.Phoneme.this.phonemeText"));
assertTrue(!result.contains("Rule.Phoneme.this.phonemeText"), "The result should contain direct this accessor for field: " + result);
canBeBuilt(output, 7);
}

Expand Down Expand Up @@ -367,7 +367,7 @@ public TokenWriter writeSeparator(String separator) {
checkRepeatingOfTokens("writeSeparator");
checkTokenWhitespace(separator, false);
//one of the separators
assertTrue("Unexpected separator: "+separator, separators.contains(separator));
assertTrue(separators.contains(separator), "Unexpected separator: " + separator);
handleTabs();
allTokens.append(separator);
return this;
Expand All @@ -377,7 +377,7 @@ public TokenWriter writeSeparator(String separator) {
public TokenWriter writeOperator(String operator) {
checkRepeatingOfTokens("writeOperator");
checkTokenWhitespace(operator, false);
assertTrue("Unexpected operator: "+operator, operators.contains(operator));
assertTrue(operators.contains(operator), "Unexpected operator: " + operator);
handleTabs();
allTokens.append(operator);
return this;
Expand All @@ -396,7 +396,7 @@ public TokenWriter writeLiteral(String literal) {
public TokenWriter writeKeyword(String keyword) {
checkRepeatingOfTokens("writeKeyword");
checkTokenWhitespace(keyword, false);
assertTrue("Unexpected java keyword: "+keyword, javaKeywords.contains(keyword));
assertTrue(javaKeywords.contains(keyword), "Unexpected java keyword: " + keyword);
handleTabs();
allTokens.append(keyword);
return this;
Expand All @@ -417,7 +417,7 @@ public TokenWriter writeIdentifier(String identifier) {
assertTrue(Character.isJavaIdentifierPart(c));
}
}
assertEquals("Keyword found in Identifier: " + identifier, false, javaKeywords.contains(identifier));
assertEquals(false, javaKeywords.contains(identifier), "Keyword found in Identifier: " + identifier);
handleTabs();
allTokens.append(identifier);
return this;
Expand Down Expand Up @@ -511,7 +511,7 @@ private void checkRepeatingOfTokens(String tokenType) {
// nothing
} else {
//check only other tokens then writeln, which is the only one which can repeat
assertEquals("Two tokens of same type current:" + tokenType + " " + allTokens.toString(), false, tokenType.equals(this.lastToken));
assertEquals(false, tokenType.equals(this.lastToken), "Two tokens of same type current:" + tokenType + " " + allTokens.toString());
}
this.lastToken = tokenType;
}
Expand Down