333 lines
10 KiB
Java
333 lines
10 KiB
Java
|
|
/**
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2021 Exercism
|
|
* Copyright (c) 2026 Networked Embedded Systems, University of Duisburg-Essen
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
import java.lang.reflect.Method;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
import java.lang.annotation.Retention;
|
|
import java.lang.annotation.RetentionPolicy;
|
|
import java.lang.annotation.Target;
|
|
import java.lang.annotation.ElementType;
|
|
|
|
public class BobMain {
|
|
|
|
@Test(name = "stating something")
|
|
public void saySomething() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Whatever.", bob.hey("Tom-ay-to, tom-aaaah-to."));
|
|
});
|
|
}
|
|
|
|
@Test(name = "shouting")
|
|
@Disabled
|
|
public void shouting() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Whoa, chill out!", bob.hey("WATCH OUT!"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "shouting gibberish")
|
|
@Disabled
|
|
public void shoutingGibberish() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Whoa, chill out!", bob.hey("FCECDFCAAB"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "asking a question")
|
|
@Disabled
|
|
public void askingAQuestion() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Sure.", bob.hey("Does this cryogenic chamber make me look fat?"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "asking a numeric question")
|
|
@Disabled
|
|
public void askingANumericQuestion() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Sure.", bob.hey("You are, what, like 15?"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "asking gibberish")
|
|
@Disabled
|
|
public void askingGibberish() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Sure.", bob.hey("fffbbcbeab?"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "talking forcefully")
|
|
@Disabled
|
|
public void talkingForcefully() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Whatever.", bob.hey("Hi there!"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "using acronyms in regular speech")
|
|
@Disabled
|
|
public void usingAcronymsInRegularSpeech() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Whatever.", bob.hey("It's OK if you don't want to go work for NASA."));
|
|
});
|
|
}
|
|
|
|
@Test(name = "forceful question")
|
|
@Disabled
|
|
public void forcefulQuestions() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Calm down, I know what I'm doing!", bob.hey("WHAT'S GOING ON?"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "shouting numbers")
|
|
@Disabled
|
|
public void shoutingNumbers() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Whoa, chill out!", bob.hey("1, 2, 3 GO!"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "no letters")
|
|
@Disabled
|
|
public void onlyNumbers() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Whatever.", bob.hey("1, 2, 3"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "question with no letters")
|
|
@Disabled
|
|
public void questionWithOnlyNumbers() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Sure.", bob.hey("4?"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "shouting with special characters")
|
|
@Disabled
|
|
public void shoutingWithSpecialCharacters() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Whoa, chill out!", bob.hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "shouting with no exclamation mark")
|
|
@Disabled
|
|
public void shoutingWithNoExclamationMark() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Whoa, chill out!", bob.hey("I HATE THE DENTIST"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "statement containing question mark")
|
|
@Disabled
|
|
public void statementContainingQuestionMark() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Whatever.", bob.hey("Ending with ? means a question."));
|
|
});
|
|
}
|
|
|
|
@Test(name = "non-letters with question")
|
|
@Disabled
|
|
public void nonLettersWithQuestion() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Sure.", bob.hey(":) ?"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "prattling on")
|
|
@Disabled
|
|
public void prattlingOn() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Sure.", bob.hey("Wait! Hang on. Are you going to be OK?"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "silence")
|
|
@Disabled
|
|
public void silence() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Fine. Be that way!", bob.hey(""));
|
|
});
|
|
}
|
|
|
|
@Test(name = "prolonged silence")
|
|
@Disabled
|
|
public void prolongedSilence() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Fine. Be that way!", bob.hey(" "));
|
|
});
|
|
}
|
|
|
|
@Test(name = "alternate silence")
|
|
@Disabled
|
|
public void alternateSilence() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Fine. Be that way!", bob.hey("\t\t\t\t\t\t\t\t\t\t"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "starting with whitespace")
|
|
@Disabled
|
|
public void startingWithWhitespace() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Whatever.", bob.hey(" hmmmmmmm..."));
|
|
});
|
|
}
|
|
|
|
@Test(name = "ending with whitespace")
|
|
@Disabled
|
|
public void endingWithWhiteSpace() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Sure.", bob.hey("Okay if like my spacebar quite a bit? "));
|
|
});
|
|
}
|
|
|
|
@Test(name = "other whitespace")
|
|
@Disabled
|
|
public void otherWhiteSpace() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Fine. Be that way!", bob.hey("\n\r \t"));
|
|
});
|
|
}
|
|
|
|
@Test(name = "non-question ending with whitespace")
|
|
@Disabled
|
|
public void nonQuestionEndingWithWhiteSpace() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Whatever.", bob.hey("This is a statement ending with whitespace "));
|
|
});
|
|
}
|
|
|
|
@Test(name = "multiple line question")
|
|
@Disabled
|
|
public void multipleLineQuestion() {
|
|
runTest(() -> {
|
|
Bob bob = new Bob();
|
|
assertEquals("Sure.", bob.hey("\nDoes this cryogenic chamber make\n me look fat?"));
|
|
});
|
|
}
|
|
|
|
private void runTest(Runnable body) {
|
|
try {
|
|
body.run();
|
|
System.out.println("TEST PASSED");
|
|
} catch (Exception | AssertionError t) {
|
|
System.out.println("TEST FAILED: " + t.getMessage());
|
|
}
|
|
}
|
|
|
|
private void assertEquals(Object expected, Object actual) {
|
|
if (expected == null && actual == null)
|
|
return;
|
|
if (expected != null && expected.equals(actual))
|
|
return;
|
|
throw new AssertionError("Expected: " + expected + ", but got: " + actual);
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
new BobMain().runTests();
|
|
}
|
|
|
|
/**
|
|
* Annotation to mark test methods. The name parameter is used to identify the
|
|
* test in the output.
|
|
*/
|
|
@Retention(RetentionPolicy.RUNTIME)
|
|
@Target(ElementType.METHOD)
|
|
private @interface Test {
|
|
String name();
|
|
}
|
|
|
|
/**
|
|
* Annotation to mark test methods as disabled. Disabled tests will not be
|
|
* executed when running the tests.
|
|
*/
|
|
@Retention(RetentionPolicy.RUNTIME)
|
|
@Target(ElementType.METHOD)
|
|
private @interface Disabled {
|
|
}
|
|
|
|
private void runTests() {
|
|
Method[] methods = this.getClass().getDeclaredMethods();
|
|
List<Method> allTestMethods = Arrays.asList(methods).stream().filter(m -> m.isAnnotationPresent(Test.class))
|
|
.collect(Collectors.toList());
|
|
List<Method> enabledTestMethods = allTestMethods.stream().filter(m -> !m.isAnnotationPresent(Disabled.class))
|
|
.collect(Collectors.toList());
|
|
System.out
|
|
.println("Running " + enabledTestMethods.size() + " out of " + allTestMethods.size() + " tests... \n");
|
|
for (Method method : enabledTestMethods) {
|
|
try {
|
|
System.out.println("Running test: " + method.getAnnotation(Test.class).name());
|
|
method.invoke(this);
|
|
} catch (Exception e) {
|
|
System.out.println("Test " + method.getAnnotation(Test.class).name()
|
|
+ " failed with an unexpected exception: " + e.getCause());
|
|
}
|
|
}
|
|
System.out.println("Finished running tests.");
|
|
if (allTestMethods.size() != enabledTestMethods.size()) {
|
|
System.out.println("\nThe following tests are disabled and were not executed:");
|
|
for (Method method : allTestMethods) {
|
|
if (method.isAnnotationPresent(Disabled.class)) {
|
|
System.out.println("- " + method.getAnnotation(Test.class).name());
|
|
}
|
|
}
|
|
System.out.println(
|
|
"To enable these tests, remove the @Disabled annotation from the corresponding test methods.");
|
|
}
|
|
}
|
|
}
|