summaryrefslogtreecommitdiffstats
path: root/dom/quota/test/gtest/TestResultExtensions.cpp
blob: 137acc1423a8fda47ee6fbc3f076a046617dd903 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "Common.h"
#include "gtest/gtest.h"
#include "mozilla/dom/QMResult.h"
#include "mozilla/dom/quota/ResultExtensions.h"

using namespace mozilla;
using namespace mozilla::dom::quota;

namespace {
class TestClass {
 public:
  static constexpr int kTestValue = 42;

  nsresult NonOverloadedNoInputComplex(std::pair<int, int>* aOut) {
    *aOut = std::pair{kTestValue, kTestValue};
    return NS_OK;
  }
  nsresult NonOverloadedNoInputFailsComplex(std::pair<int, int>* aOut) {
    return NS_ERROR_FAILURE;
  }
};
}  // namespace

class DOM_Quota_ResultExtensions_ToResult : public DOM_Quota_Test {};
class DOM_Quota_ResultExtensions_GenericErrorResult : public DOM_Quota_Test {};

TEST_F(DOM_Quota_ResultExtensions_ToResult, FromBool) {
  // success
  {
    auto res = ToResult(true);
    static_assert(std::is_same_v<decltype(res), Result<Ok, nsresult>>);
    EXPECT_TRUE(res.isOk());
  }

  // failure
  {
    auto res = ToResult(false);
    static_assert(std::is_same_v<decltype(res), Result<Ok, nsresult>>);
    EXPECT_TRUE(res.isErr());
    EXPECT_EQ(res.unwrapErr(), NS_ERROR_FAILURE);
  }
}

TEST_F(DOM_Quota_ResultExtensions_ToResult, FromQMResult_Failure) {
  // copy
  {
    const auto res = ToQMResult(NS_ERROR_FAILURE);
    auto okOrErr = ToResult<QMResult>(res);
    static_assert(std::is_same_v<decltype(okOrErr), OkOrErr>);
    ASSERT_TRUE(okOrErr.isErr());
    auto err = okOrErr.unwrapErr();

#ifdef QM_ERROR_STACKS_ENABLED
    IncreaseExpectedStackId();

    ASSERT_EQ(err.StackId(), ExpectedStackId());
    ASSERT_EQ(err.FrameId(), 1u);
    ASSERT_EQ(err.NSResult(), NS_ERROR_FAILURE);
#else
    ASSERT_EQ(err, NS_ERROR_FAILURE);
#endif
  }

  // move
  {
    auto res = ToQMResult(NS_ERROR_FAILURE);
    auto okOrErr = ToResult<QMResult>(std::move(res));
    static_assert(std::is_same_v<decltype(okOrErr), OkOrErr>);
    ASSERT_TRUE(okOrErr.isErr());
    auto err = okOrErr.unwrapErr();

#ifdef QM_ERROR_STACKS_ENABLED
    IncreaseExpectedStackId();

    ASSERT_EQ(err.StackId(), ExpectedStackId());
    ASSERT_EQ(err.FrameId(), 1u);
    ASSERT_EQ(err.NSResult(), NS_ERROR_FAILURE);
#else
    ASSERT_EQ(err, NS_ERROR_FAILURE);
#endif
  }
}

TEST_F(DOM_Quota_ResultExtensions_ToResult, FromNSResult_Failure_Macro) {
  auto okOrErr = QM_TO_RESULT(NS_ERROR_FAILURE);
  static_assert(std::is_same_v<decltype(okOrErr), OkOrErr>);
  ASSERT_TRUE(okOrErr.isErr());
  auto err = okOrErr.unwrapErr();

#ifdef QM_ERROR_STACKS_ENABLED
  IncreaseExpectedStackId();

  ASSERT_EQ(err.StackId(), ExpectedStackId());
  ASSERT_EQ(err.FrameId(), 1u);
  ASSERT_EQ(err.NSResult(), NS_ERROR_FAILURE);
#else
  ASSERT_EQ(err, NS_ERROR_FAILURE);
#endif
}

