Created
June 15, 2011 13:56
-
-
Save yasuhito/1027152 to your computer and use it in GitHub Desktop.
legacy C でモック
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// たとえばこういうシグニチャの関数があったとして、 | |
// こいつをテストの実行時に mock 関数に置き換えたいとする | |
void die( const char *format, ... ); | |
// die.h では関数ポインタを公開する (extern)。 | |
extern void ( *die )( const char *format, ... ); | |
// die.c では関数ポインタを定義してて、 | |
// 同時にデフォルトの実装 _die() を代入してやる | |
static _die( const char *format, ... ) { | |
// snip | |
} | |
void ( *die )( const char *format, ... ) = _die; | |
// これで、die() を呼び出す側はふつうの関数と同じ要領で呼び出せる。 | |
die( "bye" ); | |
// テストコードでは、以下のようにすることで die() の中身を実行時に置き換えできる。 | |
// モック | |
static void mock_die( const char *format, ... ) { | |
// snip | |
} | |
// テストの setup() | |
static void setup() { | |
// 置き換える | |
die = mock_die; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment