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