Leosac  0.8.0
Open Source Access Control
Visitor.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "tools/Visitor.hpp"
21 #include "tools/IVisitable.hpp"
22 #include "gtest/gtest.h"
23 
24 using namespace Leosac;
25 using namespace Leosac::Tools;
26 
27 namespace Leosac
28 {
29 namespace Test
30 {
31 
32 struct DummyVisitable : public virtual IVisitable
33 {
34  DummyVisitable() = default;
36 };
37 
38 struct DummyVisitor : public Visitor<DummyVisitable>
39 {
41  {
42  }
43 
44  void visit(DummyVisitable &) override
45  {
46  visited_non_const_ = true;
47  }
48 
49  void visit(const DummyVisitable &) override
50  {
51  visited_const_ = true;
52  }
53 
54  bool visited_non_const_{false};
55  bool visited_const_{false};
56 };
57 
58 TEST(TestVisitor, simple_non_const)
59 {
60  DummyVisitable visitable;
61  DummyVisitor visitor;
62  visitable.accept(visitor);
63  ASSERT_TRUE(visitor.visited_non_const_);
64  ASSERT_FALSE(visitor.visited_const_);
65 }
66 
67 TEST(TestVisitor, simple_const)
68 {
69  const DummyVisitable visitable;
70  DummyVisitor visitor;
71  visitable.accept(visitor);
72  ASSERT_TRUE(visitor.visited_const_);
73  ASSERT_FALSE(visitor.visited_non_const_);
74 }
75 
76 struct DummyConstOnlyVisitor : public Visitor<DummyVisitable>
77 {
79  : visited_const_(false)
80  {
81  }
82 
83  void visit(const DummyVisitable &) override
84  {
85  visited_const_ = true;
86  }
87 
89 };
90 
91 TEST(TestVisitor, const_visit_accept_non_const_visitable)
92 {
93  DummyVisitable visitable;
94  DummyConstOnlyVisitor visitor;
95  visitable.accept(visitor);
96  ASSERT_TRUE(visitor.visited_const_);
97 }
98 
99 TEST(TestVisitor, const_visit_accept_const_visitable)
100 {
101  const DummyVisitable visitable;
102  DummyConstOnlyVisitor visitor;
103  visitable.accept(visitor);
104  ASSERT_TRUE(visitor.visited_const_);
105 }
106 
113 struct DummyVisitor2 : public Visitor<DummyVisitable>
114 {
115  DummyVisitor2() = default;
116 
117  void visit(const DummyVisitable &) override
118  {
119  visited_ = true;
120  }
121 
122  bool visited_{false};
123 };
124 
125 struct DummyChildVisitable : public virtual DummyVisitable
126 {
127  DummyChildVisitable() = default;
128 
130 };
131 
132 TEST(TestVisitor, test_class_hierarchy)
133 {
134  DummyChildVisitable visitable;
135  DummyVisitor2 visitor;
136  visitable.accept(visitor);
137  ASSERT_TRUE(visitor.visited_);
138 }
139 }
140 }
Leosac::Test::TEST
TEST(TestVisitor, test_class_hierarchy)
Definition: Visitor.cpp:132
Leosac::Test::DummyVisitable
Definition: Visitor.cpp:32
Leosac::Test::DummyVisitor
Definition: Visitor.cpp:38
Leosac::Test::DummyVisitor2::visit
void visit(const DummyVisitable &) override
Definition: Visitor.cpp:117
Leosac::Test::DummyVisitor2::visited_
bool visited_
Definition: Visitor.cpp:122
Leosac::Test::DummyVisitor::visit
void visit(DummyVisitable &) override
Definition: Visitor.cpp:44
Leosac::Test::DummyVisitor::visited_non_const_
bool visited_non_const_
Definition: Visitor.cpp:54
Leosac::Test::DummyConstOnlyVisitor::visited_const_
bool visited_const_
Definition: Visitor.cpp:88
Leosac::Test::DummyConstOnlyVisitor
Definition: Visitor.cpp:76
Leosac::Tools::Visitor
A Visitor object.
Definition: Visitor.hpp:64
Leosac::Test::DummyVisitor::DummyVisitor
DummyVisitor()
Definition: Visitor.cpp:40
Leosac::Test::DummyConstOnlyVisitor::visit
void visit(const DummyVisitable &) override
Definition: Visitor.cpp:83
Leosac::Tools::IVisitable::accept
virtual void accept(::Leosac::Tools::BaseVisitor &)=0
Accept a visitor that may mutate this.
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Test::DummyVisitor::visited_const_
bool visited_const_
Definition: Visitor.cpp:55
Leosac::Test::DummyVisitor2
Visitor2 that is ready to visit object of type DummyVisitable, but not of type DummyChildVisitable.
Definition: Visitor.cpp:113
MAKE_VISITABLE_FALLBACK
#define MAKE_VISITABLE_FALLBACK(PARENT_CLASS)
Similar to the MAKE_VISITABLE() macro, with 1 major difference.
Definition: IVisitable.hpp:104
Leosac::Test::DummyChildVisitable
Definition: Visitor.cpp:125
Visitor.hpp
Leosac::Tools::IVisitable
Base class to make an object visitable.
Definition: IVisitable.hpp:43
MAKE_VISITABLE
#define MAKE_VISITABLE()
Provide the object calling this macro in its definition with an accept() method that will accept Base...
Definition: IVisitable.hpp:85
IVisitable.hpp
Leosac::Tools
Definition: DatabaseLogSink.hpp:27
Leosac::Test::DummyVisitor::visit
void visit(const DummyVisitable &) override
Definition: Visitor.cpp:49
Leosac::Test::DummyConstOnlyVisitor::DummyConstOnlyVisitor
DummyConstOnlyVisitor()
Definition: Visitor.cpp:78