comment-parser 0.2.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,139 @@
1
+ var fs = require('fs');
2
+ var expect = require('chai').expect;
3
+ var parse = require('../index');
4
+
5
+ describe('parse() with custom tag parsers', function() {
6
+
7
+ function parsed(func, opts) {
8
+ var str = func.toString();
9
+ return parse(str.slice(
10
+ str.indexOf('{') + 1,
11
+ str.lastIndexOf('}')
12
+ ).trim(), opts);
13
+ }
14
+
15
+ function sample() {
16
+ /**
17
+ * @tag {type} name description
18
+ */
19
+ var a;
20
+ }
21
+
22
+ it('should use `opts.parsers`', function() {
23
+ var parsers = [
24
+ function everything(str) {
25
+ return {
26
+ source : str,
27
+ data : {
28
+ tag : 'tag',
29
+ type : 'type',
30
+ name : 'name',
31
+ optional : false,
32
+ description : 'description'
33
+ }
34
+ };
35
+ }
36
+ ];
37
+
38
+ expect(parsed(sample, {parsers: parsers})[0])
39
+ .to.eql({
40
+ line : 0,
41
+ description : '',
42
+ source : '@tag {type} name description',
43
+ tags: [{
44
+ tag : 'tag',
45
+ type : 'type',
46
+ name : 'name',
47
+ description : 'description',
48
+ optional : false,
49
+ source : '@tag {type} name description',
50
+ line : 1
51
+ }]
52
+ });
53
+ });
54
+
55
+ it('should merge parsers result', function() {
56
+ var parsers = [
57
+ function parser1(str) {
58
+ return {
59
+ source : '',
60
+ data : {tag: 'tag'},
61
+ };
62
+ },
63
+ function parser2(str) {
64
+ return {
65
+ source : '',
66
+ data : {type: 'type'},
67
+ };
68
+ },
69
+ function parser3(str) {
70
+ return {
71
+ source : '',
72
+ data : {
73
+ name : 'name',
74
+ description : 'description'
75
+ },
76
+ };
77
+ }
78
+ ];
79
+
80
+ expect(parsed(sample, {parsers: parsers})[0])
81
+ .to.eql({
82
+ line : 0,
83
+ description : '',
84
+ source : '@tag {type} name description',
85
+ tags: [{
86
+ tag : 'tag',
87
+ type : 'type',
88
+ name : 'name',
89
+ description : 'description',
90
+ optional : false,
91
+ source : '@tag {type} name description',
92
+ line : 1
93
+ }]
94
+ });
95
+ });
96
+
97
+ it('should catch parser exceptions and populate `errors` field', function() {
98
+ var parsers = [
99
+ function parser1(str) {
100
+ return {
101
+ source : '',
102
+ data : {tag: 'tag'}
103
+ };
104
+ },
105
+ function parser2(str) {
106
+ throw new Error('error 1');
107
+ },
108
+ function parser3(str) {
109
+ throw new Error('error 2');
110
+ },
111
+ function parser4(str) {
112
+ return {
113
+ source : '',
114
+ data : {name: 'name'}
115
+ };
116
+ },
117
+ ];
118
+
119
+ expect(parsed(sample, {parsers: parsers})[0])
120
+ .to.eql({
121
+ line : 0,
122
+ description : '',
123
+ source : '@tag {type} name description',
124
+ tags: [{
125
+ tag : 'tag',
126
+ type : '',
127
+ name : 'name',
128
+ description : '',
129
+ optional : false,
130
+ source : '@tag {type} name description',
131
+ errors : [
132
+ 'parser2: error 1',
133
+ 'parser3: error 2'
134
+ ],
135
+ line : 1
136
+ }]
137
+ });
138
+ });
139
+ });