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