@@ -459,16 +459,78 @@ def test_n_max_elements_to_show():
459
459
assert pp .pformat (gs ) == expected
460
460
461
461
462
- def test_length_constraint ():
463
- # When repr is still too long, use bruteforce ellipsis
464
- # repr is a very long line so we don't check for equality here, just that
465
- # ellipsis has been done. It's not the ellipsis from before because the
466
- # number of elements in the dict is only 1.
467
- vocabulary = {0 : 'hello' * 1000 }
468
- vectorizer = CountVectorizer (vocabulary = vocabulary )
469
- repr_ = vectorizer .__repr__ ()
470
- assert '...' in repr_
462
+ def test_bruteforce_ellipsis ():
463
+ # Check that the bruteforce ellipsis (used when the number of non-blank
464
+ # characters exceeds N_CHAR_MAX) renders correctly.
465
+
466
+ lr = LogisticRegression ()
467
+
468
+ # test when the left and right side of the ellipsis aren't on the same
469
+ # line.
470
+ expected = """
471
+ LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
472
+ in...
473
+ multi_class='warn', n_jobs=None, penalty='l2',
474
+ random_state=None, solver='warn', tol=0.0001, verbose=0,
475
+ warm_start=False)"""
476
+
477
+ expected = expected [1 :] # remove first \n
478
+ assert expected == lr .__repr__ (N_CHAR_MAX = 150 )
479
+
480
+ # test with very small N_CHAR_MAX
481
+ # Note that N_CHAR_MAX is not strictly enforced, but it's normal: to avoid
482
+ # weird reprs we still keep the whole line of the right part (after the
483
+ # ellipsis).
484
+ expected = """
485
+ Lo...
486
+ warm_start=False)"""
487
+
488
+ expected = expected [1 :] # remove first \n
489
+ assert expected == lr .__repr__ (N_CHAR_MAX = 4 )
490
+
491
+ # test with N_CHAR_MAX == number of non-blank characters: In this case we
492
+ # don't want ellipsis
493
+ full_repr = lr .__repr__ (N_CHAR_MAX = float ('inf' ))
494
+ n_nonblank = len ('' .join (full_repr .split ()))
495
+ assert lr .__repr__ (N_CHAR_MAX = n_nonblank ) == full_repr
496
+ assert '...' not in full_repr
497
+
498
+ # test with N_CHAR_MAX == number of non-blank characters - 10: the left and
499
+ # right side of the ellispsis are on different lines. In this case we
500
+ # want to expend the whole line of the right side
501
+ expected = """
502
+ LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
503
+ intercept_scaling=1, l1_ratio=None, max_i...
504
+ multi_class='warn', n_jobs=None, penalty='l2',
505
+ random_state=None, solver='warn', tol=0.0001, verbose=0,
506
+ warm_start=False)"""
507
+ expected = expected [1 :] # remove first \n
508
+ assert expected == lr .__repr__ (N_CHAR_MAX = n_nonblank - 10 )
509
+
510
+ # test with N_CHAR_MAX == number of non-blank characters - 10: the left and
511
+ # right side of the ellispsis are on the same line. In this case we don't
512
+ # want to expend the whole line of the right side, just add the ellispsis
513
+ # between the 2 sides.
514
+ expected = """
515
+ LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
516
+ intercept_scaling=1, l1_ratio=None, max_iter...,
517
+ multi_class='warn', n_jobs=None, penalty='l2',
518
+ random_state=None, solver='warn', tol=0.0001, verbose=0,
519
+ warm_start=False)"""
520
+ expected = expected [1 :] # remove first \n
521
+ assert expected == lr .__repr__ (N_CHAR_MAX = n_nonblank - 4 )
471
522
523
+ # test with N_CHAR_MAX == number of non-blank characters - 2: the left and
524
+ # right side of the ellispsis are on the same line, but adding the ellipsis
525
+ # would actually make the repr longer. So we don't add the ellipsis.
526
+ expected = """
527
+ LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
528
+ intercept_scaling=1, l1_ratio=None, max_iter=100,
529
+ multi_class='warn', n_jobs=None, penalty='l2',
530
+ random_state=None, solver='warn', tol=0.0001, verbose=0,
531
+ warm_start=False)"""
532
+ expected = expected [1 :] # remove first \n
533
+ assert expected == lr .__repr__ (N_CHAR_MAX = n_nonblank - 2 )
472
534
473
535
def test_builtin_prettyprinter ():
474
536
# non regression test than ensures we can still use the builtin
0 commit comments