Преглед на файлове

Update to C++17, convert test cases to using GoogleTest

Sam Jaffe преди 6 години
родител
ревизия
6166a95dcf
променени са 3 файла, в които са добавени 61 реда и са изтрити 63 реда
  1. 2 2
      either.stream.xcodeproj/project.pbxproj
  2. 1 1
      include/stream/either_stream.hpp
  3. 58 60
      test/either_stream_test.cxx

+ 2 - 2
either.stream.xcodeproj/project.pbxproj

@@ -336,7 +336,7 @@
 			buildSettings = {
 				CLANG_ANALYZER_NONNULL = YES;
 				CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
-				CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
+				CLANG_CXX_LANGUAGE_STANDARD = "c++17";
 				CLANG_ENABLE_OBJC_WEAK = YES;
 				CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
 				CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
@@ -358,7 +358,7 @@
 			buildSettings = {
 				CLANG_ANALYZER_NONNULL = YES;
 				CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
-				CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
+				CLANG_CXX_LANGUAGE_STANDARD = "c++17";
 				CLANG_ENABLE_OBJC_WEAK = YES;
 				CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
 				CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;

+ 1 - 1
include/stream/either_stream.hpp

@@ -85,4 +85,4 @@ namespace stream { namespace either {
   auto make_stream(typename detail::either_left<E>::type const & opt) -> detail::either_stream<E> {
     return {{opt}};
   }
-} }
+} }

+ 58 - 60
test/either_stream_test.cxx

@@ -5,11 +5,10 @@
 //  Created by Sam Jaffe on 1/30/17.
 //
 
-#pragma once
+#include <gmock/gmock.h>
+#include <cmath>
 
-#include <cxxtest/TestSuite.h>
-
-#include "either_stream.hpp"
+#include "stream/either_stream.hpp"
 
 enum class MathError {
   OutOfBounds,
@@ -37,61 +36,60 @@ MathObject log(double value, double base) {
   }
 }
 
-class either_stream_TestSuite : public CxxTest::TestSuite {
-public:
-  void test_either_becomes_error() {
-    auto strm = stream::either::make_stream<MathObject>(1.0);
-    strm.flatmap([](double d) { return divide(d, 0); })
-    .match([](double) { TS_FAIL("Expected Error Type"); },
-           [](MathError e) { TS_ASSERT_EQUALS( MathError::DivideByZero, e ); });
-  }
-  
-  void test_either_computes_result() {
-    auto strm = stream::either::make_stream<MathObject>(1.0);
-    strm.map([](double d) { return multiply(d, 10.0); })
-    .match([](double d) { TS_ASSERT_DELTA(10.0, d, 0.00001); },
-           [](MathError) { TS_FAIL("Expected Value"); });
-  }
-  
-  void test_either_computes_result2() {
-    auto strm = stream::either::make_stream<MathObject>(1.0);
-    bool is_error = strm.map([](double d) { return multiply(d, 10.0); })
-    .match([](double) { return false; },
-           [](MathError) { return true; });
-    TS_ASSERT(!is_error);
-  }
-  
-  void test_either_propogates_error() {
-    auto strm = stream::either::make_stream<MathObject>(1.0);
-    strm.flatmap([](double d) { return divide(d, 0); })
-    .map([](double) { TS_FAIL("Operating on bad data"); return 0; })
-    .match([](double) { TS_FAIL("Expected Error Type"); },
-           [](MathError e) { TS_ASSERT_EQUALS( MathError::DivideByZero, e ); });
-  }
-  
-  void test_either_propogates_same_error() {
-    auto strm = stream::either::make_stream<MathObject>(1.0);
-    strm.flatmap([](double d) { return log(1.0, d); })
-    .flatmap([](double d) { return log(d, 1.0); })
-    .match([](double) { TS_FAIL("Expected Error Type"); },
-           [](MathError e) { TS_ASSERT_EQUALS( MathError::DivideByZero, e ); });
-  }
+using ::testing::DoubleNear;
+using ::testing::Eq;
 
-  void test_either_propogates_same_error2() {
-    auto strm = stream::either::make_stream<MathObject>(1.0);
-    strm.flatmap([](double d) { return log(0.0, d); })
-    .flatmap([](double d) { return log(d, 1.0); })
-    .match([](double) { TS_FAIL("Expected Error Type"); },
-           [](MathError e) { TS_ASSERT_EQUALS( MathError::DomainError, e ); });
-  }
-  
-  void test_either_propogates_same_error3() {
-    auto strm = stream::either::make_stream<MathObject>(1.0);
-    bool is_error = strm.flatmap([](double d) { return log(0.0, d); })
-    .flatmap([](double d) { return log(d, 1.0); })
-    .match([](double) { return false; },
-           [](MathError) { return true; });
-    TS_ASSERT(is_error);
-  }
+TEST(EitherStreamTest, becomes_error) {
+  auto strm = stream::either::make_stream<MathObject>(1.0);
+  strm.flatmap([](double d) { return divide(d, 0); })
+  .match([](double) { throw std::runtime_error("Expected error"); },
+         [](MathError e) { EXPECT_THAT(e, Eq(MathError::DivideByZero)); });
+}
 
-};
+TEST(EitherStreamTest, computes_result) {
+  auto strm = stream::either::make_stream<MathObject>(1.0);
+  strm.map([](double d) { return multiply(d, 10.0); })
+  .match([](double d) { EXPECT_THAT(10.0, DoubleNear(d, 0.00001)); },
+         [](MathError) { throw std::runtime_error("Expected concrete"); });
+}
+
+TEST(EitherStreamTest, computes_result2) {
+  auto strm = stream::either::make_stream<MathObject>(1.0);
+  bool is_error = strm.map([](double d) { return multiply(d, 10.0); })
+  .match([](double) { return false; },
+         [](MathError) { return true; });
+  EXPECT_FALSE(is_error);
+}
+
+TEST(EitherStreamTest, propogates_error) {
+  auto strm = stream::either::make_stream<MathObject>(1.0);
+  strm.flatmap([](double d) { return divide(d, 0); })
+  .map([](double) { throw std::runtime_error("Mapping while invalid"); return 0; })
+  .match([](double) { throw std::runtime_error("Expected error"); },
+         [](MathError e) { EXPECT_THAT(e, Eq(MathError::DivideByZero)); });
+}
+
+TEST(EitherStreamTest, propogates_same_error) {
+  auto strm = stream::either::make_stream<MathObject>(1.0);
+  strm.flatmap([](double d) { return log(1.0, d); })
+  .flatmap([](double d) { return log(d, 1.0); })
+  .match([](double) { throw std::runtime_error("Expected error"); },
+         [](MathError e) { EXPECT_THAT(e, Eq(MathError::DivideByZero)); });
+}
+
+TEST(EitherStreamTest, propogates_same_error2) {
+  auto strm = stream::either::make_stream<MathObject>(1.0);
+  strm.flatmap([](double d) { return log(0.0, d); })
+  .flatmap([](double d) { return log(d, 1.0); })
+  .match([](double) { throw std::runtime_error("Expected error"); },
+         [](MathError e) { EXPECT_THAT(e, Eq(MathError::DomainError)); });
+}
+
+TEST(EitherStreamTest, propogates_same_error3) {
+  auto strm = stream::either::make_stream<MathObject>(1.0);
+  bool is_error = strm.flatmap([](double d) { return log(0.0, d); })
+  .flatmap([](double d) { return log(d, 1.0); })
+  .match([](double) { return false; },
+         [](MathError) { return true; });
+  EXPECT_TRUE(is_error);
+}