Newer
Older
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#ifndef MULTIFILEVALIDATORTEST_H_
#define MULTIFILEVALIDATORTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidKernel/MultiFileValidator.h"
#include <Poco/File.h>
#include <string>
namespace {
// Convenience function, that wraps a string in a vector and adds it to a vector
// of vectors.
void addSingleFile(std::vector<std::vector<std::string>> &fileNames,
const std::string &fileNameToAdd) {
const std::vector<std::string> fileNameList(1, fileNameToAdd);
fileNames.emplace_back(fileNameList);
using namespace Mantid::Kernel;
class MultiFileValidatorTest : public CxxTest::TestSuite {
public:
std::vector<std::string> vec{"raw", "RAW"};
FileValidator v(vec);
// File extensions are converted to lowercase so should have one unique
// extension
TS_ASSERT_EQUALS(v.allowedValues().size(), 1);
FileValidator v({"raw", "RAW"});
// File extensions are converted to lowercase so should have one unique
// extension
TS_ASSERT_EQUALS(copy.allowedValues().size(), 1);
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
void testPassesOnExistentFiles() {
// Create two pairs of files, one with the extension within the validator
// and one without.
const std::string file_stub = "scratch.";
const std::string ext1 = "txt";
const std::string ext2 = "raw";
Poco::File txt_file_1(file_stub + "_1" + ext1);
Poco::File txt_file_2(file_stub + "_2" + ext1);
Poco::File raw_file_1(file_stub + "_1" + ext2);
Poco::File raw_file_2(file_stub + "_2" + ext2);
std::vector<std::vector<std::string>> txt_files;
std::vector<std::vector<std::string>> raw_files;
try {
txt_file_1.createFile();
txt_file_2.createFile();
raw_file_1.createFile();
raw_file_2.createFile();
} catch (std::exception &) {
TS_FAIL(
"Error creating test file for \"testPassesOnExistentFile\" test.");
}
addSingleFile(txt_files, txt_file_1.path());
addSingleFile(txt_files, txt_file_2.path());
addSingleFile(raw_files, txt_file_1.path());
addSingleFile(raw_files, txt_file_2.path());
// FileValidator will suggest txt files as correct extension
std::vector<std::string> vec(1, "txt");
MultiFileValidator v1(vec);
TS_ASSERT_EQUALS(v1.isValid(txt_files), "");
// Not correct extension but the file exists so we allow it
TS_ASSERT_EQUALS(v1.isValid(raw_files), "");
txt_file_1.remove();
txt_file_2.remove();
raw_file_1.remove();
raw_file_2.remove();
}
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
void testFailsOnSomeNonExistentFiles() {
// Create two files, numbered 1 and 3.
const std::string file_stub = "scratch.";
const std::string ext = "txt";
Poco::File txt_file_1(file_stub + "_1" + ext);
Poco::File txt_file_3(file_stub + "_3" + ext);
std::vector<std::vector<std::string>> txt_files;
try {
txt_file_1.createFile();
txt_file_3.createFile();
} catch (std::exception &) {
TS_FAIL(
"Error creating test file for \"testPassesOnExistentFile\" test.");
}
addSingleFile(txt_files, txt_file_1.path());
addSingleFile(txt_files, "doesNotExist_2.txt");
addSingleFile(txt_files, txt_file_3.path());
addSingleFile(txt_files, "doesNotExist_4.txt");
// FileValidator will suggest txt files as correct extension
std::vector<std::string> vec(1, "txt");
MultiFileValidator v(vec);
TS_ASSERT_EQUALS(v.isValid(txt_files), "Could not validate the following "
"file(s): doesNotExist_2.txt, "
"doesNotExist_4.txt");
// Clean up.
txt_file_1.remove();
txt_file_3.remove();
}
void testFailsOnNoFiles() {
MultiFileValidator file_val;
TS_ASSERT_EQUALS(
file_val.isValid(std::vector<std::vector<std::string>>()).empty(),
false);
}
void testFailsOnNonExistingFiles() {
std::vector<std::string> vec{"foo"};
MultiFileValidator file_val(vec);
std::vector<std::vector<std::string>> file{{"myJunkFile.foo"}};
TS_ASSERT(!file_val.isValid(file).empty());
}
void testPassesOnNonExistingFiles() {
std::vector<std::string> vec{"foo"};
std::vector<std::vector<std::string>> file{{"myJunkFile.foo"}};
TS_ASSERT(file_val.isValid(file).empty());
}
};
#endif /*MULTIFILEVALIDATORTEST_H_*/