atom: add a mock for struct atom

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2024-02-17 02:13:46 +00:00
parent e5bcfaccbe
commit a1bedd2d32
No known key found for this signature in database
GPG Key ID: D3A4405BE6CC17F4
2 changed files with 44 additions and 0 deletions

View File

@ -138,3 +138,41 @@ void destroy_atoms(struct atom *a) {
cache_invalidate_all(&atoms->c, atom_entry_free);
free(a);
}
#ifdef UNIT_TEST
static inline int mock_atom_getter(struct cache *cache, const char *atom_name attr_unused,
struct cache_handle **value, void *user_data attr_unused) {
auto atoms = container_of(cache, struct atom_impl, c);
xcb_atom_t atom = (xcb_atom_t)HASH_COUNT(atoms->atom_to_name) + 1;
struct atom_entry *entry = ccalloc(1, struct atom_entry);
entry->atom = atom;
HASH_ADD_INT(atoms->atom_to_name, atom, entry);
*value = &entry->entry;
return 0;
}
static inline char *
mock_atom_name_getter(xcb_atom_t atom attr_unused, xcb_connection_t *c attr_unused) {
abort();
}
struct atom *init_mock_atoms(xcb_connection_t *c) {
auto atoms = ccalloc(1, struct atom_impl);
atoms->c = CACHE_INIT;
atoms->getter = mock_atom_getter;
atoms->name_getter = mock_atom_name_getter;
#define ATOM_GET(x) atoms->base.a##x = get_atom(&atoms->base, #x, c)
LIST_APPLY(ATOM_GET, SEP_COLON, ATOM_LIST1);
LIST_APPLY(ATOM_GET, SEP_COLON, ATOM_LIST2);
#undef ATOM_GET
return &atoms->base;
}
#else
struct atom *init_mock_atoms(xcb_connection_t *c) {
abort();
}
#endif

View File

@ -67,3 +67,9 @@ const char *get_atom_name(struct atom *a, xcb_atom_t, xcb_connection_t *c);
const char *get_atom_name_cached(struct atom *a, xcb_atom_t atom);
void destroy_atoms(struct atom *a);
/// A mock atom object for unit testing. Successive calls to get_atom will return
/// secutive integers as atoms, starting from 1. Calling get_atom_name with atoms
/// previously seen will result in the string that was used to create the atom; if
/// the atom was never returned by get_atom, it will abort.
struct atom *init_mock_atoms(xcb_connection_t *c);