Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/signal_watcher.h
23446 views
1
/**************************************************************************/
2
/* signal_watcher.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/object/object.h"
34
#include "tests/test_macros.h"
35
36
// Utility class / macros for testing signals
37
//
38
// Use SIGNAL_WATCH(*object, "signal_name") to start watching
39
// Makes sure to call SIGNAL_UNWATCH(*object, "signal_name") to stop watching in cleanup, this is not done automatically.
40
//
41
// The SignalWatcher will capture all signals and their args sent between checks.
42
//
43
// Use SIGNAL_CHECK("signal_name"), Vector<Vector<Variant>>), to check the arguments of all fired signals.
44
// The outer vector is each fired signal, the inner vector the list of arguments for that signal. Order does matter.
45
//
46
// Use SIGNAL_CHECK_FALSE("signal_name") to check if a signal was not fired.
47
//
48
// Use SIGNAL_DISCARD("signal_name") to discard records all of the given signal, use only in placed you don't need to check.
49
//
50
// All signals are automatically discarded between test/sub test cases.
51
52
class SignalWatcher : public Object {
53
inline static SignalWatcher *singleton = nullptr;
54
55
HashMap<String, Array> _signals;
56
57
void _add_signal_entry(const Array &p_args, const String &p_name);
58
void _signal_callback_zero(const String &p_name);
59
void _signal_callback_one(Variant p_arg1, const String &p_name);
60
void _signal_callback_two(Variant p_arg1, Variant p_arg2, const String &p_name);
61
void _signal_callback_three(Variant p_arg1, Variant p_arg2, Variant p_arg3, const String &p_name);
62
63
public:
64
static SignalWatcher *get_singleton() { return singleton; }
65
66
void watch_signal(Object *p_object, const String &p_signal);
67
void unwatch_signal(Object *p_object, const String &p_signal);
68
bool check(const String &p_name, const Array &p_args);
69
bool check_false(const String &p_name);
70
void discard_signal(const String &p_name);
71
72
void _clear_signals();
73
74
SignalWatcher();
75
~SignalWatcher();
76
};
77
78
#define SIGNAL_WATCH(m_object, m_signal) SignalWatcher::get_singleton()->watch_signal(m_object, m_signal);
79
#define SIGNAL_UNWATCH(m_object, m_signal) SignalWatcher::get_singleton()->unwatch_signal(m_object, m_signal);
80
81
#define SIGNAL_CHECK(m_signal, m_args) CHECK(SignalWatcher::get_singleton()->check(m_signal, m_args));
82
#define SIGNAL_CHECK_FALSE(m_signal) CHECK(SignalWatcher::get_singleton()->check_false(m_signal));
83
#define SIGNAL_DISCARD(m_signal) SignalWatcher::get_singleton()->discard_signal(m_signal);
84
85