summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/wtf/OptionalTest.cpp
blob: e96d509db8405297959947c447d05d96d66d219a (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
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "wtf/Optional.h"

#include "testing/gtest/include/gtest/gtest.h"

namespace WTF {
namespace {

struct IntBox {
    IntBox(int n) : number(n) { }
    int number;
};

class DestructionNotifier {
public:
    DestructionNotifier(bool& flag) : m_flag(flag) { }
    ~DestructionNotifier() { m_flag = true; }
private:
    bool& m_flag;
};

TEST(OptionalTest, BooleanTest)
{
    Optional<int> optional;
    EXPECT_FALSE(optional);
    optional.emplace(0);
    EXPECT_TRUE(optional);
}

TEST(OptionalTest, Dereference)
{
    Optional<int> optional;
    optional.emplace(1);
    EXPECT_EQ(1, *optional);

    Optional<IntBox> optionalIntbox;
    optionalIntbox.emplace(42);
    EXPECT_EQ(42, optionalIntbox->number);
}

TEST(OptionalTest, DestructorCalled)
{
    // Destroying a disengaged optional shouldn't do anything.
    {
        Optional<DestructionNotifier> optional;
    }

    // Destroying an engaged optional should call the destructor.
    bool isDestroyed = false;
    {
        Optional<DestructionNotifier> optional;
        optional.emplace(isDestroyed);
        EXPECT_FALSE(isDestroyed);
    }
    EXPECT_TRUE(isDestroyed);
}

} // namespace
} // namespace WTF