Last active
June 15, 2023 15:44
Revisions
-
Jegors Čemisovs revised this gist
Jun 15, 2023 . 1 changed file with 21 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ 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 } } -
Jegors Čemisovs created this gist
Jun 15, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ 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)); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ 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; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ 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 */"); } }