/*
 * Copyright (C) Birb Developers
 *
 * Birb is the legal property of its developers, whose names are too numerous
 * to list here. Please refer to the AUTHORS file distributed with this source
 * distribution
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * This library 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <https://www.gnu.org/licenses/>.
 */

#if !defined(BIRB_GLOBAL_HEADER_INSIDE) && !defined(BIRB_COMPILATION)
# error "only <birb.h> may be included directly"
#endif

#ifndef BIRB_ASSERT_H
#define BIRB_ASSERT_H

#include <glib.h>
#include <glib-object.h>

#include <gio/gio.h>

#include "birbversion.h"

G_BEGIN_DECLS

/**
 * birb_assert_type:
 * @instance: the instance whose type to check
 * @type: the required type
 *
 * Asserts that @instance is of type @type.
 *
 * Since: 0.3
 */
#define birb_assert_type(instance, type) G_STMT_START { \
    gpointer _instance = (instance); \
    GType _type = (type); \
    if(_instance == NULL) { \
        char *msg = g_strdup_printf("%s is NULL and not an instance of %s", \
                                    #instance, \
                                    g_type_name(_type)); \
        g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg); \
        g_free(msg); \
    } else if(!G_IS_OBJECT(_instance)) { \
        char *msg = g_strdup_printf("%s is not a GObject", #instance); \
        g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg); \
        g_free(msg); \
    } else if(G_OBJECT_TYPE(_instance) != _type) { \
        char *msg = g_strdup_printf("%s is of type %s not %s", \
                                    #instance, \
                                    G_OBJECT_TYPE_NAME(_instance), \
                                    g_type_name(_type)); \
        g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg); \
        g_free(msg); \
    } \
    } G_STMT_END \
    BIRB_AVAILABLE_MACRO_IN_0_3

/**
 * birb_assert_list_model_n_items:
 * @model: the model
 * @n_items: the number of items
 *
 * Asserts that a model has the given number of items.
 *
 * Since: 0.4
 */
#define birb_assert_list_model_n_items(model, n_items) G_STMT_START { \
    g_assert_true(G_IS_LIST_MODEL(model)); \
    g_assert_cmpuint(g_list_model_get_n_items(G_LIST_MODEL(model)), ==, n_items); \
    } G_STMT_END \
    BIRB_AVAILABLE_MACRO_IN_0_4

G_END_DECLS

#endif /* BIRB_ASSERT_H */
