From 5cd507d28dc08fb55aa2ab5b05af8372661e7730 Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Sun, 9 Jun 2019 16:56:10 +0100 Subject: [PATCH] drop magic type The DT_MAGIC type is unnecessary; it's an enumeration of four strings. That's better handled by the new DT_ENUM type. --- Makefile.autosetup | 2 +- config/lib.h | 2 - config/magic.c | 208 ----------- config/magic.h | 50 --- config/types.h | 1 - contrib/lua/test_lua-api_spec.lua | 4 - doc/makedoc.c | 14 - init.c | 1 - mailbox.h | 18 + maildir/maildir_private.h | 1 + mutt_lua.c | 6 +- mx.h | 3 +- notmuch/notmuch_private.h | 1 + po/POTFILES.in | 1 - test/Makefile.autosetup | 1 - test/config/dump.c | 46 ++- test/config/magic.c | 596 ------------------------------ test/config/magic.h | 30 -- test/main.c | 1 - 19 files changed, 58 insertions(+), 928 deletions(-) delete mode 100644 config/magic.c delete mode 100644 config/magic.h delete mode 100644 test/config/magic.c delete mode 100644 test/config/magic.h diff --git a/Makefile.autosetup b/Makefile.autosetup index 7acf73cf3..3e97becf9 100644 --- a/Makefile.autosetup +++ b/Makefile.autosetup @@ -228,7 +228,7 @@ ALLOBJS+= $(PGPEWRAPOBJS) # libconfig LIBCONFIG= libconfig.a LIBCONFIGOBJS= config/address.o config/bool.o config/dump.o config/enum.o \ - config/long.o config/magic.o config/mbtable.o config/number.o \ + config/long.o config/mbtable.o config/number.o \ config/quad.o config/regex.o config/set.o \ config/slist.o config/sort.o config/string.o diff --git a/config/lib.h b/config/lib.h index caf96a8e4..dba8ed7ce 100644 --- a/config/lib.h +++ b/config/lib.h @@ -32,7 +32,6 @@ * | config/dump.c | @subpage config_dump | * | config/enum.c | @subpage config_enum | * | config/long.c | @subpage config_long | - * | config/magic.c | @subpage config_magic | * | config/mbtable.c | @subpage config_mbtable | * | config/number.c | @subpage config_number | * | config/quad.c | @subpage config_quad | @@ -52,7 +51,6 @@ #include "enum.h" #include "inheritance.h" #include "long.h" -#include "magic.h" #include "mbtable.h" #include "number.h" #include "quad.h" diff --git a/config/magic.c b/config/magic.c deleted file mode 100644 index a07302349..000000000 --- a/config/magic.c +++ /dev/null @@ -1,208 +0,0 @@ -/** - * @file - * Type representing a mailbox - * - * @authors - * Copyright (C) 2017-2018 Richard Russon - * - * @copyright - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 2 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -/** - * @page config_magic Type: Mailbox types - * - * Type representing a mailbox. - */ - -#include "config.h" -#include -#include -#include -#include "mutt/mutt.h" -#include "set.h" -#include "types.h" - -/** - * MagicValues - Valid strings for mailbox types - * - * These strings are case-insensitive. - */ -const char *MagicValues[] = { - NULL, "mbox", "MMDF", "MH", "Maildir", NULL, -}; - -/** - * magic_string_set - Set a Mailbox Magic by string - Implements ::cst_string_set() - */ -static int magic_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, - const char *value, struct Buffer *err) -{ - if (!cs || !cdef) - return CSR_ERR_CODE; /* LCOV_EXCL_LINE */ - - if (!value || !value[0]) - { - mutt_buffer_printf(err, "Option %s may not be empty", cdef->name); - return CSR_ERR_INVALID | CSR_INV_TYPE; - } - - int num = -1; - for (size_t i = 1; MagicValues[i]; i++) - { - if (mutt_str_strcasecmp(MagicValues[i], value) == 0) - { - num = i; - break; - } - } - - if (num < 1) - { - mutt_buffer_printf(err, "Invalid magic value: %s", value); - return CSR_ERR_INVALID | CSR_INV_TYPE; - } - - if (var) - { - if (num == (*(short *) var)) - return CSR_SUCCESS | CSR_SUC_NO_CHANGE; - - if (cdef->validator) - { - int rc = cdef->validator(cs, cdef, (intptr_t) num, err); - - if (CSR_RESULT(rc) != CSR_SUCCESS) - return rc | CSR_INV_VALIDATOR; - } - - *(short *) var = num; - } - else - { - cdef->initial = num; - } - - return CSR_SUCCESS; -} - -/** - * magic_string_get - Get a Mailbox Magic as a string - Implements ::cst_string_get() - */ -static int magic_string_get(const struct ConfigSet *cs, void *var, - const struct ConfigDef *cdef, struct Buffer *result) -{ - if (!cs || !cdef) - return CSR_ERR_CODE; /* LCOV_EXCL_LINE */ - - unsigned int value; - - if (var) - value = *(short *) var; - else - value = (int) cdef->initial; - - if ((value < 1) || (value >= (mutt_array_size(MagicValues) - 1))) - { - mutt_debug(LL_DEBUG1, "Variable has an invalid value: %d\n", value); - return CSR_ERR_INVALID | CSR_INV_TYPE; - } - - mutt_buffer_addstr(result, MagicValues[value]); - return CSR_SUCCESS; -} - -/** - * magic_native_set - Set a Mailbox Magic config item by int - Implements ::cst_native_set() - */ -static int magic_native_set(const struct ConfigSet *cs, void *var, - const struct ConfigDef *cdef, intptr_t value, struct Buffer *err) -{ - if (!cs || !var || !cdef) - return CSR_ERR_CODE; /* LCOV_EXCL_LINE */ - - if ((value < 1) || (value >= (mutt_array_size(MagicValues) - 1))) - { - mutt_buffer_printf(err, "Invalid magic value: %ld", value); - return CSR_ERR_INVALID | CSR_INV_TYPE; - } - - if (value == (*(short *) var)) - return CSR_SUCCESS | CSR_SUC_NO_CHANGE; - - if (cdef->validator) - { - int rc = cdef->validator(cs, cdef, value, err); - - if (CSR_RESULT(rc) != CSR_SUCCESS) - return rc | CSR_INV_VALIDATOR; - } - - *(short *) var = value; - return CSR_SUCCESS; -} - -/** - * magic_native_get - Get an int from a Mailbox Magic config item - Implements ::cst_native_get() - */ -static intptr_t magic_native_get(const struct ConfigSet *cs, void *var, - const struct ConfigDef *cdef, struct Buffer *err) -{ - if (!cs || !var || !cdef) - return INT_MIN; /* LCOV_EXCL_LINE */ - - return *(short *) var; -} - -/** - * magic_reset - Reset a Mailbox Magic to its initial value - Implements ::cst_reset() - */ -static int magic_reset(const struct ConfigSet *cs, void *var, - const struct ConfigDef *cdef, struct Buffer *err) -{ - if (!cs || !var || !cdef) - return CSR_ERR_CODE; /* LCOV_EXCL_LINE */ - - if (cdef->initial == (*(short *) var)) - return CSR_SUCCESS | CSR_SUC_NO_CHANGE; - - if (cdef->validator) - { - int rc = cdef->validator(cs, cdef, cdef->initial, err); - - if (CSR_RESULT(rc) != CSR_SUCCESS) - return rc | CSR_INV_VALIDATOR; - } - - *(short *) var = cdef->initial; - return CSR_SUCCESS; -} - -/** - * magic_init - Register the Mailbox Magic config type - * @param cs Config items - */ -void magic_init(struct ConfigSet *cs) -{ - const struct ConfigSetType cst_magic = { - "magic", - magic_string_set, - magic_string_get, - magic_native_set, - magic_native_get, - magic_reset, - NULL, - }; - cs_register_type(cs, DT_MAGIC, &cst_magic); -} diff --git a/config/magic.h b/config/magic.h deleted file mode 100644 index c5080dd0a..000000000 --- a/config/magic.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * @file - * Type representing a mailbox - * - * @authors - * Copyright (C) 2017-2018 Richard Russon - * - * @copyright - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 2 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -#ifndef MUTT_CONFIG_MAGIC_H -#define MUTT_CONFIG_MAGIC_H - -struct ConfigSet; - -extern const char *MagicValues[]; - -/** - * enum MailboxType - Supported mailbox formats - */ -enum MailboxType -{ - MUTT_MAILBOX_ERROR = -1, ///< Error occurred examining mailbox - MUTT_UNKNOWN = 0, ///< Mailbox wasn't recognised - MUTT_MBOX, ///< 'mbox' Mailbox type - MUTT_MMDF, ///< 'mmdf' Mailbox type - MUTT_MH, ///< 'MH' Mailbox type - MUTT_MAILDIR, ///< 'Maildir' Mailbox type - MUTT_NNTP, ///< 'NNTP' (Usenet) Mailbox type - MUTT_IMAP, ///< 'IMAP' Mailbox type - MUTT_NOTMUCH, ///< 'Notmuch' (virtual) Mailbox type - MUTT_POP, ///< 'POP3' Mailbox type - MUTT_COMPRESSED, ///< Compressed file Mailbox type -}; - -void magic_init(struct ConfigSet *cs); - -#endif /* MUTT_CONFIG_MAGIC_H */ diff --git a/config/types.h b/config/types.h index 65ac19bd5..96606718e 100644 --- a/config/types.h +++ b/config/types.h @@ -31,7 +31,6 @@ #define DT_ENUM 4 ///< an enumeration #define DT_HCACHE 5 ///< header cache backend #define DT_LONG 6 ///< a number (long) -#define DT_MAGIC 7 ///< mailbox type #define DT_MBTABLE 8 ///< multibyte char table #define DT_NUMBER 9 ///< a number #define DT_QUAD 11 ///< quad-option (no/yes/ask-no/ask-yes) diff --git a/contrib/lua/test_lua-api_spec.lua b/contrib/lua/test_lua-api_spec.lua index dc8df5d68..7eb2bd9b5 100644 --- a/contrib/lua/test_lua-api_spec.lua +++ b/contrib/lua/test_lua-api_spec.lua @@ -64,10 +64,6 @@ describe('lua API', function() test_config_type("alias_file", "contrib/lua/test_lua-api_runner.neomuttrc", "/dev/null") end) - it('works with DT_MAGIC', function() - test_config_type("mbox_type", "mbox", "Maildir") - end) - it('works with DT_SORT', function() test_config_type("sort", "from", "date") end) diff --git a/doc/makedoc.c b/doc/makedoc.c index e8c01d937..8920082e6 100644 --- a/doc/makedoc.c +++ b/doc/makedoc.c @@ -955,7 +955,6 @@ enum DataType DT_QUAD, DT_SORT, DT_REGEX, - DT_MAGIC, DT_SYNONYM, DT_ADDRESS, DT_MBTABLE, @@ -976,7 +975,6 @@ struct VariableTypes { "DT_QUAD", "quadoption" }, { "DT_SORT", "sort order" }, { "DT_REGEX", "regular expression" }, - { "DT_MAGIC", "folder magic" }, { "DT_SYNONYM", NULL }, { "DT_ADDRESS", "e-mail address" }, { "DT_MBTABLE", "string" }, @@ -1036,18 +1034,6 @@ static void pretty_default(char *t, size_t l, const char *s, int type) *t = tolower((unsigned char) *t); break; } - case DT_MAGIC: - { - /* heuristic! */ - if (strncmp(s, "MUTT_", 5) != 0) - fprintf(stderr, "WARNING: expected prefix of MUTT_ for type DT_MAGIC " - "instead of %s\n", - s); - strncpy(t, s + 5, l); - for (; *t; t++) - *t = tolower((unsigned char) *t); - break; - } case DT_STRING: case DT_REGEX: case DT_ADDRESS: diff --git a/init.c b/init.c index 92824f583..9de0c77d1 100644 --- a/init.c +++ b/init.c @@ -3817,7 +3817,6 @@ struct ConfigSet *init_config(size_t size) bool_init(cs); enum_init(cs); long_init(cs); - magic_init(cs); mbtable_init(cs); number_init(cs); quad_init(cs); diff --git a/mailbox.h b/mailbox.h index d88b8e4ff..1644b00c1 100644 --- a/mailbox.h +++ b/mailbox.h @@ -46,6 +46,24 @@ extern bool C_MaildirCheckCur; #define MB_NORMAL 0 #define MB_HIDDEN 1 +/** + * enum MailboxType - Supported mailbox formats + */ +enum MailboxType +{ + MUTT_MAILBOX_ERROR = -1, ///< Error occurred examining mailbox + MUTT_UNKNOWN = 0, ///< Mailbox wasn't recognised + MUTT_MBOX, ///< 'mbox' Mailbox type + MUTT_MMDF, ///< 'mmdf' Mailbox type + MUTT_MH, ///< 'MH' Mailbox type + MUTT_MAILDIR, ///< 'Maildir' Mailbox type + MUTT_NNTP, ///< 'NNTP' (Usenet) Mailbox type + MUTT_IMAP, ///< 'IMAP' Mailbox type + MUTT_NOTMUCH, ///< 'Notmuch' (virtual) Mailbox type + MUTT_POP, ///< 'POP3' Mailbox type + MUTT_COMPRESSED, ///< Compressed file Mailbox type +}; + /** * enum MailboxNotification - Notifications about changes to a Mailbox */ diff --git a/maildir/maildir_private.h b/maildir/maildir_private.h index 585a11dad..f42a0926a 100644 --- a/maildir/maildir_private.h +++ b/maildir/maildir_private.h @@ -29,6 +29,7 @@ #include #include #include "config/lib.h" +#include "mailbox.h" struct Account; struct Buffer; diff --git a/mutt_lua.c b/mutt_lua.c index 0db640678..0591394c0 100644 --- a/mutt_lua.c +++ b/mutt_lua.c @@ -163,11 +163,12 @@ static int lua_mutt_set(lua_State *l) switch (DTYPE(cdef->type)) { case DT_ADDRESS: + case DT_ENUM: case DT_MBTABLE: case DT_REGEX: + case DT_SLIST: case DT_SORT: case DT_STRING: - case DT_MAGIC: { const char *value = lua_tostring(l, -1); int rv = cs_he_string_set(Config, he, value, err); @@ -239,9 +240,10 @@ static int lua_mutt_get(lua_State *l) switch (DTYPE(cdef->type)) { case DT_ADDRESS: - case DT_MAGIC: + case DT_ENUM: case DT_MBTABLE: case DT_REGEX: + case DT_SLIST: case DT_SORT: case DT_STRING: { diff --git a/mx.h b/mx.h index 72caad97a..04f4d31e3 100644 --- a/mx.h +++ b/mx.h @@ -30,6 +30,7 @@ #include #include "config/lib.h" #include "hcache/hcache.h" +#include "mailbox.h" struct Email; struct Context; @@ -43,7 +44,7 @@ extern unsigned char C_MboxType; extern unsigned char C_Move; extern char * C_Trash; -struct EnumDef MagicDef; +extern struct EnumDef MagicDef; /* flags for mutt_open_mailbox() */ typedef uint8_t OpenMailboxFlags; ///< Flags for mutt_open_mailbox(), e.g. #MUTT_NOSORT diff --git a/notmuch/notmuch_private.h b/notmuch/notmuch_private.h index ae20cdc92..ced303be7 100644 --- a/notmuch/notmuch_private.h +++ b/notmuch/notmuch_private.h @@ -25,6 +25,7 @@ #include #include "config/lib.h" #include "progress.h" +#include "mailbox.h" #ifndef MUTT_NOTMUCH_NOTMUCH_PRIVATE_H #define MUTT_NOTMUCH_NOTMUCH_PRIVATE_H diff --git a/po/POTFILES.in b/po/POTFILES.in index da6c32f6c..46976a6a9 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -17,7 +17,6 @@ config/bool.c config/dump.c config/enum.c config/long.c -config/magic.c config/mbtable.c config/number.c config/quad.c diff --git a/test/Makefile.autosetup b/test/Makefile.autosetup index cbf64e757..1a7e05d22 100644 --- a/test/Makefile.autosetup +++ b/test/Makefile.autosetup @@ -89,7 +89,6 @@ CONFIG_OBJS = test/config/account.o \ test/config/enum.o \ test/config/initial.o \ test/config/long.o \ - test/config/magic.o \ test/config/mbtable.o \ test/config/number.o \ test/config/quad.o \ diff --git a/test/config/dump.c b/test/config/dump.c index ce08cac77..0b7b20063 100644 --- a/test/config/dump.c +++ b/test/config/dump.c @@ -42,22 +42,38 @@ static struct Regex *VarLemon; static short VarMango; static char *VarNectarine; +// clang-format off +static struct Mapping MagicMap[] = { + { "mbox", MUTT_MBOX, }, + { "MMDF", MUTT_MMDF, }, + { "MH", MUTT_MH, }, + { "Maildir", MUTT_MAILDIR, }, + { NULL, 0, }, +}; +// clang-format on + +struct EnumDef MagicDef = { + "mbox_type", + 4, + (struct Mapping *) &MagicMap, +}; + // clang-format off static struct ConfigDef Vars[] = { - { "Apple", DT_BOOL, &VarApple, false, 0, NULL }, - { "Banana", DT_BOOL, &VarBanana, true, 0, NULL }, - { "Cherry", DT_NUMBER, &VarCherry, 0, 0, NULL }, - { "Damson", DT_SYNONYM, NULL, IP "Cherry", 0, NULL }, - { "Elderberry", DT_ADDRESS, &VarElderberry, IP "elderberry@example.com", 0, NULL }, - { "Fig", DT_STRING|DT_COMMAND|DT_NOT_EMPTY, &VarFig, IP "fig", 0, NULL }, - { "Guava", DT_LONG, &VarGuava, 0, 0, NULL }, - { "Hawthorn", DT_MAGIC, &VarHawthorn, 1, 0, NULL }, - { "Ilama", DT_MBTABLE, &VarIlama, 0, 0, NULL }, - { "Jackfruit", DT_STRING|DT_PATH, &VarJackfruit, IP "/etc/passwd", 0, NULL }, - { "Kumquat", DT_QUAD, &VarKumquat, 0, 0, NULL }, - { "Lemon", DT_REGEX, &VarLemon, 0, 0, NULL }, - { "Mango", DT_SORT, &VarMango, 1, 0, NULL }, - { "Nectarine", DT_STRING|DT_SENSITIVE, &VarNectarine, IP "nectarine", 0, NULL }, + { "Apple", DT_BOOL, &VarApple, false, 0, NULL }, + { "Banana", DT_BOOL, &VarBanana, true, 0, NULL }, + { "Cherry", DT_NUMBER, &VarCherry, 0, 0, NULL }, + { "Damson", DT_SYNONYM, NULL, IP "Cherry", 0, NULL }, + { "Elderberry", DT_ADDRESS, &VarElderberry, IP "elderberry@example.com", 0, NULL }, + { "Fig", DT_STRING|DT_COMMAND|DT_NOT_EMPTY, &VarFig, IP "fig", 0, NULL }, + { "Guava", DT_LONG, &VarGuava, 0, 0, NULL }, + { "Hawthorn", DT_ENUM, &VarHawthorn, 1, IP &MagicDef, NULL }, + { "Ilama", DT_MBTABLE, &VarIlama, 0, 0, NULL }, + { "Jackfruit", DT_STRING|DT_PATH, &VarJackfruit, IP "/etc/passwd", 0, NULL }, + { "Kumquat", DT_QUAD, &VarKumquat, 0, 0, NULL }, + { "Lemon", DT_REGEX, &VarLemon, 0, 0, NULL }, + { "Mango", DT_SORT, &VarMango, 1, 0, NULL }, + { "Nectarine", DT_STRING|DT_SENSITIVE, &VarNectarine, IP "nectarine", 0, NULL }, { NULL }, }; // clang-format on @@ -166,8 +182,8 @@ struct ConfigSet *create_sample_data(void) address_init(cs); bool_init(cs); + enum_init(cs); long_init(cs); - magic_init(cs); mbtable_init(cs); number_init(cs); quad_init(cs); diff --git a/test/config/magic.c b/test/config/magic.c deleted file mode 100644 index 52919a865..000000000 --- a/test/config/magic.c +++ /dev/null @@ -1,596 +0,0 @@ -/** - * @file - * Test code for the Magic object - * - * @authors - * Copyright (C) 2017-2018 Richard Russon - * - * @copyright - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 2 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -#define TEST_NO_MAIN -#include "acutest.h" -#include "config.h" -#include -#include -#include -#include -#include "mutt/mutt.h" -#include "config/common.h" -#include "config/lib.h" -#include "account.h" - -static short VarApple; -static short VarBanana; -static short VarCherry; -static short VarDamson; -static short VarElderberry; -static short VarFig; -static short VarGuava; -static short VarHawthorn; -static short VarIlama; -static short VarJackfruit; -static short VarKumquat; -static short VarLemon; -static short VarMango; - -// clang-format off -static struct ConfigDef Vars[] = { - { "Apple", DT_MAGIC, &VarApple, 1, 0, NULL }, /* test_initial_values */ - { "Banana", DT_MAGIC, &VarBanana, 3, 0, NULL }, - { "Cherry", DT_MAGIC, &VarCherry, 1, 0, NULL }, - { "Damson", DT_MAGIC, &VarDamson, 1, 0, NULL }, /* test_string_set */ - { "Elderberry", DT_MAGIC, &VarElderberry, 1, 0, NULL }, /* test_string_get */ - { "Fig", DT_MAGIC, &VarFig, 1, 0, NULL }, /* test_native_set */ - { "Guava", DT_MAGIC, &VarGuava, 1, 0, NULL }, /* test_native_get */ - { "Hawthorn", DT_MAGIC, &VarHawthorn, 1, 0, NULL }, /* test_reset */ - { "Ilama", DT_MAGIC, &VarIlama, 1, 0, validator_fail }, - { "Jackfruit", DT_MAGIC, &VarJackfruit, 1, 0, validator_succeed }, /* test_validator */ - { "Kumquat", DT_MAGIC, &VarKumquat, 1, 0, validator_warn }, - { "Lemon", DT_MAGIC, &VarLemon, 1, 0, validator_fail }, - { "Mango", DT_MAGIC, &VarMango, 1, 0, NULL }, /* test_inherit */ - { NULL }, -}; -// clang-format on - -static bool test_initial_values(struct ConfigSet *cs, struct Buffer *err) -{ - log_line(__func__); - TEST_MSG("Apple = %d\n", VarApple); - TEST_MSG("Banana = %d\n", VarBanana); - - if (!TEST_CHECK(VarApple == 1)) - { - TEST_MSG("Expected: %d\n", 1); - TEST_MSG("Actual : %d\n", VarApple); - } - - if (!TEST_CHECK(VarBanana == 3)) - { - TEST_MSG("Expected: %d\n", 3); - TEST_MSG("Actual : %d\n", VarBanana); - } - - cs_str_string_set(cs, "Apple", "MMDF", err); - cs_str_string_set(cs, "Banana", "Maildir", err); - - struct Buffer value; - mutt_buffer_init(&value); - value.dsize = 256; - value.data = mutt_mem_calloc(1, value.dsize); - mutt_buffer_reset(&value); - - int rc; - - mutt_buffer_reset(&value); - rc = cs_str_initial_get(cs, "Apple", &value); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("%s\n", value.data); - FREE(&value.data); - return false; - } - - if (!TEST_CHECK(mutt_str_strcmp(value.data, "mbox") == 0)) - { - TEST_MSG("Apple's initial value is wrong: '%s'\n", value.data); - FREE(&value.data); - return false; - } - TEST_MSG("Apple = %d\n", VarApple); - TEST_MSG("Apple's initial value is %s\n", value.data); - - mutt_buffer_reset(&value); - rc = cs_str_initial_get(cs, "Banana", &value); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("%s\n", value.data); - FREE(&value.data); - return false; - } - - if (!TEST_CHECK(mutt_str_strcmp(value.data, "MH") == 0)) - { - TEST_MSG("Banana's initial value is wrong: %s\n", value.data); - FREE(&value.data); - return false; - } - TEST_MSG("Banana = %d\n", VarBanana); - TEST_MSG("Banana's initial value is %s\n", NONULL(value.data)); - - mutt_buffer_reset(&value); - rc = cs_str_initial_set(cs, "Cherry", "mmdf", &value); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("%s\n", value.data); - FREE(&value.data); - return false; - } - - mutt_buffer_reset(&value); - rc = cs_str_initial_get(cs, "Cherry", &value); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("%s\n", value.data); - FREE(&value.data); - return false; - } - - TEST_MSG("Cherry = %s\n", MagicValues[VarCherry]); - TEST_MSG("Cherry's initial value is %s\n", value.data); - - FREE(&value.data); - log_line(__func__); - return true; -} - -static bool test_string_set(struct ConfigSet *cs, struct Buffer *err) -{ - log_line(__func__); - - const char *valid[] = { "mbox", "mmdf", "mh", "maildir" }; - const char *invalid[] = { "mbox2", "mm", "", NULL }; - const char *name = "Damson"; - - int rc; - for (unsigned int i = 0; i < mutt_array_size(valid); i++) - { - VarDamson = ((i + 1) % 4) + 1; - - mutt_buffer_reset(err); - rc = cs_str_string_set(cs, name, valid[i], err); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("%s\n", err->data); - return false; - } - - if (!TEST_CHECK(VarDamson != (((i + 1) % 4) + 1))) - { - TEST_MSG("Value of %s wasn't changed\n", name); - return false; - } - TEST_MSG("%s = %d, set by '%s'\n", name, VarDamson, valid[i]); - } - - mutt_buffer_reset(err); - rc = cs_str_string_set(cs, name, "maildir", err); - if (rc & CSR_SUC_NO_CHANGE) - { - TEST_MSG("Value of %s wasn't changed\n", name); - } - else - { - TEST_MSG("This test should have failed\n"); - return false; - } - - for (unsigned int i = 0; i < mutt_array_size(invalid); i++) - { - mutt_buffer_reset(err); - rc = cs_str_string_set(cs, name, invalid[i], err); - if (TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) - { - TEST_MSG("Expected error: %s\n", err->data); - } - else - { - TEST_MSG("%s = %d, set by '%s'\n", name, VarDamson, invalid[i]); - TEST_MSG("This test should have failed\n"); - return false; - } - } - - log_line(__func__); - return true; -} - -static bool test_string_get(struct ConfigSet *cs, struct Buffer *err) -{ - log_line(__func__); - const char *name = "Elderberry"; - - int valid[] = { MUTT_MBOX, MUTT_MMDF, MUTT_MH, MUTT_MAILDIR }; - - int rc; - for (unsigned int i = 0; i < mutt_array_size(valid); i++) - { - VarElderberry = valid[i]; - mutt_buffer_reset(err); - rc = cs_str_string_get(cs, name, err); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("Get failed: %s\n", err->data); - return false; - } - TEST_MSG("%s = %d, %s\n", name, VarElderberry, err->data); - } - - VarElderberry = 5; - mutt_buffer_reset(err); - TEST_MSG("Expect error for next test\n"); - rc = cs_str_string_get(cs, name, err); - if (!TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) - { - TEST_MSG("%s\n", err->data); - return false; - } - - log_line(__func__); - return true; -} - -static bool test_native_set(struct ConfigSet *cs, struct Buffer *err) -{ - log_line(__func__); - const char *name = "Fig"; - short value = MUTT_MAILDIR; - - VarFig = MUTT_MBOX; - mutt_buffer_reset(err); - int rc = cs_str_native_set(cs, name, value, err); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("%s\n", err->data); - return false; - } - - if (!TEST_CHECK(VarFig == value)) - { - TEST_MSG("Value of %s wasn't changed\n", name); - return false; - } - - TEST_MSG("%s = %d, set to '%d'\n", name, VarFig, value); - - mutt_buffer_reset(err); - rc = cs_str_native_set(cs, name, MUTT_MAILDIR, err); - if (rc & CSR_SUC_NO_CHANGE) - { - TEST_MSG("Value of %s wasn't changed\n", name); - } - else - { - TEST_MSG("This test should have failed\n"); - return false; - } - - int invalid[] = { 0, 5 }; - for (unsigned int i = 0; i < mutt_array_size(invalid); i++) - { - VarFig = MUTT_MBOX; - mutt_buffer_reset(err); - rc = cs_str_native_set(cs, name, invalid[i], err); - if (TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) - { - TEST_MSG("Expected error: %s\n", err->data); - } - else - { - TEST_MSG("%s = %d, set by '%d'\n", name, VarFig, invalid[i]); - TEST_MSG("This test should have failed\n"); - return false; - } - } - - log_line(__func__); - return true; -} - -static bool test_native_get(struct ConfigSet *cs, struct Buffer *err) -{ - log_line(__func__); - const char *name = "Guava"; - - VarGuava = MUTT_MAILDIR; - mutt_buffer_reset(err); - intptr_t value = cs_str_native_get(cs, name, err); - if (!TEST_CHECK(value != INT_MIN)) - { - TEST_MSG("Get failed: %s\n", err->data); - return false; - } - TEST_MSG("%s = %ld\n", name, value); - - log_line(__func__); - return true; -} - -static bool test_reset(struct ConfigSet *cs, struct Buffer *err) -{ - log_line(__func__); - - const char *name = "Hawthorn"; - VarHawthorn = MUTT_MAILDIR; - mutt_buffer_reset(err); - - int rc = cs_str_reset(cs, name, err); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("%s\n", err->data); - return false; - } - - if (!TEST_CHECK(VarHawthorn != MUTT_MAILDIR)) - { - TEST_MSG("Value of %s wasn't changed\n", name); - return false; - } - - TEST_MSG("Reset: %s = %d\n", name, VarHawthorn); - - mutt_buffer_reset(err); - rc = cs_str_reset(cs, name, err); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("%s\n", err->data); - return false; - } - - name = "Ilama"; - mutt_buffer_reset(err); - - TEST_MSG("Initial: %s = %d\n", name, VarIlama); - dont_fail = true; - rc = cs_str_string_set(cs, name, "maildir", err); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - return false; - TEST_MSG("Set: %s = %d\n", name, VarIlama); - dont_fail = false; - - rc = cs_str_reset(cs, name, err); - if (TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) - { - TEST_MSG("Expected error: %s\n", err->data); - } - else - { - TEST_MSG("%s\n", err->data); - return false; - } - - if (!TEST_CHECK(VarIlama == MUTT_MAILDIR)) - { - TEST_MSG("Value of %s changed\n", name); - return false; - } - - TEST_MSG("Reset: %s = %d\n", name, VarIlama); - - log_line(__func__); - return true; -} - -static bool test_validator(struct ConfigSet *cs, struct Buffer *err) -{ - log_line(__func__); - - const char *name = "Jackfruit"; - VarJackfruit = MUTT_MBOX; - mutt_buffer_reset(err); - int rc = cs_str_string_set(cs, name, "maildir", err); - if (TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("%s\n", err->data); - } - else - { - TEST_MSG("%s\n", err->data); - return false; - } - TEST_MSG("String: %s = %d\n", name, VarJackfruit); - - VarJackfruit = MUTT_MBOX; - mutt_buffer_reset(err); - rc = cs_str_native_set(cs, name, MUTT_MAILDIR, err); - if (TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("%s\n", err->data); - } - else - { - TEST_MSG("%s\n", err->data); - return false; - } - TEST_MSG("Native: %s = %d\n", name, VarJackfruit); - - name = "Kumquat"; - VarKumquat = MUTT_MBOX; - mutt_buffer_reset(err); - rc = cs_str_string_set(cs, name, "maildir", err); - if (TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("%s\n", err->data); - } - else - { - TEST_MSG("%s\n", err->data); - return false; - } - TEST_MSG("String: %s = %d\n", name, VarKumquat); - - VarKumquat = MUTT_MBOX; - mutt_buffer_reset(err); - rc = cs_str_native_set(cs, name, MUTT_MAILDIR, err); - if (TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("%s\n", err->data); - } - else - { - TEST_MSG("%s\n", err->data); - return false; - } - TEST_MSG("Native: %s = %d\n", name, VarKumquat); - - name = "Lemon"; - VarLemon = MUTT_MBOX; - mutt_buffer_reset(err); - rc = cs_str_string_set(cs, name, "maildir", err); - if (TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) - { - TEST_MSG("Expected error: %s\n", err->data); - } - else - { - TEST_MSG("%s\n", err->data); - return false; - } - TEST_MSG("String: %s = %d\n", name, VarLemon); - - VarLemon = MUTT_MBOX; - mutt_buffer_reset(err); - rc = cs_str_native_set(cs, name, MUTT_MAILDIR, err); - if (TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) - { - TEST_MSG("Expected error: %s\n", err->data); - } - else - { - TEST_MSG("%s\n", err->data); - return false; - } - TEST_MSG("Native: %s = %d\n", name, VarLemon); - - log_line(__func__); - return true; -} - -static void dump_native(struct ConfigSet *cs, const char *parent, const char *child) -{ - intptr_t pval = cs_str_native_get(cs, parent, NULL); - intptr_t cval = cs_str_native_get(cs, child, NULL); - - TEST_MSG("%15s = %ld\n", parent, pval); - TEST_MSG("%15s = %ld\n", child, cval); -} - -static bool test_inherit(struct ConfigSet *cs, struct Buffer *err) -{ - log_line(__func__); - bool result = false; - - const char *account = "fruit"; - const char *parent = "Mango"; - char child[128]; - snprintf(child, sizeof(child), "%s:%s", account, parent); - - const char *AccountVarStr[] = { - parent, - NULL, - }; - - struct Account *a = account_new(); - account_add_config(a, cs, account, AccountVarStr); - - // set parent - mutt_buffer_reset(err); - int rc = cs_str_string_set(cs, parent, "maildir", err); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("Error: %s\n", err->data); - goto ti_out; - } - dump_native(cs, parent, child); - - // set child - mutt_buffer_reset(err); - rc = cs_str_string_set(cs, child, "mh", err); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("Error: %s\n", err->data); - goto ti_out; - } - dump_native(cs, parent, child); - - // reset child - mutt_buffer_reset(err); - rc = cs_str_reset(cs, child, err); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("Error: %s\n", err->data); - goto ti_out; - } - dump_native(cs, parent, child); - - // reset parent - mutt_buffer_reset(err); - rc = cs_str_reset(cs, parent, err); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) - { - TEST_MSG("Error: %s\n", err->data); - goto ti_out; - } - dump_native(cs, parent, child); - - log_line(__func__); - result = true; -ti_out: - account_free(&a); - return result; -} - -void config_magic(void) -{ - struct Buffer err; - mutt_buffer_init(&err); - err.dsize = 256; - err.data = mutt_mem_calloc(1, err.dsize); - mutt_buffer_reset(&err); - - struct ConfigSet *cs = cs_new(30); - - magic_init(cs); - dont_fail = true; - if (!cs_register_variables(cs, Vars, 0)) - return; - dont_fail = false; - - notify_observer_add(cs->notify, NT_CONFIG, 0, log_observer, 0); - - set_list(cs); - - TEST_CHECK(test_initial_values(cs, &err)); - TEST_CHECK(test_string_set(cs, &err)); - TEST_CHECK(test_string_get(cs, &err)); - TEST_CHECK(test_native_set(cs, &err)); - TEST_CHECK(test_native_get(cs, &err)); - TEST_CHECK(test_reset(cs, &err)); - TEST_CHECK(test_validator(cs, &err)); - TEST_CHECK(test_inherit(cs, &err)); - - cs_free(&cs); - FREE(&err.data); -} diff --git a/test/config/magic.h b/test/config/magic.h deleted file mode 100644 index 4cecf8aa0..000000000 --- a/test/config/magic.h +++ /dev/null @@ -1,30 +0,0 @@ -/** - * @file - * Test code for the Magic object - * - * @authors - * Copyright (C) 2017-2018 Richard Russon - * - * @copyright - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 2 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -#ifndef _TEST_MAGIC_H -#define _TEST_MAGIC_H - -#include - -void config_magic(void); - -#endif /* _TEST_MAGIC_H */ diff --git a/test/main.c b/test/main.c index 64a9979c1..6323ce281 100644 --- a/test/main.c +++ b/test/main.c @@ -112,7 +112,6 @@ NEOMUTT_TEST_ITEM(config_bool) \ NEOMUTT_TEST_ITEM(config_enum) \ NEOMUTT_TEST_ITEM(config_long) \ - NEOMUTT_TEST_ITEM(config_magic) \ NEOMUTT_TEST_ITEM(config_mbtable) \ NEOMUTT_TEST_ITEM(config_number) \ NEOMUTT_TEST_ITEM(config_quad) \ -- 2.40.0