#include <string>

#include <scpp_os.hpp>
#include <scpp_unittest.hpp>
#include <scpp_env.hpp>

using namespace scpp;


/***
 * Env 単体テスト.
 */
class EnvTest : public TestCase
{
	public:
		EnvTest() {}
		~EnvTest() {}

		void testGetEnvExistsKey()
		{
			std::string buf;

			Env::setEnv("UT_KEY", "UT_VALUE");
			bool ret = Env::getEnv(&buf, "UT_KEY");
			Assertion::assertTrue(ret);
			Assertion::assertEquals("UT_VALUE", buf);
			
		}

		void testGetEnvNoExistsKey()
		{
			std::string buf;
			bool ret = Env::getEnv(&buf,"XXXXX_____NO___KEY_____");
			Assertion::assertFalse(ret);
			Assertion::assertEquals("", buf);

		}

		void testGetEnvKeyIncludeEquals()
		{
			Env::setEnv("UT_KEY", "UT_VALUE");

			std::string buf;
			bool ret = Env::getEnv(&buf,"UT_KEY=AAA");
			Assertion::assertTrue(ret);
			Assertion::assertEquals("UT_VALUE", buf);

		}

		void testSetEnvKeyIncludeEquals()
		{
			bool ret = Env::setEnv("UT_KEY=A", "UT_VALUE");
			Assertion::assertFalse(ret);

		}

		void testSetOverwrite()
		{
			std::string buf;

			// 一回目
			bool ret = Env::setEnv("UT_KEY", "UT_VALUE");
			Assertion::assertTrue(ret);
			ret = Env::getEnv(&buf, "UT_KEY");
			Assertion::assertTrue(ret);
			Assertion::assertEquals("UT_VALUE", buf);

			// 上書き(上書き指定なし)
			ret = Env::setEnv("UT_KEY", "UT_VALUE_XXX");
			Assertion::assertTrue(ret);
			ret = Env::getEnv(&buf, "UT_KEY");
			Assertion::assertTrue(ret);
			Assertion::assertEquals("UT_VALUE", buf);

			// 上書き(上書き指定あり)
			ret = Env::setEnv("UT_KEY", "UT_VALUE_XXX", true);
			ret = Env::getEnv(&buf, "UT_KEY");
			Assertion::assertTrue(ret);
			Assertion::assertEquals("UT_VALUE_XXX", buf);

		}

		void testUnset()
		{
			std::string buf;

			bool ret = Env::setEnv("UT_KEY", "UT_VALUE", true);
			Assertion::assertTrue(ret);

			ret = Env::getEnv(&buf, "UT_KEY");
			Assertion::assertTrue(ret);
			Assertion::assertEquals("UT_VALUE", buf);

			Env::unsetEnv("UT_KEY");

			ret = Env::getEnv(&buf, "UT_KEY");
			Assertion::assertFalse(ret);

		}

		void suite()
		{
			RUN_TEST(testGetEnvExistsKey);
			RUN_TEST(testGetEnvNoExistsKey);
			RUN_TEST(testGetEnvKeyIncludeEquals);
			RUN_TEST(testSetEnvKeyIncludeEquals);
			RUN_TEST(testSetOverwrite);
			RUN_TEST(testUnset);
		}


};

