PragmaTwice commented on code in PR #768: URL: https://github.com/apache/incubator-kvrocks/pull/768#discussion_r938446284
########## tests/cppunit/status_test.cc: ########## @@ -0,0 +1,146 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#include <gtest/gtest.h> + +#include <memory> +#include <status.h> + +TEST(StatusOr, Scalar) { + auto f = [](int x) -> StatusOr<int> { + if (x > 10) { + return {Status::NotOK, "x large than 10"}; + } + + return 2 * x + 5; + }; + + ASSERT_EQ(*f(1), 7); + ASSERT_EQ(*f(5), 15); + ASSERT_EQ(f(7).GetValue(), 19); + ASSERT_EQ(f(7).GetCode(), Status::cOK); + ASSERT_EQ(f(7).Msg(), "ok"); + ASSERT_TRUE(f(6)); + ASSERT_EQ(f(11).GetCode(), Status::NotOK); + ASSERT_EQ(f(11).Msg(), "x large than 10"); Review Comment: BTW, The classic usage of `StatusOr<T>` we expected is: ```c++ // 1 StatusOr<T> someProcess(...); // as a return type // 2 T val; ... return val; // return val typed T to StatusOr<T> // 3 return {Status::someErrCode, "msg..."}; // return an error status // 4 auto res = someProcess(...); // get result typed StatusOr<T> if(!res) return res; // forward it if it is an error status doSomething(*res); // process the value inside the StatusOr ``` Use `StatusOr` as an rvalue is totally (well) supported, but everyone should be aware of what they are doing, otherwise just follow the classic usage is fine. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
