1 |
|
2 |
|
3 |
|
4 |
|
5 | 'use strict';
|
6 |
|
7 | var util = require('util');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function Visitor(delegate) {
|
27 | this.delegate = delegate;
|
28 | }
|
29 |
|
30 | ['Start', 'End', 'Summary', 'SummaryEnd', 'Detail'].forEach(function(k) {
|
31 | var f = 'on' + k;
|
32 | Visitor.prototype[f] = function(node, state) {
|
33 | if (this.delegate[f] && typeof this.delegate[f] === 'function') {
|
34 | this.delegate[f].call(this.delegate, node, state);
|
35 | }
|
36 | };
|
37 | });
|
38 |
|
39 | function CompositeVisitor(visitors) {
|
40 | if (!Array.isArray(visitors)) {
|
41 | visitors = [visitors];
|
42 | }
|
43 | this.visitors = visitors.map(function(v) {
|
44 | if (v instanceof Visitor) {
|
45 | return v;
|
46 | }
|
47 | return new Visitor(v);
|
48 | });
|
49 | }
|
50 |
|
51 | util.inherits(CompositeVisitor, Visitor);
|
52 |
|
53 | ['Start', 'Summary', 'SummaryEnd', 'Detail', 'End'].forEach(function(k) {
|
54 | var f = 'on' + k;
|
55 | CompositeVisitor.prototype[f] = function(node, state) {
|
56 | this.visitors.forEach(function(v) {
|
57 | v[f](node, state);
|
58 | });
|
59 | };
|
60 | });
|
61 |
|
62 | function Node() {}
|
63 |
|
64 |
|
65 | Node.prototype.getQualifiedName = function() {
|
66 | throw new Error('getQualifiedName must be overridden');
|
67 | };
|
68 |
|
69 |
|
70 | Node.prototype.getRelativeName = function() {
|
71 | throw new Error('getRelativeName must be overridden');
|
72 | };
|
73 |
|
74 |
|
75 | Node.prototype.isRoot = function() {
|
76 | return !this.getParent();
|
77 | };
|
78 |
|
79 |
|
80 | Node.prototype.getParent = function() {
|
81 | throw new Error('getParent must be overridden');
|
82 | };
|
83 |
|
84 |
|
85 | Node.prototype.getChildren = function() {
|
86 | throw new Error('getChildren must be overridden');
|
87 | };
|
88 |
|
89 |
|
90 | Node.prototype.isSummary = function() {
|
91 | throw new Error('isSummary must be overridden');
|
92 | };
|
93 |
|
94 |
|
95 | Node.prototype.getCoverageSummary = function(/* filesOnly */) {
|
96 | throw new Error('getCoverageSummary must be overridden');
|
97 | };
|
98 |
|
99 |
|
100 | Node.prototype.getFileCoverage = function() {
|
101 | throw new Error('getFileCoverage must be overridden');
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | Node.prototype.visit = function(visitor, state) {
|
111 | var that = this,
|
112 | visitChildren = function() {
|
113 | that.getChildren().forEach(function(child) {
|
114 | child.visit(visitor, state);
|
115 | });
|
116 | };
|
117 |
|
118 | if (this.isSummary()) {
|
119 | visitor.onSummary(this, state);
|
120 | } else {
|
121 | visitor.onDetail(this, state);
|
122 | }
|
123 |
|
124 | visitChildren();
|
125 |
|
126 | if (this.isSummary()) {
|
127 | visitor.onSummaryEnd(this, state);
|
128 | }
|
129 | };
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | function Tree() {}
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | Tree.prototype.getRoot = function() {
|
142 | throw new Error('getRoot must be overridden');
|
143 | };
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | Tree.prototype.visit = function(visitor, state) {
|
151 | if (!(visitor instanceof Visitor)) {
|
152 | visitor = new Visitor(visitor);
|
153 | }
|
154 | visitor.onStart(this.getRoot(), state);
|
155 | this.getRoot().visit(visitor, state);
|
156 | visitor.onEnd(this.getRoot(), state);
|
157 | };
|
158 |
|
159 | module.exports = {
|
160 | Tree: Tree,
|
161 | Node: Node,
|
162 | Visitor: Visitor,
|
163 | CompositeVisitor: CompositeVisitor
|
164 | };
|