mirror of
https://github.com/meekrosoft/fff
synced 2026-01-28 02:34:28 +01:00
Moved out test code to seperate directory
This commit is contained in:
114
src/test/cmocktest.c
Normal file
114
src/test/cmocktest.c
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* cmock.c
|
||||
*
|
||||
* Created on: Dec 9, 2010
|
||||
* Author: mlong
|
||||
*/
|
||||
|
||||
#include "embedded.h"
|
||||
#include "cmock.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
FAKE_VOID_FUNC0(DISPLAY_init);
|
||||
FAKE_VOID_FUNC0(DISPLAY_clear);
|
||||
FAKE_VOID_FUNC1(DISPLAY_output_message, const char*);
|
||||
FAKE_VALUE_FUNC0(int, DISPLAY_get_line_capacity);
|
||||
FAKE_VALUE_FUNC0(int, DISPLAY_get_line_insert_index);
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Register resets
|
||||
RESET_FAKE(DISPLAY_init);
|
||||
RESET_FAKE(DISPLAY_clear);
|
||||
RESET_FAKE(DISPLAY_output_message);
|
||||
RESET_FAKE(DISPLAY_get_line_capacity);
|
||||
RESET_FAKE(DISPLAY_get_line_insert_index);
|
||||
|
||||
// non default init
|
||||
DISPLAY_get_line_capacity_return_val = 10;
|
||||
}
|
||||
|
||||
#define TEST_F(IGNORE, NAME) void NAME()
|
||||
TEST_F(GreeterTests, init_initialises_display)
|
||||
{
|
||||
UI_init();
|
||||
assert(1 == DISPLAY_init_call_count);
|
||||
}
|
||||
|
||||
TEST_F(GreeterTests, given_name_when_greet_called_outputs_name)
|
||||
{
|
||||
// given
|
||||
char name[] = "mike";
|
||||
// when
|
||||
UI_greet(name);
|
||||
// then
|
||||
assert(1 == DISPLAY_output_message_call_count);
|
||||
assert(name == DISPLAY_output_message_arg0_val);
|
||||
}
|
||||
|
||||
|
||||
TEST_F(GreeterTests, given_name_and_3_times_when_greetmultiple_called_outputs_name_3_times)
|
||||
{
|
||||
// given
|
||||
char name[] = "mike";
|
||||
// when
|
||||
UI_greet_multiple_times(name, 3);
|
||||
// then
|
||||
assert(3 == DISPLAY_output_message_call_count);
|
||||
assert(name == DISPLAY_output_message_arg0_val);
|
||||
}
|
||||
|
||||
TEST_F(GreeterTests, given_non_full_screen_will_not_reset_display)
|
||||
{
|
||||
char name[] = "mike";
|
||||
// given
|
||||
DISPLAY_get_line_capacity_return_val = 10;
|
||||
DISPLAY_get_line_insert_index_return_val = 0;
|
||||
// when
|
||||
UI_greet(name);
|
||||
// then
|
||||
assert(0 == DISPLAY_clear_call_count);
|
||||
assert(1 == DISPLAY_output_message_call_count);
|
||||
}
|
||||
|
||||
// Order assumption
|
||||
TEST_F(GreeterTests, given_full_screen_single_will_reset_display_then_output)
|
||||
{
|
||||
char name[] = "mike";
|
||||
// given
|
||||
DISPLAY_get_line_capacity_return_val = 1;
|
||||
DISPLAY_get_line_insert_index_return_val = 1;
|
||||
// when
|
||||
UI_greet(name);
|
||||
// then
|
||||
assert(1 == DISPLAY_clear_call_count);
|
||||
assert(1 == DISPLAY_output_message_call_count);
|
||||
}
|
||||
|
||||
// Order assumption
|
||||
TEST_F(GreeterTests, given_full_screen_multiple_will_reset_display_then_output)
|
||||
{
|
||||
char name[] = "mike";
|
||||
// given
|
||||
DISPLAY_get_line_capacity_return_val = 4;
|
||||
DISPLAY_get_line_insert_index_return_val = 4;
|
||||
// when
|
||||
UI_greet_multiple_times(name, 1);
|
||||
// then
|
||||
assert(1 == DISPLAY_clear_call_count);
|
||||
assert(1 == DISPLAY_output_message_call_count);
|
||||
}
|
||||
|
||||
#define RUN_TEST(TESTNAME) printf("Running %s\n", #TESTNAME); setup(); TESTNAME();
|
||||
int main()
|
||||
{
|
||||
RUN_TEST(init_initialises_display);
|
||||
RUN_TEST(given_name_when_greet_called_outputs_name);
|
||||
RUN_TEST(given_name_when_greet_called_outputs_name);
|
||||
RUN_TEST(given_name_and_3_times_when_greetmultiple_called_outputs_name_3_times);
|
||||
RUN_TEST(given_full_screen_single_will_reset_display_then_output);
|
||||
RUN_TEST(given_full_screen_multiple_will_reset_display_then_output);
|
||||
return 0;
|
||||
}
|
||||
106
src/test/cppmocktest.cpp
Normal file
106
src/test/cppmocktest.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
//============================================================================
|
||||
// Name : cmock.cpp
|
||||
// Author : Mike Long
|
||||
// Version :
|
||||
// Copyright : Don't steal
|
||||
// Description : Hello World in C++, Ansi-style
|
||||
//============================================================================
|
||||
|
||||
#include "cppmock.hpp"
|
||||
|
||||
extern "C"{
|
||||
#include "embedded.h"
|
||||
}
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
FAKE_VOID_FUNC0(DISPLAY_init);
|
||||
FAKE_VOID_FUNC0(DISPLAY_clear);
|
||||
FAKE_VOID_FUNC1(DISPLAY_output_message, const char*);
|
||||
FAKE_VALUE_FUNC0(int, DISPLAY_get_line_capacity);
|
||||
FAKE_VALUE_FUNC0(int, DISPLAY_get_line_insert_index);
|
||||
|
||||
|
||||
class GreeterTests : public testing::Test
|
||||
{
|
||||
public:
|
||||
|
||||
void SetUp()
|
||||
{
|
||||
// Register resets
|
||||
RESET_FAKES();
|
||||
// non default init
|
||||
DISPLAY_get_line_capacity_return_val = 10;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(GreeterTests, init_initialises_display)
|
||||
{
|
||||
UI_init();
|
||||
ASSERT_EQ(1, DISPLAY_init_call_count);
|
||||
}
|
||||
|
||||
TEST_F(GreeterTests, given_name_when_greet_called_outputs_name)
|
||||
{
|
||||
// given
|
||||
char name[] = "mike";
|
||||
// when
|
||||
UI_greet(name);
|
||||
// then
|
||||
ASSERT_EQ(1, DISPLAY_output_message_call_count);
|
||||
ASSERT_EQ(name, DISPLAY_output_message_arg0_val);
|
||||
}
|
||||
|
||||
|
||||
TEST_F(GreeterTests, given_name_and_3_times_when_greetmultiple_called_outputs_name_3_times)
|
||||
{
|
||||
// given
|
||||
char name[] = "mike";
|
||||
// when
|
||||
UI_greet_multiple_times(name, 3);
|
||||
// then
|
||||
ASSERT_EQ(3, DISPLAY_output_message_call_count);
|
||||
ASSERT_EQ(name, DISPLAY_output_message_arg0_val);
|
||||
}
|
||||
|
||||
TEST_F(GreeterTests, given_non_full_screen_will_not_reset_display)
|
||||
{
|
||||
char name[] = "mike";
|
||||
// given
|
||||
DISPLAY_get_line_capacity_return_val = 10;
|
||||
DISPLAY_get_line_insert_index_return_val = 0;
|
||||
// when
|
||||
UI_greet(name);
|
||||
// then
|
||||
ASSERT_EQ(0, DISPLAY_clear_call_count);
|
||||
ASSERT_EQ(1, DISPLAY_output_message_call_count);
|
||||
}
|
||||
|
||||
// Order assumption
|
||||
TEST_F(GreeterTests, given_full_screen_single_will_reset_display_then_output)
|
||||
{
|
||||
char name[] = "mike";
|
||||
// given
|
||||
DISPLAY_get_line_capacity_return_val = 1;
|
||||
DISPLAY_get_line_insert_index_return_val = 1;
|
||||
// when
|
||||
UI_greet(name);
|
||||
// then
|
||||
ASSERT_EQ(1, DISPLAY_clear_call_count);
|
||||
ASSERT_EQ(1, DISPLAY_output_message_call_count);
|
||||
}
|
||||
|
||||
// Order assumption
|
||||
TEST_F(GreeterTests, given_full_screen_multiple_will_reset_display_then_output)
|
||||
{
|
||||
char name[] = "mike";
|
||||
// given
|
||||
DISPLAY_get_line_capacity_return_val = 4;
|
||||
DISPLAY_get_line_insert_index_return_val = 4;
|
||||
// when
|
||||
UI_greet_multiple_times(name, 1);
|
||||
// then
|
||||
ASSERT_EQ(1, DISPLAY_clear_call_count);
|
||||
ASSERT_EQ(1, DISPLAY_output_message_call_count);
|
||||
}
|
||||
|
||||
34
src/test/embedded.c
Normal file
34
src/test/embedded.c
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
|
||||
|
||||
void DISPLAY_init();
|
||||
void DISPLAY_clear();
|
||||
int DISPLAY_get_line_capacity();
|
||||
int DISPLAY_get_line_insert_index();
|
||||
void DISPLAY_output_message(char * message);
|
||||
|
||||
void UI_init()
|
||||
{
|
||||
DISPLAY_init();
|
||||
}
|
||||
|
||||
void UI_greet(char * name)
|
||||
{
|
||||
if(DISPLAY_get_line_capacity() == DISPLAY_get_line_insert_index())
|
||||
{
|
||||
DISPLAY_clear();
|
||||
}
|
||||
DISPLAY_output_message(name);
|
||||
}
|
||||
|
||||
void UI_greet_multiple_times(char * name, int times)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < times; i++)
|
||||
{
|
||||
UI_greet(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
10
src/test/embedded.h
Normal file
10
src/test/embedded.h
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
#ifndef EMBEDDED_CODE
|
||||
#define EMBEDDED_CODE
|
||||
|
||||
void UI_init();
|
||||
void UI_greet(char * name);
|
||||
void UI_greet_multiple_times(char * name, int times);
|
||||
|
||||
|
||||
#endif /* EMBEDDED_CODE */
|
||||
Reference in New Issue
Block a user