14
14
# ==============================================================================
15
15
"""Tests for tools.docs.doc_generator_visitor."""
16
16
17
- import argparse
18
17
import inspect
19
18
import io
20
19
import os
@@ -262,8 +261,8 @@ class Parent(object):
262
261
class PathTreeTest (absltest .TestCase ):
263
262
264
263
def test_contains (self ):
265
- tf = argparse . Namespace ( )
266
- tf .sub = argparse . Namespace ( )
264
+ tf = types . ModuleType ( 'tf' )
265
+ tf .sub = types . ModuleType ( 'sub' )
267
266
268
267
tree = doc_generator_visitor .PathTree ()
269
268
tree [('tf' ,)] = tf
@@ -273,8 +272,8 @@ def test_contains(self):
273
272
self .assertIn (('tf' , 'sub' ), tree )
274
273
275
274
def test_node_insertion (self ):
276
- tf = argparse . Namespace ( )
277
- tf .sub = argparse . Namespace ( )
275
+ tf = types . ModuleType ( 'tf' )
276
+ tf .sub = types . ModuleType ( 'sub' )
278
277
tf .sub .object = object ()
279
278
280
279
tree = doc_generator_visitor .PathTree ()
@@ -290,10 +289,10 @@ def test_node_insertion(self):
290
289
self .assertIs (node .children ['thing' ], tree [('tf' , 'sub' , 'thing' )])
291
290
292
291
def test_duplicate (self ):
293
- tf = argparse . Namespace ( )
294
- tf .sub = argparse . Namespace ( )
292
+ tf = types . ModuleType ( 'tf' )
293
+ tf .sub = types . ModuleType ( 'sub' )
295
294
tf .sub .thing = object ()
296
- tf .sub2 = argparse . Namespace ( )
295
+ tf .sub2 = types . ModuleType ( 'sub2' )
297
296
tf .sub2 .thing = tf .sub .thing
298
297
299
298
tree = doc_generator_visitor .PathTree ()
@@ -308,10 +307,10 @@ def test_duplicate(self):
308
307
[tree [('tf' , 'sub' , 'thing' )], tree [('tf' , 'sub2' , 'thing' )]])
309
308
310
309
def test_duplicate_singleton (self ):
311
- tf = argparse . Namespace ( )
312
- tf .sub = argparse . Namespace ( )
310
+ tf = types . ModuleType ( 'tf' )
311
+ tf .sub = types . ModuleType ( 'sub' )
313
312
tf .sub .thing = 999
314
- tf .sub2 = argparse . Namespace ( )
313
+ tf .sub2 = types . ModuleType ( 'sub2' )
315
314
tf .sub2 .thing = tf .sub .thing
316
315
317
316
tree = doc_generator_visitor .PathTree ()
@@ -322,8 +321,7 @@ def test_duplicate_singleton(self):
322
321
tree [('tf' , 'sub2' , 'thing' )] = tf .sub2 .thing
323
322
324
323
found = tree .nodes_for_obj (tf .sub .thing )
325
- self .assertIsNotNone (found )
326
- self .assertEmpty (found )
324
+ self .assertEqual ([], found )
327
325
328
326
329
327
class ApiTreeTest (absltest .TestCase ):
@@ -469,6 +467,42 @@ def test_api_tree_toc_integration(self):
469
467
470
468
self .assertEqual (expected , stream .getvalue ())
471
469
470
+ def test_non_priority_name (self ):
471
+
472
+ class Class1 :
473
+ pass
474
+
475
+ mod = types .ModuleType ('mod' )
476
+ mod .a = types .ModuleType ('sub' )
477
+ mod .a .Class1 = Class1
478
+ mod .b = mod .a
479
+
480
+ path_tree = doc_generator_visitor .PathTree ()
481
+ path_tree [('mod' ,)] = mod
482
+ path_tree [('mod' , 'a' )] = mod .a
483
+ path_tree [('mod' , 'a' , 'Class1' )] = mod .a .Class1
484
+ path_tree [('mod' , 'b' )] = mod .b
485
+ path_tree [('mod' , 'b' , 'Class1' )] = mod .b .Class1
486
+
487
+ def inconsistent_name_score (path ):
488
+ # `mod.a` is prefered over `mod.b`, but `b.Class1` is prefered over
489
+ # `a.Class1`!
490
+ scores = {
491
+ ('mod' ,): 0 ,
492
+ ('mod' , 'a' ): 0 , # prefer 'a'
493
+ ('mod' , 'b' ): 1 ,
494
+ ('mod' , 'a' , 'Class1' ): 1 ,
495
+ ('mod' , 'b' , 'Class1' ): 0 , # prefer 'b.Class1'
496
+ }
497
+ return scores [path ]
498
+
499
+ api_tree = doc_generator_visitor .ApiTree .from_path_tree (
500
+ path_tree , inconsistent_name_score )
501
+ node = api_tree .node_for_object (Class1 )
502
+
503
+ # `Class1` can't choose `b.Class1` as its priority_path because
504
+ # `a` is the priority_path for `sub`.
505
+ self .assertEqual ('mod.a.Class1' , node .full_name )
472
506
473
507
if __name__ == '__main__' :
474
508
absltest .main ()
0 commit comments