File size: 5,330 Bytes
7510827 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
#include "sqliteInt.h"
#include "unity.h"
#include <string.h>
#include <stdlib.h>
/* Wrapper for the static function provided by the build system */
extern int test_renameColumnExprCb(Walker *pWalker, Expr *pExpr);
/* sqlite internal helper we rely on to seed the token-map */
extern void sqlite3RenameTokenMap(Parse*, void*, Token*);
static void initParse(Parse *pParse, sqlite3 *db){
memset(pParse, 0, sizeof(*pParse));
pParse->db = db;
}
static void initWalkerCtx(Parse *pParse, Walker *pWalker, RenameCtx *pCtx, Table *pTab, int iCol){
memset(pWalker, 0, sizeof(*pWalker));
memset(pCtx, 0, sizeof(*pCtx));
pCtx->pTab = pTab;
pCtx->iCol = iCol;
pWalker->u.pRename = pCtx;
pWalker->pParse = pParse;
}
static void mapTokenForExpr(Parse *pParse, Expr *pExpr, const char *zTok){
Token t;
t.z = zTok;
t.n = (int)strlen(zTok);
sqlite3RenameTokenMap(pParse, (void*)pExpr, &t);
}
void setUp(void) {
/* no-op */
}
void tearDown(void) {
/* no-op */
}
/* Positive: TK_COLUMN match (iCol and pTab match) triggers token capture */
void test_renameColumnExprCb_column_match(void){
sqlite3 *db = 0;
TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));
Parse parse; initParse(&parse, db);
Walker w; RenameCtx ctx; Table tab;
initWalkerCtx(&parse, &w, &ctx, &tab, 2);
Expr e; memset(&e, 0, sizeof(e));
e.op = TK_COLUMN;
e.iColumn = 2;
e.y.pTab = &tab;
mapTokenForExpr(&parse, &e, "coltok");
int rc = test_renameColumnExprCb(&w, &e);
TEST_ASSERT_EQUAL_INT(WRC_Continue, rc);
TEST_ASSERT_NOT_NULL(ctx.pList);
TEST_ASSERT_EQUAL_PTR(&e, ctx.pList->p);
sqlite3_close(db);
}
/* Positive: TK_TRIGGER match (iCol matches and pTriggerTab==pTab) triggers token capture */
void test_renameColumnExprCb_trigger_match(void){
sqlite3 *db = 0;
TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));
Parse parse; initParse(&parse, db);
Walker w; RenameCtx ctx; Table tab;
initWalkerCtx(&parse, &w, &ctx, &tab, 5);
parse.pTriggerTab = &tab;
Expr e; memset(&e, 0, sizeof(e));
e.op = TK_TRIGGER;
e.iColumn = 5;
mapTokenForExpr(&parse, &e, "trigtok");
int rc = test_renameColumnExprCb(&w, &e);
TEST_ASSERT_EQUAL_INT(WRC_Continue, rc);
TEST_ASSERT_NOT_NULL(ctx.pList);
TEST_ASSERT_EQUAL_PTR(&e, ctx.pList->p);
sqlite3_close(db);
}
/* Negative: TK_COLUMN but iCol mismatch -> no token captured */
void test_renameColumnExprCb_column_mismatch_iCol(void){
sqlite3 *db = 0;
TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));
Parse parse; initParse(&parse, db);
Walker w; RenameCtx ctx; Table tab;
initWalkerCtx(&parse, &w, &ctx, &tab, 1);
Expr e; memset(&e, 0, sizeof(e));
e.op = TK_COLUMN;
e.iColumn = 3; /* mismatch */
e.y.pTab = &tab;
mapTokenForExpr(&parse, &e, "coltok");
int rc = test_renameColumnExprCb(&w, &e);
TEST_ASSERT_EQUAL_INT(WRC_Continue, rc);
TEST_ASSERT_NULL(ctx.pList);
sqlite3_close(db);
}
/* Negative: TK_COLUMN with table mismatch -> no token captured */
void test_renameColumnExprCb_column_mismatch_table(void){
sqlite3 *db = 0;
TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));
Parse parse; initParse(&parse, db);
Walker w; RenameCtx ctx; Table tab1, tab2;
initWalkerCtx(&parse, &w, &ctx, &tab1, 2);
Expr e; memset(&e, 0, sizeof(e));
e.op = TK_COLUMN;
e.iColumn = 2;
e.y.pTab = &tab2; /* different table pointer */
mapTokenForExpr(&parse, &e, "coltok");
int rc = test_renameColumnExprCb(&w, &e);
TEST_ASSERT_EQUAL_INT(WRC_Continue, rc);
TEST_ASSERT_NULL(ctx.pList);
sqlite3_close(db);
}
/* Negative: TK_TRIGGER with trigger table mismatch -> no token captured */
void test_renameColumnExprCb_trigger_mismatch_table(void){
sqlite3 *db = 0;
TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));
Parse parse; initParse(&parse, db);
Walker w; RenameCtx ctx; Table tab1, tab2;
initWalkerCtx(&parse, &w, &ctx, &tab1, 4);
parse.pTriggerTab = &tab2; /* different trigger table */
Expr e; memset(&e, 0, sizeof(e));
e.op = TK_TRIGGER;
e.iColumn = 4;
mapTokenForExpr(&parse, &e, "trigtok");
int rc = test_renameColumnExprCb(&w, &e);
TEST_ASSERT_EQUAL_INT(WRC_Continue, rc);
TEST_ASSERT_NULL(ctx.pList);
sqlite3_close(db);
}
/* Negative: Unrelated opcode -> no token captured */
void test_renameColumnExprCb_other_opcode_noop(void){
sqlite3 *db = 0;
TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));
Parse parse; initParse(&parse, db);
Walker w; RenameCtx ctx; Table tab;
initWalkerCtx(&parse, &w, &ctx, &tab, 0);
Expr e; memset(&e, 0, sizeof(e));
e.op = TK_INTEGER; /* some non-matching opcode */
e.iColumn = 0;
/* e.y.pTab unused for this opcode */
mapTokenForExpr(&parse, &e, "othertok");
int rc = test_renameColumnExprCb(&w, &e);
TEST_ASSERT_EQUAL_INT(WRC_Continue, rc);
TEST_ASSERT_NULL(ctx.pList);
sqlite3_close(db);
}
int main(void){
UNITY_BEGIN();
RUN_TEST(test_renameColumnExprCb_column_match);
RUN_TEST(test_renameColumnExprCb_trigger_match);
RUN_TEST(test_renameColumnExprCb_column_mismatch_iCol);
RUN_TEST(test_renameColumnExprCb_column_mismatch_table);
RUN_TEST(test_renameColumnExprCb_trigger_mismatch_table);
RUN_TEST(test_renameColumnExprCb_other_opcode_noop);
return UNITY_END();
} |