diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-08-18 17:29:46 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-08-18 17:29:46 -0300 |
| commit | 9405472565cb4b0cb0c339d65babdef4d4cb7abd (patch) | |
| tree | 386bcb066803310f7481b60e119748c85b13d618 /ltests.c | |
| parent | 45948e7e55c753cf0e370b251ac1d4e7f3aabedd (diff) | |
| download | lua-9405472565cb4b0cb0c339d65babdef4d4cb7abd.tar.gz lua-9405472565cb4b0cb0c339d65babdef4d4cb7abd.tar.bz2 lua-9405472565cb4b0cb0c339d65babdef4d4cb7abd.zip | |
Improvement in warn-mode '@store' (for testing)
When using warn-mode '@store', from the test library, the tests ensure
not only that the expected warnings were issued, but also that there was
no extra warnings.
Diffstat (limited to 'ltests.c')
| -rw-r--r-- | ltests.c | 28 |
1 files changed, 20 insertions, 8 deletions
| @@ -62,8 +62,10 @@ static void pushobject (lua_State *L, const TValue *o) { | |||
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | 64 | ||
| 65 | static void badexit (const char *fmt, const char *s) { | 65 | static void badexit (const char *fmt, const char *s1, const char *s2) { |
| 66 | fprintf(stderr, fmt, s); | 66 | fprintf(stderr, fmt, s1); |
| 67 | if (s2) | ||
| 68 | fprintf(stderr, "extra info: %s\n", s2); | ||
| 67 | /* avoid assertion failures when exiting */ | 69 | /* avoid assertion failures when exiting */ |
| 68 | l_memcontrol.numblocks = l_memcontrol.total = 0; | 70 | l_memcontrol.numblocks = l_memcontrol.total = 0; |
| 69 | exit(EXIT_FAILURE); | 71 | exit(EXIT_FAILURE); |
| @@ -72,7 +74,7 @@ static void badexit (const char *fmt, const char *s) { | |||
| 72 | 74 | ||
| 73 | static int tpanic (lua_State *L) { | 75 | static int tpanic (lua_State *L) { |
| 74 | return (badexit("PANIC: unprotected error in call to Lua API (%s)\n", | 76 | return (badexit("PANIC: unprotected error in call to Lua API (%s)\n", |
| 75 | lua_tostring(L, -1)), | 77 | lua_tostring(L, -1), NULL), |
| 76 | 0); /* do not return to Lua */ | 78 | 0); /* do not return to Lua */ |
| 77 | } | 79 | } |
| 78 | 80 | ||
| @@ -88,13 +90,14 @@ static int tpanic (lua_State *L) { | |||
| 88 | ** - 2.store: all warnings go to the global '_WARN'; | 90 | ** - 2.store: all warnings go to the global '_WARN'; |
| 89 | */ | 91 | */ |
| 90 | static void warnf (void *ud, const char *msg, int tocont) { | 92 | static void warnf (void *ud, const char *msg, int tocont) { |
| 93 | lua_State *L = cast(lua_State *, ud); | ||
| 91 | static char buff[200] = ""; /* should be enough for tests... */ | 94 | static char buff[200] = ""; /* should be enough for tests... */ |
| 92 | static int onoff = 1; | 95 | static int onoff = 1; |
| 93 | static int mode = 0; /* start in normal mode */ | 96 | static int mode = 0; /* start in normal mode */ |
| 94 | static int lasttocont = 0; | 97 | static int lasttocont = 0; |
| 95 | if (!lasttocont && !tocont && *msg == '@') { /* control message? */ | 98 | if (!lasttocont && !tocont && *msg == '@') { /* control message? */ |
| 96 | if (buff[0] != '\0') | 99 | if (buff[0] != '\0') |
| 97 | badexit("Control warning during warning: %s\naborting...\n", msg); | 100 | badexit("Control warning during warning: %s\naborting...\n", msg, buff); |
| 98 | if (strcmp(msg, "@off") == 0) | 101 | if (strcmp(msg, "@off") == 0) |
| 99 | onoff = 0; | 102 | onoff = 0; |
| 100 | else if (strcmp(msg, "@on") == 0) | 103 | else if (strcmp(msg, "@on") == 0) |
| @@ -106,18 +109,28 @@ static void warnf (void *ud, const char *msg, int tocont) { | |||
| 106 | else if (strcmp(msg, "@store") == 0) | 109 | else if (strcmp(msg, "@store") == 0) |
| 107 | mode = 2; | 110 | mode = 2; |
| 108 | else | 111 | else |
| 109 | badexit("Invalid control warning in test mode: %s\naborting...\n", msg); | 112 | badexit("Invalid control warning in test mode: %s\naborting...\n", |
| 113 | msg, NULL); | ||
| 110 | return; | 114 | return; |
| 111 | } | 115 | } |
| 112 | lasttocont = tocont; | 116 | lasttocont = tocont; |
| 113 | if (strlen(msg) >= sizeof(buff) - strlen(buff)) | 117 | if (strlen(msg) >= sizeof(buff) - strlen(buff)) |
| 114 | badexit("%s", "warnf-buffer overflow"); | 118 | badexit("warnf-buffer overflow (%s)\n", msg, buff); |
| 115 | strcat(buff, msg); /* add new message to current warning */ | 119 | strcat(buff, msg); /* add new message to current warning */ |
| 116 | if (!tocont) { /* message finished? */ | 120 | if (!tocont) { /* message finished? */ |
| 121 | lua_unlock(L); | ||
| 122 | if (lua_getglobal(L, "_WARN") == LUA_TNIL) | ||
| 123 | lua_pop(L, 1); /* ok, no previous unexpected warning */ | ||
| 124 | else { | ||
| 125 | badexit("Unhandled warning in store mode: %s\naborting...\n", | ||
| 126 | lua_tostring(L, -1), buff); | ||
| 127 | } | ||
| 128 | lua_lock(L); | ||
| 117 | switch (mode) { | 129 | switch (mode) { |
| 118 | case 0: { /* normal */ | 130 | case 0: { /* normal */ |
| 119 | if (buff[0] != '#' && onoff) /* unexpected warning? */ | 131 | if (buff[0] != '#' && onoff) /* unexpected warning? */ |
| 120 | badexit("Unexpected warning in test mode: %s\naborting...\n", buff); | 132 | badexit("Unexpected warning in test mode: %s\naborting...\n", |
| 133 | buff, NULL); | ||
| 121 | /* else */ /* FALLTHROUGH */ | 134 | /* else */ /* FALLTHROUGH */ |
| 122 | } | 135 | } |
| 123 | case 1: { /* allow */ | 136 | case 1: { /* allow */ |
| @@ -126,7 +139,6 @@ static void warnf (void *ud, const char *msg, int tocont) { | |||
| 126 | break; | 139 | break; |
| 127 | } | 140 | } |
| 128 | case 2: { /* store */ | 141 | case 2: { /* store */ |
| 129 | lua_State *L = cast(lua_State *, ud); | ||
| 130 | lua_unlock(L); | 142 | lua_unlock(L); |
| 131 | lua_pushstring(L, buff); | 143 | lua_pushstring(L, buff); |
| 132 | lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */ | 144 | lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */ |
