Welcome to mirror list, hosted at ThFree Co, Russian Federation.

example_tests.cpp « tests - github.com/dosbox-staging/dosbox-staging.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 06084f9fbd24ff1d0605c4d79ac38421d72ae2f2 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *
 *  Copyright (C) 2020-2021  The DOSBox Staging Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/* This sample shows how to write a simple unit test for dosbox-staging using
 * Google C++ testing framework.
 *
 * Read Google Test Primer for reference of most available features, macros,
 * and guidance about writing unit tests:
 *
 * https://github.com/google/googletest/blob/master/googletest/docs/primer.md#googletest-primer
 */

/* Include necessary header files; order of headers should be as follows:
 *
 * 1. Header declaring functions/classes being tested
 * 2. <gtest/gtest.h>, which declares the testing framework and
      <gmock/gmock.h>, which declares the mock framework.
 * 3. Additional system headers (if needed)
 * 4. Additional dosbox-staging headers (if needed)
 */

#include "support.h"

#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include <cstdint>

// Open anonymous namespace (this is Google Test requirement)

namespace {

/* Use the TEST macro to define your tests. You can also create test fixtures
 * using a similar TEST_F macro (see the primer for details).
 *
 * TEST has two parameters: the test case name and the test name.
 * After using the macro, you should define your test logic between a
 * pair of braces.
 *
 * You can use a bunch of macros to indicate the success or failure of a test.
 * EXPECT_TRUE and EXPECT_EQ are examples of such macros, but there are many
 * similar ones, appropriate for various situations.
 */

TEST(DriveIndexTest, BigLetters)
{
	EXPECT_EQ(0, drive_index('A'));
	EXPECT_EQ(25, drive_index('Z'));
}

/* Remember: tests are not code - they should exercise specific edge cases,
 * not re-implement the functionality.
 *
 * When writing *non-trivial* unit tests, it's recommended to follow 
 * "Given-When-Then" style.  It's a style of writing tests, where a single
 * case is split into three sections. Here's a test similar to the previous one,
 * but rewritten using this style:
 */

TEST(DriveIndexTest, SmallLetters)
{
	// "Given" section describes test-prerequisites
	const char input_letter = 'b';

	// "When" section runs the tested code and records the results
	const uint8_t result = drive_index(input_letter);

	// "Then" section describes expectations for the test result
	EXPECT_EQ(1, result);
}

/* Below you'll find an example failing test.
 *
 * Remember to pass code results as second parameter to the macro - this will
 * result in Google Test generating more helpful test output.
 *
 * In this case output will look like this:
 *
 *     [ RUN      ] DriveIndexTest.ExampleFailingTest
 *     example.cpp:110: Failure
 *     Expected equality of these values:
 *       42
 *       drive_index('C')
 *         Which is: '\x2' (2)
 *     [  FAILED  ] DriveIndexTest.ExampleFailingTest (0 ms)
 */

TEST(DriveIndexTest, ExampleFailingTest)
{
	EXPECT_EQ(42, drive_index('C')); // this is clearly incorrect test :)
}

/* Sometimes it might be convenient to disable a test during development for
 * a while. You can accomplish this by prefixing test name with "DISABLED_"
 * prefix.
 *
 * Google Test will print a reminder about disabled tests in test results.
 */

TEST(DriveIndexTest, DISABLED_ExampleFailingTest2)
{
	EXPECT_EQ(42, drive_index('C')); // this is clearly incorrect test :)
}

/* Once the tests are built, run ./tests to see the testing results.
 *
 * You can also use "make test" to build and run tests in one go.
 *
 * See "./tests --help" for additional options (like e.g. filtering which tests
 * should be run or randomizing the tests order).
 */

} // namespace