TEST_F(DOM_Quota_ResultExtensions_GenericErrorResult, ErrorPropagation) {
  OkOrErr okOrErr1 = ToResult<QMResult>(ToQMResult(NS_ERROR_FAILURE));
  const auto& err1 = okOrErr1.inspectErr();

#ifdef QM_ERROR_STACKS_ENABLED
  IncreaseExpectedStackId();

  ASSERT_EQ(err1.StackId(), ExpectedStackId());
  ASSERT_EQ(err1.FrameId(), 1u);
  ASSERT_EQ(err1.NSResult(), NS_ERROR_FAILURE);
#else
  ASSERT_EQ(err1, NS_ERROR_FAILURE);
#endif

  OkOrErr okOrErr2 = okOrErr1.propagateErr();
  const auto& err2 = okOrErr2.inspectErr();

#ifdef QM_ERROR_STACKS_ENABLED
  ASSERT_EQ(err2.StackId(), ExpectedStackId());
  ASSERT_EQ(err2.FrameId(), 2u);
  ASSERT_EQ(err2.NSResult(), NS_ERROR_FAILURE);
#else
  ASSERT_EQ(err2, NS_ERROR_FAILURE);
#endif

  OkOrErr okOrErr3 = okOrErr2.propagateErr();
  const auto& err3 = okOrErr3.inspectErr();

#ifdef QM_ERROR_STACKS_ENABLED
  ASSERT_EQ(err3.StackId(), ExpectedStackId());
  ASSERT_EQ(err3.FrameId(), 3u);
  ASSERT_EQ(err3.NSResult(), NS_ERROR_FAILURE);
#else
  ASSERT_EQ(err3, NS_ERROR_FAILURE);
#endif
}

TEST(DOM_Quota_ResultExtensions_ToResultGet, Lambda_NoInput)
{
  auto res = ToResultGet<int32_t>([](nsresult* aRv) -> int32_t {
    *aRv = NS_OK;
    return 42;
  });

  static_assert(std::is_same_v<decltype(res), Result<int32_t, nsresult>>);

  EXPECT_TRUE(res.isOk());
  EXPECT_EQ(res.unwrap(), 42);
}

TEST(DOM_Quota_ResultExtensions_ToResultGet, Lambda_NoInput_Err)
{
  auto res = ToResultGet<int32_t>([](nsresult* aRv) -> int32_t {
    *aRv = NS_ERROR_FAILURE;
    return -1;
  });

  static_assert(std::is_same_v<decltype(res), Result<int32_t, nsresult>>);

  EXPECT_TRUE(res.isErr());
  EXPECT_EQ(res.unwrapErr(), NS_ERROR_FAILURE);
}

TEST(DOM_Quota_ResultExtensions_ToResultGet, Lambda_WithInput)
{
  auto res = ToResultGet<int32_t>(
      [](int32_t aValue, nsresult* aRv) -> int32_t {
        *aRv = NS_OK;
        return aValue * 2;
      },
      42);

  static_assert(std::is_same_v<decltype(res), Result<int32_t, nsresult>>);

  EXPECT_TRUE(res.isOk());
  EXPECT_EQ(res.unwrap(), 84);
}

TEST(DOM_Quota_ResultExtensions_ToResultGet, Lambda_WithInput_Err)
{
  auto res = ToResultGet<int32_t>(
      [](int32_t aValue, nsresult* aRv) -> int32_t {
        *aRv = NS_ERROR_FAILURE;
        return -1;
      },
      42);

  static_assert(std::is_same_v<decltype(res), Result<int32_t, nsresult>>);

  EXPECT_TRUE(res.isErr());
  EXPECT_EQ(res.unwrapErr(), NS_ERROR_FAILURE);
}

TEST(DOM_Quota_ResultExtensions_ToResultGet, Lambda_NoInput_Macro_Typed)
{
  auto res = MOZ_TO_RESULT_GET_TYPED(int32_t, [](nsresult* aRv) -> int32_t {
    *aRv = NS_OK;
    return 42;
  });

  static_assert(std::is_same_v<decltype(res), Result<int32_t, nsresult>>);

  EXPECT_TRUE(res.isOk());
  EXPECT_EQ(res.unwrap(), 42);
}

TEST(DOM_Quota_ResultExtensions_ToResultGet, Lambda_NoInput_Macro_Typed_Parens)
{
  auto res =
      MOZ_TO_RESULT_GET_TYPED((std::pair<int32_t, int32_t>),
                              [](nsresult* aRv) -> std::pair<int32_t, int32_t> {
                                *aRv = NS_OK;
                                return std::pair{42, 42};
                              });

  static_assert(std::is_same_v<decltype(res),
                               Result<std::pair<int32_t, int32_t>, nsresult>>);

  EXPECT_TRUE(res.isOk());
  EXPECT_EQ(res.unwrap(), (std::pair{42, 42}));
}

