Giter VIP home page Giter VIP logo

Comments (3)

mvandervoord avatar mvandervoord commented on June 18, 2024

A trick is similar to how we tell people to use STATIC:

#ifdef TEST
#define STATIC
#else
#define STATIC static
#endif

This can be used to give test access to static variables, like so:

STATIC int privateInteger = 0;

Your case is similar, except we're going to basically move the variable out of the function for testing only:

#include <stdio.h>
#include "ProductionCode.h"

#ifdef TEST
int x = 0;
#endif

int foo()
{
    #ifndef TEST
    static int x = 0;
    #endif
    x++;
    printf("x=%u\n", x);
    return x;
}

You now have access to directly effect x:

#include "ProductionCode.h"
#include "unity.h"

extern int x;

void setUp(void)
{
   x = 0;
}

void tearDown(void)
{
}

void test_foo_shouldEqualOne_A(void)
{
    TEST_ASSERT_EQUAL(1, foo());
}

void test_foo_shouldEqualOne_B(void)
{
    TEST_ASSERT_EQUAL(1, foo());
}

from unity.

AgainPsychoX avatar AgainPsychoX commented on June 18, 2024

Thanks!

I found similar way: exposing pointer to the variable to global variable (valid after function runs first).

test/TestProductionCode.c

#include "ProductionCode.h"
#include "unity.h"

extern int* foo_static_x;

void setUp(void)
{
    if (foo_static_x != NULL) 
    {
        *foo_static_x = 0;
    }
}

void tearDown(void)
{
}

void test_foo_shouldEqualOne_A(void)
{
    TEST_ASSERT_EQUAL(1, foo());
}

void test_foo_shouldEqualOne_B(void)
{
    TEST_ASSERT_EQUAL(1, foo());
}

src/ProductionCode.c

#include <stdio.h>
#include "ProductionCode.h"

#ifdef UNIT_TESTS
int* foo_static_x = NULL;
#endif

int foo(void)
{
    static int x = 0;
    #ifdef UNIT_TESTS
    foo_static_x = &x;
    #endif
    x++;
    printf("x=%u\n", x);
    return x;
}

It works:

./test1.out
x=1
test/TestProductionCode.c:19:test_foo_shouldEqualOne_A:PASS
x=1
test/TestProductionCode.c:24:test_foo_shouldEqualOne_B:PASS

Could you share your opinion @mvandervoord ?

I guess there is a small disadvantage of this approach: not being able to set the variable to custom value for the very first test - but as for resetting it's perfectly fine. Advantage is I can have multiple functions with multiple static variables inside of the same name.

from unity.

mvandervoord avatar mvandervoord commented on June 18, 2024

That option makes sense to me.

Also, with your version, your first test could actually choose to verify that the pointer isn't null AND that it's set to exactly 1 after calling it the function for the first time.

from unity.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.