1 module feature_test.core;
2 
3 debug (featureTest) {
4 	import feature_test.exceptions;
5 	import feature_test.runner;
6 	import feature_test.callbacks;
7 
8 	import colorize;
9 
10 	import std.stdio;
11 	import std.algorithm;
12 
13 	alias FTImplementation = void delegate(FeatureTest);
14 
15 	struct FeatureTestScenario {
16 		string name;
17 		FTCallback implementation;
18 	}
19 
20 	class FeatureTest {
21 		mixin FTCallbacks;
22 
23 		@property ref string name() { return _name; }
24 		@property ref string description() { return _description; }
25 		@property string[] tags() { return _tags; }
26 		@property ref FeatureTestScenario[] scenarios() { return _scenarios; }
27 
28 		void info(A...)(string fmt, A args) {
29 			FeatureTestRunner.instance.info(fmt, args);
30 		}
31 		
32 		void addTags(string[] tags ...) {
33 			foreach(tag; tags) { if (!_tags.canFind(tag)) _tags ~= tag; }
34 		}
35 		
36 		void scenario(string name, FTCallback implementation) {
37 			_scenarios ~= FeatureTestScenario(name, implementation);
38 		}
39 		
40 	private:
41 		string _name;
42 		string _description;
43 		string[] _tags;
44 		
45 		FeatureTestScenario[] _scenarios;
46 	}
47 
48 	void feature(T)(string name, string description, void delegate(T) implementation, string[] tags ...) {
49 		if (FeatureTestRunner.instance.shouldInclude(tags)) {
50 			auto f = new T();
51 			f.name = name;
52 			f.description = description;
53 			f.addTags(tags);
54 			implementation(f);
55 			FeatureTestRunner.instance.features ~= f;
56 		}
57 	}
58 
59 	void feature(T)(string name, void delegate(T) implementation, string[] tags ...) {
60 		feature!T(name, "", implementation, tags);
61 	}
62 
63 	void feature(string name, string description, void delegate(FeatureTest) implementation, string[] tags ...) {
64 		feature!FeatureTest(name, description, implementation, tags);
65 	}
66 
67 	void feature(string name, void delegate(FeatureTest) implementation, string[] tags ...) {
68 		feature!FeatureTest(name, "", implementation, tags);
69 	}
70 
71 	/// Marks a scenario as pending
72 	void featureTestPending(string file = __FILE__, typeof(__LINE__) line = __LINE__) {
73 		throw new FeatureTestException(file, line);
74 	}
75 }