<s> import os <EOL> from os . path import join as j <EOL> import unittest <EOL> from nose . tools import assert_true , assert_equals , assert_is_none <EOL> from robotide . robotapi import TestDataDirectory , TestCaseFile , ResourceFile <EOL> from robotide . controller import Project <EOL> from robotide . namespace import Namespace <EOL> from robotide . controller . filecontrollers import TestCaseFileController , TestDataDirectoryController , ResourceFileController <EOL> from robotide . publish . messages import RideOpenSuite , RideOpenResource <EOL> from robotide . spec . librarymanager import LibraryManager <EOL> from resources import ( COMPLEX_SUITE_PATH , MINIMAL_SUITE_PATH , RESOURCE_PATH , <EOL> MessageRecordingLoadObserver , SUITEPATH , <EOL> DATAPATH , RELATIVE_PATH_TO_RESOURCE_FILE , <EOL> RESOURCE_PATH2 , RESOURCE_PATH3 , RESOURCE_PATH_TXT , <EOL> EXTERNAL_RES_UNSORTED_PATH , FakeSettings ) <EOL> from resources . mocks import PublisherListener <EOL> import datafilereader <EOL> ALL_RESOURCE_PATH_RELATED_RESOURCE_IMPORTS = [ RESOURCE_PATH , RESOURCE_PATH2 , RESOURCE_PATH3 , RESOURCE_PATH_TXT ] <EOL> def _library_manager ( ) : <EOL> library_manager = LibraryManager ( '<STR_LIT>' ) <EOL> library_manager . create_database ( ) <EOL> return library_manager <EOL> class ProjectTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . _library_manager = _library_manager ( ) <EOL> self . ctrl = Project ( Namespace ( FakeSettings ( ) ) , FakeSettings ( ) , self . _library_manager ) <EOL> self . load_observer = MessageRecordingLoadObserver ( ) <EOL> self . suite_listener = PublisherListener ( RideOpenSuite ) <EOL> self . resource_listener = PublisherListener ( RideOpenResource ) <EOL> def tearDown ( self ) : <EOL> self . suite_listener . unsubscribe ( ) <EOL> self . resource_listener . unsubscribe ( ) <EOL> self . ctrl . close ( ) <EOL> self . _library_manager . stop ( ) <EOL> def test_loading_suite_at_startup ( self ) : <EOL> self . _load ( MINIMAL_SUITE_PATH ) <EOL> assert_true ( self . ctrl . _controller is not None ) <EOL> self . _test_listeners ( [ MINIMAL_SUITE_PATH ] , [ ] ) <EOL> def _test_listeners ( self , suite_paths , resource_paths ) : <EOL> self . assertEqual ( self . _get_paths ( self . suite_listener . data ) , suite_paths ) <EOL> self . assertEqual ( self . _get_paths ( self . resource_listener . data ) , resource_paths ) <EOL> def _get_paths ( self , data ) : <EOL> return [ item . path for item in data ] <EOL> def test_loading_resource_at_startup ( self ) : <EOL> self . _load ( RESOURCE_PATH ) <EOL> assert_true ( self . ctrl . resources != [ ] ) <EOL> self . _test_listeners ( [ ] , ALL_RESOURCE_PATH_RELATED_RESOURCE_IMPORTS ) <EOL> def test_loading_invalid_data_at_startup ( self ) : <EOL> msg = "<STR_LIT>" <EOL> self . ctrl . load_data ( '<STR_LIT>' , self . load_observer ) <EOL> assert_true ( self . load_observer . finished ) <EOL> assert_equals ( self . load_observer . message , msg ) <EOL> self . _test_listeners ( [ ] , [ ] ) <EOL> def _load ( self , path ) : <EOL> self . ctrl . load_data ( path , self . load_observer ) <EOL> assert_true ( self . load_observer . notified ) <EOL> assert_true ( self . load_observer . finished ) <EOL> def test_loading_datafile ( self ) : <EOL> data = self . ctrl . load_datafile ( MINIMAL_SUITE_PATH , self . load_observer ) <EOL> assert_true ( self . load_observer . finished ) <EOL> assert_true ( data is not None ) <EOL> self . _test_listeners ( [ MINIMAL_SUITE_PATH ] , [ ] ) <EOL> def test_reloading ( self ) : <EOL> self . ctrl . new_file_project ( MINIMAL_SUITE_PATH ) <EOL> files1 = self . ctrl . datafiles <EOL> self . ctrl . new_file_project ( MINIMAL_SUITE_PATH ) <EOL> files2 = self . ctrl . datafiles <EOL> assert_true ( files1 != files2 ) <EOL> def test_loading_resource_file ( self ) : <EOL> resource = self . ctrl . load_resource ( RESOURCE_PATH , self . load_observer ) <EOL> assert_true ( self . load_observer . finished ) <EOL> assert_true ( resource is not None ) <EOL> self . _test_listeners ( [ ] , ALL_RESOURCE_PATH_RELATED_RESOURCE_IMPORTS ) <EOL> def test_loading_invalid_datafile ( self ) : <EOL> self . ctrl . load_datafile ( '<STR_LIT>' , self . load_observer ) <EOL> assert_equals ( self . load_observer . message , "<STR_LIT>" ) <EOL> self . _test_listeners ( [ ] , [ ] ) <EOL> def test_loading_invalid_resource ( self ) : <EOL> assert_is_none ( self . ctrl . load_resource ( '<STR_LIT>' , self . load_observer ) ) <EOL> assert_equals ( self . load_observer . message , "<STR_LIT>" ) <EOL> self . _test_listeners ( [ ] , [ ] ) <EOL> def test_dirtyness ( self ) : <EOL> self . ctrl . load_data ( COMPLEX_SUITE_PATH , MessageRecordingLoadObserver ( ) ) <EOL> assert_true ( not self . ctrl . is_dirty ( ) ) <EOL> self . ctrl . data . create_test ( '<STR_LIT>' ) <EOL> assert_true ( self . ctrl . is_dirty ( ) ) <EOL> def test_load_dirty_controllers ( self ) : <EOL> self . ctrl . load_data ( SUITEPATH , MessageRecordingLoadObserver ( ) ) <EOL> assert_equals ( len ( self . ctrl . _get_all_dirty_controllers ( ) ) , <NUM_LIT:0> ) <EOL> tcf = self . _find_suite_by_type ( self . ctrl . data . children , TestCaseFileController ) <EOL> tcf . create_test ( '<STR_LIT>' ) <EOL> assert_equals ( len ( self . ctrl . _get_all_dirty_controllers ( ) ) , <NUM_LIT:1> ) <EOL> self . ctrl . data . mark_dirty ( ) <EOL> assert_equals ( len ( self . ctrl . _get_all_dirty_controllers ( ) ) , <NUM_LIT:2> ) <EOL> sub_dir = self . _find_suite_by_type ( self . ctrl . data . children , TestDataDirectoryController ) <EOL> sub_dir_tcf = self . _find_suite_by_type ( sub_dir . children , TestCaseFileController ) <EOL> sub_dir_tcf . create_test ( '<STR_LIT>' ) <EOL> assert_equals ( len ( self . ctrl . _get_all_dirty_controllers ( ) ) , <NUM_LIT:3> ) <EOL> def _find_suite_by_type ( self , suites , type ) : <EOL> for child in suites : <EOL> if isinstance ( child , type ) : <EOL> return child <EOL> return None <EOL> def test_creating_new_resource ( self ) : <EOL> controller = self . ctrl . new_resource ( '<STR_LIT>' ) <EOL> assert_equals ( controller . name , '<STR_LIT>' ) <EOL> def test_resource_with_same_path_is_not_added_twice ( self ) : <EOL> self . ctrl . new_resource ( '<STR_LIT>' ) <EOL> self . ctrl . new_resource ( '<STR_LIT>' ) <EOL> assert_equals ( len ( self . ctrl . resources ) , <NUM_LIT:1> ) <EOL> def test_load_data_with_external_resources_all_externals_are_used ( self ) : <EOL> are_used = [ ] <EOL> def handle ( message ) : <EOL> are_used . append ( message . datafile . is_used ( ) ) <EOL> self . resource_listener . outer_listener = handle <EOL> self . _load ( EXTERNAL_RES_UNSORTED_PATH ) <EOL> assert_true ( self . ctrl . resources != [ ] ) <EOL> res_path = os . path . join ( os . path . split ( EXTERNAL_RES_UNSORTED_PATH ) [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> abc_path = os . path . join ( res_path , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> bar_path = os . path . join ( res_path , '<STR_LIT>' , '<STR_LIT>' ) <EOL> foo_path = os . path . join ( res_path , '<STR_LIT>' , '<STR_LIT>' ) <EOL> hello_path = os . path . join ( res_path , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> resource_path = os . path . join ( res_path , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( are_used , [ True for _ in range ( <NUM_LIT:5> ) ] ) <EOL> self . _test_listeners ( [ EXTERNAL_RES_UNSORTED_PATH ] , [ abc_path , bar_path , foo_path , hello_path , resource_path ] ) <EOL> def test_sort_external_resources ( self ) : <EOL> self . ctrl . load_data ( EXTERNAL_RES_UNSORTED_PATH , MessageRecordingLoadObserver ( ) ) <EOL> assert_equals ( [ res . name for res in self . ctrl . external_resources ] , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def test_datafiles_property_with_resource_file_only ( self ) : <EOL> resource = self . ctrl . load_resource ( RESOURCE_PATH , self . load_observer ) <EOL> assert_equals ( self . ctrl . datafiles [ <NUM_LIT:0> ] , resource ) <EOL> def test_get_all_keywords_with_resource_file_only ( self ) : <EOL> project = datafilereader . construct_project ( RESOURCE_PATH ) <EOL> all_kws = project . get_all_keywords ( ) <EOL> project . close ( ) <EOL> res_kws = [ kw for kw in all_kws if kw . name == '<STR_LIT>' ] <EOL> assert_equals ( len ( res_kws ) , <NUM_LIT:1> ) <EOL> def test_resource_import_modified ( self ) : <EOL> self . ctrl . resource_import_modified ( RELATIVE_PATH_TO_RESOURCE_FILE , DATAPATH ) <EOL> self . _test_listeners ( [ ] , ALL_RESOURCE_PATH_RELATED_RESOURCE_IMPORTS ) <EOL> def _data_directory ( path ) : <EOL> data = TestDataDirectory ( ) <EOL> data . source = data . directory = os . path . normpath ( path ) <EOL> return data <EOL> def _testcasefile ( path ) : <EOL> data = TestCaseFile ( ) <EOL> data . source = os . path . normpath ( path ) <EOL> return data <EOL> class TestResolvingResourceDirectories ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . project = Project ( Namespace ( FakeSettings ( ) ) , FakeSettings ( ) , _library_manager ( ) ) <EOL> def tearDown ( self ) : <EOL> self . project . close ( ) <EOL> def test_resource_file_outside_of_topsuite_is_an_external_resource ( self ) : <EOL> self . _set_data_directory_controller ( '<STR_LIT>' ) <EOL> self . _set_resources ( j ( '<STR_LIT:foo>' , '<STR_LIT>' ) ) <EOL> assert_equals ( self . project . external_resources , self . project . resources ) <EOL> def _set_data_directory_controller ( self , dir ) : <EOL> self . project . _controller = TestDataDirectoryController ( _data_directory ( dir ) ) <EOL> def test_resource_file_in_own_directory_is_added_to_top_suite ( self ) : <EOL> self . _set_data_directory_controller ( '<STR_LIT:foo>' ) <EOL> self . _set_resources ( j ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ) ) <EOL> self . _assert_resource_dir_was_created_as_child_of ( self . project . data ) <EOL> self . _assert_resource_dir_contains_resources ( ) <EOL> assert_true ( len ( self . project . external_resources ) == <NUM_LIT:0> ) <EOL> def test_two_resource_in_same_directory_get_same_parent ( self ) : <EOL> self . _set_data_directory_controller ( '<STR_LIT:foo>' ) <EOL> self . _set_resources ( j ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ) , j ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ) ) <EOL> self . _assert_resource_dir_was_created_as_child_of ( self . project . data ) <EOL> self . _assert_resource_dir_contains_resources ( ) <EOL> def test_two_nested_resources_in_same_directory_get_same_parent ( self ) : <EOL> self . _set_data_directory_controller ( '<STR_LIT>' ) <EOL> self . _set_resources ( j ( '<STR_LIT>' , '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ) , j ( '<STR_LIT>' , '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ) ) <EOL> assert_equals ( self . project . data . children [ <NUM_LIT:0> ] . children [ <NUM_LIT:0> ] . children , <EOL> self . project . resources ) <EOL> def test_resource_directory_gets_nearest_possible_parent ( self ) : <EOL> self . _set_data_directory_controller ( '<STR_LIT>' ) <EOL> self . project . data . add_child ( TestDataDirectoryController ( _data_directory ( j ( '<STR_LIT>' , '<STR_LIT>' ) ) ) ) <EOL> self . _set_resources ( j ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> assert_equals ( len ( self . project . data . children ) , <NUM_LIT:1> ) <EOL> assert_equals ( len ( self . project . data . children [ <NUM_LIT:0> ] . children ) , <NUM_LIT:1> ) <EOL> assert_equals ( self . project . data . children [ <NUM_LIT:0> ] . children [ <NUM_LIT:0> ] . children , [ self . project . resources [ <NUM_LIT:0> ] ] ) <EOL> def test_nested_resource_directories ( self ) : <EOL> self . _set_data_directory_controller ( '<STR_LIT>' ) <EOL> self . _set_resources ( j ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , j ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> assert_equals ( len ( self . project . data . children ) , <NUM_LIT:1> ) <EOL> assert_equals ( len ( self . project . data . children [ <NUM_LIT:0> ] . children ) , <NUM_LIT:2> ) <EOL> assert_equals ( self . project . data . children [ <NUM_LIT:0> ] . children [ <NUM_LIT:1> ] . children , [ self . project . resources [ <NUM_LIT:1> ] ] ) <EOL> def test_resource_in_nested_directory ( self ) : <EOL> self . _set_data_directory_controller ( '<STR_LIT>' ) <EOL> self . _set_resources ( j ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> assert_equals ( len ( self . project . data . children ) , <NUM_LIT:1> ) <EOL> assert_equals ( len ( self . project . data . children [ <NUM_LIT:0> ] . children ) , <NUM_LIT:1> ) <EOL> assert_equals ( self . project . data . children [ <NUM_LIT:0> ] . children [ <NUM_LIT:0> ] . children , [ self . project . resources [ <NUM_LIT:0> ] ] ) <EOL> assert_true ( len ( self . project . external_resources ) == <NUM_LIT:0> ) <EOL> def _set_resources ( self , * paths ) : <EOL> for p in paths : <EOL> resource = ResourceFileController ( ResourceFile ( os . path . normpath ( p ) ) ) <EOL> self . project . resources . append ( resource ) <EOL> self . project . insert_into_suite_structure ( resource ) <EOL> def _assert_resource_dir_was_created_as_child_of ( self , ctrl ) : <EOL> assert_equals ( len ( ctrl . children ) , <NUM_LIT:1> ) <EOL> def _assert_resource_dir_contains_resources ( self ) : <EOL> assert_equals ( self . project . data . children [ <NUM_LIT:0> ] . children , self . project . resources ) <EOL> class TestFindingControllers ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . project = Project ( Namespace ( FakeSettings ( ) ) , FakeSettings ( ) , _library_manager ( ) ) <EOL> def tearDown ( self ) : <EOL> self . project . close ( ) <EOL> def test_finding_root_directory_controller ( self ) : <EOL> self . project . _controller = TestDataDirectoryController ( _data_directory ( '<STR_LIT>' ) ) <EOL> result = self . project . find_controller_by_longname ( '<STR_LIT>' ) <EOL> assert_equals ( result , self . project . _controller ) <EOL> def test_finding_subdirectory_controller ( self ) : <EOL> directory_controller = TestDataDirectoryController ( _data_directory ( '<STR_LIT>' ) ) <EOL> subdirectory_controller = TestDataDirectoryController ( _data_directory ( '<STR_LIT>' ) ) <EOL> directory_controller . add_child ( subdirectory_controller ) <EOL> self . project . _controller = directory_controller <EOL> result = self . project . find_controller_by_longname ( '<STR_LIT>' ) <EOL> assert_equals ( result , subdirectory_controller ) <EOL> def test_finding_testcase_controller ( self ) : <EOL> suite_controller = TestCaseFileController ( _testcasefile ( '<STR_LIT>' ) ) <EOL> test = suite_controller . create_test ( '<STR_LIT>' ) <EOL> self . project . _controller = suite_controller <EOL> result = self . project . find_controller_by_longname ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert_equals ( result , test ) <EOL> def test_finding_correct_testcase_when_two_with_same_name ( self ) : <EOL> test1 , test2 = self . _create_suite_structure_with_two_tests_with_same_name ( ) <EOL> result1 = self . project . find_controller_by_longname ( '<STR_LIT>' + test1 . longname , test1 . display_name ) <EOL> assert_equals ( result1 , test1 ) <EOL> result2 = self . project . find_controller_by_longname ( '<STR_LIT>' + test2 . longname , test2 . display_name ) <EOL> assert_equals ( result2 , test2 ) <EOL> def test_finding_correct_testcase_when_two_files_with_same_name_start ( self ) : <EOL> directory_controller = TestDataDirectoryController ( _data_directory ( '<STR_LIT:t>' ) ) <EOL> suite1_controller = TestCaseFileController ( _testcasefile ( '<STR_LIT>' ) ) <EOL> test1 = suite1_controller . create_test ( '<STR_LIT:A>' ) <EOL> suite2_controller = TestCaseFileController ( _testcasefile ( '<STR_LIT>' ) ) <EOL> test2 = suite2_controller . create_test ( '<STR_LIT:A>' ) <EOL> directory_controller . add_child ( suite1_controller ) <EOL> directory_controller . add_child ( suite2_controller ) <EOL> self . project . _controller = directory_controller <EOL> result1 = self . project . find_controller_by_longname ( '<STR_LIT>' + test1 . longname , test1 . display_name ) <EOL> assert_equals ( result1 , test1 ) <EOL> result2 = self . project . find_controller_by_longname ( '<STR_LIT>' + test2 . longname , test2 . display_name ) <EOL> assert_equals ( result2 , test2 ) <EOL> def _create_suite_structure_with_two_tests_with_same_name ( self ) : <EOL> directory_controller = TestDataDirectoryController ( _data_directory ( '<STR_LIT>' ) ) <EOL> suite1_controller = TestCaseFileController ( _testcasefile ( '<STR_LIT>' ) ) <EOL> test1 = suite1_controller . create_test ( '<STR_LIT>' ) <EOL> suite2_controller = TestCaseFileController ( _testcasefile ( '<STR_LIT>' ) ) <EOL> test2 = suite2_controller . create_test ( '<STR_LIT>' ) <EOL> directory_controller . add_child ( suite1_controller ) <EOL> directory_controller . add_child ( suite2_controller ) <EOL> self . project . _controller = directory_controller <EOL> return test1 , test2 <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
