main
  1using System;
  2using developwithpassion.bdd.contexts;
  3using Gorilla.Commons.Infrastructure.FileSystem;
  4using Gorilla.Commons.Testing;
  5using gorilla.commons.utility;
  6using momoney.presentation.model.eventing;
  7using momoney.service.infrastructure;
  8using MoMoney.Service.Infrastructure.Eventing;
  9using momoney.service.infrastructure.transactions;
 10
 11namespace MoMoney.Presentation.Model.Projects
 12{
 13    public class ProjectControllerSpecs
 14    {
 15        [Concern(typeof (ProjectController))]
 16        public abstract class behaves_like_a_project : concerns_for<IProjectController, ProjectController>
 17        {
 18            context c = () =>
 19            {
 20                broker = the_dependency<IEventAggregator>();
 21                tasks = the_dependency<IProjectTasks>();
 22            };
 23
 24            static protected IEventAggregator broker;
 25            static protected IProjectTasks tasks;
 26        }
 27
 28        public class when_saving_the_current_project : behaves_like_a_project
 29        {
 30            it should_notify_the_rest_of_the_application = () => broker.was_told_to(x => x.publish<SavedChangesEvent>());
 31
 32            context c = () =>
 33            {
 34                file_to_update = an<File>();
 35                when_the(file_to_update).is_told_to(x => x.does_the_file_exist()).it_will_return(true);
 36            };
 37
 38            because b = () =>
 39            {
 40                sut.open_project_from(file_to_update);
 41                sut.save_changes();
 42            };
 43
 44            static File file_to_update;
 45        }
 46
 47        public class when_attempting_to_save_the_changes_to_a_project_and_a_file_to_save_to_has_not_been_specified :
 48            behaves_like_a_project
 49        {
 50            it should_inform_the_user_of_an_error = () => the_call.should_have_thrown<FileNotSpecifiedException>();
 51
 52            because b = () =>
 53            {
 54                the_call = call.to(() => sut.save_changes());
 55            };
 56
 57            static Action the_call;
 58        }
 59
 60        public class when_specifying_a_new_path_to_save_an_opened_project_to : behaves_like_a_project
 61        {
 62            it should_save_the_current_database_to_the_new_path = () => tasks.was_told_to(x => x.copy_to("blah"));
 63
 64            context c = () =>
 65            {
 66                original_file = an<File>();
 67                new_file = an<File>();
 68                when_the(new_file).is_told_to(x => x.path).it_will_return("blah");
 69            };
 70
 71            because b = () =>
 72            {
 73                sut.open_project_from(original_file);
 74                sut.save_project_to(new_file);
 75            };
 76
 77            static File original_file;
 78            static File new_file;
 79        }
 80
 81        public class when_attempting_to_open_an_invalid_project_file_path : behaves_like_a_project
 82        {
 83            it should_not_change_the_current_working_file = () => result.should_be_equal_to(false);
 84
 85            context c = () =>
 86            {
 87                invalid_file = an<File>();
 88                when_the(invalid_file).is_told_to(x => x.does_the_file_exist()).it_will_return(false);
 89            };
 90
 91            because b = () =>
 92            {
 93                sut.open_project_from(invalid_file);
 94                result = sut.has_been_saved_at_least_once();
 95            };
 96
 97            static File invalid_file;
 98            static bool result;
 99        }
100
101        public class when_attempting_to_save_all_changes_to_a_new_file_with_an_invalid_path : behaves_like_a_project
102        {
103            it should_not_change_the_current_file_to_the_invalid_one = () => result.should_be_equal_to(false);
104
105            context c = () =>
106            {
107                invalid_file = an<File>();
108
109                when_the(invalid_file).is_told_to(x => x.path).it_will_return(string.Empty);
110            };
111
112            because b = () =>
113            {
114                sut.save_project_to(invalid_file);
115                result = sut.has_been_saved_at_least_once();
116            };
117
118            static File invalid_file;
119            static bool result;
120        }
121
122        public class when_opening_a_new_file : behaves_like_a_project
123        {
124            context c = () =>
125            {
126                file = an<File>();
127                when_the(file).is_told_to(x => x.does_the_file_exist()).it_will_return(true);
128            };
129
130            because b = () => sut.open_project_from(file);
131
132            static File file;
133        }
134
135        public class when_checking_if_there_are_any_unsaved_changes_and_a_project_is_not_open : behaves_like_a_project
136        {
137            it should_return_false = () => result.should_be_equal_to(false);
138
139            because b = () =>
140            {
141                result = sut.has_unsaved_changes();
142            };
143
144            static bool result;
145        }
146
147        public class when_checking_if_there_are_any_unsaved_changes_and_there_are : behaves_like_a_project
148        {
149            it should_return_true = () => result.should_be_true();
150
151            context c = () =>
152            {
153                unit_of_work = an<IUnitOfWork>();
154                when_the(unit_of_work).is_told_to(x => x.is_dirty()).it_will_return(true);
155            };
156
157            because b = () =>
158            {
159                sut.downcast_to<ProjectController>().run(unit_of_work);
160                result = sut.has_unsaved_changes();
161            };
162
163            static bool result;
164            static IUnitOfWork unit_of_work;
165        }
166
167        public class when_starting_a_new_project_and_a_project_was_already_open : behaves_like_a_project
168        {
169            it should_close_the_previous_project = () => broker.was_told_to(x => x.publish<ClosingProjectEvent>());
170
171            because b = () =>
172            {
173                sut.start_new_project();
174                sut.start_new_project();
175            };
176        }
177
178        public class when_opening_an_existing_project_and_a_project_was_already_open : behaves_like_a_project
179        {
180            it should_close_the_previous_project = () => broker.was_told_to(x => x.publish<ClosingProjectEvent>());
181
182            context c = () =>
183            {
184                file = an<File>();
185                when_the(file).is_told_to(x => x.does_the_file_exist()).it_will_return(true);
186            };
187
188            because b = () =>
189            {
190                sut.open_project_from(file);
191                sut.start_new_project();
192            };
193
194            static File file;
195        }
196    }
197}