2016年10月1日土曜日

開発環境

テスト駆動開発による組み込みプログラミング (James W. Grenning (著)、蛸島 昭之 (監修)、笹井 崇司(翻訳)、オライリージャパン)のI部(TDDを始めよう)、2章(テスト駆動ツールと約束事)、2.7(練習問題)、4(Unity)を取り組んでみる。

4(Unity)

コード(Emacs)

Makefile

#Set this to @ to keep the makefile quiet
SILENCE = @

#---- Outputs ----#
COMPONENT_NAME = BookCode_Unity
TARGET1 = test1

#--- Inputs ----#
UNITY_HOME = $(HOME)/root/Unity
CPP_PLATFORM = Gcc
PROJECT_HOME_DIR = .
PROJECT_TEST_DIR = unity
UNITY_BUILD_HOME = $(UNITY_HOE)/extras/fixture/build

UNITY_CFLAGS += -DUNITY_OUTPUT_CHAR=UnityOutputCharSpy_OutputChar
UNITY_WARNINGFLAGS = -Wall -Werror -Wswitch-default
#UNITY_WARNINGFLAGS += -Wshadow 

SRC_DIRS = \
  $(PROJECT_HOME_DIR)/src/

TEST_SRC_DIRS = \
  $(PROJECT_TEST_DIR)\
  $(PROJECT_TEST_DIR)/stdio\
  $(UNITY_HOME)/src\
  $(UNITY_HOME)/extras/fixture/src\
  $(UNITY_HOME)/extras/fixture/test\

TEST_SRC_FILES = \
  $(PROJECT_TEST_DIR)/*.c\
  $(PROJECT_TEST_DIR)/stdio/*.c\
  $(UNITY_HOME)/src/*.c\
  $(UNITY_HOME)/extras/fixture/src/*.c\
  $(UNITY_HOME)/extras/fixture/test/*.c

MOCKS_SRC_DIRS = \
  $(PROJECT_TEST_DIR)/mocks\

INCLUDE_DIRS =\
  -I.\
  -I/$(UNITY_HOME)/src\
  -I/$(UNITY_HOME)/test\
  -I/$(UNITY_HOME)/extras/fixture/src\
  -I/$(UNITY_HOME)/extras/fixture/test\
  -I/$(PROJECT_HOME_DIR)/mocks\
  
# include $(UNITY_BUILD_HOME)/MakefileWorker.mk

all: default

default:
 $(CC) $(UNITY_CFLAGS) $(UNITY_WARNINGFLAGS) $(INCLUDE_DIRS) \
 $(TEST_SRC_FILES) -o $(TARGET1) && ./$(TARGET1)

SprintfTest.c

#include <stdio.h> // memset
#include <string.h> // strlen

#include "unity_fixture.h"

TEST_GROUP(sprintf);
static char output[100];
static const char * expected;
static void expect(const char * s) {
  expected = s;
}
static void given(int charsWritten) {
  TEST_ASSERT_EQUAL_INT(strlen(expected), charsWritten);
  TEST_ASSERT_EQUAL_STRING(expected, &output[1]);
  TEST_ASSERT_BYTES_EQUAL(0xaa, output[0]);
  TEST_ASSERT_BYTES_EQUAL(0xaa, output[1 + strlen(expected) + 1]);
}

TEST_SETUP(sprintf) {
  memset(output, 0xaa, sizeof(output));
  expected = "";
}

TEST_TEAR_DOWN(sprintf) {
}

TEST(sprintf, NoFormatOperations) {
  expect("hey");
  given(sprintf(&output[1], "hey"));
}
TEST(sprintf, InsertString) {
  expect("Hello, World\n");
  given(sprintf(&output[1], "Hello, %s\n", "World"));
}

入出力結果(Terminal, IPython)

$ make -s
Unity test run 1 of 1
.unity/stdio/SprintfTest.c:14:TEST(sprintf, NoFormatOperations):FAIL: Expected 'hey' Was 'ey'
.unity/stdio/SprintfTest.c:14:TEST(sprintf, InsertString):FAIL: Expected 'Hello, World\n' Was 'ello, World\n'


-----------------------
2 Tests 2 Failures 0 Ignored 
FAIL
make: *** [default] Error 2
$ make -s
Unity test run 1 of 1
..unity/stdio/SprintfTest.c:14:TEST(sprintf, InsertString):FAIL: Expected 'Hello, World\n' Was 'ello, World\n'


-----------------------
2 Tests 1 Failures 0 Ignored 
FAIL
make: *** [default] Error 1
$ make -s
Unity test run 1 of 1
..

-----------------------
2 Tests 0 Failures 0 Ignored 
OK
$ ls -R
./  Makefile src/  test1*
../  Makefile~ stdio/  unity/

./src:
./ ../

./stdio:
./ ../

./unity:
./  ../  AllTests.c AllTests.c~ stdio/

./unity/stdio:
./   SprintfTest.c  SprintfTestRunner.c
../   SprintfTest.c~  SprintfTestRunner.c~
$ 

0 コメント:

コメントを投稿

Comments on Google+: