Skip to main content

Testing for Loops

Simple Story

The instructions on this page forbid answering with a loop. The pupil has to add up a list of weights without going through the list one by one in any of the four usual ways. While marking by the grading instructions, the teacher reads what the pupil wrote and looks for those four ways.

The teacher finds only the four ways the instructions name. A pupil who walks the list in some other way, or who solves a smaller version of the question again and again, passes.

Who is who

Position in the example test class​

Red marks the test method this page documents. Every page in this section shows the same example class, so reading them in order walks it from top to bottom.

src/test/java/org/example/PenguinStructureTest.java (Maven)
package org.example;

import com.github.javaparser.ParserConfiguration.LanguageLevel;

import de.tum.cit.ase.ares.api.Policy;
import de.tum.cit.ase.ares.api.ast.asserting.UnwantedNodesAssert;
import de.tum.cit.ase.ares.api.ast.asserting.UnwantedRecursionAssert;
import de.tum.cit.ase.ares.api.ast.type.ClassType;
import de.tum.cit.ase.ares.api.ast.type.ConditionalType;
import de.tum.cit.ase.ares.api.ast.type.ExceptionHandlingType;
import de.tum.cit.ase.ares.api.ast.type.LoopType;
import de.tum.cit.ase.ares.api.jupiter.PublicTest;

/** One check per family. A real exercise keeps only the checks its task needs. */
@Policy(value = "src/test/resources/SecurityPolicy.yaml", withinPath = "classes/org/example")
public class PenguinStructureTest {

/** Checks that the source contains no if, no ?: and no switch. */
@PublicTest
void usesNoConditionals() {
UnwantedNodesAssert.assertThatProjectSources()
.withinPackage("org.example")
.withLanguageLevel(LanguageLevel.JAVA_17)
.excludeMainMethod()
.hasNo(ConditionalType.ANY);
}

/** Checks that the source contains no for, no for-each, no while and no do-while. */
@PublicTest
void usesNoLoops() {
UnwantedNodesAssert.assertThatProjectSources()
.withinPackage("org.example")
.withLanguageLevel(LanguageLevel.JAVA_17)
.excludeMainMethod()
.hasNo(LoopType.ANY);
}

/** Checks that the source contains no assert, no throw and no catch. */
@PublicTest
void usesNoExceptionHandling() {
UnwantedNodesAssert.assertThatProjectSources()
.withinPackage("org.example")
.withLanguageLevel(LanguageLevel.JAVA_17)
.excludeMainMethod()
.hasNo(ExceptionHandlingType.ANY);
}

/** Checks that the source declares no class and no record inside a body of code. */
@PublicTest
void declaresNoLocalClassesOrRecords() {
UnwantedNodesAssert.assertThatProjectSources()
.withinPackage("org.example")
.withLanguageLevel(LanguageLevel.JAVA_17)
.excludeMainMethod()
.hasNo(ClassType.ANY);
}

/** Checks that the source contains at least one recursive chain of calls. */
@PublicTest
void solvesRecursively() {
UnwantedRecursionAssert.assertThatProjectSources()
.withLanguageLevel(LanguageLevel.JAVA_17)
.hasRecursion();
}
}

The example uses the Maven layout. For Gradle, write withinPath = "classes/java/main/org/example", and set value to wherever your policy file lives. PenguinStructureTest has to appear in theFollowingClassesAreTestClasses of that policy, as the overview explains.

Constants​

Implemented by LoopType in ast/type/LoopType.java. The abstract syntax tree (AST) check reports each find under the name in the second column.

ConstantReported asWhat it findsExample
ANYAll four names belowEvery construct of the rows belowAny example below
ANY_FORFor-Statement, For-Each-StatementBoth forms of forThe examples of the next two rows
FORSTMTFor-StatementThe counting forfor (int i = 0; i < n; i++) { }
FOR_EACHSTMTFor-Each-StatementThe enhanced for over an array or a collectionfor (Penguin p : penguins) { }
ANY_WHILEWhile-Statement, Do-While-StatementBoth forms of whileThe examples of the next two rows
WHILESTMTWhile-Statementwhilewhile (n > 0) { n--; }
DO_WHILESTMTDo-While-Statementdo-whiledo { n--; } while (n > 0);

Notes​

  • Repetition without one of these constructs passes. The check does not report penguins.forEach(...), a stream such as IntStream.range(0, n).sum() or a recursive call. Combine this check with Testing for Recursion when the exercise asks for recursion in place of the loop.
  • A loop with a label, such as outer: for (...), counts like any other loop.