|
| 1 | +/* |
| 2 | +* Copyright (c) 2022, Masatake YAMATO |
| 3 | +* |
| 4 | +* This source code is released into the public domain. |
| 5 | +* |
| 6 | +* Testing tagsFindPseudoTag() API function |
| 7 | +*/ |
| 8 | + |
| 9 | +#include "readtags.h" |
| 10 | + |
| 11 | +#include <stdio.h> |
| 12 | +#include <stdlib.h> |
| 13 | +#include <string.h> |
| 14 | +#include <unistd.h> |
| 15 | + |
| 16 | + |
| 17 | +struct expectation { |
| 18 | + char *name; |
| 19 | + char *file; |
| 20 | + char *pattern; |
| 21 | +}; |
| 22 | + |
| 23 | +#define COUNT(x) (sizeof(x)/sizeof(x[0])) |
| 24 | + |
| 25 | +static int |
| 26 | +check_finding0 (tagFile *t, struct expectation *expectations, int count, int match) |
| 27 | +{ |
| 28 | + tagEntry e; |
| 29 | + struct expectation *x; |
| 30 | + int err; |
| 31 | + |
| 32 | + for (int i = 0; i < count; i++) |
| 33 | + { |
| 34 | + x = expectations + i; |
| 35 | + fprintf (stderr, "[%d/%d] finding ptags in %s matching...", i + 1, count, |
| 36 | + match == TAG_FULLMATCH? "full": "partial"); |
| 37 | + if (tagsFindPseudoTag (t, &e, x->name, match) != TagSuccess) |
| 38 | + { |
| 39 | + if ((err = tagsGetErrno (t))) |
| 40 | + fprintf (stderr, "error: %d\n", err); |
| 41 | + else |
| 42 | + fprintf (stderr, "cannot find: %s\n", x->name); |
| 43 | + return 1; |
| 44 | + } |
| 45 | + fprintf (stderr, "found\n"); |
| 46 | + |
| 47 | + fprintf (stderr, "checking name field..."); |
| 48 | + if (match == TAG_FULLMATCH) |
| 49 | + { |
| 50 | + if (!(e.name && strcmp (x->name, e.name) == 0)) |
| 51 | + { |
| 52 | + fprintf (stderr, "unexpected: %s\n", e.name? e.name: "<NULL>"); |
| 53 | + return 1; |
| 54 | + } |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + if (!(e.name && strncmp (x->name, e.name, strlen(x->name)) == 0)) |
| 59 | + { |
| 60 | + fprintf (stderr, "unexpected: strncmp(%s, %s)\n", |
| 61 | + x->name, e.name? e.name: "<NULL>"); |
| 62 | + return 1; |
| 63 | + } |
| 64 | + } |
| 65 | + fprintf (stderr, "ok\n"); |
| 66 | + |
| 67 | + fprintf (stderr, "checking file field..."); |
| 68 | + if (!(e.file && strcmp (x->file, e.file) == 0)) |
| 69 | + { |
| 70 | + fprintf (stderr, "unexpected: %s\n", e.file? e.file: "<NULL>"); |
| 71 | + return 1; |
| 72 | + } |
| 73 | + fprintf (stderr, "ok\n"); |
| 74 | + |
| 75 | + fprintf (stderr, "checking pattern field..."); |
| 76 | + if (!(e.address.pattern && strcmp (x->pattern, e.address.pattern) == 0)) |
| 77 | + { |
| 78 | + fprintf (stderr, "unexpected: %s\n", |
| 79 | + e.address.pattern? e.address.pattern: "<NULL>"); |
| 80 | + return 1; |
| 81 | + } |
| 82 | + fprintf (stderr, "ok\n"); |
| 83 | + } |
| 84 | + |
| 85 | + return 0; |
| 86 | +} |
| 87 | + |
| 88 | +static int |
| 89 | +check_finding(const char *tags, struct expectation *expectations, int count, int match) |
| 90 | +{ |
| 91 | + tagFile *t; |
| 92 | + tagFileInfo info; |
| 93 | + |
| 94 | + fprintf (stderr, "opening %s...", tags); |
| 95 | + t = tagsOpen (tags, &info); |
| 96 | + if (!t) |
| 97 | + { |
| 98 | + fprintf (stderr, "unexpected result (t: %p, opened: %d, error_number: %d)\n", |
| 99 | + t, info.status.opened, info.status.error_number); |
| 100 | + return 1; |
| 101 | + } |
| 102 | + fprintf (stderr, "ok\n"); |
| 103 | + |
| 104 | + if (check_finding0 (t, expectations, count, match) != 0) |
| 105 | + return 1; |
| 106 | + |
| 107 | + fprintf (stderr, "closing the tag file..."); |
| 108 | + if (tagsClose (t) != TagSuccess) |
| 109 | + { |
| 110 | + fprintf (stderr, "unexpected result\n"); |
| 111 | + return 1; |
| 112 | + } |
| 113 | + fprintf (stderr, "ok\n"); |
| 114 | + return 0; |
| 115 | +} |
| 116 | + |
| 117 | +int |
| 118 | +main (void) |
| 119 | +{ |
| 120 | + char *srcdir = getenv ("srcdir"); |
| 121 | + if (srcdir) |
| 122 | + { |
| 123 | + if (chdir (srcdir) == -1) |
| 124 | + { |
| 125 | + perror ("chdir"); |
| 126 | + return 99; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /* |
| 131 | + * sorted=yes |
| 132 | + */ |
| 133 | + const char *tags_sorted_yes = "./ptag-sort-yes.tags"; |
| 134 | + struct expectation exp_sorted_yes [] = { |
| 135 | + { "!_JSON_OUTPUT_VERSION", "0.0", "/in development/", }, |
| 136 | + { "!_TAG_FILE_FORMAT", "2", "/extended format; --format=1 will not append ;\" to lines/", }, |
| 137 | + { "!_TAG_FILE_SORTED", "1", "/0=unsorted, 1=sorted, 2=foldcase/", }, |
| 138 | + { "!_TAG_KIND_DESCRIPTION!C", "D,macroparam", "/parameters inside macro definitions/", }, |
| 139 | + { "!_TAG_KIND_DESCRIPTION!EmacsLisp", "C,custom", "/customizable variables/", }, |
| 140 | + { "!_TAG_OUTPUT_FILESEP", "slash", "/slash or backslash/", }, |
| 141 | + { "!_TAG_OUTPUT_MODE", "u-ctags", "/u-ctags or e-ctags/", }, |
| 142 | + { "!_TAG_PATTERN_LENGTH_LIMIT", "96", "/0 for no limit/", }, |
| 143 | + { "!_TAG_PROGRAM_AUTHOR", "Universal Ctags Team", "//", }, |
| 144 | + { "!_TAG_PROGRAM_NAME", "Universal Ctags", "/Derived from Exuberant Ctags/", }, |
| 145 | + { "!_TAG_PROGRAM_URL", "https://ctags.io/", "/official site/", }, |
| 146 | + { "!_TAG_PROGRAM_VERSION", "0.0.0", "/9b73623f/", }, |
| 147 | + }; |
| 148 | + |
| 149 | + if (check_finding (tags_sorted_yes, exp_sorted_yes, COUNT(exp_sorted_yes), |
| 150 | + TAG_FULLMATCH) != 0) |
| 151 | + return 1; |
| 152 | + |
| 153 | + struct expectation exp_sorted_yes_partial [] = { |
| 154 | + { "!_JSON", "0.0", "/in development/", }, |
| 155 | + { "!_TAG_FILE", "2", "/extended format; --format=1 will not append ;\" to lines/", }, |
| 156 | + { "!_TAG_KIND_DESCRIPTION", "D,macroparam", "/parameters inside macro definitions/", }, |
| 157 | + { "!_TAG_OUTPUT_", "slash", "/slash or backslash/", }, |
| 158 | + { "!_TAG_PATT", "96", "/0 for no limit/", }, |
| 159 | + { "!_TAG_PROGRAM_AUTHOR", "Universal Ctags Team", "//", }, |
| 160 | + { "!_TAG_PROGRAM_N", "Universal Ctags", "/Derived from Exuberant Ctags/", }, |
| 161 | + }; |
| 162 | + |
| 163 | + if (check_finding (tags_sorted_yes, exp_sorted_yes_partial, COUNT(exp_sorted_yes_partial), |
| 164 | + TAG_PARTIALMATCH) != 0) |
| 165 | + return 1; |
| 166 | + |
| 167 | + /* |
| 168 | + * sorted=no |
| 169 | + */ |
| 170 | + const char *tags_sorted_no = "./ptag-sort-no.tags"; |
| 171 | + struct expectation exp_sorted_no [] = { |
| 172 | + { "!_JSON_OUTPUT_VERSION", "0.0", "/in development/", }, |
| 173 | + { "!_TAG_FILE_FORMAT", "2", "/extended format; --format=1 will not append ;\" to lines/", }, |
| 174 | + { "!_TAG_FILE_SORTED", "0", "/0=unsorted, 1=sorted, 2=foldcase/", }, |
| 175 | + { "!_TAG_PROGRAM_AUTHOR", "Universal Ctags Team", "//", }, |
| 176 | + { "!_TAG_PROGRAM_NAME", "Universal Ctags", "/Derived from Exuberant Ctags/", }, |
| 177 | + { "!_TAG_PROGRAM_URL", "https://ctags.io/", "/official site/", }, |
| 178 | + { "!_TAG_PROGRAM_VERSION", "0.0.0", "/9b73623f/", }, |
| 179 | + { "!_TAG_OUTPUT_MODE", "u-ctags", "/u-ctags or e-ctags/", }, |
| 180 | + { "!_TAG_OUTPUT_FILESEP", "slash", "/slash or backslash/", }, |
| 181 | + { "!_TAG_PATTERN_LENGTH_LIMIT", "96", "/0 for no limit/", }, |
| 182 | + { "!_TAG_KIND_DESCRIPTION!C", "d,macro", "/macro definitions/", }, |
| 183 | + { "!_TAG_KIND_DESCRIPTION!EmacsLisp", "u,unknown", "/unknown type of definitions/", }, |
| 184 | + }; |
| 185 | + |
| 186 | + if (check_finding (tags_sorted_no, exp_sorted_no, COUNT(exp_sorted_no), TAG_FULLMATCH) != 0) |
| 187 | + return 1; |
| 188 | + |
| 189 | + struct expectation exp_sorted_no_partial [] = { |
| 190 | + { "!_JSON", "0.0", "/in development/", }, |
| 191 | + { "!_TAG_FILE", "2", "/extended format; --format=1 will not append ;\" to lines/", }, |
| 192 | + { "!_TAG_KIND_DESCRIPTION", "d,macro", "/macro definitions/", }, |
| 193 | + { "!_TAG_OUTPUT_", "u-ctags", "/u-ctags or e-ctags/", }, |
| 194 | + { "!_TAG_PATT", "96", "/0 for no limit/", }, |
| 195 | + { "!_TAG_PROGRAM_AUTHOR", "Universal Ctags Team", "//", }, |
| 196 | + { "!_TAG_PROGRAM_N", "Universal Ctags", "/Derived from Exuberant Ctags/", }, |
| 197 | + }; |
| 198 | + |
| 199 | + if (check_finding (tags_sorted_no, exp_sorted_no_partial, COUNT(exp_sorted_no_partial), |
| 200 | + TAG_PARTIALMATCH) != 0) |
| 201 | + return 1; |
| 202 | + |
| 203 | + /* No entry */ |
| 204 | + const char *tags = "./ptag-sort-yes.tags"; |
| 205 | + tagFileInfo info; |
| 206 | + |
| 207 | + fprintf (stderr, "opening %s...", tags); |
| 208 | + tagFile *t = tagsOpen (tags, &info); |
| 209 | + if (!t) |
| 210 | + { |
| 211 | + fprintf (stderr, "unexpected result (t: %p, opened: %d, error_number: %d)\n", |
| 212 | + t, info.status.opened, info.status.error_number); |
| 213 | + return 1; |
| 214 | + } |
| 215 | + fprintf (stderr, "ok\n"); |
| 216 | + |
| 217 | + struct non_existing_testcase { |
| 218 | + const char *matchstr; |
| 219 | + int match; |
| 220 | + } ne_tcase [2] = { |
| 221 | + { |
| 222 | + .matchstr = "full", |
| 223 | + .match = TAG_FULLMATCH, |
| 224 | + }, |
| 225 | + { |
| 226 | + .matchstr = "partial", |
| 227 | + .match = TAG_PARTIALMATCH, |
| 228 | + } |
| 229 | + }; |
| 230 | + for (int i = 0; i < COUNT(ne_tcase); i++) |
| 231 | + { |
| 232 | + fprintf (stderr, "try to find non-existing tag in %s matching...", |
| 233 | + ne_tcase[i].matchstr); |
| 234 | + tagResult r = tagsFindPseudoTag (t, NULL, "!NO_SUCH_PTAG", |
| 235 | + ne_tcase[i].match); |
| 236 | + int err = tagsGetErrno (t); |
| 237 | + if (r == TagSuccess) |
| 238 | + { |
| 239 | + fprintf (stderr, "found one unexpectedly\n"); |
| 240 | + return 1; |
| 241 | + } |
| 242 | + else if (err != 0) |
| 243 | + { |
| 244 | + fprintf (stderr, "unexpected errno: %d\n", err); |
| 245 | + return 1; |
| 246 | + } |
| 247 | + fprintf (stderr, "ok\n"); |
| 248 | + } |
| 249 | + |
| 250 | + fprintf (stderr, "closing the tag file..."); |
| 251 | + if (tagsClose (t) != TagSuccess) |
| 252 | + { |
| 253 | + fprintf (stderr, "unexpected result\n"); |
| 254 | + return 1; |
| 255 | + } |
| 256 | + fprintf (stderr, "ok\n"); |
| 257 | + |
| 258 | + return 0; |
| 259 | +} |
0 commit comments