Last active
November 29, 2016 19:39
-
-
Save gtrefs/0531037b479572011e55e15bb48f34c0 to your computer and use it in GitHub Desktop.
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 characters
package de.gtrefs.fizzbuzz.more; | |
import javaslang.collection.Stream; | |
import javaslang.test.Arbitrary; | |
import javaslang.test.Property; | |
import org.junit.jupiter.api.Test; | |
import static org.assertj.core.api.Assertions.assertThat; | |
public class FizzBuzzStream { | |
@Test | |
public void generate_a_stream(){ | |
assertThat(fizzBuzz()).isNotNull(); | |
} | |
@Test | |
public void generate_a_stream_with_third_element_being_Fizz(){ | |
nth_element_should_be(3,"Fizz"); | |
} | |
@Test | |
public void generate_a_stream_with_fifth_element_being_Buzz(){ | |
nth_element_should_be(5,"Buzz"); | |
} | |
@Test | |
public void generate_a_stream_with_first_element_being_1(){ | |
nth_element_should_be(1,"1"); | |
} | |
private void nth_element_should_be(Integer n, String result){ | |
assertThat(fizzBuzz().drop(n-1).head()).isEqualTo(result); | |
} | |
@Test | |
public void generate_a_stream_with_every_third_element_being_Fizz() { | |
final Arbitrary<Integer> multiplesOf3 = Arbitrary.integer() | |
.filter(i -> i > 0) | |
.filter(i -> i % 3 == 0); | |
Property.def("Every third element must contain Fizz") | |
.forAll(multiplesOf3) | |
.suchThat(i -> fizzBuzz().drop(i - 1).head().contains("Fizz")) | |
.check() | |
.assertIsSatisfied(); | |
} | |
private Stream<String> fizzBuzz(){ | |
return Stream.of("","","Fizz").cycle() | |
.zip(Stream.of("","","","","Buzz").cycle()) | |
.zip(Stream.from(1)) | |
.map(flatten()); | |
} | |
private Function<Tuple2<Tuple2<String, String>, Integer>, String> flatten() { | |
return t -> { | |
final String fizzBuzz = t._1._1 + t._1._2; | |
return fizzBuzz.isEmpty()?t._2.toString():fizzBuzz; | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment