env_ut.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <util/generic/string.h>
  3. #include "env.h"
  4. Y_UNIT_TEST_SUITE(EnvTest) {
  5. Y_UNIT_TEST(GetSetEnvTest) {
  6. TString key = "util_GETENV_TestVar";
  7. TString value = "Some value for env var";
  8. TString def = "Some default value for env var";
  9. // first of all, it should be clear
  10. UNIT_ASSERT_VALUES_EQUAL(GetEnv(key), TString());
  11. UNIT_ASSERT_VALUES_EQUAL(GetEnv(key, def), def);
  12. SetEnv(key, value);
  13. // set and see what value we get here
  14. UNIT_ASSERT_VALUES_EQUAL(GetEnv(key), value);
  15. UNIT_ASSERT_VALUES_EQUAL(GetEnv(key, def), value);
  16. // set empty value
  17. SetEnv(key, TString());
  18. UNIT_ASSERT_VALUES_EQUAL(GetEnv(key), TString());
  19. // check for long values, see IGNIETFERRO-214
  20. TString longKey = "util_GETENV_TestVarLong";
  21. TString longValue{1500, 't'};
  22. UNIT_ASSERT_VALUES_EQUAL(GetEnv(longKey), TString());
  23. SetEnv(longKey, longValue);
  24. UNIT_ASSERT_VALUES_EQUAL(GetEnv(longKey), longValue);
  25. SetEnv(longKey, TString());
  26. UNIT_ASSERT_VALUES_EQUAL(GetEnv(longKey), TString());
  27. }
  28. Y_UNIT_TEST(TryGetEnv) {
  29. TString key = "util_TryGetEnv_var";
  30. UNIT_ASSERT_NO_EXCEPTION(TryGetEnv(key));
  31. SetEnv(key, "value");
  32. UNIT_ASSERT(TryGetEnv(key).Defined());
  33. UNIT_ASSERT_VALUES_EQUAL(*TryGetEnv(key), "value");
  34. UnsetEnv(key);
  35. UNIT_ASSERT(TryGetEnv(key).Empty());
  36. }
  37. Y_UNIT_TEST(UnsetEnv) {
  38. TString key = "util_UnsetEnv_var";
  39. SetEnv(key, "value");
  40. UnsetEnv(key);
  41. UNIT_ASSERT_VALUES_EQUAL(GetEnv(key, "default_value"), "default_value");
  42. }
  43. Y_UNIT_TEST(UnsetNonexistingEnv) {
  44. TString key = "util_UnsetNonexistingEnv_var";
  45. UNIT_ASSERT_NO_EXCEPTION(UnsetEnv(key));
  46. UNIT_ASSERT_NO_EXCEPTION(UnsetEnv(key));
  47. }
  48. Y_UNIT_TEST(SetEnvInvalidName) {
  49. UNIT_ASSERT_EXCEPTION(SetEnv("", "value"), yexception);
  50. UNIT_ASSERT_EXCEPTION(SetEnv("A=B", "C=D"), yexception);
  51. }
  52. } // Y_UNIT_TEST_SUITE(EnvTest)