1

I want to know how I can test the access to a private function. Example:

class Random{
    public:
        ...
    private:
        Serial();
        OR
        random_function();

In this case it is impossible to instantiate an object of the class random because Serial is private.

If there is just the function random_function() and wee have an object of Serial we can not access the function like object.random_function().

But if you consider a case in which a group is working on a project and there are methods who should obvious be private...

How is it possible to regard that when writing test cases (in C++) ?

EDIT: What I was looking for is a test scenario where I can test the access: The test failure if the function is changed to have public access.

goulashsoup
  • 2,639
  • 2
  • 34
  • 60
  • 4
    I don't know about you but if I try to use a private function I get a compiler error. Doesn't that happen to you? – NathanOliver Mar 03 '16 at 14:35
  • 3
    @BoBTFish OP: "No I do not want to know how I can test a private function." -- So no, I don't think that's what they want. I think they want to test that the access is private (e.g., want a test failure if the function is changed to have `public` access). – James Adkison Mar 03 '16 at 14:37
  • 1
    Could you edit your question to clarify a bit? You have all of us guessing what it is you want to do – Nacho Mar 03 '16 at 14:40
  • 2
    why it should be impossible to instantiate an object of that class? Is `Serial()` supposed to be the constructor (which would have to have the same name as the class) or am I missing something? – 463035818_is_not_an_ai Mar 03 '16 at 14:40
  • 1
    Perhaps if we had a code snippet that compiles we could help? – wally Mar 03 '16 at 14:46
  • You can test the method if a function to return a pointer is static (singleton), but you would still need other public interface functions, unless you test your private method in the private constructor. (still needs the public static creation function) See [here](http://stackoverflow.com/a/2062578/1460794). – wally Mar 03 '16 at 14:49
  • 1
    Its exatly what James Adkison said...! – goulashsoup Mar 03 '16 at 14:53
  • If it is exactly what James Adkison said, then you would probably have your answer from NathanOliver. :) – wally Mar 03 '16 at 14:55
  • So you actually want a compile time / preprocessing check if specific methods (e.g. defined in a list in a txt file) are private? As you would have to maintain such a list everytime you add such a class/method, why not just set the methods to private in the first case? – Wortex17 Mar 03 '16 at 14:56
  • "The test failure if the function is changed to have public access." -- Run your compiler with code that accesses these supposedly private methods, and if it compiles, mark the test as failed. – Wortex17 Mar 03 '16 at 15:00
  • Well sure I get an compiler error if I try to use a private function and of course thats a way to find out that a private function really is private, but I thought there is a better, smarter way to do that... especially if I have a bunch of private methods... – goulashsoup Mar 03 '16 at 15:04

2 Answers2

3

Answer from here

#include <iostream>

class Random1
{
public:
    int random_function() { return 0; }
};

class Random2
{
private:
    int random_function() { return 0; }
};

// SFINAE test
template <typename T>
class has_random_function
{
    typedef char one;
    typedef long two;

    template <typename C> static one test( typeof(&C::random_function) ) ;
    template <typename C> static two test(...);    

public:
    enum { value = sizeof(test<T>(0)) == sizeof(char) };
};

int main(int argc, char *argv[])
{
    std::cout << "Random1: " << has_random_function<Random1>::value << std::endl;
    std::cout << "Random2: " << has_random_function<Random2>::value << std::endl;
    return 0;
}
Community
  • 1
  • 1
Adrian Krupa
  • 1,877
  • 1
  • 15
  • 24
2

The trick is to use SFINAE: write a pair of functions, one which defined if Random::random_function is public and one that's defined if Random::random_function is private.

Per your description, the first function causes the test to fail and the second function causes the test to pass, but you can obviously reverse that logic when testing the opposite case.

Community
  • 1
  • 1
MSalters
  • 173,980
  • 10
  • 155
  • 350