OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /// Unit tests for doc. | |
6 #library('dartdoc_tests'); | |
7 | |
8 #import('../../../dartdoc/dartdoc.dart', prefix: 'dd'); | |
9 #import('../../../dartdoc/markdown.dart', prefix: 'md'); | |
10 | |
11 // TODO(rnystrom): Better path to unittest. | |
12 #import('../../../../client/testing/unittest/unittest_vm.dart'); | |
13 #import('../../../../frog/lang.dart'); | |
14 #import('../../../../frog/file_system_vm.dart'); | |
15 | |
16 main() { | |
17 var files = new VMFileSystem(); | |
18 parseOptions('../../frog', [], files); | |
19 initializeWorld(files); | |
20 | |
21 group('countOccurrences', () { | |
22 test('empty text returns 0', () { | |
23 expect(dd.countOccurrences('', 'needle')).equals(0); | |
24 }); | |
25 | |
26 test('one occurrence', () { | |
27 expect(dd.countOccurrences('bananarama', 'nara')).equals(1); | |
28 }); | |
29 | |
30 test('multiple occurrences', () { | |
31 expect(dd.countOccurrences('bananarama', 'a')).equals(5); | |
32 }); | |
33 | |
34 test('overlapping matches do not count', () { | |
35 expect(dd.countOccurrences('bananarama', 'ana')).equals(1); | |
36 }); | |
37 }); | |
38 | |
39 group('repeat', () { | |
40 test('zero times returns an empty string', () { | |
41 expect(dd.repeat('ba', 0)).equals(''); | |
42 }); | |
43 | |
44 test('one time returns the string', () { | |
45 expect(dd.repeat('ba', 1)).equals('ba'); | |
46 }); | |
47 | |
48 test('multiple times', () { | |
49 expect(dd.repeat('ba', 3)).equals('bababa'); | |
50 }); | |
51 | |
52 test('multiple times with a separator', () { | |
53 expect(dd.repeat('ba', 3, separator: ' ')).equals('ba ba ba'); | |
54 }); | |
55 }); | |
56 | |
57 group('isAbsolute', () { | |
58 final doc = new dd.Dartdoc(); | |
59 | |
60 test('returns false if there is no scheme', () { | |
61 expect(doc.isAbsolute('index.html')).isFalse(); | |
62 expect(doc.isAbsolute('foo/index.html')).isFalse(); | |
63 expect(doc.isAbsolute('foo/bar/index.html')).isFalse(); | |
64 }); | |
65 | |
66 test('returns true if there is a scheme', () { | |
67 expect(doc.isAbsolute('http://google.com')).isTrue(); | |
68 expect(doc.isAbsolute('hTtPs://google.com')).isTrue(); | |
69 expect(doc.isAbsolute('mailto:fake@email.com')).isTrue(); | |
70 }); | |
71 }); | |
72 | |
73 group('relativePath', () { | |
74 final doc = new dd.Dartdoc(); | |
75 | |
76 test('absolute path is unchanged', () { | |
77 doc.startFile('dir/sub/file.html'); | |
78 expect(doc.relativePath('http://foo.com')).equals('http://foo.com'); | |
79 }); | |
80 | |
81 test('from root to root', () { | |
82 doc.startFile('root.html'); | |
83 expect(doc.relativePath('other.html')).equals('other.html'); | |
84 }); | |
85 | |
86 test('from root to directory', () { | |
87 doc.startFile('root.html'); | |
88 expect(doc.relativePath('dir/file.html')).equals('dir/file.html'); | |
89 }); | |
90 | |
91 test('from root to nested', () { | |
92 doc.startFile('root.html'); | |
93 expect(doc.relativePath('dir/sub/file.html')).equals( | |
94 'dir/sub/file.html'); | |
95 }); | |
96 | |
97 test('from directory to root', () { | |
98 doc.startFile('dir/file.html'); | |
99 expect(doc.relativePath('root.html')).equals('../root.html'); | |
100 }); | |
101 | |
102 test('from nested to root', () { | |
103 doc.startFile('dir/sub/file.html'); | |
104 expect(doc.relativePath('root.html')).equals('../../root.html'); | |
105 }); | |
106 | |
107 test('from dir to dir with different path', () { | |
108 doc.startFile('dir/file.html'); | |
109 expect(doc.relativePath('other/file.html')).equals( | |
110 '../other/file.html'); | |
111 }); | |
112 | |
113 test('from nested to nested with different path', () { | |
114 doc.startFile('dir/sub/file.html'); | |
115 expect(doc.relativePath('other/sub/file.html')).equals( | |
116 '../../other/sub/file.html'); | |
117 }); | |
118 | |
119 test('from nested to directory with different path', () { | |
120 doc.startFile('dir/sub/file.html'); | |
121 expect(doc.relativePath('other/file.html')).equals( | |
122 '../../other/file.html'); | |
123 }); | |
124 }); | |
125 | |
126 group('name reference', () { | |
127 // TODO(rnystrom): The paths here are a bit strange. They're relative to | |
128 // where test.dart happens to be invoked from. | |
129 final dummyPath = 'utils/tests/dartdoc/src/dummy.dart'; | |
130 | |
131 // TODO(rnystrom): Bail if we couldn't find the test file. The problem is | |
132 // that loading dummy.dart is sensitive to the location that dart was | |
133 // *invoked* from and not relative to *this* file like we'd like. That | |
134 // means these tests only run correctly from one place. Unfortunately, | |
135 // test.py/test.dart runs this from one directory and frog/presubmit.py | |
136 // runs it from another. | |
137 // See Bug 1145. | |
138 var fileSystem = new VMFileSystem(); | |
139 if (!fileSystem.fileExists(dummyPath)) { | |
140 print("Can't run dartdoc name reference tests because dummy.dart " + | |
141 "could not be found."); | |
142 return; | |
143 } | |
144 | |
145 var doc = new dd.Dartdoc(); | |
146 doc.startFile('someLib/someType.html'); | |
147 | |
148 world.processDartScript(dummyPath); | |
149 world.resolveAll(); | |
150 var dummy = world.libraries[dummyPath]; | |
151 var klass = dummy.findTypeByName('Class'); | |
152 var method = klass.getMember('method'); | |
153 | |
154 String render(md.Node node) => md.renderToHtml([node]); | |
155 | |
156 test('to a parameter of the current method', () { | |
157 expect(render(doc.resolveNameReference('param', member: method))). | |
158 equals('<span class="param">param</span>'); | |
159 }); | |
160 | |
161 test('to a member of the current type', () { | |
162 expect(render(doc.resolveNameReference('method', type: klass))). | |
163 equals('<a class="crossref" href="../dummy/Class.html#method">' + | |
164 'method</a>'); | |
165 }); | |
166 | |
167 test('to a property with only a getter links to the getter', () { | |
168 expect(render(doc.resolveNameReference('getterOnly', type: klass))). | |
169 equals('<a class="crossref" ' + | |
170 'href="../dummy/Class.html#get:getterOnly">getterOnly</a>'); | |
171 }); | |
172 | |
173 test('to a property with only a setter links to the setter', () { | |
174 expect(render(doc.resolveNameReference('setterOnly', type: klass))). | |
175 equals('<a class="crossref" ' + | |
176 'href="../dummy/Class.html#set:setterOnly">setterOnly</a>'); | |
177 }); | |
178 | |
179 test('to a property with a getter and setter links to the getter', () { | |
180 expect(render(doc.resolveNameReference('getterAndSetter', type: klass))). | |
181 equals('<a class="crossref" ' + | |
182 'href="../dummy/Class.html#get:getterAndSetter">' + | |
183 'getterAndSetter</a>'); | |
184 }); | |
185 | |
186 test('to a type in the current library', () { | |
187 expect(render(doc.resolveNameReference('Class', library: dummy))). | |
188 equals('<a class="crossref" href="../dummy/Class.html">Class</a>'); | |
189 }); | |
190 | |
191 test('to a top-level member in the current library', () { | |
192 expect(render(doc.resolveNameReference('topLevelMethod', | |
193 library: dummy))). | |
194 equals('<a class="crossref" href="../dummy.html#topLevelMethod">' + | |
195 'topLevelMethod</a>'); | |
196 }); | |
197 | |
198 test('to an unknown name', () { | |
199 expect(render(doc.resolveNameReference('unknownName', library: dummy, | |
200 type: klass, member: method))). | |
201 equals('<code>unknownName</code>'); | |
202 }); | |
203 | |
204 test('to a member of another class', () { | |
205 expect(render(doc.resolveNameReference('Class.method', library: dummy))). | |
206 equals('<a class="crossref" href="../dummy/Class.html#method">' + | |
207 'Class.method</a>'); | |
208 }); | |
209 | |
210 test('to a constructor', () { | |
211 expect(render(doc.resolveNameReference('new Class', library: dummy))). | |
212 equals('<a class="crossref" href="../dummy/Class.html#new:Class">' + | |
213 'new Class</a>'); | |
214 }); | |
215 | |
216 test('to a named constructor', () { | |
217 expect(render(doc.resolveNameReference('new Class.namedConstructor', | |
218 library: dummy))). | |
219 equals('<a class="crossref" ' + | |
220 'href="../dummy/Class.html#new:Class.namedConstructor">new ' + | |
221 'Class.namedConstructor</a>'); | |
222 }); | |
223 }); | |
224 } | |
OLD | NEW |