unity_output_Spy.c 1.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
//- Copyright (c) 2010 James Grenning and Contributed to Unity Project
/* ==========================================
    Unity Project - A Test Framework for C
    Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
    [Released under MIT License. Please refer to license.txt for details]
========================================== */


#include "unity_output_Spy.h"
10 11
#include "unity_fixture.h"

12
#include <stdio.h>
13 14 15 16 17 18 19 20 21 22 23 24
#include <string.h>

static int size;
static int count;
static char* buffer;
static int spy_enable;

void UnityOutputCharSpy_Create(int s)
{
    size = s;
    count = 0;
    spy_enable = 0;
25
    buffer = malloc((size_t)size);
26
    TEST_ASSERT_NOT_NULL_MESSAGE(buffer, "Internal malloc failed in Spy Create():" __FILE__);
27
    memset(buffer, 0, (size_t)size);
28 29
}

30
void UnityOutputCharSpy_Destroy(void)
31 32
{
    size = 0;
33
    free(buffer);
34 35 36 37 38 39 40
}

int UnityOutputCharSpy_OutputChar(int c)
{
    if (spy_enable)
    {
        if (count < (size-1))
41
            buffer[count++] = (char)c;
42 43 44 45 46 47 48 49
    }
    else
    {
        putchar(c);
    }
    return c;
}

50
const char * UnityOutputCharSpy_Get(void)
51 52 53 54 55 56 57 58
{
    return buffer;
}

void UnityOutputCharSpy_Enable(int enable)
{
    spy_enable = enable;
}