UNPKG

4.86 kBJavaScriptView Raw
1/*
2 Copyright 2012-2015, Yahoo Inc.
3 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
4 */
5"use strict";
6
7var util = require('util');
8/**
9 * An object with methods that are called during the traversal of the coverage tree.
10 * A visitor has the following methods that are called during tree traversal.
11 *
12 * * `onStart(root, state)` - called before traversal begins
13 * * `onSummary(node, state)` - called for every summary node
14 * * `onDetail(node, state)` - called for every detail node
15 * * `onSummaryEnd(node, state)` - called after all children have been visited for
16 * a summary node.
17 * * `onEnd(root, state)` - called after traversal ends
18 *
19 * @param delegate - a partial visitor that only implements the methods of interest
20 * The visitor object supplies the missing methods as noops. For example, reports
21 * that only need the final coverage summary need implement `onStart` and nothing
22 * else. Reports that use only detailed coverage information need implement `onDetail`
23 * and nothing else.
24 * @constructor
25 */
26function 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
39function 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
51util.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
62function Node() {
63}
64
65/* istanbul ignore next: abstract method */
66Node.prototype.getQualifiedName = function () {
67 throw new Error('getQualifiedName must be overridden');
68};
69
70/* istanbul ignore next: abstract method */
71Node.prototype.getRelativeName = function () {
72 throw new Error('getRelativeName must be overridden');
73};
74
75/* istanbul ignore next: abstract method */
76Node.prototype.isRoot = function () {
77 return !this.getParent();
78};
79
80/* istanbul ignore next: abstract method */
81Node.prototype.getParent = function () {
82 throw new Error('getParent must be overridden');
83};
84
85/* istanbul ignore next: abstract method */
86Node.prototype.getChildren = function () {
87 throw new Error('getChildren must be overridden');
88};
89
90/* istanbul ignore next: abstract method */
91Node.prototype.isSummary = function () {
92 throw new Error('isSummary must be overridden');
93};
94
95/* istanbul ignore next: abstract method */
96Node.prototype.getCoverageSummary = function (/* filesOnly */) {
97 throw new Error('getCoverageSummary must be overridden');
98};
99
100/* istanbul ignore next: abstract method */
101Node.prototype.getFileCoverage = function () {
102 throw new Error('getFileCoverage must be overridden');
103};
104/**
105 * visit all nodes depth-first from this node down. Note that `onStart`
106 * and `onEnd` are never called on the visitor even if the current
107 * node is the root of the tree.
108 * @param visitor a full visitor that is called during tree traversal
109 * @param state optional state that is passed around
110 */
111Node.prototype.visit = function (visitor, state) {
112
113 var that = this,
114 visitChildren = function () {
115 that.getChildren().forEach(function (child) {
116 child.visit(visitor, state);
117 });
118 };
119
120 if (this.isSummary()) {
121 visitor.onSummary(this, state);
122 } else {
123 visitor.onDetail(this, state);
124 }
125
126 visitChildren();
127
128 if (this.isSummary()) {
129 visitor.onSummaryEnd(this, state);
130 }
131};
132
133/**
134 * abstract base class for a coverage tree.
135 * @constructor
136 */
137function Tree() {
138}
139
140/**
141 * returns the root node of the tree
142 */
143/* istanbul ignore next: abstract method */
144Tree.prototype.getRoot = function () {
145 throw new Error('getRoot must be overridden');
146};
147
148/**
149 * visits the tree depth-first with the supplied partial visitor
150 * @param visitor - a potentially partial visitor
151 * @param state - the state to be passed around during tree traversal
152 */
153Tree.prototype.visit = function (visitor, state) {
154 if (!(visitor instanceof Visitor)) {
155 visitor = new Visitor(visitor);
156 }
157 visitor.onStart(this.getRoot(), state);
158 this.getRoot().visit(visitor, state);
159 visitor.onEnd(this.getRoot(), state);
160};
161
162module.exports = {
163 Tree: Tree,
164 Node: Node,
165 Visitor: Visitor,
166 CompositeVisitor: CompositeVisitor
167};