Add quiet fixture for commandline tests 10/35610/2
authorPawel Wieczorek <p.wieczorek2@samsung.com>
Tue, 17 Feb 2015 15:29:15 +0000 (16:29 +0100)
committerPawel Wieczorek <p.wieczorek2@samsung.com>
Tue, 24 Feb 2015 15:02:32 +0000 (16:02 +0100)
This patch introduces fixture which suppresses printing output to
std::cout or std::cerr. Data is redirected to temporary buffers and
accessible from there.

Change-Id: Ia1b8b240be95d1d672a56cd9eaf6e13320bb375b

test/test-common/QuietCommandlineTest.h [new file with mode: 0644]

diff --git a/test/test-common/QuietCommandlineTest.h b/test/test-common/QuietCommandlineTest.h
new file mode 100644 (file)
index 0000000..5cba131
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed 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.
+ */
+/**
+ * @file        test/test-common/QuietCommandlineTest.h
+ * @author      Pawel Wieczorek <p.wieczorek2@samsung.com>
+ * @version     1.0
+ * @brief       Fixture for tests of commandline parsers with no output to std::cout nor std::cerr
+ */
+
+#ifndef TEST_TEST_COMMON_QUIETCOMMANDLINETEST_H_
+#define TEST_TEST_COMMON_QUIETCOMMANDLINETEST_H_
+
+#include <iostream>
+#include <memory>
+#include <sstream>
+#include <string>
+
+#include "BaseCommandlineTest.h"
+
+class QuietCommandlineTest : public BaseCommandlineTest {
+public:
+    void clearOutput(void) {
+        m_cerr.str(std::string());
+        m_cout.str(std::string());
+    }
+
+    void getOutput(std::string &out, std::string &err) const {
+        err = m_cerr.str();
+        out = m_cout.str();
+    }
+
+protected:
+    virtual void SetUp(void) {
+        m_cerrBackup.reset(std::cerr.rdbuf());
+        std::cerr.rdbuf(m_cerr.rdbuf());
+
+        m_coutBackup.reset(std::cout.rdbuf());
+        std::cout.rdbuf(m_cout.rdbuf());
+    }
+
+    virtual void TearDown(void) {
+        destroy_argv();
+        std::cerr.rdbuf(m_cerrBackup.release());
+        std::cout.rdbuf(m_coutBackup.release());
+    }
+
+    std::unique_ptr<std::streambuf> m_cerrBackup;
+    std::unique_ptr<std::streambuf> m_coutBackup;
+    std::stringstream m_cerr;
+    std::stringstream m_cout;
+};
+
+#endif /* TEST_TEST_COMMONS_QUIETCOMMANDLINETEST_H_ */