aboutsummaryrefslogtreecommitdiffstats
path: root/util/folder/filelist_ut.cpp
blob: 295c358c8a2b7b95d03b8e5213bc7dffcdb4e00a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "dirut.h"
#include "filelist.h"
#include "tempdir.h"

#include <library/cpp/testing/unittest/registar.h>

#include <util/system/file.h>
#include <util/generic/string.h>

class TFileListTest: public TTestBase {
    UNIT_TEST_SUITE(TFileListTest);
    UNIT_TEST(TestSimple);
    UNIT_TEST(TestPrefix); 
    UNIT_TEST_SUITE_END();

public:
    void TestSimple();
    void TestPrefix(); 
};

void TFileListTest::TestSimple() {
    TTempDir tempDir("nonexistingdir");
    MakeDirIfNotExist((tempDir() + LOCSLASH_S "subdir").data());
    TFile((tempDir() + LOCSLASH_S "subdir" LOCSLASH_S "file").data(), CreateAlways);

    TFileList fileList;
    fileList.Fill(tempDir().data(), "", "", 1000);
    TString fileName(fileList.Next());
    UNIT_ASSERT_EQUAL(fileName, "subdir" LOCSLASH_S "file");
    UNIT_ASSERT_EQUAL(fileList.Next(), nullptr);
}

void TFileListTest::TestPrefix() { 
    TTempDir tempDir("nonexistingdir"); 
    TFile((tempDir() + LOCSLASH_S "good_file1").data(), CreateAlways);
    TFile((tempDir() + LOCSLASH_S "good_file2").data(), CreateAlways);
    TFile((tempDir() + LOCSLASH_S "bad_file1").data(), CreateAlways);
    TFile((tempDir() + LOCSLASH_S "bad_file2").data(), CreateAlways);
 
    const bool SORT = true; 
    TFileList fileList; 
    { 
        fileList.Fill(tempDir().data(), "good_file", SORT);
        UNIT_ASSERT_EQUAL(TString(fileList.Next()), "good_file1");
        UNIT_ASSERT_EQUAL(TString(fileList.Next()), "good_file2");
        UNIT_ASSERT_EQUAL(fileList.Next(), nullptr); 
    } 
    { 
        fileList.Fill(tempDir().data(), "bad_file", SORT);
        UNIT_ASSERT_EQUAL(TString(fileList.Next()), "bad_file1");
        UNIT_ASSERT_EQUAL(TString(fileList.Next()), "bad_file2");
        UNIT_ASSERT_EQUAL(fileList.Next(), nullptr); 
    } 
} 
 
UNIT_TEST_SUITE_REGISTRATION(TFileListTest);