filelist_ut.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "dirut.h"
  2. #include "filelist.h"
  3. #include "tempdir.h"
  4. #include <library/cpp/testing/unittest/registar.h>
  5. #include <util/system/file.h>
  6. #include <util/generic/string.h>
  7. class TFileListTest: public TTestBase {
  8. UNIT_TEST_SUITE(TFileListTest);
  9. UNIT_TEST(TestSimple);
  10. UNIT_TEST(TestPrefix);
  11. UNIT_TEST_SUITE_END();
  12. public:
  13. void TestSimple();
  14. void TestPrefix();
  15. };
  16. void TFileListTest::TestSimple() {
  17. TTempDir tempDir("nonexistingdir");
  18. MakeDirIfNotExist((tempDir() + LOCSLASH_S "subdir").data());
  19. TFile((tempDir() + LOCSLASH_S "subdir" LOCSLASH_S "file").data(), CreateAlways);
  20. TFileList fileList;
  21. fileList.Fill(tempDir().data(), "", "", 1000);
  22. TString fileName(fileList.Next());
  23. UNIT_ASSERT_EQUAL(fileName, "subdir" LOCSLASH_S "file");
  24. UNIT_ASSERT_EQUAL(fileList.Next(), nullptr);
  25. }
  26. void TFileListTest::TestPrefix() {
  27. TTempDir tempDir("nonexistingdir");
  28. TFile((tempDir() + LOCSLASH_S "good_file1").data(), CreateAlways);
  29. TFile((tempDir() + LOCSLASH_S "good_file2").data(), CreateAlways);
  30. TFile((tempDir() + LOCSLASH_S "bad_file1").data(), CreateAlways);
  31. TFile((tempDir() + LOCSLASH_S "bad_file2").data(), CreateAlways);
  32. const bool SORT = true;
  33. TFileList fileList;
  34. {
  35. fileList.Fill(tempDir().data(), "good_file", SORT);
  36. UNIT_ASSERT_EQUAL(TString(fileList.Next()), "good_file1");
  37. UNIT_ASSERT_EQUAL(TString(fileList.Next()), "good_file2");
  38. UNIT_ASSERT_EQUAL(fileList.Next(), nullptr);
  39. }
  40. {
  41. fileList.Fill(tempDir().data(), "bad_file", SORT);
  42. UNIT_ASSERT_EQUAL(TString(fileList.Next()), "bad_file1");
  43. UNIT_ASSERT_EQUAL(TString(fileList.Next()), "bad_file2");
  44. UNIT_ASSERT_EQUAL(fileList.Next(), nullptr);
  45. }
  46. }
  47. UNIT_TEST_SUITE_REGISTRATION(TFileListTest);