// test_transformer.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "malloc.h"
#include "string.h"
#include "assert.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
{
TranView trans_view;
const char **trans;
int trans_num;
SelTransList sel_trans;
char orgString[MAX_TRAN_LEN];
char result[MAX_TRAN_LEN];
} MockView;
void mockview_init(TranView *tran_view, const char **trans, int num)
{
MockView *mock_view = (MockView *)tran_view;
mock_view->trans = trans;
mock_view->trans_num = num;
}
void mockview_refresh_sel_trans(TranView *tran_view, SelTransList *sel_tran)
{
MockView *mock_view = (MockView *)tran_view;
mock_view->sel_trans = *sel_tran;
}
void mockview_get_org_string(TranView *tran_view, char *str)
{
MockView *mock_view = (MockView *)tran_view;
strcpy(str, mock_view->orgString);
}
void mockview_set_result_string(TranView *tran_view, char *str)
{
MockView *mock_view = (MockView *)tran_view;
strcpy(mock_view->result, str);
}
MockView *newMockView()
{
MockView *mock_view = (MockView *)malloc(sizeof(MockView));
if (NULL != mock_view)
{
memset(mock_view, 0, sizeof(MockView));
mock_view->trans_view.init_view = mockview_init;
mock_view->trans_view.refresh_sel_trans = mockview_refresh_sel_trans;
mock_view->trans_view.get_org_string = mockview_get_org_string;
mock_view->trans_view.set_result_string = mockview_set_result_string;
mock_view->trans_num = 0;
}
return mock_view;
}
int mockview_getAllTransLen(MockView *mock_view)
{
return mock_view->trans_num;
}
int mockview_getAllSelTransLen(MockView *mock_view)
{
return mock_view->sel_trans.sel_num;
}
void mockview_setOrgString(MockView *mock_view, const char *str)
{
strcpy(mock_view->orgString, str);
}
void mockview_getOrgString(MockView *mock_view, char *result)
{
strcpy(result, mock_view->result);
}
void test_init()
{
MockView *mock_view = NULL;
TransApp *app = NULL;
mock_view = newMockView();
app = newTransApp(&mock_view->trans_view, TRANS, 3);
INT_EQUAL(3, mockview_getAllTransLen(mock_view));
}
void test_add()
{
MockView *mock_view = NULL;
TransApp *app = NULL;
mock_view = newMockView();
app = newTransApp(&mock_view->trans_view, TRANS, 3);
tran_addTransformer(app, "Upper");
INT_EQUAL(1, mockview_getAllSelTransLen(mock_view));
}
void test_remove()
{
MockView *mock_view = NULL;
TransApp *app = NULL;
mock_view = newMockView();
app = newTransApp(&mock_view->trans_view, TRANS, 3);
tran_addTransformer(app, "Upper");
tran_addTransformer(app, "Lower");
tran_removeTransformer(app, "Upper");
INT_EQUAL(1, mockview_getAllSelTransLen(mock_view));
}
void test_remove_all()
{
MockView *mock_view = NULL;
TransApp *app = NULL;
mock_view = newMockView();
app = newTransApp(&mock_view->trans_view, TRANS, 3);
tran_addTransformer(app, "Upper");
tran_addTransformer(app, "Lower");
tran_removeTransformer(app, "Upper");
tran_removeAllTransformer(app);
INT_EQUAL(0, mockview_getAllSelTransLen(mock_view));
}
void test_apply()
{
MockView *mock_view = NULL;
TransApp *app = NULL;
char result[MAX_TRAN_LEN] = "";
mock_view = newMockView();
app = newTransApp(&mock_view->trans_view, TRANS, 3);
tran_addTransformer(app, "Upper");
mockview_setOrgString(mock_view, "hello, world.");
tran_applyTransformer(app);
mockview_getOrgString(mock_view, result);
assert(0 == strcmp(result, "HELLO, WORLD."));
}
int _tmain(int argc, _TCHAR* argv[])
{
test_init();
test_add();
test_remove();
test_remove_all();
test_apply();
return 0;
}
//trans_app.cpp
#include "stdafx.h"
#include "malloc.h"
#include "string.h"
#include "trans_app.h"
const char *TRANS[] = {"Upper", "Lower", "TrimPrefix"};
TransApp *newTransApp(TranView *tran_view, const char **trans, int num)
{
TransApp *app = (TransApp *)malloc(sizeof(TransApp));
if (NULL != app)
{
memset(app, 0, sizeof(TransApp));
app->tranView = tran_view;
app->tran_num = num;
app->tranView->init_view(tran_view, trans, num);
}
return app;
}
void tran_addTransformer(TransApp *app, const char *tran)
{
app->sel_tran.sel_tran[app->sel_tran.sel_num++] = tran;
app->tranView->refresh_sel_trans(app->tranView, &app->sel_tran);
}
void tran_removeTransformer(TransApp *app, const char *tran)
{
for (int i = 0; i < app->sel_tran.sel_num; i++)
{
if (0 == strcmp(tran, app->sel_tran.sel_tran[i]))
{
memcpy(&app->sel_tran.sel_tran[i],
&app->sel_tran.sel_tran[i+1], sizeof(char *)*(app->sel_tran.sel_num - i -1));
app->sel_tran.sel_num--;
app->tranView->refresh_sel_trans(app->tranView, &app->sel_tran);
}
}
}
void tran_removeAllTransformer(TransApp *app)
{
app->sel_tran.sel_num = 0;
app->tranView->refresh_sel_trans(app->tranView, &app->sel_tran);
}
void trans_form(const char *sel_tran, char *result, char* org)
{
if (0 == strcmp(sel_tran, "Upper"))
{
//TODO:
}
else if (0 == strcmp(sel_tran, "Lower"))
{
//TODO:
}
else if (0 == strcmp(sel_tran, "TrimPrefix"))
{
//TODO:
}
}
void tran_applyTransformer(TransApp *app)
{
char org[MAX_TRAN_LEN] = "";
char result[MAX_TRAN_LEN] = "";
app->tranView->get_org_string(app->tranView, org);
for (int i = 0; i < app->sel_tran.sel_num; i++)
{
trans_form(app->sel_tran.sel_tran[i], result, org);
strcpy(org, result);
}
app->tranView->set_result_string(app->tranView, result);
}
//trans_app.h
#ifndef _TRANS_APP_H
#define _TRANS_APP_H
const char *TRANS[];
#define MAX_TRAN_LEN 256
#define MAX_TRANS 3
typedef struct
{
int sel_num;
const char *sel_tran[MAX_TRANS];
} SelTransList;
typedef struct tranView
{
void (*init_view)(struct tranView *tran_view, const char **trans, int num);
void (*refresh_sel_trans)(struct tranView *tran_view, SelTransList *sel_tran);
void (*get_org_string)(struct tranView *tran_view, char *str);
void (*set_result_string)(struct tranView *tran_view, char *str);
} TranView;
typedef struct
{
TranView *tranView;
const char **trans;
int tran_num;
SelTransList sel_tran;
} TransApp;
TransApp *newTransApp(TranView *tran_view, const char **trans, int num);
void tran_addTransformer(TransApp *app, const char *tran);
void tran_removeTransformer(TransApp *app, const char *tran);
void tran_removeAllTransformer(TransApp *app);
void tran_applyTransformer(TransApp *app);
#endif