Rework envMock
Instead of mocking all the functions, only mock std::getenv.
Now each test only needs to provide an EnvImpl that delegate calls to
mockEnv in order to inject dependencies on std::getenv. This for several
reasons:
1. Any call to env::getEnv() will be calling the real implementation of
the C++ code, and testing real code is better than testing mocks.
2. It is easier to write a fake class that takes a config string which
can greatly simplify test cases.
3. We can now write unit tests that ensure the number of times
std::getenv gets called (should be once, but multiple times right now).
Tested: unit tests still pass
Signed-off-by: Kun Yi <kunyi731@gmail.com>
Change-Id: I3e5aff7fa5d025de1b8ae798af43b97d31151ab9
diff --git a/test/env_mock.hpp b/test/env_mock.hpp
index 4cf232f..9aad8c1 100644
--- a/test/env_mock.hpp
+++ b/test/env_mock.hpp
@@ -1,40 +1,20 @@
#pragma once
-#include "sensorset.hpp"
+#include "env.hpp"
#include <string>
#include <gmock/gmock.h>
-class EnvInterface
+namespace env
+{
+
+class EnvMock : public Env
{
public:
- virtual ~EnvInterface() = default;
-
- virtual std::string getEnv(const char* key) const = 0;
- virtual std::string getEnv(const char* prefix,
- const SensorSet::key_type& sensor) const = 0;
- virtual std::string getEnv(const char* prefix, const std::string& type,
- const std::string& id) const = 0;
- virtual std::string
- getIndirectID(std::string path, const std::string& fileSuffix,
- const SensorSet::key_type& sensor) const = 0;
+ MOCK_CONST_METHOD1(get, const char*(const char*));
};
-class EnvMock : public EnvInterface
-{
- public:
- virtual ~EnvMock() = default;
+static inline EnvMock mockEnv;
- MOCK_CONST_METHOD1(getEnv, std::string(const char*));
- MOCK_CONST_METHOD2(getEnv,
- std::string(const char*, const SensorSet::key_type&));
- MOCK_CONST_METHOD3(getEnv, std::string(const char*, const std::string&,
- const std::string&));
- MOCK_CONST_METHOD3(getIndirectID,
- std::string(std::string, const std::string&,
- const SensorSet::key_type&));
-};
-
-// Set this before each test that hits a call to getEnv().
-extern EnvInterface* envIntf;
+} // namespace env