@@ -45,7 +45,7 @@ def __contains__(self, key) -> bool:
45
45
46
46
47
47
def check (key , value , env ) -> None :
48
- assert int (value ) == 6 * 9 , "key %s = %s" % ( key , repr ( value ))
48
+ assert int (value ) == 6 * 9 , f "key { key !r } = { value !r } "
49
49
50
50
# Check saved option file by executing and comparing against
51
51
# the expected dictionary
@@ -55,7 +55,7 @@ def checkSave(file, expected) -> None:
55
55
with open (file , 'r' ) as f :
56
56
exec (f .read (), gdict , ldict )
57
57
58
- assert expected == ldict , "%s \n ...not equal to...\n %s" % ( expected , ldict )
58
+ assert expected == ldict , f" { expected } \n ...not equal to...\n { ldict } "
59
59
60
60
class VariablesTestCase (unittest .TestCase ):
61
61
@@ -97,12 +97,9 @@ def test_Add(self) -> None:
97
97
o .validator (o .key , o .converter (o .default ), {})
98
98
99
99
def test_it (var , opts = opts ) -> None :
100
- exc_caught = None
101
- try :
100
+ with self .assertRaises (SCons .Errors .UserError ):
102
101
opts .Add (var )
103
- except SCons .Errors .UserError :
104
- exc_caught = 1
105
- assert exc_caught , "did not catch UserError for '%s'" % var
102
+
106
103
test_it ('foo/bar' )
107
104
test_it ('foo-bar' )
108
105
test_it ('foo.bar' )
@@ -168,19 +165,12 @@ def test_Update(self) -> None:
168
165
169
166
env = Environment ()
170
167
exc_caught = None
171
- try :
168
+ with self . assertRaises ( AssertionError ) :
172
169
opts .Update (env )
173
- except AssertionError :
174
- exc_caught = 1
175
- assert exc_caught , "did not catch expected assertion"
176
170
177
171
env = Environment ()
178
- exc_caught = None
179
- try :
172
+ with self .assertRaises (AssertionError ):
180
173
opts .Update (env , {})
181
- except AssertionError :
182
- exc_caught = 1
183
- assert exc_caught , "did not catch expected assertion"
184
174
185
175
# Test that a good value from the file is used and validated.
186
176
test = TestSCons .TestSCons ()
@@ -216,12 +206,8 @@ def test_Update(self) -> None:
216
206
lambda x : int (x ) + 12 )
217
207
218
208
env = Environment ()
219
- exc_caught = None
220
- try :
209
+ with self .assertRaises (AssertionError ):
221
210
opts .Update (env , {'ANSWER' :'54' })
222
- except AssertionError :
223
- exc_caught = 1
224
- assert exc_caught , "did not catch expected assertion"
225
211
226
212
# Test that a good value from an args dictionary
227
213
# passed to Update() is used and validated.
@@ -303,12 +289,8 @@ def test_args(self) -> None:
303
289
lambda x : int (x ) + 12 )
304
290
305
291
env = Environment ()
306
- exc_caught = None
307
- try :
292
+ with self .assertRaises (AssertionError ):
308
293
opts .Update (env )
309
- except AssertionError :
310
- exc_caught = 1
311
- assert exc_caught , "did not catch expected assertion"
312
294
313
295
# Test that a good (command-line) argument is used and validated.
314
296
test = TestSCons .TestSCons ()
@@ -466,7 +448,8 @@ def test_GenerateHelpText(self) -> None:
466
448
"""
467
449
468
450
text = opts .GenerateHelpText (env )
469
- assert text == expect , text
451
+ with self .subTest ():
452
+ self .assertEqual (expect , text )
470
453
471
454
expectAlpha = """
472
455
A: a - alpha test
@@ -496,25 +479,26 @@ def test_GenerateHelpText(self) -> None:
496
479
actual: 54
497
480
"""
498
481
text = opts .GenerateHelpText (env , sort = cmp )
499
- assert text == expectAlpha , text
482
+ with self .subTest ():
483
+ self .assertEqual (expectAlpha , text )
500
484
501
485
textBool = opts .GenerateHelpText (env , sort = True )
502
- assert text == expectAlpha , text
486
+ with self .subTest ():
487
+ self .assertEqual (expectAlpha , textBool )
503
488
504
489
textBackwards = opts .GenerateHelpText (env , sort = lambda x , y : cmp (y , x ))
505
- assert textBackwards == expectBackwards , "Expected:\n %s\n Got:\n %s\n " % (
506
- textBackwards ,
507
- expectBackwards ,
508
- )
490
+ with self .subTest ():
491
+ self .assertEqual (expectBackwards , textBackwards )
509
492
510
493
def test_FormatVariableHelpText (self ) -> None :
511
494
"""Test generating custom format help text"""
512
495
opts = SCons .Variables .Variables ()
513
496
514
497
def my_format (env , opt , help , default , actual , aliases ) -> str :
515
- return '%s %s %s %s %s \n ' % ( opt , default , actual , help , aliases )
498
+ return f' { opt } { default } { actual } { help } { aliases } \n '
516
499
517
- opts .FormatVariableHelpText = my_format
500
+ _save = opts .FormatVariableHelpText
501
+ setattr (opts , 'FormatVariableHelpText' , my_format )
518
502
519
503
opts .Add ('ANSWER' ,
520
504
'THE answer to THE question' ,
@@ -544,15 +528,19 @@ def my_format(env, opt, help, default, actual, aliases) -> str:
544
528
"""
545
529
546
530
text = opts .GenerateHelpText (env )
547
- assert text == expect , text
531
+ with self .subTest ():
532
+ self .assertEqual (expect , text )
548
533
549
534
expectAlpha = """\
550
535
A 42 54 a - alpha test ['A']
551
536
ANSWER 42 54 THE answer to THE question ['ANSWER']
552
537
B 42 54 b - alpha test ['B']
553
538
"""
554
539
text = opts .GenerateHelpText (env , sort = cmp )
555
- assert text == expectAlpha , text
540
+ with self .subTest ():
541
+ self .assertEqual (expectAlpha , text )
542
+
543
+ setattr (opts , 'FormatVariableHelpText' , _save )
556
544
557
545
def test_Aliases (self ) -> None :
558
546
"""Test option aliases"""
@@ -567,12 +555,15 @@ def test_Aliases(self) -> None:
567
555
env = Environment ()
568
556
opts .Update (env , {'ANSWER' : 'answer' })
569
557
570
- assert 'ANSWER' in env
558
+ with self .subTest ():
559
+ self .assertIn ('ANSWER' , env )
571
560
572
561
env = Environment ()
573
562
opts .Update (env , {'ANSWERALIAS' : 'answer' })
574
563
575
- assert 'ANSWER' in env and 'ANSWERALIAS' not in env
564
+ with self .subTest ():
565
+ self .assertIn ('ANSWER' , env )
566
+ self .assertNotIn ('ANSWERALIAS' , env )
576
567
577
568
# test alias as a list
578
569
opts = SCons .Variables .Variables ()
@@ -585,12 +576,15 @@ def test_Aliases(self) -> None:
585
576
env = Environment ()
586
577
opts .Update (env , {'ANSWER' : 'answer' })
587
578
588
- assert 'ANSWER' in env
579
+ with self .subTest ():
580
+ self .assertIn ('ANSWER' , env )
589
581
590
582
env = Environment ()
591
583
opts .Update (env , {'ANSWERALIAS' : 'answer' })
592
584
593
- assert 'ANSWER' in env and 'ANSWERALIAS' not in env
585
+ with self .subTest ():
586
+ self .assertIn ('ANSWER' , env )
587
+ self .assertNotIn ('ANSWERALIAS' , env )
594
588
595
589
596
590
class UnknownVariablesTestCase (unittest .TestCase ):
@@ -612,8 +606,8 @@ def test_unknown(self) -> None:
612
606
opts .Update (env , args )
613
607
614
608
r = opts .UnknownVariables ()
615
- assert r == {'UNKNOWN' : 'unknown' }, r
616
- assert env [ 'ANSWER' ] == ' answer' , env ['ANSWER' ]
609
+ self . assertEqual ( {'UNKNOWN' : 'unknown' }, r )
610
+ self . assertEqual ( ' answer' , env ['ANSWER' ])
617
611
618
612
def test_AddOptionUpdatesUnknown (self ) -> None :
619
613
"""Test updating of the 'unknown' dict"""
@@ -632,8 +626,9 @@ def test_AddOptionUpdatesUnknown(self) -> None:
632
626
opts .Update (env ,args )
633
627
634
628
r = opts .UnknownVariables ()
635
- assert r == {'ADDEDLATER' : 'notaddedyet' }, r
636
- assert env ['A' ] == 'a' , env ['A' ]
629
+ with self .subTest ():
630
+ self .assertEqual ({'ADDEDLATER' : 'notaddedyet' }, r )
631
+ self .assertEqual ('a' , env ['A' ])
637
632
638
633
opts .Add ('ADDEDLATER' ,
639
634
'An option not present initially' ,
@@ -647,8 +642,9 @@ def test_AddOptionUpdatesUnknown(self) -> None:
647
642
opts .Update (env , args )
648
643
649
644
r = opts .UnknownVariables ()
650
- assert len (r ) == 0 , r
651
- assert env ['ADDEDLATER' ] == 'added' , env ['ADDEDLATER' ]
645
+ with self .subTest ():
646
+ self .assertEqual (0 , len (r ))
647
+ self .assertEqual ('added' , env ['ADDEDLATER' ])
652
648
653
649
def test_AddOptionWithAliasUpdatesUnknown (self ) -> None :
654
650
"""Test updating of the 'unknown' dict (with aliases)"""
0 commit comments