Im relatively new to cpp. Im currently trying to write new testcases for a class foo,
Class foo2 {
Public:
Getsomevariable();
// do something
}
Class foo {
Private:
Foo2 foo2_obj;
}
Class testfoo {
protected:
Foo foo_obj;
}
TEST_F(testfoo, getsomevariabletest)
{ // ? How to access getsomevariable method of foo2 from foo_obj
Temp = foo_obj?
ASSERT_EQ(100,Temp);
}
Now Im testing using object of foo, but how should I be able to access foo2.Getsomevariable() from foo object?
Please help
I created a main to demonstrate the purposes of this, but tweak it for your own intended purpose.
class foo2 {
public:
std::string Getsomevariable() {
return "Hey!";
}
};
class foo {
public:
foo2 foo2_obj;
};
int main() {
foo f;
std::cout << f.foo2_obj.Getsomevariable();
}
Related
I am trying to use EXPECT_CALL to set the return value of the function that resides in a different class than what the mock class inherits from.
In the following snippet, I was wondering if there's a way to use EXPECT_CALL on someFunction() that's a public function of Bar inside a unit test through bar object of UnitTest class but it seems to error out.
One thing I know is I would need to use MOCK_METHOD for someFunction to override it to expect EXPECT_CALL but not sure how could I do inside the Mock class?
// Bar.hpp
class Bar
{
public:
virtual bool someFunction();
};
// Foo.hpp
namespace sw::foo_state
{
class Foo
{
Bar _bar;
public:
Foo(Bar&& bar) : _bar(std::move(bar)) {}
void TestFunction()
{
_bar.someFunction();
}
};
};
// MockClass.hpp
namespace sw
{
class Mock : public foo_state::Foo
{
Mock(Bar&& bar) : Foo(std::move(bar)) {}
};
};
// UnitTest.cpp
using namespace sw::foo_state;
class UnitTest
{
public:
Bar bar;
auto ptr = std::make_unique<Mock>(std::move(bar));
};
TEST_F(UnitTest, Test)
{
EXPECT_CALL(bar, someFunction()).WillOnce(Return(true)); // error: ‘class sw::foo_state::Bar’ has no member named ‘gmock_someFunction’; did you mean ‘someFunction’?
ptr->TestFunction();
}
EDIT:
You only need to change your class Foo constructor to accept a pointer or a reference to take advantage of class Bar polymorphism:
class Bar
{
public:
virtual bool someFunction()
{
return true;
}
};
class Foo
{
public:
Foo(Bar& bar): _bar{ bar} {}
void TestFunction()
{
std::cout << _bar.someFunction() << std::endl;
}
private:
Bar& _bar; // can also be std::unique_ptr<Bar> if you prefer to own the object
};
struct MockBar : public Bar
{
MOCK_METHOD0(someFunction, bool());
};
using testing::Return;
TEST(xxx, yyy)
{
MockBar mBar;
// ON_CALL(mBar, someFunction()).WillByDefault(Return(false)); // if you don't want to force expectation
EXPECT_CALL(mBar, someFunction()).WillOnce(Return(false));
Foo foo(mBar);
foo.TestFunction();
}
I've been trying to reference another class inside of a class in C++ and I have no idea how.
I have created a small program to demonstrate the issue
#include <iostream>
class foo{
public:
int variable1 = 012;
};
class bar{
public:
int getFooVariable(){
return variable1; // How would I get bar to refrence foo in foobar?
}
};
class foobar{
public:
bar p1;
foo p2;
};
int main(){
foobar fb;
std::cout << fb.p1.getFooVariable(); << std::endl;
return 0;
}
How would I solve this problem?
You're looking for dependency injection:
class foo
{
public:
int variable1 = 012;
};
class bar
{
foo _foo;
public:
bar(foo& fooInjected) :
_foo(fooInjected)
{}
int getFooVariable() { return _foo.variable1; }
};
There is an idea of inversion of control: bar has no control over the creation of foo instance, which is created outside. It's a common way to inject for instance a service. It's also a very important way to loosely couple classes working together and to mock and to test them. But here foo is a concrete class, instead and ideally you're refering only an interface (c#) or an abstract class (c++) in bar. Which concrete class is behind is out of control of bar. I recommend M. Seeman's book Dependency Injection in .NET to understand this completely.
or inheritance:
class foo
{
public:
int variable1 = 012;
};
class bar: public foo
{
public:
int getFooVariable() { return variable1; }
};
Here, bar builds up heavily on foo. bar is a "richer version" of foo if this makes sense. This is what to choose if there is a high cohesion inside bar.
Invoking a variable through another's instance function is a most horrid idea, that goes against class cohesion:
int getFooVariable(foo* foo) { return foo->variable1; }
Sorry to change the formatting, here's a solution:
#include iostream
class foo
{
public:
int variable1 = 012;
};
class bar
{
public:
int getFooVariable(foo* foo)
{
return foo->variable1; // How would I get bar to refrence foo in foobar?
}
};
class foobar
{
public:
bar p1;
foo p2;
};
int main()
{
foobar fb;
std::cout << fb.p1.getFooVariable(&(fb.p2));
<< std::endl;
return 0;
}
You can add a parameter to the getFooVariable() function to take a foo* reference. The reference can be found when you call the function.
I have a situation that I am attempting to write some c++ code to implement and I think I need to use templates but I am unsure. I don't think I understand templates well enough and just want to know if I am on the right track. I have never implemented a class that requires templates before.
I have a base class that needs to instantiate a new class and store it on a list for future access. The class that needs to be instantiated is derived from a base class that has one virtual method. After the class is instantiated, this method is called and the new instance is stored on a list. Another method is provided to get the instantiated object.
Say the virtual class is named foo:
class foo
{
public:
virtual void process() = 0;
}
the user would create a class bar:
class bar : public foo
{
public:
void process() {};
}
The class that I think needs to be a template class:
class Filter<Foo* T>
{
public:
// use value junk to derive foo
// and put the new instance on a std::list
add(Junk* junk);
T get();
private:
std::list<T> mFooList;
}
void Filter::add(Junk* junk)
{
T* foo = new T();
foo.p(junk);
mFooList.push_back(foo);
}
T* Filter::get()
{
if(!mFoolist.empty())
{
T* res = mFooList.back();
mFooList.pop_back();
return res;
}
return nullptr;
}
You don't need to use a templatevin this situation. In c++ a derived class can be assigned to a pointer of the base class. You can just use foo pointers.
class foo
{
public:
virtual void process();
};
class bar1: public foo
{
public:
void process();
};
class bar2 : public foo
{
public:
void process();
};
class filter
{
private:
std::list<foo*> _foos;
public:
foo* get();
void add(foo* f);
};
void filter::add(foo* f)
{
_foos.push_back(f);
}
foo* filter::get()
{
if(!_foos.empty())
{
return _foos.pop_back();
}
return nullptr;
}
You can then just add and get foos and bars
filter fil;
fil.add(new foo());
fill.add(new bar1());
fill.add(new bar2());
foo f = fill.get();
while(f != nullptr)
{
f->process();
delete f;
f = fil.get();
}
I'm attempting to do method chaining, however, instead of using the Methods in "Foo" I want to a constructor of a class (which is inherited from the base class):
class Bar {
public:
Bar() {
std::cout << "This is bar";
}
};
class Foo : public Bar {
public:
Foo() {
cout << "This is foo";
}
};
So my main would look like the following:
Foo f = Foo().Bar();
Why is this not possible in C++/C++11? Also, is there a way in which I can integrate this standard, or would I have to create an method in "Foo" which calls the constructor to "Bar"?
Edit:
class Bar {
public:
Bar() {
}
Bar& Options() {
cout << "sf";
return *this;
}
};
class Foo : public Bar {
public:
Foo() {
}
};
And then in main:
Foo F = Foo().Options();
Your updated question is illegal because Bar::Options() returns a reference to a Bar and you don't provide a way to convert a Bar to a Foo object.
class Foo
{
public:
void action();
};
class Bar : public Foo
{
public:
void action();
};
void Foo::action ()
{
cout << "parent\n";
};
void Bar::action ()
{
cout << "child\n";
};
int main()
{
Foo* foo = new Bar ();
foo->action(); // returns "parent" - "child" expected
return 1;
}
I'm sorry for a probably trivial question, but I'm new to C++...
The 'foo' pointer must point to an instance of Foo class, since it can be any of Foo's childs e.g. Bar, Bar1, Bar2, Bar3 etc.
And at the same time 'foo->action()' should run an overridden function of the child.
Tell me please, how do I correct the code to reach my goals...
Thanks!
Unlike other languages, like Java, in C++ base class have to specifically mark the methods it allows overriding of by using keyword virtual
The member function needs to be declared virtual (unlike Java for example, where all methods are implicitly virtual):
class Foo
{
public:
virtual void action();
};
Use keyword virtual for parent-class function. And also, your parent class should have virtual destructor.
class Foo
{
public:
virtual void action();
};
class Bar : public Foo
{
public:
void action();
};
void Foo::action ()
{
cout << "parent\n";
};
void Bar::action ()
{
cout << "child\n";
};
int main()
{
Foo* foo = new Bar ();
foo->action(); // returns "parent" - "child" expected
return 1;
}