unity_output_Spy.c 1.1 KB
Newer Older
1
/* ==========================================
J
jsalling 已提交
2 3 4 5
 *  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]
 * ========================================== */
6

7
#include "unity.h"
8
#include "unity_output_Spy.h"
9

10
#include <stdio.h>
11
#include <string.h>
12
#include <stdlib.h>
13 14 15 16 17 18 19 20

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

void UnityOutputCharSpy_Create(int s)
{
J
jsalling 已提交
21
    size = (s > 0) ? s : 0;
22 23
    count = 0;
    spy_enable = 0;
24
    buffer = malloc((size_t)size);
25
    TEST_ASSERT_NOT_NULL_MESSAGE(buffer, "Internal malloc failed in Spy Create():" __FILE__);
26
    memset(buffer, 0, (size_t)size);
27 28
}

29
void UnityOutputCharSpy_Destroy(void)
30 31
{
    size = 0;
32
    free(buffer);
33 34
}

M
Mark VanderVoord 已提交
35
void UnityOutputCharSpy_OutputChar(int c)
36 37 38 39
{
    if (spy_enable)
    {
        if (count < (size-1))
40
            buffer[count++] = (char)c;
41 42 43 44 45 46 47
    }
    else
    {
        putchar(c);
    }
}

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

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