|
12 | 12 | import h2.settings
|
13 | 13 |
|
14 | 14 | from hypothesis import given, assume
|
15 |
| -from hypothesis.strategies import integers |
| 15 | +from hypothesis.strategies import ( |
| 16 | + integers, booleans, fixed_dictionaries, builds |
| 17 | +) |
16 | 18 |
|
17 | 19 |
|
18 | 20 | class TestSettings(object):
|
@@ -367,125 +369,69 @@ class TestSettingsEquality(object):
|
367 | 369 | A class defining tests for the standard implementation of == and != .
|
368 | 370 | """
|
369 | 371 |
|
370 |
| - def an_instance(self): |
371 |
| - """ |
372 |
| - Return an instance of the class under test. Each call to this method |
373 |
| - must return a different object. All objects returned must be equal to |
374 |
| - each other. |
375 |
| - """ |
376 |
| - overrides = { |
377 |
| - h2.settings.SettingCodes.HEADER_TABLE_SIZE: 0, |
378 |
| - h2.settings.SettingCodes.MAX_FRAME_SIZE: 16384, |
379 |
| - h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS: 4, |
380 |
| - h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE: 2**16, |
381 |
| - } |
382 |
| - return h2.settings.Settings(client=True, initial_values=overrides) |
383 |
| - |
384 |
| - def another_instance(self): |
385 |
| - """ |
386 |
| - Return an instance of the class under test. Each call to this method |
387 |
| - must return a different object. The objects must not be equal to the |
388 |
| - objects returned by an_instance. They may or may not be equal to |
389 |
| - each other (they will not be compared against each other). |
390 |
| - """ |
391 |
| - overrides = { |
392 |
| - h2.settings.SettingCodes.HEADER_TABLE_SIZE: 8080, |
393 |
| - h2.settings.SettingCodes.MAX_FRAME_SIZE: 16388, |
394 |
| - h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS: 100, |
395 |
| - h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE: 2**16, |
396 |
| - } |
397 |
| - return h2.settings.Settings(client=False, initial_values=overrides) |
398 |
| - |
399 |
| - def test_identical_eq(self): |
400 |
| - """ |
401 |
| - An object compares equal to itself using the == operator. |
402 |
| - """ |
403 |
| - o = self.an_instance() |
404 |
| - assert (o == o) |
405 |
| - |
406 |
| - def test_identical_ne(self): |
407 |
| - """ |
408 |
| - An object doesn't compare not equal to itself using the != operator. |
409 |
| - """ |
410 |
| - o = self.an_instance() |
411 |
| - assert not (o != o) |
412 |
| - |
413 |
| - def test_same_eq(self): |
414 |
| - """ |
415 |
| - Two objects that are equal to each other compare equal to each other |
416 |
| - using the == operator. |
417 |
| - """ |
418 |
| - a = self.an_instance() |
419 |
| - b = self.an_instance() |
420 |
| - assert (a == b) |
421 |
| - |
422 |
| - def test_same_ne(self): |
423 |
| - """ |
424 |
| - Two objects that are equal to each other do not compare not equal to |
425 |
| - each other using the != operator. |
426 |
| - """ |
427 |
| - a = self.an_instance() |
428 |
| - b = self.an_instance() |
429 |
| - assert not (a != b) |
| 372 | + SettingsStrategy = builds( |
| 373 | + h2.settings.Settings, |
| 374 | + client=booleans(), |
| 375 | + initial_values=fixed_dictionaries({ |
| 376 | + h2.settings.SettingCodes.HEADER_TABLE_SIZE: |
| 377 | + integers(0, 2**32 - 1), |
| 378 | + h2.settings.SettingCodes.ENABLE_PUSH: integers(0, 1), |
| 379 | + h2.settings.SettingCodes.INITIAL_WINDOW_SIZE: |
| 380 | + integers(0, 2**31 - 1), |
| 381 | + h2.settings.SettingCodes.MAX_FRAME_SIZE: |
| 382 | + integers(2**14, 2**24 - 1), |
| 383 | + h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS: |
| 384 | + integers(0, 2**32 - 1), |
| 385 | + h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE: |
| 386 | + integers(0, 2**32 - 1), |
| 387 | + }) |
| 388 | + ) |
430 | 389 |
|
431 |
| - def test_different_eq(self): |
| 390 | + @given(settings=SettingsStrategy) |
| 391 | + def test_equality_reflexive(self, settings): |
432 | 392 | """
|
433 |
| - Two objects that are not equal to each other do not compare equal to |
434 |
| - each other using the == operator. |
| 393 | + An object compares equal to itself using the == operator and the != |
| 394 | + operator. |
435 | 395 | """
|
436 |
| - a = self.an_instance() |
437 |
| - b = self.another_instance() |
438 |
| - assert not (a == b) |
| 396 | + assert (settings == settings) |
| 397 | + assert not (settings != settings) |
439 | 398 |
|
440 |
| - def test_different_ne(self): |
| 399 | + @given(settings=SettingsStrategy, o_settings=SettingsStrategy) |
| 400 | + def test_equality_multiple(self, settings, o_settings): |
441 | 401 | """
|
442 |
| - Two objects that are not equal to each other compare not equal to each |
443 |
| - other using the != operator. |
| 402 | + Two objects compare themselves using the == operator and the != |
| 403 | + operator. |
444 | 404 | """
|
445 |
| - a = self.an_instance() |
446 |
| - b = self.another_instance() |
447 |
| - assert (a != b) |
| 405 | + if settings == o_settings: |
| 406 | + assert settings == o_settings |
| 407 | + assert not (settings != o_settings) |
| 408 | + else: |
| 409 | + assert settings != o_settings |
| 410 | + assert not (settings == o_settings) |
448 | 411 |
|
449 |
| - def test_another_type_eq(self): |
| 412 | + @given(settings=SettingsStrategy) |
| 413 | + def test_another_type_equality(self, settings): |
450 | 414 | """
|
451 | 415 | The object does not compare equal to an object of an unrelated type
|
452 | 416 | (which does not implement the comparison) using the == operator.
|
453 | 417 | """
|
454 |
| - a = self.an_instance() |
455 |
| - b = object() |
456 |
| - assert not (a == b) |
457 |
| - |
458 |
| - def test_another_type_ne(self): |
459 |
| - """ |
460 |
| - The object compares not equal to an object of an unrelated type (which |
461 |
| - does not implement the comparison) using the != operator. |
462 |
| - """ |
463 |
| - a = self.an_instance() |
464 |
| - b = object() |
465 |
| - assert (a != b) |
| 418 | + obj = object() |
| 419 | + assert (settings != obj) |
| 420 | + assert not (settings == obj) |
466 | 421 |
|
467 |
| - def test_delegated_eq(self): |
| 422 | + @given(settings=SettingsStrategy) |
| 423 | + def test_delegated_eq(self, settings): |
468 | 424 | """
|
469 |
| - The result of comparison using == is delegated to the right-hand |
470 |
| - operand if it is of an unrelated type. |
| 425 | + The result of comparison is delegated to the right-hand operand if |
| 426 | + it is of an unrelated type. |
471 | 427 | """
|
472 | 428 | class Delegate(object):
|
473 | 429 | def __eq__(self, other):
|
474 | 430 | return [self]
|
475 | 431 |
|
476 |
| - a = self.an_instance() |
477 |
| - b = Delegate() |
478 |
| - assert (a == b) == [b] |
479 |
| - |
480 |
| - def test_delegate_ne(self): |
481 |
| - """ |
482 |
| - The result of comparison using != is delegated to the right-hand |
483 |
| - operand if it is of an unrelated type. |
484 |
| - """ |
485 |
| - class Delegate(object): |
486 | 432 | def __ne__(self, other):
|
487 | 433 | return [self]
|
488 | 434 |
|
489 |
| - a = self.an_instance() |
490 |
| - b = Delegate() |
491 |
| - assert (a != b) == [b] |
| 435 | + delg = Delegate() |
| 436 | + assert (settings == delg) == [delg] |
| 437 | + assert (settings != delg) == [delg] |
0 commit comments