summaryrefslogtreecommitdiff
path: root/test/gjs-test-utils.h
blob: ebb5e88504331f5f29e2af47985eeec301679426 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2013 Endless Mobile, Inc.
// SPDX-FileContributor: Authored By: Sam Spilsbury <sam@endlessm.com>

#ifndef TEST_GJS_TEST_UTILS_H_
#define TEST_GJS_TEST_UTILS_H_

#include <config.h>

#include <glib.h>    // for g_assert_...
#include <stdint.h>  // for uintptr_t
#include <limits>    // for numeric_limits
#include <string>
#include <type_traits>  // for is_same
#include <utility>

#include "gjs/context.h"

#include <js/TypeDecls.h>

struct GjsUnitTestFixture {
    GjsContext *gjs_context;
    JSContext *cx;
    JS::Realm* realm;
};

void gjs_unit_test_fixture_setup(GjsUnitTestFixture* fx, const void* unused);

void gjs_unit_test_destroy_context(GjsUnitTestFixture *fx);

void gjs_unit_test_fixture_teardown(GjsUnitTestFixture* fx, const void* unused);

void gjs_test_add_tests_for_coverage ();

void gjs_test_add_tests_for_parse_call_args(void);

void gjs_test_add_tests_for_rooting(void);

void gjs_test_add_tests_for_jsapi_utils();

namespace Gjs {
namespace Test {

void add_tests_for_toggle_queue();

template <typename T1, typename T2>
constexpr bool comparable_types() {
    if constexpr (std::is_same<T1, T2>()) {
        return true;
    } else if constexpr (std::is_arithmetic_v<T1> == std::is_arithmetic_v<T2>) {
        return std::is_signed_v<T1> == std::is_signed_v<T2>;
    } else if constexpr (std::is_enum_v<T1> == std::is_enum_v<T2>) {
        return std::is_signed_v<T1> == std::is_signed_v<T2>;
    } else {
        return false;
    }
}

template <typename T, typename U>
constexpr void assert_equal(T a, U b) {
    static_assert(comparable_types<T, U>());
    if constexpr (std::is_integral_v<T> || std::is_enum_v<T>) {
        if constexpr (std::is_unsigned_v<T>)
            g_assert_cmpuint(a, ==, b);
        else
            g_assert_cmpint(a, ==, b);
    } else if constexpr (std::is_arithmetic_v<T>) {
        g_assert_cmpfloat_with_epsilon(a, b, std::numeric_limits<T>::epsilon());
    } else if constexpr (std::is_same_v<T, char*>) {
        g_assert_cmpstr(a, ==, b);
    } else if constexpr (std::is_same_v<T, std::string>) {
        assert_equal(a.c_str(), b.c_str());
    } else if constexpr (std::is_pointer_v<T>) {
        assert_equal(reinterpret_cast<uintptr_t>(a),
                     reinterpret_cast<uintptr_t>(b));
    } else {
        g_assert_true(a == b);
    }
}

template <typename T, typename U>
constexpr void assert_equal(std::pair<T, U> const& pair, T first, U second) {
    assert_equal(pair.first, first);
    assert_equal(pair.second, second);
}

}  // namespace Test
}  // namespace Gjs

#endif  // TEST_GJS_TEST_UTILS_H_