|
| 1 | +/* |
| 2 | + * Copyright 2026 LiveKit |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/// @file test_result.cpp |
| 18 | +/// @brief Unit tests for the Result<T, E> and Result<void, E> types. |
| 19 | +/// |
| 20 | +/// Covers the invariants documented in result.h: |
| 21 | +/// - ok() / has_error() / bool conversion correctness |
| 22 | +/// - value() and error() accessor semantics for lvalue, rvalue, and const |
| 23 | +/// overloads |
| 24 | +/// - Move construction and forwarding behaviour |
| 25 | +/// - void specialization |
| 26 | + |
| 27 | +#include <gtest/gtest.h> |
| 28 | +#include <livekit/result.h> |
| 29 | + |
| 30 | +#include <memory> |
| 31 | +#include <string> |
| 32 | +#include <utility> |
| 33 | + |
| 34 | +namespace livekit { |
| 35 | + |
| 36 | +// --------------------------------------------------------------------------- |
| 37 | +// Helpers |
| 38 | +// --------------------------------------------------------------------------- |
| 39 | + |
| 40 | +struct SimpleError { |
| 41 | + int code{0}; |
| 42 | + std::string message; |
| 43 | +}; |
| 44 | + |
| 45 | +// --------------------------------------------------------------------------- |
| 46 | +// Result<T, E> — success path |
| 47 | +// --------------------------------------------------------------------------- |
| 48 | + |
| 49 | +TEST(ResultTest, SuccessOkIsTrue) { |
| 50 | + auto r = Result<int, SimpleError>::success(42); |
| 51 | + EXPECT_TRUE(r.ok()); |
| 52 | +} |
| 53 | + |
| 54 | +TEST(ResultTest, SuccessHasErrorIsFalse) { |
| 55 | + auto r = Result<int, SimpleError>::success(42); |
| 56 | + EXPECT_FALSE(r.has_error()); |
| 57 | +} |
| 58 | + |
| 59 | +TEST(ResultTest, SuccessBoolConversionIsTrue) { |
| 60 | + auto r = Result<int, SimpleError>::success(42); |
| 61 | + EXPECT_TRUE(static_cast<bool>(r)); |
| 62 | +} |
| 63 | + |
| 64 | +TEST(ResultTest, SuccessValueMatchesInput) { |
| 65 | + auto r = Result<int, SimpleError>::success(99); |
| 66 | + EXPECT_EQ(r.value(), 99); |
| 67 | +} |
| 68 | + |
| 69 | +TEST(ResultTest, SuccessConstValueMatchesInput) { |
| 70 | + const auto r = Result<int, SimpleError>::success(7); |
| 71 | + EXPECT_EQ(r.value(), 7); |
| 72 | +} |
| 73 | + |
| 74 | +TEST(ResultTest, SuccessValueCanBeMutated) { |
| 75 | + auto r = Result<int, SimpleError>::success(1); |
| 76 | + r.value() = 100; |
| 77 | + EXPECT_EQ(r.value(), 100); |
| 78 | +} |
| 79 | + |
| 80 | +TEST(ResultTest, SuccessStringValue) { |
| 81 | + auto r = Result<std::string, SimpleError>::success("hello"); |
| 82 | + EXPECT_EQ(r.value(), "hello"); |
| 83 | +} |
| 84 | + |
| 85 | +TEST(ResultTest, SuccessMoveValueTransfersOwnership) { |
| 86 | + auto r = Result<std::unique_ptr<int>, SimpleError>::success(std::make_unique<int>(55)); |
| 87 | + auto ptr = std::move(r).value(); |
| 88 | + ASSERT_NE(ptr, nullptr); |
| 89 | + EXPECT_EQ(*ptr, 55); |
| 90 | +} |
| 91 | + |
| 92 | +// --------------------------------------------------------------------------- |
| 93 | +// Result<T, E> — failure path |
| 94 | +// --------------------------------------------------------------------------- |
| 95 | + |
| 96 | +TEST(ResultTest, FailureOkIsFalse) { |
| 97 | + auto r = Result<int, SimpleError>::failure(SimpleError{1, "oops"}); |
| 98 | + EXPECT_FALSE(r.ok()); |
| 99 | +} |
| 100 | + |
| 101 | +TEST(ResultTest, FailureHasErrorIsTrue) { |
| 102 | + auto r = Result<int, SimpleError>::failure(SimpleError{1, "oops"}); |
| 103 | + EXPECT_TRUE(r.has_error()); |
| 104 | +} |
| 105 | + |
| 106 | +TEST(ResultTest, FailureBoolConversionIsFalse) { |
| 107 | + auto r = Result<int, SimpleError>::failure(SimpleError{1, "oops"}); |
| 108 | + EXPECT_FALSE(static_cast<bool>(r)); |
| 109 | +} |
| 110 | + |
| 111 | +TEST(ResultTest, FailureErrorCodeMatchesInput) { |
| 112 | + auto r = Result<int, SimpleError>::failure(SimpleError{42, "bad"}); |
| 113 | + EXPECT_EQ(r.error().code, 42); |
| 114 | + EXPECT_EQ(r.error().message, "bad"); |
| 115 | +} |
| 116 | + |
| 117 | +TEST(ResultTest, FailureConstErrorMatchesInput) { |
| 118 | + const auto r = Result<int, SimpleError>::failure(SimpleError{3, "err"}); |
| 119 | + EXPECT_EQ(r.error().code, 3); |
| 120 | +} |
| 121 | + |
| 122 | +TEST(ResultTest, FailureMoveErrorTransfersOwnership) { |
| 123 | + auto r = Result<int, std::unique_ptr<SimpleError>>::failure(std::make_unique<SimpleError>(SimpleError{9, "moved"})); |
| 124 | + auto err = std::move(r).error(); |
| 125 | + ASSERT_NE(err, nullptr); |
| 126 | + EXPECT_EQ(err->code, 9); |
| 127 | +} |
| 128 | + |
| 129 | +TEST(ResultTest, FailureStringError) { |
| 130 | + auto r = Result<int, std::string>::failure("something went wrong"); |
| 131 | + EXPECT_EQ(r.error(), "something went wrong"); |
| 132 | +} |
| 133 | + |
| 134 | +// --------------------------------------------------------------------------- |
| 135 | +// Result<void, E> — success path |
| 136 | +// --------------------------------------------------------------------------- |
| 137 | + |
| 138 | +TEST(ResultVoidTest, SuccessOkIsTrue) { |
| 139 | + auto r = Result<void, SimpleError>::success(); |
| 140 | + EXPECT_TRUE(r.ok()); |
| 141 | +} |
| 142 | + |
| 143 | +TEST(ResultVoidTest, SuccessHasErrorIsFalse) { |
| 144 | + auto r = Result<void, SimpleError>::success(); |
| 145 | + EXPECT_FALSE(r.has_error()); |
| 146 | +} |
| 147 | + |
| 148 | +TEST(ResultVoidTest, SuccessBoolConversionIsTrue) { |
| 149 | + auto r = Result<void, SimpleError>::success(); |
| 150 | + EXPECT_TRUE(static_cast<bool>(r)); |
| 151 | +} |
| 152 | + |
| 153 | +TEST(ResultVoidTest, SuccessValueIsCallable) { |
| 154 | + auto r = Result<void, SimpleError>::success(); |
| 155 | + EXPECT_NO_THROW(r.value()); |
| 156 | +} |
| 157 | + |
| 158 | +// --------------------------------------------------------------------------- |
| 159 | +// Result<void, E> — failure path |
| 160 | +// --------------------------------------------------------------------------- |
| 161 | + |
| 162 | +TEST(ResultVoidTest, FailureOkIsFalse) { |
| 163 | + auto r = Result<void, SimpleError>::failure(SimpleError{5, "void fail"}); |
| 164 | + EXPECT_FALSE(r.ok()); |
| 165 | +} |
| 166 | + |
| 167 | +TEST(ResultVoidTest, FailureHasErrorIsTrue) { |
| 168 | + auto r = Result<void, SimpleError>::failure(SimpleError{5, "void fail"}); |
| 169 | + EXPECT_TRUE(r.has_error()); |
| 170 | +} |
| 171 | + |
| 172 | +TEST(ResultVoidTest, FailureBoolConversionIsFalse) { |
| 173 | + auto r = Result<void, SimpleError>::failure(SimpleError{5, "void fail"}); |
| 174 | + EXPECT_FALSE(static_cast<bool>(r)); |
| 175 | +} |
| 176 | + |
| 177 | +TEST(ResultVoidTest, FailureErrorMatchesInput) { |
| 178 | + auto r = Result<void, SimpleError>::failure(SimpleError{7, "nope"}); |
| 179 | + EXPECT_EQ(r.error().code, 7); |
| 180 | + EXPECT_EQ(r.error().message, "nope"); |
| 181 | +} |
| 182 | + |
| 183 | +TEST(ResultVoidTest, FailureMoveError) { |
| 184 | + auto r = Result<void, std::string>::failure("void error"); |
| 185 | + auto msg = std::move(r).error(); |
| 186 | + EXPECT_EQ(msg, "void error"); |
| 187 | +} |
| 188 | + |
| 189 | +// --------------------------------------------------------------------------- |
| 190 | +// if-result idiom |
| 191 | +// --------------------------------------------------------------------------- |
| 192 | + |
| 193 | +TEST(ResultTest, IfResultIdiomSuccessEntersBranch) { |
| 194 | + auto r = Result<int, SimpleError>::success(1); |
| 195 | + bool entered = false; |
| 196 | + if (r) { |
| 197 | + entered = true; |
| 198 | + } |
| 199 | + EXPECT_TRUE(entered); |
| 200 | +} |
| 201 | + |
| 202 | +TEST(ResultTest, IfResultIdiomFailureSkipsBranch) { |
| 203 | + auto r = Result<int, SimpleError>::failure(SimpleError{}); |
| 204 | + bool entered = false; |
| 205 | + if (r) { |
| 206 | + entered = true; |
| 207 | + } |
| 208 | + EXPECT_FALSE(entered); |
| 209 | +} |
| 210 | + |
| 211 | +} // namespace livekit |
0 commit comments