0

I'm converting Google tests to Visual Studio Native unit tests. I'm running into an issue where the Visual Studio unit test isn't seeing variables of it's friend class. Any idea how to fix this?

In my VSUT-Br.cpp class (ported from Google unit test):

#include "DocFriendClass.h
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace VSUTBr
{       
    TEST_CLASS(BrTest)
    {
    public:

    protected:
        virtual ~BrTest();
        virtual void SetUp();
        virtual void TearDown();
        std::unique_ptr<DocFriendClass> TestDoc;
        ...

        TEST_METHOD_INITIALIZE(File_Initialize)
        {
           pP = CreateP();
           TestDoc = std::make_unique<DocFriendClass>(OurString("Test"));
           TestDoc->m_Parser.setParser(pP); //it can't see m_Parser either
            ...
            OurString sColor(kszColor);//it's not seeing kszColor in friend
        }
 };
}

In my DocFriendClass.h:

...
private:
      Parser m_Parser;
    ...
    #ifdef UNIT_TEST
        friend class BrTest;
    #endif

Then I went to my VSUT-Br project and right clicked and selected properties, and edited the C/C++ Preprocessor settings; Preprocessor Definitions now include UNIT_TEST=1. I clean built and it still couldn't find the friend variable. I exited VS and went back in, and clean built and it's not finding the friend variable. It worked having that as a friend with the old Google test. Any ideas?

I looked at unit test friend but it's not the same problem. I found elsewhere and they were saying I can't have a friend to my VS Unit Test, but I have a feeling that's internet garbage (can't find link now).

Update: I added some info on a variable, m_Parser in DocFriendClass that it's not seeing. Error info:

C2248 'DocFriendClass::m_Parser': cannot access private member declared in class 'DocFriendClass'.  File VSUT-Br.

It should be able to access this. The Google test could as a friend.

Community
  • 1
  • 1
Michele
  • 3,617
  • 12
  • 47
  • 81
  • 1
    How do you account for namespace `VSUTBr` when you declare your friend class? –  Jun 09 '16 at 15:31
  • 1
    I think we need to see the declaration of `kszColor` and maybe other things in `DocFriendClass.h`, but yes, you should not be *infecting* your code with details of test code. – quamrana Jun 09 '16 at 15:46
  • "Visual Studio unit test isn't seeing variables" When reporting a problem, it is super important to report what you actually observe, rather than your conclusions. If there are error messages, copy and paste them. "Unit test isn't seeing variables" doesn't contain the same information. – n. m. could be an AI Jun 09 '16 at 15:56
  • Sorry about that! I have to make sure I don't put the exact code for licensing purposes, and I didn't give enough of what you need to help, concentrating on that. I added m_Parser for help. I figured the other one, kszColor, out. It was a class variable I missed. – Michele Jun 09 '16 at 19:07
  • @Arkadiy - My friend class doesn't know about the namespace of the unit test class, VSUTBr. Yes, quamrana, that would be infecting the friend class. – Michele Jun 09 '16 at 19:17
  • 1
    Well, `friend class BrTest` and `friend class VSUTBr::BrTest` are two very different things :( –  Jun 09 '16 at 19:22
  • @Arkadiy - I tried changing it to friend class VSUTBr::BrTest; in my friend class, and it didn't help. I have a new error C2653 'VSUTBr': is not a class or namespace name in the friend class. So I think it was happier with the original friend class #ifdef, although it wasn't seeing the private variables of the friend class. – Michele Jun 09 '16 at 20:33
  • Please have a look at this: http://stackoverflow.com/questions/3843711/friend-classes-across-different-namespaces-is-that-possible –  Jun 09 '16 at 21:11
  • @Arkadiy - Are you referring to where it uses friend class tools::sysInput::CGeometryManager3D;? Like my comment says above, I am trying VSUTBr::BrTest, but it gives the new error on top of the other errors. Is there something else you are referring to? Maybe the syntax is different with native Visual Studio unit tests like my TEST_CLASS(BRTest)? – Michele Jun 10 '16 at 12:42
  • I tried looking into class declare like the link above and also http://stackoverflow.com/questions/19001700/how-to-forward-declare-a-class-which-is-in-a-namespace discuss. In DocFriendClass.h, I added class UTBr; right below the #includes. Now the friend class UTBr::BrTest; line is getting error C2027 use of undefined type 'UTBr'. – Michele Jun 10 '16 at 13:01
  • What is UTBR? You had `namespace VSUTBr` and the test class was, I assumed, `BrTest`. You need to forward-declare `Brtest` inside `VSUTBr` namespace as described in my link. –  Jun 10 '16 at 13:07
  • Sorry. I meant VSUTBr, the namespace name. In addition to a class declare of VSUTRBr in DocFriendClass, I also have added a class declare of BrTest, the TEST_CLASS name inside the VSUTBr namespace. It's giving error C2027 use of undefined type VSUTBr, and pointing to the DocFriendClass declare of friend class VSUTBr::BrTest. – Michele Jun 10 '16 at 13:16
  • If you change something and you get an error you don't understand, post the code and the error in a question (perhaps in a separate question) Don't describe your changes in comments, it doesn't work. – n. m. could be an AI Jun 11 '16 at 06:35

1 Answers1

0

We wound up working around the issue by making the private variables we were accessing public. Then it didn't need to be a friend anymore. (they just shared nicely)

Michele
  • 3,617
  • 12
  • 47
  • 81