summaryrefslogtreecommitdiff
path: root/valadate/testexplorer.vala
blob: df7a0b8901bcb4f753d57da26c00e84b03c8cb13 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Valadate - Unit testing library for GObject-based libraries.
 * Copyright (C) 2016  Chris Daley <chebizarro@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Authors:
 * 	Chris Daley <chebizarro@gmail.com>
 */

internal class Valadate.TestExplorer : Vala.CodeVisitor {

	internal delegate void* Constructor ();
	internal delegate void TestMethod (TestCase self);

	private TestSuite current;
	private Vala.CodeContext context;
	private TestModule module;
	private string binary;
	private string? running;

	public TestExplorer (string binary, TestSuite root) {
		this.binary = binary;
		this.current = root;
		this.running = Valadate.get_current_test_path ();
	}

	public void load () throws TestConfigError {
		string testdir = Path.get_dirname (binary).replace (".libs", "");

		string testplan = Path.get_basename (binary);
		if (testplan.has_prefix ("lt-"))
			testplan = testplan.substring (3);

		string testplanfile = testdir + GLib.Path.DIR_SEPARATOR_S + testplan + ".vapi";
		
		if (!FileUtils.test (testplanfile, FileTest.EXISTS))
			throw new TestConfigError.TESTPLAN ("Test Plan %s Not Found!", testplanfile);

		try {
			module = new TestModule (binary);
			module.load_module ();
			load_test_plan (testplanfile);
		} catch (TestModuleError e) {
			throw new TestConfigError.MODULE (e.message);
		}
	}

	internal void load_test_plan (string path) throws TestConfigError {
		setup_context ();
		context.add_source_file (new Vala.SourceFile (context, Vala.SourceFileType.PACKAGE, path));
		var parser = new Vala.Parser ();
		parser.parse (context);
		context.accept (this);
	}
	
	private void setup_context () {
		context = new Vala.CodeContext ();
		Vala.CodeContext.push (context);
		context.report.enable_warnings = false;
		context.report.set_verbose_errors (false);
		context.verbose_mode = false;
	}

	public override void visit_class (Vala.Class cl) {
		try {
			if (is_subtype_of (cl, "Valadate.TestCase") &&
				cl.is_abstract != true)
				current.add_test (visit_testcase (cl));
			else if (is_subtype_of (cl, "Valadate.TestSuite") &&
				cl.is_abstract != true)
				current.add_test (visit_testsuite (cl));

		} catch (TestModuleError e) {
			error (e.message);
		}
		cl.accept_children (this);
	}

	private bool is_subtype_of (Vala.Class cl, string typename) {
		foreach (var basetype in cl.get_base_types ())
			if (((Vala.UnresolvedType)basetype).to_qualified_string () == typename)
				return true;
		return false;
	}

	private unowned Constructor get_constructor (Vala.Class cl) throws TestModuleError {
		var attr = new Vala.CCodeAttribute (cl.default_construction_method);
		return (Constructor)module.get_method (attr.name);
	}

	public TestCase visit_testcase (Vala.Class cl) throws TestModuleError {
		unowned Constructor meth = get_constructor (cl);
		var current_test = meth () as TestCase;
		current_test.name = cl.name;
		
		foreach (var method in cl.get_methods ()) {
			if (method.name.has_prefix ("test_") &&
				method.has_result != true &&
				method.get_parameters ().size == 0) {

				if (running != null &&
					running != "/" + method.get_full_name ().replace (".","/"))
					continue;

				unowned TestMethod testmethod = null;
				var attr = new Vala.CCodeAttribute (method);
				testmethod = (TestMethod)module.get_method (attr.name);

				if (testmethod != null) {
					current_test.add_test (method.name, () => {
						testmethod (current_test);
					});
				}
			}
		}
		return current_test;
	}

	public TestSuite visit_testsuite (Vala.Class cl) throws TestModuleError {
		unowned Constructor meth = get_constructor (cl);
		var current_test = meth () as TestSuite;
		current_test.name = cl.name;
		return current_test;
	}

	public override void visit_namespace (Vala.Namespace ns) {
		if (ns.name != null) {
			var testsuite = new TestSuite (ns.name);
			current.add_test (testsuite);
			current = testsuite;
		}
		ns.accept_children (this);
	}
}