|
| 1 | +/* |
| 2 | + * Cppcheck - A tool for static C/C++ code analysis |
| 3 | + * Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <iostream> |
| 20 | +#include "testsuite.h" |
| 21 | +#include "cmdlineparser.h" |
| 22 | +#include "settings.h" |
| 23 | + |
| 24 | +extern std::ostringstream errout; |
| 25 | +extern std::ostringstream output; |
| 26 | + |
| 27 | +class RedirectInputOutput |
| 28 | +{ |
| 29 | +public: |
| 30 | + RedirectInputOutput() |
| 31 | + { |
| 32 | + // flush all old output |
| 33 | + std::cout.flush(); |
| 34 | + std::cerr.flush(); |
| 35 | + |
| 36 | + _oldCout = std::cout.rdbuf(); // back up cout's streambuf |
| 37 | + _oldCerr = std::cerr.rdbuf(); // back up cerr's streambuf |
| 38 | + |
| 39 | + std::cout.rdbuf(_out.rdbuf()); // assign streambuf to cout |
| 40 | + std::cerr.rdbuf(_err.rdbuf()); // assign streambuf to cerr |
| 41 | + } |
| 42 | + |
| 43 | + ~RedirectInputOutput() |
| 44 | + { |
| 45 | + std::cout.rdbuf(_oldCout); // restore cout's original streambuf |
| 46 | + std::cerr.rdbuf(_oldCerr); // restore cerrs's original streambuf |
| 47 | + |
| 48 | + errout << _err.str(); |
| 49 | + output << _out.str(); |
| 50 | + } |
| 51 | + |
| 52 | +private: |
| 53 | + std::stringstream _out; |
| 54 | + std::stringstream _err; |
| 55 | + std::streambuf* _oldCout; |
| 56 | + std::streambuf *_oldCerr; |
| 57 | +}; |
| 58 | + |
| 59 | +#define REDIRECT RedirectInputOutput redir; |
| 60 | + |
| 61 | +class TestCmdlineParser : public TestFixture |
| 62 | +{ |
| 63 | +public: |
| 64 | + TestCmdlineParser() : TestFixture("TestCmdlineParser") |
| 65 | + { } |
| 66 | + |
| 67 | +private: |
| 68 | + |
| 69 | + void run() |
| 70 | + { |
| 71 | + TEST_CASE(nooptions); |
| 72 | + TEST_CASE(helpshort); |
| 73 | + TEST_CASE(helplong); |
| 74 | + TEST_CASE(showversion); |
| 75 | + } |
| 76 | + |
| 77 | + void nooptions() |
| 78 | + { |
| 79 | + REDIRECT; |
| 80 | + const char *argv[] = {"cppcheck"}; |
| 81 | + Settings settings; |
| 82 | + CmdLineParser parser(&settings); |
| 83 | + ASSERT(parser.ParseFromArgs(1, argv)); |
| 84 | + ASSERT_EQUALS(true, parser.GetShowHelp()); |
| 85 | + } |
| 86 | + |
| 87 | + void helpshort() |
| 88 | + { |
| 89 | + REDIRECT; |
| 90 | + const char *argv[] = {"cppcheck", "-h"}; |
| 91 | + Settings settings; |
| 92 | + CmdLineParser parser(&settings); |
| 93 | + ASSERT(parser.ParseFromArgs(2, argv)); |
| 94 | + ASSERT_EQUALS(true, parser.GetShowHelp()); |
| 95 | + } |
| 96 | + |
| 97 | + void helplong() |
| 98 | + { |
| 99 | + REDIRECT; |
| 100 | + const char *argv[] = {"cppcheck", "--help"}; |
| 101 | + Settings settings; |
| 102 | + CmdLineParser parser(&settings); |
| 103 | + ASSERT(parser.ParseFromArgs(2, argv)); |
| 104 | + ASSERT_EQUALS(true, parser.GetShowHelp()); |
| 105 | + } |
| 106 | + |
| 107 | + void showversion() |
| 108 | + { |
| 109 | + REDIRECT; |
| 110 | + const char *argv[] = {"cppcheck", "--version"}; |
| 111 | + Settings settings; |
| 112 | + CmdLineParser parser(&settings); |
| 113 | + ASSERT(parser.ParseFromArgs(2, argv)); |
| 114 | + ASSERT_EQUALS(true, parser.GetShowVersion()); |
| 115 | + } |
| 116 | +}; |
| 117 | + |
| 118 | +REGISTER_TEST(TestCmdlineParser) |
0 commit comments