TEST(DOM_Quota_ResultExtensions_ToResultGet, Lambda_NoInput_Err_Macro_Typed)
{
  auto res = MOZ_TO_RESULT_GET_TYPED(int32_t, [](nsresult* aRv) -> int32_t {
    *aRv = NS_ERROR_FAILURE;
    return -1;
  });

  static_assert(std::is_same_v<decltype(res), Result<int32_t, nsresult>>);

  EXPECT_TRUE(res.isErr());
  EXPECT_EQ(res.unwrapErr(), NS_ERROR_FAILURE);
}

TEST(DOM_Quota_ResultExtensions_ToResultGet, Lambda_WithInput_Macro_Typed)
{
  auto res = MOZ_TO_RESULT_GET_TYPED(
      int32_t,
      [](int32_t aValue, nsresult* aRv) -> int32_t {
        *aRv = NS_OK;
        return aValue * 2;
      },
      42);

  static_assert(std::is_same_v<decltype(res), Result<int32_t, nsresult>>);

  EXPECT_TRUE(res.isOk());
  EXPECT_EQ(res.unwrap(), 84);
}

TEST(DOM_Quota_ResultExtensions_ToResultGet, Lambda_WithInput_Err_Macro_Typed)
{
  auto res = MOZ_TO_RESULT_GET_TYPED(
      int32_t,
      [](int32_t aValue, nsresult* aRv) -> int32_t {
        *aRv = NS_ERROR_FAILURE;
        return -1;
      },
      42);

  static_assert(std::is_same_v<decltype(res), Result<int32_t, nsresult>>);

  EXPECT_TRUE(res.isErr());
  EXPECT_EQ(res.unwrapErr(), NS_ERROR_FAILURE);
}

TEST(DOM_Quota_ResultExtensions_ToResultInvoke, Lambda_NoInput_Complex)
{
  TestClass foo;

  // success
  {
    auto valOrErr =
        ToResultInvoke<std::pair<int, int>>([&foo](std::pair<int, int>* out) {
          return foo.NonOverloadedNoInputComplex(out);
        });
    static_assert(std::is_same_v<decltype(valOrErr),
                                 Result<std::pair<int, int>, nsresult>>);
    ASSERT_TRUE(valOrErr.isOk());
    ASSERT_EQ((std::pair{TestClass::kTestValue, TestClass::kTestValue}),
              valOrErr.unwrap());
  }

  // failure
  {
    auto valOrErr =
        ToResultInvoke<std::pair<int, int>>([&foo](std::pair<int, int>* out) {
          return foo.NonOverloadedNoInputFailsComplex(out);
        });
    static_assert(std::is_same_v<decltype(valOrErr),
                                 Result<std::pair<int, int>, nsresult>>);
    ASSERT_TRUE(valOrErr.isErr());
    ASSERT_EQ(NS_ERROR_FAILURE, valOrErr.unwrapErr());
  }
}

TEST(DOM_Quota_ResultExtensions_ToResultInvoke,
     Lambda_NoInput_Complex_Macro_Typed)
{
  TestClass foo;

  // success
  {
    auto valOrErr = MOZ_TO_RESULT_INVOKE_TYPED(
        (std::pair<int, int>), [&foo](std::pair<int, int>* out) {
          return foo.NonOverloadedNoInputComplex(out);
        });
    static_assert(std::is_same_v<decltype(valOrErr),
                                 Result<std::pair<int, int>, nsresult>>);
    ASSERT_TRUE(valOrErr.isOk());
    ASSERT_EQ((std::pair{TestClass::kTestValue, TestClass::kTestValue}),
              valOrErr.unwrap());
  }

  // failure
  {
    auto valOrErr = MOZ_TO_RESULT_INVOKE_TYPED(
        (std::pair<int, int>), [&foo](std::pair<int, int>* out) {
          return foo.NonOverloadedNoInputFailsComplex(out);
        });
    static_assert(std::is_same_v<decltype(valOrErr),
                                 Result<std::pair<int, int>, nsresult>>);
    ASSERT_TRUE(valOrErr.isErr());
    ASSERT_EQ(NS_ERROR_FAILURE, valOrErr.unwrapErr());
  }
}