optional_stream.t.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //
  2. // optional_stream.t.h
  3. // optional.stream
  4. //
  5. // Created by Sam Jaffe on 1/29/17.
  6. //
  7. #pragma once
  8. #include <cxxtest/TestSuite.h>
  9. #include "optional_stream.hpp"
  10. class optional_stream_TestSuite : public CxxTest::TestSuite {
  11. public:
  12. void test_optional_processes_real() {
  13. auto strm = stream::optional::make_stream(5);
  14. auto plus2 = [](int i) { return i+2; };
  15. std::optional<int> out = strm.map(plus2);
  16. TS_ASSERT(out);
  17. TS_ASSERT_EQUALS(*out, 7);
  18. }
  19. void test_optional_ignores_fake() {
  20. auto strm = stream::optional::make_stream<int>();
  21. auto fail = [](int i) { TS_FAIL("Expected Empty"); return i; };
  22. std::optional<int> out = strm.map(fail);
  23. TS_ASSERT(!out);
  24. }
  25. void test_optional_flatmap_can_become_empty() {
  26. auto strm = stream::optional::make_stream(5);
  27. auto discard_odd = [](int i) { return i%2==0? std::optional<int>(i) : std::nullopt; };
  28. std::optional<int> out = strm.flatmap(discard_odd);
  29. TS_ASSERT(!out);
  30. }
  31. void test_optional_flatmap_can_remain_exists() {
  32. auto strm = stream::optional::make_stream(6);
  33. auto discard_odd = [](int i) { return i%2==0? std::optional<int>(i) : std::nullopt; };
  34. std::optional<int> out = strm.flatmap(discard_odd);
  35. TS_ASSERT(out);
  36. TS_ASSERT_EQUALS(*out, 6);
  37. }
  38. };