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.
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
):
35 class APIDataSourceTest(unittest
.TestCase
):
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
:
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'))
52 }, resolver
.GetLink('foo', namespace
='baz'))
54 'href': 'foo.html#type-foo_t1',
57 }, resolver
.GetLink('foo.foo_t1', namespace
='baz'))
59 'href': 'baz.html#event-baz_e1',
62 }, resolver
.GetLink('baz.baz_e1', namespace
='baz'))
64 'href': 'baz.html#event-baz_e1',
67 }, resolver
.GetLink('baz_e1', namespace
='baz'))
69 'href': 'foo.html#method-foo_f1',
72 }, resolver
.GetLink('foo.foo_f1', namespace
='baz'))
74 'href': 'foo.html#property-foo_p3',
77 }, resolver
.GetLink('foo.foo_p3', namespace
='baz'))
79 'href': 'bar.bon.html#type-bar_bon_t3',
80 'text': 'bar.bon.bar_bon_t3',
82 }, resolver
.GetLink('bar.bon.bar_bon_t3', namespace
='baz'))
84 'href': 'bar.bon.html#property-bar_bon_p3',
87 }, resolver
.GetLink('bar_bon_p3', namespace
='bar.bon'))
89 'href': 'bar.bon.html#property-bar_bon_p3',
92 }, resolver
.GetLink('bar.bon.bar_bon_p3', namespace
='bar.bon'))
94 'href': 'bar.html#event-bar_e2',
97 }, resolver
.GetLink('bar.bar_e2', namespace
='bar'))
99 'href': 'bar.html#type-bon',
102 }, resolver
.GetLink('bar.bon', namespace
='bar'))
104 'href': 'foo.html#event-foo_t3-foo_t3_e1',
105 'text': 'foo_t3.foo_t3_e1',
107 }, resolver
.GetLink('foo_t3.foo_t3_e1', namespace
='foo'))
109 'href': 'foo.html#event-foo_t3-foo_t3_e1',
110 'text': 'foo_t3.foo_t3_e1',
112 }, resolver
.GetLink('foo.foo_t3.foo_t3_e1', namespace
='foo'))
114 'href': 'foo.html#event-foo_t3-foo_t3_e1',
115 'text': 'foo_t3.foo_t3_e1',
117 }, resolver
.GetLink('foo.foo_p1.foo_t3_e1', namespace
='foo'))
119 'href': 'bar.html#property-bar_t1-bar_t1_p1',
120 'text': 'bar.bar_t1.bar_t1_p1',
122 }, resolver
.GetLink('bar.bar_p3.bar_t1_p1', namespace
='foo'))
124 'href': 'bar.html#property-bar_t1-bar_t1_p1',
125 'text': 'bar_t1.bar_t1_p1',
127 }, resolver
.GetLink('bar_p3.bar_t1_p1', namespace
='bar'))
130 resolver
.GetLink('bar.bar_p3.bar_t2_p1', namespace
='bar'))
133 resolver
.GetLink('bar.bon.bar_e3', namespace
='bar'))
136 resolver
.GetLink('bar_p3', namespace
='baz.bon'))
139 resolver
.GetLink('falafel.faf', namespace
='a'))
142 resolver
.GetLink('bar_p3', namespace
='foo'))
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'))
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].',
155 'Ref <a href="foo.html">It\'s foo!</a>',
156 resolver
.ResolveAllLinks('Ref $ref:[foo It\'s foo!]', namespace
='bar'))
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.
163 '<pre><a href="bar.html#type-bon">bar.bon</a>({\nkey: value})',
164 resolver
.ResolveAllLinks('<pre>$ref:[bar.bon]({\nkey: value})',
167 # Allow bare "$ref:foo.bar." at the end of a string.
169 '<a href="bar.html#type-bon">bar.bon</a>.',
170 resolver
.ResolveAllLinks('$ref:bar.bon.',
173 # If a request is provided it should construct an appropriate relative link.
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'))
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',
189 namespace
='bar.bon'))
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'))
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'))
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__':