Skip to content

Instantly share code, notes, and snippets.

@rabestro
Last active June 15, 2023 15:44
Show Gist options
  • Save rabestro/bf406d1eede86d5ca4f11e3d9d2e9dff to your computer and use it in GitHub Desktop.
Save rabestro/bf406d1eede86d5ca4f11e3d9d2e9dff to your computer and use it in GitHub Desktop.
package com.epam.flipflop;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.function.Predicate;
class CopilotTest {
@Test
void testFlipFlop() {
var flipFlop = new FlipFlop<Integer>(x -> x % 2 == 0, x -> x % 3 == 0);
assertTrue(flipFlop.test(2));
assertTrue(flipFlop.test(3));
assertTrue(flipFlop.test(4));
assertTrue(flipFlop.test(6));
assertFalse(flipFlop.test(9));
assertTrue(flipFlop.test(10));
}
}
package org.epam.flipflop;
import java.util.function.Predicate;
public final class FlipFlopPredicate<T> implements Predicate<T> {
private boolean state;
private final Predicate<T> lhs;
private final Predicate<T> rhs;
public FlipFlopPredicate(Predicate<T> leftHandSide, Predicate<T> rightHandSide) {
this.lhs = leftHandSide;
this.rhs = rightHandSide;
}
@Override
public boolean test(T value) {
var result = state || lhs.test(value);
state = result && !rhs.test(value);
return result;
}
}
package com.epam.flipflop;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import java.util.function.Predicate;
import java.util.stream.Stream;
import static com.epam.flipflop.FlipFlopPredicate.flipFlop;
import static org.assertj.core.api.Assertions.assertThat;
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class FlipFlopPredicate2Test {
@Test
void extract_all_javadoc_by_flip_flop_predicate() {
// sample test data
var sampleSourceCode = """
package sample;
/**
* Multiline javadoc
*/
void someMethod() {}
// single-line comment
/** single-line javadoc */
/*
* Multiline comment
*/
""";
// given
Predicate<String> javaDocOpen = line -> line.startsWith("/**");
Predicate<String> javaDocClose = line -> line.endsWith("*/");
// when
var javaDocsLines = sampleSourceCode
.lines()
.filter(flipFlop(javaDocOpen, javaDocClose));
// then
assertThat(javaDocsLines)
.as("JavaDoc comments extracted from the source code")
.containsExactly(
"/**",
" * Multiline javadoc",
" */",
"/** single-line javadoc */");
}
}
class FlipFlopPredicateSpec extends Specification {
def 'the flip-flop predicate returns false until the first condition is met'() {
given: 'the flip-flop left and right side predicates'
def leftSide = 'ON'::equals
def rightSide = 'OFF'::equals
when: 'we create a flip-flop predicate'
def flipFlop = FlipFlopPredicate.flipFlop(leftSide, rightSide)
then: 'the result is false while the left side is not met'
flipFlop.test('A') == false
then: 'the result is true when the left side is met'
flipFlop.test('ON') == true
then: 'the result is true until the right side is met'
flipFlop.test('A') == true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment