|
| 1 | +// MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2023 Jussi Lind <jussi.lind@iki.fi> |
| 4 | +// |
| 5 | +// https://github.com/juzzlin/Argengine |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in all |
| 15 | +// copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +// SOFTWARE. |
| 24 | + |
| 25 | +#include "../../argengine.hpp" |
| 26 | + |
| 27 | +// Don't compile asserts away |
| 28 | +#ifdef NDEBUG |
| 29 | + #undef NDEBUG |
| 30 | +#endif |
| 31 | + |
| 32 | +#include <cassert> |
| 33 | +#include <cstdlib> |
| 34 | +#include <iostream> |
| 35 | + |
| 36 | +using juzzlin::Argengine; |
| 37 | + |
| 38 | +const auto name = "Argengine"; |
| 39 | + |
| 40 | +void testConflictingArguments_ConflictingArgumentsGiven_ShouldFail() |
| 41 | +{ |
| 42 | + Argengine ae({ "test", "foo", "bar" }); |
| 43 | + bool fooCalled {}; |
| 44 | + ae.addOption({ "foo" }, [&] { |
| 45 | + fooCalled = true; |
| 46 | + }); |
| 47 | + bool barCalled {}; |
| 48 | + ae.addOption({ "bar" }, [&] { |
| 49 | + barCalled = true; |
| 50 | + }); |
| 51 | + ae.addConflictingOptions({ "foo", "bar" }); |
| 52 | + std::string error; |
| 53 | + try { |
| 54 | + ae.parse(); |
| 55 | + } catch (std::runtime_error & e) { |
| 56 | + error = e.what(); |
| 57 | + } |
| 58 | + assert(!fooCalled); |
| 59 | + assert(!barCalled); |
| 60 | + assert(error == std::string(name) + ": Conflicting options: 'bar', 'foo'. These options cannot coexist."); |
| 61 | +} |
| 62 | + |
| 63 | +void testConflictingArguments_ConflictingArgumentsSetButNotEnoughGiven_ShouldSucceed() |
| 64 | +{ |
| 65 | + Argengine ae({ "test", "foo" }); |
| 66 | + bool fooCalled {}; |
| 67 | + ae.addOption({ "foo" }, [&] { |
| 68 | + fooCalled = true; |
| 69 | + }); |
| 70 | + ae.addConflictingOptions({ "foo", "bar" }); |
| 71 | + std::string error; |
| 72 | + try { |
| 73 | + ae.parse(); |
| 74 | + } catch (std::runtime_error & e) { |
| 75 | + error = e.what(); |
| 76 | + } |
| 77 | + assert(fooCalled); |
| 78 | + assert(error.empty()); |
| 79 | +} |
| 80 | + |
| 81 | +void testConflictingArguments_ConflictingArgumentsGiven_ShouldNotReferToVariants() |
| 82 | +{ |
| 83 | + Argengine ae({ "test", "foo", "bar" }); |
| 84 | + bool fooCalled {}; |
| 85 | + ae.addOption({ "foo", "-f" }, [&] { |
| 86 | + fooCalled = true; |
| 87 | + }); |
| 88 | + bool barCalled {}; |
| 89 | + ae.addOption({ "-b", "bar" }, [&] { |
| 90 | + barCalled = true; |
| 91 | + }); |
| 92 | + ae.addConflictingOptions({ "foo", "bar" }); |
| 93 | + std::string error; |
| 94 | + try { |
| 95 | + ae.parse(); |
| 96 | + } catch (std::runtime_error & e) { |
| 97 | + error = e.what(); |
| 98 | + } |
| 99 | + assert(!fooCalled); |
| 100 | + assert(!barCalled); |
| 101 | + assert(error == std::string(name) + ": Conflicting options: 'bar', 'foo'. These options cannot coexist."); |
| 102 | +} |
| 103 | + |
| 104 | +int main(int, char **) |
| 105 | +{ |
| 106 | + testConflictingArguments_ConflictingArgumentsGiven_ShouldFail(); |
| 107 | + |
| 108 | + testConflictingArguments_ConflictingArgumentsSetButNotEnoughGiven_ShouldSucceed(); |
| 109 | + |
| 110 | + testConflictingArguments_ConflictingArgumentsGiven_ShouldNotReferToVariants(); |
| 111 | + |
| 112 | + return EXIT_SUCCESS; |
| 113 | +} |
0 commit comments