mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
Merge commit '6b9b57be226a505a9c9cdd9ed029f22495ce04ec' as 'tests/unity'
This commit is contained in:
70
tests/unity/examples/example_2/makefile
Normal file
70
tests/unity/examples/example_2/makefile
Normal file
@ -0,0 +1,70 @@
|
||||
# ==========================================
|
||||
# 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]
|
||||
# ==========================================
|
||||
|
||||
#We try to detect the OS we are running on, and adjust commands as needed
|
||||
ifeq ($(OS),Windows_NT)
|
||||
ifeq ($(shell uname -s),) # not in a bash-like shell
|
||||
CLEANUP = del /F /Q
|
||||
MKDIR = mkdir
|
||||
else # in a bash-like shell, like msys
|
||||
CLEANUP = rm -f
|
||||
MKDIR = mkdir -p
|
||||
endif
|
||||
TARGET_EXTENSION=.exe
|
||||
else
|
||||
CLEANUP = rm -f
|
||||
MKDIR = mkdir -p
|
||||
TARGET_EXTENSION=.out
|
||||
endif
|
||||
|
||||
C_COMPILER=gcc
|
||||
ifeq ($(shell uname -s), Darwin)
|
||||
C_COMPILER=clang
|
||||
endif
|
||||
|
||||
UNITY_ROOT=../..
|
||||
|
||||
CFLAGS=-std=c99
|
||||
CFLAGS += -Wall
|
||||
CFLAGS += -Wextra
|
||||
CFLAGS += -Wpointer-arith
|
||||
CFLAGS += -Wcast-align
|
||||
CFLAGS += -Wwrite-strings
|
||||
CFLAGS += -Wswitch-default
|
||||
CFLAGS += -Wunreachable-code
|
||||
CFLAGS += -Winit-self
|
||||
CFLAGS += -Wmissing-field-initializers
|
||||
CFLAGS += -Wno-unknown-pragmas
|
||||
CFLAGS += -Wstrict-prototypes
|
||||
CFLAGS += -Wundef
|
||||
CFLAGS += -Wold-style-definition
|
||||
|
||||
TARGET_BASE1=all_tests
|
||||
TARGET1 = $(TARGET_BASE1)$(TARGET_EXTENSION)
|
||||
SRC_FILES1=\
|
||||
$(UNITY_ROOT)/src/unity.c \
|
||||
$(UNITY_ROOT)/extras/fixture/src/unity_fixture.c \
|
||||
src/ProductionCode.c \
|
||||
src/ProductionCode2.c \
|
||||
test/TestProductionCode.c \
|
||||
test/TestProductionCode2.c \
|
||||
test/test_runners/TestProductionCode_Runner.c \
|
||||
test/test_runners/TestProductionCode2_Runner.c \
|
||||
test/test_runners/all_tests.c
|
||||
INC_DIRS=-Isrc -I$(UNITY_ROOT)/src -I$(UNITY_ROOT)/extras/fixture/src
|
||||
SYMBOLS=
|
||||
|
||||
all: clean default
|
||||
|
||||
default:
|
||||
$(C_COMPILER) $(CFLAGS) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES1) -o $(TARGET1)
|
||||
- ./$(TARGET1) -v
|
||||
|
||||
clean:
|
||||
$(CLEANUP) $(TARGET1)
|
||||
|
||||
ci: CFLAGS += -Werror
|
||||
ci: default
|
5
tests/unity/examples/example_2/readme.txt
Normal file
5
tests/unity/examples/example_2/readme.txt
Normal file
@ -0,0 +1,5 @@
|
||||
Example 2
|
||||
=========
|
||||
|
||||
Same as the first example, but now using Unity's test fixture to group tests
|
||||
together. Using the test fixture also makes writing test runners much easier.
|
24
tests/unity/examples/example_2/src/ProductionCode.c
Normal file
24
tests/unity/examples/example_2/src/ProductionCode.c
Normal file
@ -0,0 +1,24 @@
|
||||
|
||||
#include "ProductionCode.h"
|
||||
|
||||
int Counter = 0;
|
||||
int NumbersToFind[9] = { 0, 34, 55, 66, 32, 11, 1, 77, 888 }; //some obnoxious array to search that is 1-based indexing instead of 0.
|
||||
|
||||
// This function is supposed to search through NumbersToFind and find a particular number.
|
||||
// If it finds it, the index is returned. Otherwise 0 is returned which sorta makes sense since
|
||||
// NumbersToFind is indexed from 1. Unfortunately it's broken
|
||||
// (and should therefore be caught by our tests)
|
||||
int FindFunction_WhichIsBroken(int NumberToFind)
|
||||
{
|
||||
int i = 0;
|
||||
while (i <= 8) //Notice I should have been in braces
|
||||
i++;
|
||||
if (NumbersToFind[i] == NumberToFind) //Yikes! I'm getting run after the loop finishes instead of during it!
|
||||
return i;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int FunctionWhichReturnsLocalVariable(void)
|
||||
{
|
||||
return Counter;
|
||||
}
|
3
tests/unity/examples/example_2/src/ProductionCode.h
Normal file
3
tests/unity/examples/example_2/src/ProductionCode.h
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
int FindFunction_WhichIsBroken(int NumberToFind);
|
||||
int FunctionWhichReturnsLocalVariable(void);
|
11
tests/unity/examples/example_2/src/ProductionCode2.c
Normal file
11
tests/unity/examples/example_2/src/ProductionCode2.c
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
#include "ProductionCode2.h"
|
||||
|
||||
char* ThisFunctionHasNotBeenTested(int Poor, char* LittleFunction)
|
||||
{
|
||||
(void)Poor;
|
||||
(void)LittleFunction;
|
||||
//Since There Are No Tests Yet, This Function Could Be Empty For All We Know.
|
||||
// Which isn't terribly useful... but at least we put in a TEST_IGNORE so we won't forget
|
||||
return (char*)0;
|
||||
}
|
2
tests/unity/examples/example_2/src/ProductionCode2.h
Normal file
2
tests/unity/examples/example_2/src/ProductionCode2.h
Normal file
@ -0,0 +1,2 @@
|
||||
|
||||
char* ThisFunctionHasNotBeenTested(int Poor, char* LittleFunction);
|
64
tests/unity/examples/example_2/test/TestProductionCode.c
Normal file
64
tests/unity/examples/example_2/test/TestProductionCode.c
Normal file
@ -0,0 +1,64 @@
|
||||
#include "ProductionCode.h"
|
||||
#include "unity.h"
|
||||
#include "unity_fixture.h"
|
||||
|
||||
TEST_GROUP(ProductionCode);
|
||||
|
||||
//sometimes you may want to get at local data in a module.
|
||||
//for example: If you plan to pass by reference, this could be useful
|
||||
//however, it should often be avoided
|
||||
extern int Counter;
|
||||
|
||||
TEST_SETUP(ProductionCode)
|
||||
{
|
||||
//This is run before EACH TEST
|
||||
Counter = 0x5a5a;
|
||||
}
|
||||
|
||||
TEST_TEAR_DOWN(ProductionCode)
|
||||
{
|
||||
}
|
||||
|
||||
TEST(ProductionCode, FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode)
|
||||
{
|
||||
//All of these should pass
|
||||
TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(78));
|
||||
TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(1));
|
||||
TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(33));
|
||||
TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(999));
|
||||
TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(-1));
|
||||
}
|
||||
|
||||
TEST(ProductionCode, FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken)
|
||||
{
|
||||
// You should see this line fail in your test summary
|
||||
TEST_ASSERT_EQUAL(1, FindFunction_WhichIsBroken(34));
|
||||
|
||||
// Notice the rest of these didn't get a chance to run because the line above failed.
|
||||
// Unit tests abort each test function on the first sign of trouble.
|
||||
// Then NEXT test function runs as normal.
|
||||
TEST_ASSERT_EQUAL(8, FindFunction_WhichIsBroken(8888));
|
||||
}
|
||||
|
||||
TEST(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue)
|
||||
{
|
||||
//This should be true because setUp set this up for us before this test
|
||||
TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable());
|
||||
|
||||
//This should be true because we can still change our answer
|
||||
Counter = 0x1234;
|
||||
TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable());
|
||||
}
|
||||
|
||||
TEST(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain)
|
||||
{
|
||||
//This should be true again because setup was rerun before this test (and after we changed it to 0x1234)
|
||||
TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable());
|
||||
}
|
||||
|
||||
TEST(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed)
|
||||
{
|
||||
//Sometimes you get the test wrong. When that happens, you get a failure too... and a quick look should tell
|
||||
// you what actually happened...which in this case was a failure to setup the initial condition.
|
||||
TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable());
|
||||
}
|
33
tests/unity/examples/example_2/test/TestProductionCode2.c
Normal file
33
tests/unity/examples/example_2/test/TestProductionCode2.c
Normal file
@ -0,0 +1,33 @@
|
||||
#include "ProductionCode2.h"
|
||||
#include "unity.h"
|
||||
#include "unity_fixture.h"
|
||||
|
||||
TEST_GROUP(ProductionCode2);
|
||||
|
||||
/* These should be ignored because they are commented out in various ways:
|
||||
#include "whatever.h"
|
||||
*/
|
||||
//#include "somethingelse.h"
|
||||
|
||||
TEST_SETUP(ProductionCode2)
|
||||
{
|
||||
}
|
||||
|
||||
TEST_TEAR_DOWN(ProductionCode2)
|
||||
{
|
||||
}
|
||||
|
||||
TEST(ProductionCode2, IgnoredTest)
|
||||
{
|
||||
TEST_IGNORE_MESSAGE("This Test Was Ignored On Purpose");
|
||||
}
|
||||
|
||||
TEST(ProductionCode2, AnotherIgnoredTest)
|
||||
{
|
||||
TEST_IGNORE_MESSAGE("These Can Be Useful For Leaving Yourself Notes On What You Need To Do Yet");
|
||||
}
|
||||
|
||||
TEST(ProductionCode2, ThisFunctionHasNotBeenTested_NeedsToBeImplemented)
|
||||
{
|
||||
TEST_IGNORE(); //Like This
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
#include "unity.h"
|
||||
#include "unity_fixture.h"
|
||||
|
||||
TEST_GROUP_RUNNER(ProductionCode2)
|
||||
{
|
||||
RUN_TEST_CASE(ProductionCode2, IgnoredTest);
|
||||
RUN_TEST_CASE(ProductionCode2, AnotherIgnoredTest);
|
||||
RUN_TEST_CASE(ProductionCode2, ThisFunctionHasNotBeenTested_NeedsToBeImplemented);
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
#include "unity.h"
|
||||
#include "unity_fixture.h"
|
||||
|
||||
TEST_GROUP_RUNNER(ProductionCode)
|
||||
{
|
||||
RUN_TEST_CASE(ProductionCode, FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode);
|
||||
RUN_TEST_CASE(ProductionCode, FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken);
|
||||
RUN_TEST_CASE(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue);
|
||||
RUN_TEST_CASE(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain);
|
||||
RUN_TEST_CASE(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed);
|
||||
}
|
12
tests/unity/examples/example_2/test/test_runners/all_tests.c
Normal file
12
tests/unity/examples/example_2/test/test_runners/all_tests.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include "unity_fixture.h"
|
||||
|
||||
static void RunAllTests(void)
|
||||
{
|
||||
RUN_TEST_GROUP(ProductionCode);
|
||||
RUN_TEST_GROUP(ProductionCode2);
|
||||
}
|
||||
|
||||
int main(int argc, const char * argv[])
|
||||
{
|
||||
return UnityMain(argc, argv, RunAllTests);
|
||||
}
|
Reference in New Issue
Block a user