NeoMutt  2019-11-11
Teaching an old dog new tricks
DOXYGEN
common.c File Reference

Shared Testing Code. More...

#include "acutest.h"
#include "config.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mutt/mutt.h"
#include "config/lib.h"
#include "core/lib.h"
#include "common.h"
+ Include dependency graph for common.c:

Go to the source code of this file.

Macros

#define TEST_NO_MAIN
 

Functions

int validator_fail (const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *result)
 
int validator_warn (const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *result)
 
int validator_succeed (const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *result)
 
void log_line (const char *fn)
 
void short_line (void)
 
int log_observer (struct NotifyCallback *nc)
 
void set_list (const struct ConfigSet *cs)
 
int sort_list_cb (const void *a, const void *b)
 
void cs_dump_set (const struct ConfigSet *cs)
 

Variables

const char * line
 
bool dont_fail = false
 

Detailed Description

Shared Testing Code.

Authors
  • Richard Russon

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 http://www.gnu.org/licenses/.

Definition in file common.c.

Macro Definition Documentation

◆ TEST_NO_MAIN

#define TEST_NO_MAIN

Definition at line 23 of file common.c.

Function Documentation

◆ validator_fail()

int validator_fail ( const struct ConfigSet cs,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer result 
)

Definition at line 41 of file common.c.

43 {
44  if (dont_fail)
45  return CSR_SUCCESS;
46 
47  if (value > 1000000)
48  mutt_buffer_printf(result, "%s: %s, (ptr)", __func__, cdef->name);
49  else
50  mutt_buffer_printf(result, "%s: %s, %ld", __func__, cdef->name, value);
51  return CSR_ERR_INVALID;
52 }
bool dont_fail
Definition: common.c:39
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
const char * name
User-visible name.
Definition: set.h:155
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:46
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:49
+ Here is the call graph for this function:

◆ validator_warn()

int validator_warn ( const struct ConfigSet cs,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer result 
)

Definition at line 54 of file common.c.

56 {
57  if (value > 1000000)
58  mutt_buffer_printf(result, "%s: %s, (ptr)", __func__, cdef->name);
59  else
60  mutt_buffer_printf(result, "%s: %s, %ld", __func__, cdef->name, value);
62 }
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
const char * name
User-visible name.
Definition: set.h:155
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:46
#define CSR_SUC_WARNING
Notify the user of a warning.
Definition: set.h:54
+ Here is the call graph for this function:

◆ validator_succeed()

int validator_succeed ( const struct ConfigSet cs,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer result 
)

Definition at line 64 of file common.c.

66 {
67  if (value > 1000000)
68  mutt_buffer_printf(result, "%s: %s, (ptr)", __func__, cdef->name);
69  else
70  mutt_buffer_printf(result, "%s: %s, %ld", __func__, cdef->name, value);
71  return CSR_SUCCESS;
72 }
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
const char * name
User-visible name.
Definition: set.h:155
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:46
+ Here is the call graph for this function:

◆ log_line()

void log_line ( const char *  fn)

Definition at line 74 of file common.c.

75 {
76  int len = 44 - mutt_str_strlen(fn);
77  TEST_MSG("\033[36m---- %s %.*s\033[m\n", fn, len, line);
78 }
#define TEST_MSG(...)
Definition: acutest.h:177
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:666
const char * line
Definition: common.c:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ short_line()

void short_line ( void  )

Definition at line 80 of file common.c.

81 {
82  TEST_MSG("%s\n", line + 40);
83 }
#define TEST_MSG(...)
Definition: acutest.h:177
const char * line
Definition: common.c:36
+ Here is the caller graph for this function:

◆ log_observer()

int log_observer ( struct NotifyCallback nc)

Definition at line 85 of file common.c.

