瀏覽代碼

Make sign an enum-class.

Sam Jaffe 4 年之前
父節點
當前提交
0d9bf81fa5
共有 4 個文件被更改,包括 14 次插入14 次删除
  1. 1 1
      include/dice-roll/die.h
  2. 3 3
      src/die.cxx
  3. 5 5
      src/parser.cxx
  4. 5 5
      test/roll_test.cxx

+ 1 - 1
include/dice-roll/die.h

@@ -12,7 +12,7 @@
 #include <vector>
 
 namespace dice {
-  enum sign { PLUS = 1, MINUS = -1, ZERO = 0 };
+  enum class sign { PLUS = 1, MINUS = -1, ZERO = 0 };
   template <typename T> static sign sgn(T val) {
     return sign((T(0) < val) - (val < T(0)));
   }

+ 3 - 3
src/die.cxx

@@ -14,12 +14,12 @@
 #include "dice-roll/exception.h"
 
 namespace dice {
-  int sgn(sign s) { return s == MINUS ? -1 : 1; }
+  int sgn(sign s) { return s == sign::MINUS ? -1 : 1; }
   std::string str(sign s) {
     switch (s) {
-    case PLUS:
+    case sign::PLUS:
       return "+";
-    case MINUS:
+    case sign::MINUS:
       return "-";
     default:
       return "";

+ 5 - 5
src/parser.cxx

@@ -29,7 +29,7 @@ void parser::parse_dN(sign s, int value) {
     throw unexpected_token("Expected a number of sides", is_.tellg());
   }
   is_ >> dice_.of.back().sides;
-  parse_impl(ZERO);
+  parse_impl(sign::ZERO);
 }
 
 void parser::parse_const(sign s, int value) {
@@ -45,7 +45,7 @@ void parser::parse_impl(sign s) {
   int value = 0;
   if (isnumber(is_.peek())) {
     is_ >> value;
-  } else if (is_.peek() == EOF && s != ZERO) {
+  } else if (is_.peek() == EOF && s != sign::ZERO) {
     throw unexpected_token("Unexpected EOF while parsing", -1);
   }
   advance_over_whitespace(is_);
@@ -58,7 +58,7 @@ void parser::parse_impl(sign s) {
     // Handle 5+... cases
     parse_const(s, value);
     // Add another token
-    parse_impl((is_.get() == '+') ? PLUS : MINUS);
+    parse_impl((is_.get() == '+') ? sign::PLUS : sign::MINUS);
     break;
   default:
     parse_const(s, value);
@@ -76,13 +76,13 @@ dice parser::parse() {
   switch (is_.peek()) {
   case 'd':
   case 'D':
-    parse_impl(ZERO);
+    parse_impl(sign::ZERO);
     dice_.of.front().num = value;
     break;
   case '{':
     is_.get();
     dice_.num = value;
-    parse_impl(ZERO);
+    parse_impl(sign::ZERO);
     if (is_.get() != '}') {
       throw unexpected_token("Expected closing '}' in repeated roll",
                              is_.tellg());

+ 5 - 5
test/roll_test.cxx

@@ -41,7 +41,7 @@ TEST_F(RollTest, CanConstructNoThrow) {
 }
 
 TEST_F(RollTest, RollsOncePerDie) {
-  dice::dice mydice{1, {{dice::ZERO, 3, 4}}, {{dice::PLUS, 1}}};
+  dice::dice mydice{1, {{dice::sign::ZERO, 3, 4}}, {{dice::sign::PLUS, 1}}};
   EXPECT_CALL(*pRandom, exclusive(4)).WillOnce(Return(2)).WillOnce(Return(1))
       .WillOnce(Return(3));
   
@@ -52,7 +52,7 @@ TEST_F(RollTest, RollsOncePerDie) {
 }
 
 TEST_F(RollTest, DoesNotRollForConstants) {
-  dice::dice mydice{1, {}, {{dice::PLUS, 1}}};
+  dice::dice mydice{1, {}, {{dice::sign::PLUS, 1}}};
   EXPECT_CALL(*pRandom, exclusive(_)).Times(0);
   
   auto roll = dice::roller{pRandom}(mydice);
@@ -63,7 +63,7 @@ TEST_F(RollTest, DoesNotRollForConstants) {
 }
 
 TEST_F(RollTest, OperatorInSumsElements) {
-  dice::dice mydice{1, {{dice::ZERO, 3, 4}}, {{dice::PLUS, 1}}};
+  dice::dice mydice{1, {{dice::sign::ZERO, 3, 4}}, {{dice::sign::PLUS, 1}}};
   EXPECT_CALL(*pRandom, exclusive(4)).WillOnce(Return(2)).WillOnce(Return(1))
   .WillOnce(Return(3));
   
@@ -80,14 +80,14 @@ TEST(RollSerialTest, CanPrint0dN) {
 }
 
 TEST(RollSerialTest, Prints1dNAsRollNumber) {
-  dice::dice_roll roll{{{dice::ZERO, {3}}}};
+  dice::dice_roll roll{{{dice::sign::ZERO, {3}}}};
   std::stringstream ss;
   ss << roll;
   EXPECT_THAT(ss.str(), Eq("3"));
 }
 
 TEST(RollSerialTest, PrintsNdMAsList) {
-  dice::dice_roll roll{{{dice::ZERO, {3, 2, 4}}}};
+  dice::dice_roll roll{{{dice::sign::ZERO, {3, 2, 4}}}};
   std::stringstream ss;
   ss << roll;
   EXPECT_THAT(ss.str(), Eq("[ 3, 2, 4 ]"));