libopenraw
option.t.cpp
1/*
2 * libopenraw - option.t.cpp
3 *
4 * Copyright (C) 2017 Hubert Figuière
5 *
6 * This library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
19 */
22#include <boost/test/minimal.hpp>
23
24#include <stdlib.h>
25
26#include <string>
27
28#include "option.hpp"
29
30int test_main( int, char *[] ) // note the name!
31{
33
34 // Default, option is empty
35 BOOST_CHECK(result.empty());
36 bool unwrapped = false;
37 try {
38 result.value();
39 unwrapped = true;
40 } catch(std::runtime_error&) {
41 BOOST_CHECK(true);
42 } catch(...) {
43 BOOST_CHECK(false);
44 }
45 BOOST_CHECK(!unwrapped);
46 BOOST_CHECK(result.empty());
47
48 // Option with value
49 result = Option<std::string>("hello world");
50 BOOST_CHECK(!result.empty());
51 BOOST_CHECK(result);
52 BOOST_CHECK(result.value() == "hello world");
53 BOOST_CHECK(result.empty());
54 BOOST_CHECK(!result);
55 // try unwrapping again
56 unwrapped = false;
57 try {
58 result.value();
59 unwrapped = true;
60 } catch(std::runtime_error&) {
61 BOOST_CHECK(true);
62 } catch(...) {
63 BOOST_CHECK(false);
64 }
65 BOOST_CHECK(!unwrapped);
66 BOOST_CHECK(result.empty());
67 BOOST_CHECK(result.value_or("good bye") == "good bye");
68
69 return 0;
70}
71