86 {
87  if (!nc)
88  return -1;
89 
90  struct EventConfig *ec = (struct EventConfig *) nc->event;
91 
92  struct Buffer result;
93  mutt_buffer_init(&result);
94  result.dsize = 256;
95  result.data = mutt_mem_calloc(1, result.dsize);
96 
97  const char *events[] = { "set", "reset", "initial-set" };
98 
99  mutt_buffer_reset(&result);
100 
102  cs_he_string_get(ec->cs, ec->he, &result);
103  else
104  cs_he_initial_get(ec->cs, ec->he, &result);
105 
106  TEST_MSG("Event: %s has been %s to '%s'\n", ec->name,
107  events[nc->event_subtype - 1], result.data);
108 
109  FREE(&result.data);
110  return true;
111 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
A config-change event.
Definition: set.h:199
String manipulation buffer.
Definition: buffer.h:33
#define TEST_MSG(...)
Definition: acutest.h:177
intptr_t event
Send: event data.
Definition: observer.h:46
int event_subtype
Send: event subtype.
Definition: observer.h:45
size_t dsize
Length of data.
Definition: buffer.h:37
struct HashElem * he
Config item that changed.
Definition: set.h:202
const struct ConfigSet * cs
Config set.
Definition: set.h:201
int cs_he_initial_get(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *result)
Get the initial, or parent, value of a config item.
Definition: set.c:513
int cs_he_string_get(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *result)
Get a config item as a string.
Definition: set.c:650
#define FREE(x)
Definition: memory.h:40
Config item&#39;s initial value has been set.
Definition: set.h:42
struct Buffer * mutt_buffer_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:46
const char * name
Name of config item that changed.
Definition: set.h:203
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set_list()

void set_list ( const struct ConfigSet cs)

Definition at line 113 of file common.c.

114 {
115  log_line(__func__);
116  cs_dump_set(cs);
117  log_line(__func__);
118 }
void log_line(const char *fn)
Definition: common.c:74
void cs_dump_set(const struct ConfigSet *cs)
Definition: common.c:127
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sort_list_cb()

int sort_list_cb ( const void *  a,
const void *  b 
)

Definition at line 120 of file common.c.

121 {
122  const char *stra = *(char const *const *) a;
123  const char *strb = *(char const *const *) b;
124  return strcmp(stra, strb);
125 }
+ Here is the caller graph for this function:

◆ cs_dump_set()

void cs_dump_set ( const struct ConfigSet cs)

Definition at line 127 of file common.c.

128 {
129  if (!cs)
130  return;
131 
132  struct HashElem *he = NULL;
133  struct HashWalkState state;
134  memset(&state, 0, sizeof(state));
135 
136  struct Buffer result;
137  mutt_buffer_init(&result);
138  result.dsize = 256;
139  result.data = mutt_mem_calloc(1, result.dsize);
140 
141  char tmp[128];
142  char *list[26] = { 0 };
143  size_t index = 0;
144  size_t i = 0;
145 
146  for (i = 0; (he = mutt_hash_walk(cs->hash, &state)); i++)
147  {
148  if (he->type == DT_SYNONYM)
149  continue;
150 
151  const char *name = NULL;
152 
153  if (he->type & DT_INHERITED)
154  {
155  struct Inheritance *inh = he->data;
156  he = inh->parent;
157  name = inh->name;
158  }
159  else
160  {
161  name = he->key.strkey;
162  }
163 
164  const struct ConfigSetType *cst = cs_get_type_def(cs, he->type);
165  if (!cst)
166  {
167  snprintf(tmp, sizeof(tmp), "Unknown type: %d", he->type);
168  list[index] = mutt_str_strdup(tmp);
169  index++;
170  continue;
171  }
172 
173  mutt_buffer_reset(&result);
174  const struct ConfigDef *cdef = he->data;
175 
176  int rc = cst->string_get(cs, cdef->var, cdef, &result);
177  if (CSR_RESULT(rc) == CSR_SUCCESS)
178  snprintf(tmp, sizeof(tmp), "%s %s = %s", cst->name, name, result.data);
179  else
180  snprintf(tmp, sizeof(tmp), "%s %s: ERROR: %s", cst->name, name, result.data);
181  list[index] = mutt_str_strdup(tmp);
182  index++;
183  }
184 
185  qsort(list, index, sizeof(list[0]), sort_list_cb);
186  for (i = 0; list[i]; i++)
187  {
188  TEST_MSG("%s\n", list[i]);
189  FREE(&list[i]);
190  }
191 
192  FREE(&result.data);
193 }
int sort_list_cb(const void *a, const void *b)
Definition: common.c:120
union HashKey key
Definition: hash.h:45
Type definition for a config item.
Definition: set.h:168
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
struct Hash * hash
HashTable storing the config items.
Definition: set.h:189
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
#define CSR_RESULT(x)
Definition: set.h:62
Cursor to iterate through a Hash Table.
Definition: hash.h:96
String manipulation buffer.
Definition: buffer.h:33
#define TEST_MSG(...)
Definition: acutest.h:177
cst_string_get string_get
Initialise a variable from a string.
Definition: set.h:172
const char * name
Name of this config item.
Definition: inheritance.h:34
Config item definition.
Definition: set.h:153
An inherited config item.
Definition: inheritance.h:31
const char * name
Definition: pgpmicalg.c:45
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:46
void * data
Definition: hash.h:46
void * var
Pointer to the global variable.
Definition: set.h:157
struct HashElem * mutt_hash_walk(const struct Hash *table, struct HashWalkState *state)
Iterate through all the HashElem&#39;s in a Hash table.
Definition: hash.c:503
#define DT_SYNONYM
synonym for another variable
Definition: types.h:41
const struct ConfigSetType * cs_get_type_def(const struct ConfigSet *cs, unsigned int type)
Get the definition for a type.
Definition: set.c:239
char * mutt_str_strdup(const char *str)
Copy a string, safely.
Definition: string.c:380
const char * name
Name of the type, e.g.
Definition: set.h:170
const char * strkey
Definition: hash.h:35
int type
Definition: hash.h:44
#define FREE(x)
Definition: memory.h:40
The item stored in a Hash Table.
Definition: hash.h:42
struct HashElem * parent
HashElem of parent config item.
Definition: inheritance.h:33
struct Buffer * mutt_buffer_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:46
#define DT_INHERITED
Config item is inherited.
Definition: types.h:78
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ line

const char* line
Initial value:
= "----------------------------------------"
"----------------------------------------"

Definition at line 36 of file common.c.

◆ dont_fail

bool dont_fail = false

Definition at line 39 of file common.c.