<s> from testtools import TestCase <EOL> from testtools . helpers import ( <EOL> try_import , <EOL> try_imports , <EOL> ) <EOL> from testtools . matchers import ( <EOL> AllMatch , <EOL> AfterPreprocessing , <EOL> Equals , <EOL> Is , <EOL> Not , <EOL> ) <EOL> from testtools . tests . helpers import ( <EOL> FullStackRunTest , <EOL> hide_testtools_stack , <EOL> is_stack_hidden , <EOL> safe_hasattr , <EOL> ) <EOL> def check_error_callback ( test , function , arg , expected_error_count , <EOL> expect_result ) : <EOL> """<STR_LIT>""" <EOL> cb_calls = [ ] <EOL> def cb ( e ) : <EOL> test . assertIsInstance ( e , ImportError ) <EOL> cb_calls . append ( e ) <EOL> try : <EOL> result = function ( arg , error_callback = cb ) <EOL> except ImportError : <EOL> test . assertFalse ( expect_result ) <EOL> else : <EOL> if expect_result : <EOL> test . assertThat ( result , Not ( Is ( None ) ) ) <EOL> else : <EOL> test . assertThat ( result , Is ( None ) ) <EOL> test . assertEquals ( len ( cb_calls ) , expected_error_count ) <EOL> class TestSafeHasattr ( TestCase ) : <EOL> def test_attribute_not_there ( self ) : <EOL> class Foo ( object ) : <EOL> pass <EOL> self . assertEqual ( False , safe_hasattr ( Foo ( ) , '<STR_LIT>' ) ) <EOL> def test_attribute_there ( self ) : <EOL> class Foo ( object ) : <EOL> pass <EOL> foo = Foo ( ) <EOL> foo . attribute = None <EOL> self . assertEqual ( True , safe_hasattr ( foo , '<STR_LIT>' ) ) <EOL> def test_property_there ( self ) : <EOL> class Foo ( object ) : <EOL> @ property <EOL> def attribute ( self ) : <EOL> return None <EOL> foo = Foo ( ) <EOL> self . assertEqual ( True , safe_hasattr ( foo , '<STR_LIT>' ) ) <EOL> def test_property_raises ( self ) : <EOL> class Foo ( object ) : <EOL> @ property <EOL> def attribute ( self ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> foo = Foo ( ) <EOL> self . assertRaises ( ZeroDivisionError , safe_hasattr , foo , '<STR_LIT>' ) <EOL> class TestTryImport ( TestCase ) : <EOL> def test_doesnt_exist ( self ) : <EOL> marker = object ( ) <EOL> result = try_import ( '<STR_LIT>' , marker ) <EOL> self . assertThat ( result , Is ( marker ) ) <EOL> def test_None_is_default_alternative ( self ) : <EOL> result = try_import ( '<STR_LIT>' ) <EOL> self . assertThat ( result , Is ( None ) ) <EOL> def test_existing_module ( self ) : <EOL> result = try_import ( '<STR_LIT>' , object ( ) ) <EOL> import os <EOL> self . assertThat ( result , Is ( os ) ) <EOL> def test_existing_submodule ( self ) : <EOL> result = try_import ( '<STR_LIT>' , object ( ) ) <EOL> import os <EOL> self . assertThat ( result , Is ( os . path ) ) <EOL> def test_nonexistent_submodule ( self ) : <EOL> marker = object ( ) <EOL> result = try_import ( '<STR_LIT>' , marker ) <EOL> self . assertThat ( result , Is ( marker ) ) <EOL> def test_object_from_module ( self ) : <EOL> result = try_import ( '<STR_LIT>' ) <EOL> import os <EOL> self . assertThat ( result , Is ( os . path . join ) ) <EOL> def test_error_callback ( self ) : <EOL> check_error_callback ( self , try_import , '<STR_LIT>' , <NUM_LIT:1> , False ) <EOL> def test_error_callback_missing_module_member ( self ) : <EOL> check_error_callback ( self , try_import , '<STR_LIT>' , <NUM_LIT:1> , False ) <EOL> def test_error_callback_not_on_success ( self ) : <EOL> check_error_callback ( self , try_import , '<STR_LIT>' , <NUM_LIT:0> , True ) <EOL> class TestTryImports ( TestCase ) : <EOL> def test_doesnt_exist ( self ) : <EOL> marker = object ( ) <EOL> result = try_imports ( [ '<STR_LIT>' ] , marker ) <EOL> self . assertThat ( result , Is ( marker ) ) <EOL> def test_fallback ( self ) : <EOL> result = try_imports ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> import os <EOL> self . assertThat ( result , Is ( os ) ) <EOL> def test_None_is_default_alternative ( self ) : <EOL> e = self . assertRaises ( <EOL> ImportError , try_imports , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertThat ( <EOL> str ( e ) , <EOL> Equals ( "<STR_LIT>" ) ) <EOL> def test_existing_module ( self ) : <EOL> result = try_imports ( [ '<STR_LIT>' ] , object ( ) ) <EOL> import os <EOL> self . assertThat ( result , Is ( os ) ) <EOL> def test_existing_submodule ( self ) : <EOL> result = try_imports ( [ '<STR_LIT>' ] , object ( ) ) <EOL> import os <EOL> self . assertThat ( result , Is ( os . path ) ) <EOL> def test_nonexistent_submodule ( self ) : <EOL> marker = object ( ) <EOL> result = try_imports ( [ '<STR_LIT>' ] , marker ) <EOL> self . assertThat ( result , Is ( marker ) ) <EOL> def test_fallback_submodule ( self ) : <EOL> result = try_imports ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> import os <EOL> self . assertThat ( result , Is ( os . path ) ) <EOL> def test_error_callback ( self ) : <EOL> check_error_callback ( self , try_imports , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> <NUM_LIT:2> , False ) <EOL> check_error_callback ( self , try_imports , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> <NUM_LIT:2> , True ) <EOL> check_error_callback ( self , try_imports , <EOL> [ '<STR_LIT>' ] , <EOL> <NUM_LIT:0> , True ) <EOL> import testtools . matchers <EOL> import testtools . runtest <EOL> import testtools . testcase <EOL> def StackHidden ( is_hidden ) : <EOL> return AllMatch ( <EOL> AfterPreprocessing ( <EOL> lambda module : safe_hasattr ( module , '<STR_LIT>' ) , <EOL> Equals ( is_hidden ) ) ) <EOL> class TestStackHiding ( TestCase ) : <EOL> modules = [ <EOL> testtools . matchers , <EOL> testtools . runtest , <EOL> testtools . testcase , <EOL> ] <EOL> run_tests_with = FullStackRunTest <EOL> def setUp ( self ) : <EOL> super ( TestStackHiding , self ) . setUp ( ) <EOL> self . addCleanup ( hide_testtools_stack , is_stack_hidden ( ) ) <EOL> def test_shown_during_testtools_testsuite ( self ) : <EOL> self . assertThat ( self . modules , StackHidden ( False ) ) <EOL> def test_is_stack_hidden_consistent_true ( self ) : <EOL> hide_testtools_stack ( True ) <EOL> self . assertEqual ( True , is_stack_hidden ( ) ) <EOL> def test_is_stack_hidden_consistent_false ( self ) : <EOL> hide_testtools_stack ( False ) <EOL> self . assertEqual ( False , is_stack_hidden ( ) ) <EOL> def test_show_stack ( self ) : <EOL> hide_testtools_stack ( False ) <EOL> self . assertThat ( self . modules , StackHidden ( False ) ) <EOL> def test_suite ( ) : <EOL> from unittest import TestLoader <EOL> return TestLoader ( ) . loadTestsFromName ( __name__ ) </s>
