<s> from __future__ import absolute_import <EOL> from traits . testing . unittest_tools import unittest <EOL> from . . api import HasTraits , Any , Str <EOL> class IdentityCompare ( HasTraits ) : <EOL> bar = Any ( rich_compare = False ) <EOL> class RichCompare ( HasTraits ) : <EOL> bar = Any ( rich_compare = True ) <EOL> class RichCompareTests : <EOL> def bar_changed ( self , object , trait , old , new ) : <EOL> self . changed_object = object <EOL> self . changed_trait = trait <EOL> self . changed_old = old <EOL> self . changed_new = new <EOL> self . changed_count += <NUM_LIT:1> <EOL> def reset_change_tracker ( self ) : <EOL> self . changed_object = None <EOL> self . changed_trait = None <EOL> self . changed_old = None <EOL> self . changed_new = None <EOL> self . changed_count = <NUM_LIT:0> <EOL> def check_tracker ( self , object , trait , old , new , count ) : <EOL> self . assertEqual ( count , self . changed_count ) <EOL> self . assertIs ( object , self . changed_object ) <EOL> self . assertEqual ( trait , self . changed_trait ) <EOL> self . assertIs ( old , self . changed_old ) <EOL> self . assertIs ( new , self . changed_new ) <EOL> return <EOL> def test_id_first_assignment ( self ) : <EOL> ic = IdentityCompare ( ) <EOL> ic . on_trait_change ( self . bar_changed , '<STR_LIT:bar>' ) <EOL> self . reset_change_tracker ( ) <EOL> default_value = ic . bar <EOL> ic . bar = self . a <EOL> self . check_tracker ( ic , '<STR_LIT:bar>' , default_value , self . a , <NUM_LIT:1> ) <EOL> return <EOL> def test_rich_first_assignment ( self ) : <EOL> rich = RichCompare ( ) <EOL> rich . on_trait_change ( self . bar_changed , '<STR_LIT:bar>' ) <EOL> self . reset_change_tracker ( ) <EOL> default_value = rich . bar <EOL> rich . bar = self . a <EOL> self . check_tracker ( rich , '<STR_LIT:bar>' , default_value , self . a , <NUM_LIT:1> ) <EOL> return <EOL> def test_id_same_object ( self ) : <EOL> ic = IdentityCompare ( ) <EOL> ic . on_trait_change ( self . bar_changed , '<STR_LIT:bar>' ) <EOL> self . reset_change_tracker ( ) <EOL> default_value = ic . bar <EOL> ic . bar = self . a <EOL> self . check_tracker ( ic , '<STR_LIT:bar>' , default_value , self . a , <NUM_LIT:1> ) <EOL> ic . bar = self . a <EOL> self . check_tracker ( ic , '<STR_LIT:bar>' , default_value , self . a , <NUM_LIT:1> ) <EOL> return <EOL> def test_rich_same_object ( self ) : <EOL> rich = RichCompare ( ) <EOL> rich . on_trait_change ( self . bar_changed , '<STR_LIT:bar>' ) <EOL> self . reset_change_tracker ( ) <EOL> default_value = rich . bar <EOL> rich . bar = self . a <EOL> self . check_tracker ( rich , '<STR_LIT:bar>' , default_value , self . a , <NUM_LIT:1> ) <EOL> rich . bar = self . a <EOL> self . check_tracker ( rich , '<STR_LIT:bar>' , default_value , self . a , <NUM_LIT:1> ) <EOL> return <EOL> def test_id_different_object ( self ) : <EOL> ic = IdentityCompare ( ) <EOL> ic . on_trait_change ( self . bar_changed , '<STR_LIT:bar>' ) <EOL> self . reset_change_tracker ( ) <EOL> default_value = ic . bar <EOL> ic . bar = self . a <EOL> self . check_tracker ( ic , '<STR_LIT:bar>' , default_value , self . a , <NUM_LIT:1> ) <EOL> ic . bar = self . different_from_a <EOL> self . check_tracker ( ic , '<STR_LIT:bar>' , self . a , self . different_from_a , <NUM_LIT:2> ) <EOL> return <EOL> def test_rich_different_object ( self ) : <EOL> rich = RichCompare ( ) <EOL> rich . on_trait_change ( self . bar_changed , '<STR_LIT:bar>' ) <EOL> self . reset_change_tracker ( ) <EOL> default_value = rich . bar <EOL> rich . bar = self . a <EOL> self . check_tracker ( rich , '<STR_LIT:bar>' , default_value , self . a , <NUM_LIT:1> ) <EOL> rich . bar = self . different_from_a <EOL> self . check_tracker ( rich , '<STR_LIT:bar>' , self . a , self . different_from_a , <NUM_LIT:2> ) <EOL> return <EOL> def test_id_different_object_same_as ( self ) : <EOL> ic = IdentityCompare ( ) <EOL> ic . on_trait_change ( self . bar_changed , '<STR_LIT:bar>' ) <EOL> self . reset_change_tracker ( ) <EOL> default_value = ic . bar <EOL> ic . bar = self . a <EOL> self . check_tracker ( ic , '<STR_LIT:bar>' , default_value , self . a , <NUM_LIT:1> ) <EOL> ic . bar = self . same_as_a <EOL> self . check_tracker ( ic , '<STR_LIT:bar>' , self . a , self . same_as_a , <NUM_LIT:2> ) <EOL> return <EOL> def test_rich_different_object_same_as ( self ) : <EOL> rich = RichCompare ( ) <EOL> rich . on_trait_change ( self . bar_changed , '<STR_LIT:bar>' ) <EOL> self . reset_change_tracker ( ) <EOL> default_value = rich . bar <EOL> rich . bar = self . a <EOL> self . check_tracker ( rich , '<STR_LIT:bar>' , default_value , self . a , <NUM_LIT:1> ) <EOL> rich . bar = self . same_as_a <EOL> self . check_tracker ( rich , '<STR_LIT:bar>' , default_value , self . a , <NUM_LIT:1> ) <EOL> return <EOL> class Foo ( HasTraits ) : <EOL> name = Str <EOL> def __ne__ ( self , other ) : <EOL> return self . name != other . name <EOL> def __eq__ ( self , other ) : <EOL> return self . name == other . name <EOL> class RichCompareHasTraitsTestCase ( unittest . TestCase , RichCompareTests ) : <EOL> def setUp ( self ) : <EOL> self . a = Foo ( name = '<STR_LIT:a>' ) <EOL> self . same_as_a = Foo ( name = '<STR_LIT:a>' ) <EOL> self . different_from_a = Foo ( name = '<STR_LIT>' ) <EOL> return <EOL> def test_assumptions ( self ) : <EOL> self . assertIsNot ( self . a , self . same_as_a ) <EOL> self . assertIsNot ( self . a , self . different_from_a ) <EOL> self . assertEqual ( self . a . name , self . same_as_a . name ) <EOL> self . assertNotEqual ( self . a . name , self . different_from_a . name ) <EOL> return </s>
