Bläddra i källkod

Re-apply clang-format.

Sam Jaffe 4 år sedan
förälder
incheckning
01c1aaf303
12 ändrade filer med 79 tillägg och 68 borttagningar
  1. 1 1
      .clang-format
  2. 7 8
      include/dice-roll/die.h
  3. 3 3
      include/dice-roll/parser.h
  4. 2 1
      include/dice-roll/roll.h
  5. 10 5
      src/die.cxx
  6. 19 11
      src/io.cxx
  7. 1 1
      src/parser.cxx
  8. 2 4
      src/roll.cxx
  9. 8 6
      stateful_dice/main.cxx
  10. 4 3
      test/io_test.cxx
  11. 2 2
      test/parser_test.cxx
  12. 20 23
      test/roll_test.cxx

+ 1 - 1
.clang-format

@@ -75,7 +75,7 @@ KeepEmptyLinesAtTheStartOfBlocks: true
 MacroBlockBegin: ''
 MacroBlockEnd:   ''
 MaxEmptyLinesToKeep: 1
-NamespaceIndentation: All
+NamespaceIndentation: None
 ObjCBlockIndentWidth: 2
 ObjCSpaceAfterProperty: false
 ObjCSpaceBeforeProtocolList: true

+ 7 - 8
include/dice-roll/die.h

