1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "test-registry.h"
6
7 #include <memory>
8
9 #include <zircon/assert.h>
10 #include <zxtest/base/test-driver.h>
11 #include <zxtest/base/test-internal.h>
12
13 namespace zxtest {
14
15 using internal::TestDriver;
16 using internal::TestInternal;
17 using internal::TestStatus;
18 namespace test {
19 namespace {
20
21 // Meant to make template instantiation more readable.
22 constexpr bool kPassSetUp = false;
23 constexpr bool kFailsSetUp = true;
24 constexpr bool kPassTestBody = false;
25 constexpr bool kFailsTestBody = true;
26
27 template <bool FailOnSetUp, bool FailOnTestBody>
28 class FakeTest : public zxtest::Test {
29 public:
SetUp()30 void SetUp() final {
31 if (FailOnSetUp) {
32 driver->NotifyFail();
33 }
34 run_setup = true;
35 }
36
TearDown()37 void TearDown() final { run_teardown = true; }
38
TestBody()39 void TestBody() final {
40 if (FailOnTestBody) {
41 driver->NotifyFail();
42 }
43 run_body = true;
44 }
45
46 // Used for verying that Run behaves properly.
47 bool run_setup = false;
48 bool run_teardown = false;
49 bool run_body = false;
50 TestDriverStub* driver = nullptr;
51 };
52
53 } // namespace
54
TestRun()55 void TestRun() {
56 TestDriverStub driver;
57 auto test = Test::Create<FakeTest<kPassSetUp, kPassTestBody>>(&driver);
58 test->driver = &driver;
59
60 test->Run();
61 ZX_ASSERT_MSG(test->run_setup, "Test did not execute SetUp");
62 ZX_ASSERT_MSG(test->run_body, "Test did not execute TestBody");
63 ZX_ASSERT_MSG(test->run_teardown, "Test did not execute TearDown");
64 }
65
TestRunFailure()66 void TestRunFailure() {
67 TestDriverStub driver;
68 auto test = Test::Create<FakeTest<kPassSetUp, kFailsTestBody>>(&driver);
69 test->driver = &driver;
70
71 test->Run();
72 ZX_ASSERT_MSG(test->run_setup, "Test did not execute SetUp");
73 ZX_ASSERT_MSG(test->run_body, "Test did not execute TestBody");
74 ZX_ASSERT_MSG(test->run_teardown, "Test did not execute TearDown");
75 }
76
TestSetUpFailure()77 void TestSetUpFailure() {
78 TestDriverStub driver;
79 auto test = Test::Create<FakeTest<kFailsSetUp, kFailsTestBody>>(&driver);
80 test->driver = &driver;
81
82 test->Run();
83 ZX_ASSERT_MSG(test->run_setup, "Test did not execute SetUp");
84 ZX_ASSERT_MSG(!test->run_body, "Test did execute TestBody when its SetUp failed.");
85 ZX_ASSERT_MSG(test->run_teardown, "Test did not execute TearDown");
86 }
87
88 } // namespace test
89 } // namespace zxtest
90