xtd - Reference Guide  0.1.2
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Loading...
Searching...
No Matches
test.h
Go to the documentation of this file.
1
4#pragma once
5
6#include "../tunit_export.h"
7#include "assert.h"
8#include "assert_error.h"
10#include <functional>
11#include <chrono>
12#include <string>
13
15namespace xtd {
17 namespace tunit {
19 class test_class;
20 class unit_test;
21 class valid;
23
24 class tunit_export_ test final {
25 enum class test_status {
26 not_started,
27 ignored,
28 succeed,
29 aborted,
30 failed
31 };
32
33 public:
34 test() = default;
35 test(const std::string& name, const std::function<void()>& method, const xtd::diagnostics::stack_frame& caller) noexcept : test(name, method, false, caller) {}
36 test(const std::string& name, const std::function<void()>& method, bool ignore, const xtd::diagnostics::stack_frame& stack_frame) noexcept : stack_frame_(stack_frame), method_(method), name_(name), status_(ignore ? test_status::ignored : test_status::not_started) {}
37
38 bool aborted() const noexcept {return status_ == test_status::aborted;}
39
40 const std::string& actual() const noexcept {return actual_;}
41
42 const std::string& expect() const noexcept {return expect_;}
43
44 bool failed() const noexcept {return status_ == test_status::failed;}
45
46 bool ignored() const noexcept {return status_ == test_status::ignored;}
47
48 bool not_started() const noexcept {return status_ == test_status::not_started;}
49
50 bool succeed() const noexcept {return status_ == test_status::succeed;}
51
52 const xtd::diagnostics::stack_frame stack_frame() const noexcept {return stack_frame_;}
53
54 std::function<void()> method() const noexcept {return method_;}
55
56 const std::string& message() const noexcept {return message_;}
57
58 const std::string& name() const noexcept {return name_;}
59
60 const std::string& user_message() const noexcept {return user_message_;}
61
62 std::chrono::milliseconds elapsed_time() const noexcept {
63 using namespace std::chrono_literals;
64 if (start_time_point.time_since_epoch() == 0ms && end_time_point.time_since_epoch() == 0ms) return 0ms;
65 if (end_time_point.time_since_epoch() == 0ms) return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start_time_point);
66 return std::chrono::duration_cast<std::chrono::milliseconds>(end_time_point - start_time_point);
67 }
68
70 static intptr_t __internal_tunit_unit_tests_mode__;
72
73 private:
74 friend class xtd::tunit::assert;
75 friend class xtd::tunit::base_assert;
76 friend class xtd::tunit::test_class;
77 friend class xtd::tunit::valid;
78 static test& current_test() {return *current_test_;}
79 static const test_class& current_test_class() {return *current_test_class_;}
80 static const unit_test& current_unit_test() {return *current_unit_test_;}
81
83
84 std::chrono::high_resolution_clock::time_point end_time_point;
85 static test* current_test_;
86 static const test_class* current_test_class_;
87 static const unit_test* current_unit_test_;
88 std::string actual_;
89 std::string expect_;
91 std::string message_;
92 std::function<void()> method_;
93 std::string name_;
94 std::chrono::high_resolution_clock::time_point start_time_point;
95 test_status status_ = test_status::not_started;
96 std::string user_message_;
97 };
98 }
99}
Contains xtd::tunit::assert class.
Contains xtd::tunit::assert_error exception.
Provides information about a xtd::diagnostics::stack_frame, which represents a function call on the c...
Definition stack_frame.h:29
static stack_frame empty() noexcept
Return an empty stack frame.
The assert class contains a collection of static methods that implement the most common assertions us...
Definition assert.h:34
The base class for assert.
Definition base_assert.h:25
Definition test_class.h:25
Definition test.h:24
The template class.
Definition unit_test.h:32
The valid class contains a collection of static methods that implement the most common assertions use...
Definition valid.h:20
#define tunit_export_
Define shared library export.
Definition tunit_export.h:13
@ ignored
Test is ignored.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition system_report.h:17
Contains xtd::diagnostics::stack_frame class.