@@ -41,9 +41,9 @@ struct mod {
  */
 struct difficulty_class {
   enum class test { None, Less, LessOrEqual, Greater, GreaterOrEqual };
-  
+
   bool operator()(int value) const;
-  
+
   test comp{test::None};
   int against{0};
 };
@@ -57,12 +57,11 @@ struct dice {
 };
 
 /**
- * @brief A generator function to turn a string representation of a dice roll into a C++ object
- * @param strdice A string representation of a dice roll, represented as one of the
- * following expression classes:
- * Die = [1-9]?\d*d[1-9]\d*
- * SingleRoll: ($Die|\d+)((+|-)($Die|\d+))*
- * RepeatRoll: [1-9]\d*\{$SingleRoll\}
+ * @brief A generator function to turn a string representation of a dice roll
+ * into a C++ object
+ * @param strdice A string representation of a dice roll, represented as one of
+ * the following expression classes: Die = [1-9]?\d*d[1-9]\d* SingleRoll:
+ * ($Die|\d+)((+|-)($Die|\d+))* RepeatRoll: [1-9]\d*\{$SingleRoll\}
  * @return a dice object representing the roll
  * @throws dice::unexpected_token if a parse failure occurs
  */

+ 3 - 3
include/dice-roll/parser.h

@@ -41,12 +41,12 @@ class parser {
 private:
   std::istream & is_;
   dice dice_;
-  
+
 public:
   parser(std::istream & is) : is_(is) {}
-  
+
   dice parse();
-  
+
 private:
   /**
    * Main dispatch function for parsing a dice roll.

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

@@ -41,7 +41,7 @@ class roller {
 public:
   roller();
   roller(engine::random && g);
-  
+
   /**
    * @param d Some dice roll structure, containing any number of dice sets 'NdM'
    * as well as any number of roll modifiers (fixed numbers). Additionally,
@@ -49,6 +49,7 @@ public:
    * @return A vector of actualized rolls, where `vector.size() == d.num`.
    */
   std::vector<dice_roll> operator()(dice const & d);
+
 private:
   engine::random gen;
 };

+ 10 - 5
src/die.cxx

@@ -18,11 +18,16 @@ mod::operator int() const { return sgn(sign) * value; }
 
 bool difficulty_class::operator()(int value) const {
   switch (comp) {
-    case test::None: return true;
-    case test::Less: return value < against;
-    case test::LessOrEqual: return value <= against;
-    case test::Greater: return value > against;
-    case test::GreaterOrEqual: return value >= against;
+  case test::None:
+    return true;
+  case test::Less:
+    return value < against;
+  case test::LessOrEqual:
+    return value <= against;
+  case test::Greater:
+    return value > against;
+  case test::GreaterOrEqual:
+    return value >= against;
   }
 }
 

+ 19 - 11
src/io.cxx

@@ -17,19 +17,27 @@ namespace dice {
 
 std::ostream & operator<<(std::ostream & out, sign s) {
   switch (s) {
-    case sign::PLUS: return out << '+';
-    case sign::MINUS: return out << '-';
-    case sign::ZERO: return out;
+  case sign::PLUS:
+    return out << '+';
+  case sign::MINUS:
+    return out << '-';
+  case sign::ZERO:
+    return out;
   }
 }
 
 std::ostream & operator<<(std::ostream & out, difficulty_class::test t) {
   switch (t) {
-    case difficulty_class::test::None: return out;
-    case difficulty_class::test::Less: return out << '<';
-    case difficulty_class::test::LessOrEqual: return out << '<' << '=';
-    case difficulty_class::test::Greater: return out << '>';
-    case difficulty_class::test::GreaterOrEqual: return out << '>' << '=';
+  case difficulty_class::test::None:
+    return out;
+  case difficulty_class::test::Less:
+    return out << '<';
+  case difficulty_class::test::LessOrEqual:
+    return out << '<' << '=';
+  case difficulty_class::test::Greater:
+    return out << '>';
+  case difficulty_class::test::GreaterOrEqual:
+    return out << '>' << '=';
   }
 }
 
@@ -52,11 +60,11 @@ std::ostream & operator<<(std::ostream & out, die_roll const & r) {
   out << r.sign;
   switch (r.rolled.size()) {
   case 0:
-      // Prevent crashes if we somehow get a 0dM expression
+    // Prevent crashes if we somehow get a 0dM expression
     return out << "0";
   case 1:
-      // Don't bother with braces if there's only a single roll,
-      // the braces are for grouping purposes.
+    // Don't bother with braces if there's only a single roll,
+    // the braces are for grouping purposes.
     return out << r.rolled[0];
   default:
     out << "[ ";

+ 1 - 1
src/parser.cxx

@@ -93,7 +93,7 @@ dice parser::parse() {
   advance_over_whitespace(is_);
   if (isnumber(is_.peek())) { is_ >> value; }
   advance_over_whitespace(is_);
-  
+
   switch (is_.peek()) {
   case 'd':
   case 'D':

+ 2 - 4
src/roll.cxx

@@ -19,7 +19,7 @@ die_roll::operator int() const {
 
 dice_roll::operator int() const {
   auto subtotal = std::accumulate(sub_rolls.begin(), sub_rolls.end(), 0) +
-         std::accumulate(modifiers.begin(), modifiers.end(), 0);
+                  std::accumulate(modifiers.begin(), modifiers.end(), 0);
   if (dc.comp != difficulty_class::test::None) {
     return static_cast<int>(dc(subtotal));
   } else {
@@ -44,9 +44,7 @@ dice_roll roll_impl(dice const & d, engine::random & gen) {
 }
 
 roller::roller() : gen() {}
-roller::roller(engine::random && g)
-: gen(std::forward<engine::random>(g)) {
-}
+roller::roller(engine::random && g) : gen(std::forward<engine::random>(g)) {}
 
 std::vector<dice_roll> roller::operator()(dice const & d) {
   std::vector<dice_roll> out;

+ 8 - 6
stateful_dice/main.cxx

@@ -23,8 +23,10 @@ void usage() {
 void print_binding_os(std::pair<const std::string, std::string> const & pair,
                       std::ostream & os) {
   os << pair.first;
-  if (pair.second == "") os << " is not defined\n";
-  else os << " := " << pair.second << "\n";
+  if (pair.second == "")
+    os << " is not defined\n";
+  else
+    os << " := " << pair.second << "\n";
 }
 
 void print_binding(std::pair<const std::string, std::string> const & pair) {
@@ -37,10 +39,10 @@ public:
     if (!path.empty()) { load(path); }
   }
   void operator()(std::string const & str);
- 
+
 private:
   using binding_t = std::map<std::string, std::string>;
-  
+
   bool regex_search(char const * regex) {
     return std::regex_match(curr, match, std::regex(regex));
   }
@@ -49,7 +51,7 @@ private:
     auto it = bindings.find(key);
     return it == bindings.cend() ? binding_t::value_type(key, "") : *it;
   }
-  
+
   void save(std::string const & file) const {
     using namespace std::placeholders;
     std::ofstream out(file);
@@ -64,6 +66,7 @@ private:
       }
     }
   }
+
 private:
   std::string curr;
   std::smatch match;
@@ -104,7 +107,6 @@ void evaluator::operator()(std::string const & str) {
   }
 }
 
-
 int main(int argc, const char * argv[]) {
   std::string line;
   std::cout << "> ";

+ 4 - 3
test/io_test.cxx

@@ -42,8 +42,8 @@ TEST(DiceIOTest, CanReadDirectlyFromStream) {
 }
 
 TEST(DiceIOTest, PrintsOutDCMarker) {
-  std::string const tokens[] = { "<", "<=", ">", ">=" };
-  for (auto &tok : tokens) {
+  std::string const tokens[] = {"<", "<=", ">", ">="};
+  for (auto & tok : tokens) {
     std::stringstream ss;
     ss << dice::from_string("1d20" + tok + "10");
     EXPECT_THAT(ss.str(), "1d20" + tok + "10");
@@ -72,7 +72,8 @@ TEST(RollIOTest, PrintsNdMAsList) {
 }
 
 TEST(RollIOTest, PrintsModifiersAfterList) {
-  dice::dice_roll roll{{{dice::sign::ZERO, {3, 2, 4}}}, {{dice::sign::PLUS, 2}}};
+  dice::dice_roll roll{{{dice::sign::ZERO, {3, 2, 4}}},
+                       {{dice::sign::PLUS, 2}}};
   std::stringstream ss;
   ss << roll;
   EXPECT_THAT(ss.str(), Eq("[ 3, 2, 4 ]+2"));

+ 2 - 2
test/parser_test.cxx

@@ -61,8 +61,8 @@ TEST(ParserTest, AllowsMultipleDice) {
   dice::dice capture;
   EXPECT_NO_THROW(capture = dice::from_string("1d4+1d6"));
   EXPECT_THAT(capture.of, SizeIs(2));
-  EXPECT_THAT(capture.of, ElementsAre(Eq(unsigned_die(1, 4)),
-                                      Eq(unsigned_die(1, 6))));
+  EXPECT_THAT(capture.of,
+              ElementsAre(Eq(unsigned_die(1, 4)), Eq(unsigned_die(1, 6))));
 }
 
 TEST(ParserTest, CanIncludeConstant) {

+ 20 - 23
test/roll_test.cxx

@@ -21,14 +21,10 @@ struct MockRandomImpl : public engine::detail::random_impl {
 
 class RollerTest : public ::testing::Test {
 protected:
-  void SetUp() override {
-    pRandom = std::make_shared<MockRandomImpl>();
-  }
-  
-  void TearDown() override {
-    pRandom.reset();
-  }
-  
+  void SetUp() override { pRandom = std::make_shared<MockRandomImpl>(); }
+
+  void TearDown() override { pRandom.reset(); }
+
 protected:
   std::shared_ptr<MockRandomImpl> pRandom;
 };
@@ -42,9 +38,11 @@ TEST_F(RollerTest, CanConstructNoThrow) {
 
 TEST_F(RollerTest, RollsOncePerDie) {
   dice::dice mydice{1, {{dice::sign::ZERO, 3, 4}}, {{dice::sign::PLUS, 1}}};
-  EXPECT_CALL(*pRandom, exclusive(4)).WillOnce(Return(2)).WillOnce(Return(1))
+  EXPECT_CALL(*pRandom, exclusive(4))
+      .WillOnce(Return(2))
+      .WillOnce(Return(1))
       .WillOnce(Return(3));
-  
+
   auto roll = dice::roller{pRandom}(mydice);
   EXPECT_THAT(roll, SizeIs(1));
   EXPECT_THAT(roll[0].sub_rolls, SizeIs(1));
@@ -54,7 +52,7 @@ TEST_F(RollerTest, RollsOncePerDie) {
 TEST_F(RollerTest, DoesNotRollForConstants) {
   dice::dice mydice{1, {}, {{dice::sign::PLUS, 1}}};
   EXPECT_CALL(*pRandom, exclusive(_)).Times(0);
-  
+
   auto roll = dice::roller{pRandom}(mydice);
   EXPECT_THAT(roll, SizeIs(1));
   EXPECT_THAT(roll[0].sub_rolls, SizeIs(0));
@@ -64,9 +62,11 @@ TEST_F(RollerTest, DoesNotRollForConstants) {
 
 TEST_F(RollerTest, InjectsDCIntoRollObjects) {
   using test = dice::difficulty_class::test;
-  dice::dice mydice{1, {{dice::sign::ZERO, 3, 4}},
-      {{dice::sign::PLUS, 1}}, {test::Less, 20}};
-  EXPECT_CALL(*pRandom, exclusive(4)).WillOnce(Return(2)).WillOnce(Return(1))
+  dice::dice mydice{
+      1, {{dice::sign::ZERO, 3, 4}}, {{dice::sign::PLUS, 1}}, {test::Less, 20}};
+  EXPECT_CALL(*pRandom, exclusive(4))
+      .WillOnce(Return(2))
+      .WillOnce(Return(1))
       .WillOnce(Return(3));
 
   auto roll = dice::roller{pRandom}(mydice);
@@ -75,21 +75,18 @@ TEST_F(RollerTest, InjectsDCIntoRollObjects) {
 }
 
 TEST(DiceRollTest, OperatorIntSumsElements) {
-  dice::dice_roll const roll{
-    {{dice::sign::ZERO, {3, 2, 4}}},
-    {{dice::sign::PLUS, 1}}};
+  dice::dice_roll const roll{{{dice::sign::ZERO, {3, 2, 4}}},
+                             {{dice::sign::PLUS, 1}}};
   // 3 + 2 + 4 + 1 = 10
   EXPECT_THAT(int(roll), Eq(10));
 }
 
 TEST(DiceRollTest, OperatorIntWithDCReturnsOneOrZero) {
   using test = dice::difficulty_class::test;
-  dice::dice_roll roll{
-    {{dice::sign::ZERO, {3, 2, 4}}},
-    {{dice::sign::PLUS, 1}},
-    {test::Less, 10}
-  };
-  
+  dice::dice_roll roll{{{dice::sign::ZERO, {3, 2, 4}}},
+                       {{dice::sign::PLUS, 1}},
+                       {test::Less, 10}};
+
   EXPECT_THAT(int(roll), Eq(0));
   --roll.modifiers[0].value;
   EXPECT_THAT(int(roll), Eq(1));