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
|
#include <pg_query.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "scan_tests.c"
#include "protobuf/pg_query.pb-c.h"
int main() {
size_t i;
size_t j;
bool ret_code = 0;
PgQuery__ScanResult *scan_result;
PgQuery__ScanToken *scan_token;
const ProtobufCEnumValue *token_kind;
const ProtobufCEnumValue *keyword_kind;
PgQueryScanResult result;
// tests contains pairs of strings
assert(testsCount % 2 == 0);
for (i = 0; i < testsCount * 2; i += 2) {
char buffer[1024];
buffer[0] = '\0';
result = pg_query_scan(tests[i]);
if (result.error) {
ret_code = -1;
printf("%s\n", result.error->message);
} else {
scan_result = pg_query__scan_result__unpack(NULL, result.pbuf.len, (void*) result.pbuf.data);
for (j = 0; j < scan_result->n_tokens; j++) {
char buffer2[1024];
scan_token = scan_result->tokens[j];
token_kind = protobuf_c_enum_descriptor_get_value(&pg_query__token__descriptor, scan_token->token);
keyword_kind = protobuf_c_enum_descriptor_get_value(&pg_query__keyword_kind__descriptor, scan_token->keyword_kind);
if (token_kind == NULL) {
ret_code = -1;
printf("INVALID result for \"%s\": scan_result token %zu token_kind == NULL\n", tests[i], j);
break;
}
sprintf(buffer2, "%.*s = %s, %s\n", scan_token->end - scan_token->start, &(tests[i][scan_token->start]), token_kind->name, keyword_kind->name);
strcat(buffer, buffer2);
}
pg_query__scan_result__free_unpacked(scan_result, NULL);
if (strcmp(buffer, tests[i + 1]) == 0) {
printf(".");
} else {
ret_code = -1;
printf("INVALID result for \"%s\"\nexpected:\n%s\nactual:\n%s\n", tests[i], tests[i + 1], buffer);
}
}
pg_query_free_scan_result(result);
}
printf("\n");
pg_query_exit();
return ret_code;
}
|