PageRenderTime 22ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/itemspecification.d

http://github.com/wilkie/djehuty
D | 74 lines | 42 code | 17 blank | 15 comment | 3 complexity | be81ff8604819e765cd9ce26014841a3 MD5 | raw file
 1/*
 2 * specification.d
 3 *
 4 * This module implements a class that wraps the specification of a particular
 5 * subsection of a module.
 6 *
 7 * Originated: May 6th, 2010
 8 *
 9 */
10
11module spec.itemspecification;
12
13import spec.logic;
14
15import djehuty;
16
17class ItemSpecification {
18
19	this(string name) {
20		_name = name.dup;
21	}
22
23	string name() {
24		return _name;
25	}
26
27	void add(string specification, it function() testBody) {
28		_tests[specification] = testBody;
29	}
30
31	// Description: This will test a particular item.
32	// Returns: When the test is successful, it returns true. It returns false otherwise.
33	bool test(string name) {
34		if (!(name in _tests)) {
35			throw new Exception("Unknown Test");
36		}
37		it ret = _tests[name]();
38		return ret == it.does;
39	}
40
41	size_t length() {
42		return _tests.length;
43	}
44
45	int opApply(int delegate(ref string) loopBody) {
46		foreach(test; _tests.keys) {
47			if (loopBody(test)) {
48				return 1;
49			}
50		}
51		return 1;
52	}
53
54	// Description: Print out the specification of the package, which serves as
55	//   documentation for the application.
56	string toString() {
57		// Item should do this
58		// Item should do that
59		string ret = "";
60
61		foreach(spec; _tests.keys) {
62			ret ~= _name ~ " " ~ spec ~ "\n";
63		}
64
65		return ret;
66	}
67
68private:
69
70	string _name;
71
72	it function()[string] _tests;
73}
74