CMock's behaviour for IgnoreAndReturn

68 views Asked by At

I am currently trying to understand the intricacies of CMock's IgnoreAndReturn feature.

This is the function under test


    void USBDriver_Exec(void)
    {
        uint8_t Val;
    
        // When the tx fifo is empty and we have tx data in the buffer, move data
        while (serial_writable(&USBDriver_serial) && (!Buffer_IsEmpty(&tx)) && (STATUS_OK == Buffer_Get(&tx, &Val)))
        {
            serial_putc(&USBDriver_serial, Val);
        }
    
        // When the rx fifo has data and the rx buffer isn't full, move data
        while (serial_readable(&USBDriver_serial) && !Buffer_IsFull(&rx))
        {
            Val = serial_getc(&USBDriver_serial);
            Buffer_Put(&rx, Val);
        }
    }

Now this is a sample of a test i am trying to write


    void test_USBDriver_Exec_should_ReadBytes_when_BytesInBufferedAndReadable(void)
    {
        int i;
        uint8_t vals[5] = {'Q', 'R', 'S', 'T', 'U'};
        serial_writable_IgnoreAndReturn(FALSE);
        Buffer_IsEmpty_IgnoreAndReturn(TRUE);
        Buffer_IsFull_IgnoreAndReturn(FALSE);
        serial_readable_IgnoreAndReturn(TRUE);
        for (i = 0; i < DIMENSION_OF(vals); i++)
        {
            serial_getc_ExpectAndReturn(&USBDriver_serial, vals[i]);
            Buffer_Put_ExpectAndReturn(&rx, vals[i], STATUS_OK);
        }
        serial_readable_IgnoreAndReturn(FALSE);
        USBDriver_Exec();
    }

whereby when i run the test, i get the test failure

[TestUSBDriver.c] Test: test_USBDriver_Exec_should_ReadBytes_when_BytesInBufferedAndReadable. At line (143): "Function Buffer_Put. Called fewer times than expected."

Now, if i put serial_readable_IgnoreAndReturn() into the loop


    void test_USBDriver_Exec_should_ReadBytes_when_BytesInBufferedAndReadable(void)
    {
        int i;
        uint8_t vals[5] = {'Q', 'R', 'S', 'T', 'U'};
        serial_writable_IgnoreAndReturn(FALSE);
        Buffer_IsEmpty_IgnoreAndReturn(TRUE);
        Buffer_IsFull_IgnoreAndReturn(FALSE);
    
        for (i = 0; i < DIMENSION_OF(vals); i++)
        {
            serial_readable_IgnoreAndReturn(TRUE); //Line of interest
            serial_getc_ExpectAndReturn(&USBDriver_serial, vals[i]);
            Buffer_Put_ExpectAndReturn(&rx, vals[i], STATUS_OK);
        }
        serial_readable_IgnoreAndReturn(FALSE);
        USBDriver_Exec();
    }

The test passes! I am not exactly sure what to make of the error log and the docs are not helping as it makes no sense under the ignore section. It is supposed to ignore any calls to func_IgnoreAndReturn and will return whatever i set. But it seems like i have to explicitly call it every loop if not a pedantic(?) error will throw

The version i am using is as follows:

Ceedling: 0.30.0 Unity: 2.5.1 CMock: 2.5.2 CException: 1.3.2

0

There are 0 answers