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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
|
/*
* This API is subset plunit lib with http://www.apollo-pro.com/help/pl_unit_assertions.htm
*
*/
#include "postgres.h"
#include <math.h>
#include "funcapi.h"
#include "catalog/pg_collation.h"
#include "parser/parse_oper.h"
#include "utils/builtins.h"
#include "orafce.h"
#include "builtins.h"
PG_FUNCTION_INFO_V1(plunit_assert_true);
PG_FUNCTION_INFO_V1(plunit_assert_true_message);
PG_FUNCTION_INFO_V1(plunit_assert_false);
PG_FUNCTION_INFO_V1(plunit_assert_false_message);
PG_FUNCTION_INFO_V1(plunit_assert_null);
PG_FUNCTION_INFO_V1(plunit_assert_null_message);
PG_FUNCTION_INFO_V1(plunit_assert_not_null);
PG_FUNCTION_INFO_V1(plunit_assert_not_null_message);
PG_FUNCTION_INFO_V1(plunit_assert_equals);
PG_FUNCTION_INFO_V1(plunit_assert_equals_message);
PG_FUNCTION_INFO_V1(plunit_assert_equals_range);
PG_FUNCTION_INFO_V1(plunit_assert_equals_range_message);
PG_FUNCTION_INFO_V1(plunit_assert_not_equals);
PG_FUNCTION_INFO_V1(plunit_assert_not_equals_message);
PG_FUNCTION_INFO_V1(plunit_assert_not_equals_range);
PG_FUNCTION_INFO_V1(plunit_assert_not_equals_range_message);
PG_FUNCTION_INFO_V1(plunit_fail);
PG_FUNCTION_INFO_V1(plunit_fail_message);
static bool assert_equals_base(FunctionCallInfo fcinfo);
static bool assert_equals_range_base(FunctionCallInfo fcinfo);
static char *assert_get_message(FunctionCallInfo fcinfo, int nargs, char *default_message);
/****************************************************************
* plunit.assert_true
* plunit.assert_true_message
*
* Syntax:
* PROCEDURE assert_true(condition boolean, message varchar default '');
*
* Purpose:
* Asserts that the condition is true. The optional message will be
* displayed if the assertion fails. If not supplied, a default message
* is displayed.
*
****************************************************************/
Datum
plunit_assert_true(PG_FUNCTION_ARGS)
{
return plunit_assert_true_message(fcinfo);
}
Datum
plunit_assert_true_message(PG_FUNCTION_ARGS)
{
char *message = assert_get_message(fcinfo, 2, "plunit.assert_true exception");
bool condition = PG_GETARG_BOOL(0);
if (PG_ARGISNULL(0) || !condition)
ereport(ERROR,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("%s", message),
errdetail("Plunit.assertation fails (assert_true).")));
PG_RETURN_VOID();
}
/****************************************************************
* plunit.assert_false
* plunit.assert_false_message
*
* Syntax:
* PROCEDURE assert_false(condition boolean, message varchar default '');
*
* Purpose:
* Asserts that the condition is false. The optional message will be
* displayed if the assertion fails. If not supplied, a default message
* is displayed.
*
****************************************************************/
Datum
plunit_assert_false(PG_FUNCTION_ARGS)
{
return plunit_assert_false_message(fcinfo);
}
Datum
plunit_assert_false_message(PG_FUNCTION_ARGS)
{
char *message = assert_get_message(fcinfo, 2, "plunit.assert_false exception");
bool condition = PG_GETARG_BOOL(0);
if (PG_ARGISNULL(0) || condition)
ereport(ERROR,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("%s", message),
errdetail("Plunit.assertation fails (assert_false).")));
PG_RETURN_VOID();
}
/****************************************************************
* plunit.assert_null
* plunit.assert_null_message
*
* Syntax:
* PROCEDURE assert_null(actual anyelement, message varchar default '');
*
* Purpose:
* Asserts that the actual is null. The optional message will be
* displayed if the assertion fails. If not supplied, a default message
* is displayed.
*
****************************************************************/
Datum
plunit_assert_null(PG_FUNCTION_ARGS)
{
return plunit_assert_null_message(fcinfo);
}
Datum
plunit_assert_null_message(PG_FUNCTION_ARGS)
{
char *message = assert_get_message(fcinfo, 2, "plunit.assert_null exception");
if (!PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("%s", message),
errdetail("Plunit.assertation fails (assert_null).")));
PG_RETURN_VOID();
}
/****************************************************************
* plunit.assert_not_null
* plunit.assert_not_null_message
*
* Syntax:
* PROCEDURE assert_not_null(actual anyelement, message varchar default '');
*
* Purpose:
* Asserts that the actual isn't null. The optional message will be
* displayed if the assertion fails. If not supplied, a default message
* is displayed.
*
****************************************************************/
Datum
plunit_assert_not_null(PG_FUNCTION_ARGS)
{
return plunit_assert_not_null_message(fcinfo);
}
Datum
plunit_assert_not_null_message(PG_FUNCTION_ARGS)
{
char *message = assert_get_message(fcinfo, 2, "plunit.assert_not_null exception");
if (PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("%s", message),
errdetail("Plunit.assertation fails (assert_not_null).")));
PG_RETURN_VOID();
}
/****************************************************************
* plunit.assert_equals
* plunit.assert_equals_message
* plunit.assert_equals_range
* plunit.assert_equals_range_message
*
* Syntax:
* PROCEDURE assert_equals(expected anyelement,actual anyelement,
* message varchar default '');
* PROCEDURE assert_equals(expected double precision, actual double precision,
* range double precision, message varchar default '');
*
* Purpose:
* Asserts that expected and actual are equal. The optional message will be
* displayed if the assertion fails. If not supplied, a default
* message is displayed.
* Asserts that expected and actual are within the specified range.
* The optional message will be displayed if the assertion fails.
* If not supplied, a default message is displayed.
*
****************************************************************/
static char *
assert_get_message(FunctionCallInfo fcinfo, int nargs, char *message)
{
char *result;
if (PG_NARGS() == nargs)
{
text *msg;
if (PG_ARGISNULL(nargs - 1))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("message is NULL"),
errdetail("Message may not be NULL.")));
msg = PG_GETARG_TEXT_P(nargs - 1);
result = text_to_cstring(msg);
}
else
result = message;
return result;
}
static bool
assert_equals_base(FunctionCallInfo fcinfo)
{
Datum value1 = PG_GETARG_DATUM(0);
Datum value2 = PG_GETARG_DATUM(1);
Oid *ptr;
ptr = (Oid *) fcinfo->flinfo->fn_extra;
if (ptr == NULL)
{
Oid valtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
Oid eqopfcid;
if (!OidIsValid(valtype))
elog(ERROR, "could not determine data type of input");
eqopfcid = equality_oper_funcid(valtype);
if (!OidIsValid(eqopfcid))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("unknown equal operand for datatype")));
/* First time calling for current query: allocate storage */
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
sizeof(Oid));
ptr = (Oid *) fcinfo->flinfo->fn_extra;
*ptr = eqopfcid;
}
return DatumGetBool(OidFunctionCall2Coll(*ptr, DEFAULT_COLLATION_OID, value1, value2));
}
Datum
plunit_assert_equals(PG_FUNCTION_ARGS)
{
return plunit_assert_equals_message(fcinfo);
}
Datum
plunit_assert_equals_message(PG_FUNCTION_ARGS)
{
char *message = assert_get_message(fcinfo, 3, "plunit.assert_equal exception");
/* skip all tests for NULL value */
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
ereport(ERROR,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("%s", message),
errdetail("Plunit.assertation fails (assert_equals).")));
if (!assert_equals_base(fcinfo))
ereport(ERROR,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("%s", message),
errdetail("Plunit.assertation fails (assert_equals).")));
PG_RETURN_VOID();
}
Datum
plunit_assert_equals_range(PG_FUNCTION_ARGS)
{
return plunit_assert_equals_range_message(fcinfo);
}
static bool
assert_equals_range_base(FunctionCallInfo fcinfo)
{
float8 expected_value;
float8 actual_value;
float8 range_value;
range_value = PG_GETARG_FLOAT8(2);
if (range_value < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("cannot set range to negative number")));
expected_value = PG_GETARG_FLOAT8(0);
actual_value = PG_GETARG_FLOAT8(1);
return fabs(expected_value - actual_value) < range_value;
}
Datum
plunit_assert_equals_range_message(PG_FUNCTION_ARGS)
{
char *message = assert_get_message(fcinfo, 4, "plunit.assert_equal exception");
/* skip all tests for NULL value */
if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
ereport(ERROR,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("%s", message),
errdetail("Plunit.assertation fails (assert_equals).")));
if (!assert_equals_range_base(fcinfo))
ereport(ERROR,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("%s", message),
errdetail("Plunit.assertation fails (assert_equals).")));
PG_RETURN_VOID();
}
/****************************************************************
* plunit.assert_not_equals
* plunit.assert_not_equals_message
* plunit.assert_not_equals_range
* plunit.assert_not_equals_range_message
*
* Syntax:
* PROCEDURE assert_not_equals(expected anyelement,actual anyelement,
* message varchar default '');
* PROCEDURE assert_not_equals(expected double precision, expected double precision,
* range double precision, message varchar default '');
*
* Purpose:
* Asserts that expected and actual are equal. The optional message will be
* displayed if the assertion fails. If not supplied, a default
* message is displayed.
* Asserts that expected and actual are within the specified range.
* The optional message will be displayed if the assertion fails.
* If not supplied, a default message is displayed.
*
****************************************************************/
Datum
plunit_assert_not_equals(PG_FUNCTION_ARGS)
{
return plunit_assert_not_equals_message(fcinfo);
}
Datum
plunit_assert_not_equals_message(PG_FUNCTION_ARGS)
{
char *message = assert_get_message(fcinfo, 3, "plunit.assert_not_equal exception");
/* skip all tests for NULL value */
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
ereport(ERROR,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("%s", message),
errdetail("Plunit.assertation fails (assert_not_equals).")));
if (assert_equals_base(fcinfo))
ereport(ERROR,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("%s", message),
errdetail("Plunit.assertation fails (assert_not_equals).")));
PG_RETURN_VOID();
}
Datum
plunit_assert_not_equals_range(PG_FUNCTION_ARGS)
{
return plunit_assert_not_equals_range_message(fcinfo);
}
Datum
plunit_assert_not_equals_range_message(PG_FUNCTION_ARGS)
{
char *message = assert_get_message(fcinfo, 4, "plunit.assert_not_equal exception");
/* skip all tests for NULL value */
if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
ereport(ERROR,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("%s", message),
errdetail("Plunit.assertation fails (assert_not_equals).")));
if (assert_equals_range_base(fcinfo))
ereport(ERROR,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("%s", message),
errdetail("Plunit.assertation fails (assert_not_equals).")));
PG_RETURN_VOID();
}
/****************************************************************
* plunit.fail
* plunit.fail_message
*
* Syntax:
* PROCEDURE fail(message varchar default '');
*
* Purpose:
* Fail can be used to cause a test procedure to fail
* immediately using the supplied message.
*
****************************************************************/
Datum
plunit_fail(PG_FUNCTION_ARGS)
{
return plunit_fail_message(fcinfo);
}
Datum
plunit_fail_message(PG_FUNCTION_ARGS)
{
char *message = assert_get_message(fcinfo, 1, "plunit.assert_fail exception");
ereport(ERROR,
(errcode(ERRCODE_CHECK_VIOLATION),
errmsg("%s", message),
errdetail("Plunit.assertation (assert_fail).")));
PG_RETURN_VOID();
}
|