// tranformer.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <cassert>
#include <malloc.h>
#include <string.h>
#include "trans_app.h"
#define INT_EQUAL(x, y) do {\
if (x != y)\
{\
printf("%d is not equal to %d.\n", x, y);\
}\
} while(0)
typedef struct
{
TransView tran_view;
const char **trans;
int trans_num;
TransList selTrans;
} MockView;
void mockview_init_trans_view(TransView *trans_view, const char **trans, int num)
{
MockView *mock_view = (MockView *)trans_view;
mock_view->trans = trans;
mock_view->trans_num = num;
}
void mockview_refresh_trans_view(struct tranView *trans_view, TransList selTrans)
{
MockView *mock_view = (MockView *)trans_view;
mock_view->selTrans = selTrans;
}
MockView * newMockView()
{
MockView *mock_view = (MockView *)malloc(sizeof(MockView));
if (NULL != mock_view)
{
memset(mock_view, 0, sizeof(MockView));
mock_view->tran_view.init_trans_view = mockview_init_trans_view;
mock_view->tran_view.refresh_trans_view = mockview_refresh_trans_view;
}
return mock_view;
}
int mockview_getAllTransLen(MockView *mock_view)
{
return mock_view->trans_num;
}
int mockview_getAllSelTransLen(MockView *mock_view)
{
return mock_view->selTrans.num;
}
void test_init()
{
MockView *mock_view = NULL;
TransApp *tans_app = NULL;
mock_view = newMockView();
tans_app = newTransApp(&mock_view->tran_view, TRANS, 3 );
INT_EQUAL(3, mockview_getAllTransLen(mock_view));
}
void test_add()
{
MockView *mock_view = NULL;
TransApp *tans_app = NULL;
mock_view = newMockView();
tans_app = newTransApp(&mock_view->tran_view, TRANS, 3 );
trans_addTransFormer(tans_app, "Upper");
INT_EQUAL(1, mockview_getAllSelTransLen(mock_view));
}
int _tmain(int argc, _TCHAR* argv[])
{
test_init();
test_add();
return 0;
}
#ifndef _TRANS_APP_H_
#define _TRANS_APP_H_
#include <stdio.h>
const char *TRANS[];
#define MAX_TRANS 3
typedef struct
{
const char *trans[MAX_TRANS];
int num;
} TransList;
typedef struct tranView
{
void (* init_trans_view)(struct tranView *trans_view, const char **trans, int num);
void (* refresh_trans_view)(struct tranView *trans_view, TransList);
} TransView;
typedef struct
{
TransView *trans_view;
const char **trans;
int num;
TransList selTrans;
} TransApp;
TransApp *newTransApp(TransView *trans_view, const char **trans, int num);
void trans_addTransFormer(TransApp *tans_app, const char *trans);
#endif
trans_app.cpp
#include "stdafx.h"
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "trans_app.h"
const char *TRANS[] = {"Upper", "Lower", "TrimPrefix"};
TransApp *newTransApp(TransView *trans_view, const char **trans, int num)
{
TransApp *trans_app = (TransApp *)malloc(sizeof(TransApp));
if (NULL != trans_app)
{
memset(trans_app, 0, sizeof(TransApp));
trans_app->trans_view = trans_view;
trans_app->selTrans.num = 0;
trans_view->init_trans_view(trans_view, trans, num);
}
return trans_app;
}
void trans_addTransFormer(TransApp *trans_app, const char *trans)
{
trans_app->selTrans.trans[trans_app->selTrans.num++] = trans;
trans_app->trans_view->refresh_trans_view(trans_app->trans_view, trans_app->selTrans);
}