Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / reference_resolver_test.py
blob0c4167522e2a90daa184488cdcefad53f24d9c32
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 import json
7 import os
8 import sys
9 import unittest
11 from file_system import FileNotFoundError
12 from reference_resolver import ReferenceResolver
13 from test_object_store import TestObjectStore
16 class FakeAPIDataSource(object):
17 def __init__(self, json_data):
18 self._json = json_data
20 def get(self, key, disable_refs=False):
21 assert disable_refs, 'ReferenceResolve should be disabling refs'
22 if key not in self._json:
23 raise FileNotFoundError(key)
24 return self._json[key]
27 class FakeAPIModels(object):
28 def __init__(self, names):
29 self._names = names
31 def GetNames(self):
32 return self._names
35 class APIDataSourceTest(unittest.TestCase):
36 def setUp(self):
37 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json')
39 def _ReadLocalFile(self, filename):
40 with open(os.path.join(self._base_path, filename), 'r') as f:
41 return f.read()
43 def testGetLink(self):
44 test_data = json.loads(self._ReadLocalFile('fake_data_source.json'))
45 resolver = ReferenceResolver(FakeAPIDataSource(test_data),
46 FakeAPIModels(test_data.keys()),
47 TestObjectStore('test'))
48 self.assertEqual({
49 'href': 'foo.html',
50 'text': 'foo',
51 'name': 'foo'
52 }, resolver.GetLink('foo', namespace='baz'))
53 self.assertEqual({
54 'href': 'foo.html#type-foo_t1',
55 'text': 'foo.foo_t1',
56 'name': 'foo_t1'
57 }, resolver.GetLink('foo.foo_t1', namespace='baz'))
58 self.assertEqual({
59 'href': 'baz.html#event-baz_e1',
60 'text': 'baz_e1',
61 'name': 'baz_e1'
62 }, resolver.GetLink('baz.baz_e1', namespace='baz'))
63 self.assertEqual({
64 'href': 'baz.html#event-baz_e1',
65 'text': 'baz_e1',
66 'name': 'baz_e1'
67 }, resolver.GetLink('baz_e1', namespace='baz'))
68 self.assertEqual({
69 'href': 'foo.html#method-foo_f1',
70 'text': 'foo.foo_f1',
71 'name': 'foo_f1'
72 }, resolver.GetLink('foo.foo_f1', namespace='baz'))
73 self.assertEqual({
74 'href': 'foo.html#property-foo_p3',
75 'text': 'foo.foo_p3',
76 'name': 'foo_p3'
77 }, resolver.GetLink('foo.foo_p3', namespace='baz'))
78 self.assertEqual({
79 'href': 'bar.bon.html#type-bar_bon_t3',
80 'text': 'bar.bon.bar_bon_t3',
81 'name': 'bar_bon_t3'
82 }, resolver.GetLink('bar.bon.bar_bon_t3', namespace='baz'))
83 self.assertEqual({
84 'href': 'bar.bon.html#property-bar_bon_p3',
85 'text': 'bar_bon_p3',
86 'name': 'bar_bon_p3'
87 }, resolver.GetLink('bar_bon_p3', namespace='bar.bon'))
88 self.assertEqual({
89 'href': 'bar.bon.html#property-bar_bon_p3',
90 'text': 'bar_bon_p3',
91 'name': 'bar_bon_p3'
92 }, resolver.GetLink('bar.bon.bar_bon_p3', namespace='bar.bon'))
93 self.assertEqual({
94 'href': 'bar.html#event-bar_e2',
95 'text': 'bar_e2',
96 'name': 'bar_e2'
97 }, resolver.GetLink('bar.bar_e2', namespace='bar'))
98 self.assertEqual({
99 'href': 'bar.html#type-bon',
100 'text': 'bon',
101 'name': 'bon'
102 }, resolver.GetLink('bar.bon', namespace='bar'))
103 self.assertEqual({
104 'href': 'foo.html#event-foo_t3-foo_t3_e1',
105 'text': 'foo_t3.foo_t3_e1',
106 'name': 'foo_t3_e1'
107 }, resolver.GetLink('foo_t3.foo_t3_e1', namespace='foo'))
108 self.assertEqual({
109 'href': 'foo.html#event-foo_t3-foo_t3_e1',
110 'text': 'foo_t3.foo_t3_e1',
111 'name': 'foo_t3_e1'
112 }, resolver.GetLink('foo.foo_t3.foo_t3_e1', namespace='foo'))
113 self.assertEqual({
114 'href': 'foo.html#event-foo_t3-foo_t3_e1',
115 'text': 'foo_t3.foo_t3_e1',
116 'name': 'foo_t3_e1'
117 }, resolver.GetLink('foo.foo_p1.foo_t3_e1', namespace='foo'))
118 self.assertEqual({
119 'href': 'bar.html#property-bar_t1-bar_t1_p1',
120 'text': 'bar.bar_t1.bar_t1_p1',
121 'name': 'bar_t1_p1'
122 }, resolver.GetLink('bar.bar_p3.bar_t1_p1', namespace='foo'))
123 self.assertEqual({
124 'href': 'bar.html#property-bar_t1-bar_t1_p1',
125 'text': 'bar_t1.bar_t1_p1',
126 'name': 'bar_t1_p1'
127 }, resolver.GetLink('bar_p3.bar_t1_p1', namespace='bar'))
128 self.assertEqual(
129 None,
130 resolver.GetLink('bar.bar_p3.bar_t2_p1', namespace='bar'))
131 self.assertEqual(
132 None,
133 resolver.GetLink('bar.bon.bar_e3', namespace='bar'))
134 self.assertEqual(
135 None,
136 resolver.GetLink('bar_p3', namespace='baz.bon'))
137 self.assertEqual(
138 None,
139 resolver.GetLink('falafel.faf', namespace='a'))
140 self.assertEqual(
141 None,
142 resolver.GetLink('bar_p3', namespace='foo'))
143 self.assertEqual(
144 'Hello <a href="bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>, '
145 '<a href="bar.bon.html#property-bar_bon_p3">Bon Bon</a>, '
146 '<a href="bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>',
147 resolver.ResolveAllLinks(
148 'Hello $ref:bar_bon_p3, $ref:[bar_bon_p3 Bon Bon], $ref:bar_bon_p3',
149 namespace='bar.bon'))
150 self.assertEqual(
151 'I like <a href="bar.html#property-bar_t1-bar_t1_p1">food</a>.',
152 resolver.ResolveAllLinks('I like $ref:[bar.bar_p3.bar_t1_p1 food].',
153 namespace='foo'))
154 self.assertEqual(
155 'Ref <a href="foo.html">It\'s foo!</a>',
156 resolver.ResolveAllLinks('Ref $ref:[foo It\'s foo!]', namespace='bar'))
157 self.assertEqual(
158 'Ref <a href="bar.html#type-bon">Bon</a>',
159 resolver.ResolveAllLinks('Ref $ref:[bar.bon Bon]', namespace='bar'))
161 # Different kinds of whitespace can be significant inside <pre> tags.
162 self.assertEqual(
163 '<pre><a href="bar.html#type-bon">bar.bon</a>({\nkey: value})',
164 resolver.ResolveAllLinks('<pre>$ref:[bar.bon]({\nkey: value})',
165 namespace='baz'))
167 # Allow bare "$ref:foo.bar." at the end of a string.
168 self.assertEqual(
169 '<a href="bar.html#type-bon">bar.bon</a>.',
170 resolver.ResolveAllLinks('$ref:bar.bon.',
171 namespace='baz'))
173 # If a request is provided it should construct an appropriate relative link.
174 self.assertEqual(
175 'Hi <a href="../../bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>, '
176 '<a href="../../bar.bon.html#property-bar_bon_p3">Bon Bon</a>, '
177 '<a href="../../bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>',
178 resolver.ResolveAllLinks(
179 'Hi $ref:bar_bon_p3, $ref:[bar_bon_p3 Bon Bon], $ref:bar_bon_p3',
180 relative_to='big/long/path/bar.html',
181 namespace='bar.bon'))
182 self.assertEqual(
183 'Hi <a href="bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>, '
184 '<a href="bar.bon.html#property-bar_bon_p3">Bon Bon</a>, '
185 '<a href="bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>',
186 resolver.ResolveAllLinks(
187 'Hi $ref:bar_bon_p3, $ref:[bar_bon_p3 Bon Bon], $ref:bar_bon_p3',
188 relative_to='',
189 namespace='bar.bon'))
190 self.assertEqual(
191 'Hi <a href="bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>, '
192 '<a href="bar.bon.html#property-bar_bon_p3">Bon Bon</a>, '
193 '<a href="bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>',
194 resolver.ResolveAllLinks(
195 'Hi $ref:bar_bon_p3, $ref:[bar_bon_p3 Bon Bon], $ref:bar_bon_p3',
196 relative_to='bar.html',
197 namespace='bar.bon'))
198 self.assertEqual(
199 'Hi <a href="bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>, '
200 '<a href="bar.bon.html#property-bar_bon_p3">Bon Bon</a>, '
201 '<a href="bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>',
202 resolver.ResolveAllLinks(
203 'Hi $ref:bar_bon_p3, $ref:[bar_bon_p3 Bon Bon], $ref:bar_bon_p3',
204 relative_to='foo/bar.html',
205 namespace='bar.bon'))
206 self.assertEqual(
207 'Hi <a href="../bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>, '
208 '<a href="../bar.bon.html#property-bar_bon_p3">Bon Bon</a>, '
209 '<a href="../bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>',
210 resolver.ResolveAllLinks(
211 'Hi $ref:bar_bon_p3, $ref:[bar_bon_p3 Bon Bon], $ref:bar_bon_p3',
212 relative_to='foo/baz/bar.html',
213 namespace='bar.bon'))
215 if __name__ == '__main__':
216 unittest.main()