aboutsummaryrefslogtreecommitdiffstats
path: root/test/TestFs.cxx
blob: aa840f8482a43243d839626420b6ebbce269c307 (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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Unit tests for src/fs/
 */

#include "config.h"
#include "fs/Glob.hxx"
#include "util/Error.hxx"

#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>

#include <string.h>
#include <stdlib.h>

#ifdef HAVE_CLASS_GLOB

class TestGlob : public CppUnit::TestFixture {
	CPPUNIT_TEST_SUITE(TestGlob);
	CPPUNIT_TEST(Basic);
	CPPUNIT_TEST(Asterisk);
	CPPUNIT_TEST(QuestionMark);
	CPPUNIT_TEST(Wildcard);
	CPPUNIT_TEST(PrefixWildcard);
	CPPUNIT_TEST(SuffixWildcard);
	CPPUNIT_TEST_SUITE_END();

public:
	void Basic() {
		const Glob glob("foo");
		CPPUNIT_ASSERT(glob.Check("foo"));
		CPPUNIT_ASSERT(!glob.Check("fooo"));
		CPPUNIT_ASSERT(!glob.Check("_foo"));
		CPPUNIT_ASSERT(!glob.Check("a/foo"));
		CPPUNIT_ASSERT(!glob.Check(""));
		CPPUNIT_ASSERT(!glob.Check("*"));
	}

	void Asterisk() {
		const Glob glob("*");
		CPPUNIT_ASSERT(glob.Check("foo"));
		CPPUNIT_ASSERT(glob.Check("bar"));
		CPPUNIT_ASSERT(glob.Check("*"));
		CPPUNIT_ASSERT(glob.Check("?"));
	}

	void QuestionMark() {
		const Glob glob("foo?bar");
		CPPUNIT_ASSERT(glob.Check("foo_bar"));
		CPPUNIT_ASSERT(glob.Check("foo?bar"));
		CPPUNIT_ASSERT(glob.Check("foo bar"));
		CPPUNIT_ASSERT(!glob.Check("foobar"));
		CPPUNIT_ASSERT(!glob.Check("foo__bar"));
	}

	void Wildcard() {
		const Glob glob("foo*bar");
		CPPUNIT_ASSERT(glob.Check("foo_bar"));
		CPPUNIT_ASSERT(glob.Check("foo?bar"));
		CPPUNIT_ASSERT(glob.Check("foo bar"));
		CPPUNIT_ASSERT(glob.Check("foobar"));
		CPPUNIT_ASSERT(glob.Check("foo__bar"));
		CPPUNIT_ASSERT(!glob.Check("_foobar"));
		CPPUNIT_ASSERT(!glob.Check("foobar_"));
	}

	void PrefixWildcard() {
		const Glob glob("*bar");
		CPPUNIT_ASSERT(glob.Check("foo_bar"));
		CPPUNIT_ASSERT(glob.Check("foo?bar"));
		CPPUNIT_ASSERT(glob.Check("foo bar"));
		CPPUNIT_ASSERT(glob.Check("foobar"));
		CPPUNIT_ASSERT(glob.Check("foo__bar"));
		CPPUNIT_ASSERT(glob.Check("_foobar"));
		CPPUNIT_ASSERT(glob.Check("bar"));
		CPPUNIT_ASSERT(!glob.Check("foobar_"));
	}

	void SuffixWildcard() {
		const Glob glob("foo*");
		CPPUNIT_ASSERT(glob.Check("foo_bar"));
		CPPUNIT_ASSERT(glob.Check("foo?bar"));
		CPPUNIT_ASSERT(glob.Check("foo bar"));
		CPPUNIT_ASSERT(glob.Check("foobar"));
		CPPUNIT_ASSERT(glob.Check("foo__bar"));
		CPPUNIT_ASSERT(glob.Check("foobar_"));
		CPPUNIT_ASSERT(glob.Check("foo"));
	}
};

CPPUNIT_TEST_SUITE_REGISTRATION(TestGlob);

#endif

int
main(gcc_unused int argc, gcc_unused char **argv)
{
#ifdef HAVE_CLASS_GLOB
	CppUnit::TextUi::TestRunner runner;
	auto &registry = CppUnit::TestFactoryRegistry::getRegistry();
	runner.addTest(registry.makeTest());
	return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
#else
	return EXIT_SUCCESS;
#endif
}