robust_excel_ole 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog +13 -0
  3. data/README.rdoc +107 -73
  4. data/{README_excel.rdoc → docs/README_excel.rdoc} +2 -0
  5. data/{README_open.rdoc → docs/README_open.rdoc} +12 -16
  6. data/{README_ranges.rdoc → docs/README_ranges.rdoc} +1 -1
  7. data/{README_save_close.rdoc → docs/README_save_close.rdoc} +0 -0
  8. data/{README_sheet.rdoc → docs/README_sheet.rdoc} +0 -0
  9. data/examples/open_save_close/example_control_to_excel.rb +3 -3
  10. data/examples/open_save_close/example_default_excel.rb +2 -2
  11. data/examples/open_save_close/example_force_excel.rb +2 -2
  12. data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +2 -2
  13. data/examples/open_save_close/example_if_obstructed_forget.rb +2 -2
  14. data/examples/open_save_close/example_if_obstructed_save.rb +2 -2
  15. data/examples/open_save_close/example_if_unsaved_accept.rb +3 -3
  16. data/examples/open_save_close/example_if_unsaved_forget.rb +7 -3
  17. data/examples/open_save_close/example_if_unsaved_forget_more.rb +1 -1
  18. data/examples/open_save_close/example_read_only.rb +2 -2
  19. data/examples/open_save_close/example_rename_cells.rb +2 -2
  20. data/examples/open_save_close/example_reuse.rb +2 -2
  21. data/examples/open_save_close/example_simple.rb +1 -1
  22. data/examples/open_save_close/example_unobtrusively.rb +2 -2
  23. data/lib/robust_excel_ole/book.rb +75 -76
  24. data/lib/robust_excel_ole/bookstore.rb +9 -9
  25. data/lib/robust_excel_ole/excel.rb +5 -5
  26. data/lib/robust_excel_ole/general.rb +11 -4
  27. data/lib/robust_excel_ole/reo_common.rb +3 -0
  28. data/lib/robust_excel_ole/version.rb +1 -1
  29. data/robust_excel_ole.gemspec +2 -2
  30. data/spec/book_spec.rb +67 -63
  31. data/spec/book_specs/book_close_spec.rb +12 -10
  32. data/spec/book_specs/book_misc_spec.rb +279 -279
  33. data/spec/book_specs/book_open_spec.rb +258 -105
  34. data/spec/book_specs/book_save_spec.rb +28 -28
  35. data/spec/book_specs/book_unobtr_spec.rb +862 -164
  36. data/spec/bookstore_spec.rb +22 -22
  37. data/spec/data/another_workbook.xls +0 -0
  38. data/spec/data/different_workbook.xls +0 -0
  39. data/spec/data/workbook.xls +0 -0
  40. data/spec/excel_spec.rb +256 -254
  41. data/spec/general_spec.rb +7 -7
  42. data/spec/ruby1.8.6_rspec2.14/book_spec.rb +1421 -0
  43. data/spec/ruby1.8.6_rspec2.14/book_specs/book_all_spec.rb +22 -0
  44. data/spec/ruby1.8.6_rspec2.14/book_specs/book_close_spec.rb +252 -0
  45. data/spec/ruby1.8.6_rspec2.14/book_specs/book_misc_spec.rb +1070 -0
  46. data/spec/ruby1.8.6_rspec2.14/book_specs/book_open_spec.rb +1855 -0
  47. data/spec/ruby1.8.6_rspec2.14/book_specs/book_save_spec.rb +514 -0
  48. data/spec/ruby1.8.6_rspec2.14/book_specs/book_sheet_spec.rb +395 -0
  49. data/spec/ruby1.8.6_rspec2.14/book_specs/book_subclass_spec.rb +51 -0
  50. data/spec/ruby1.8.6_rspec2.14/book_specs/book_unobtr_spec.rb +1737 -0
  51. data/spec/ruby1.8.6_rspec2.14/bookstore_spec.rb +495 -0
  52. data/spec/ruby1.8.6_rspec2.14/cell_spec.rb +76 -0
  53. data/spec/ruby1.8.6_rspec2.14/cygwin_spec.rb +42 -0
  54. data/spec/ruby1.8.6_rspec2.14/excel_spec.rb +1820 -0
  55. data/spec/ruby1.8.6_rspec2.14/general_spec.rb +212 -0
  56. data/spec/ruby1.8.6_rspec2.14/range_spec.rb +131 -0
  57. data/spec/ruby1.8.6_rspec2.14/reo_common_spec.rb +130 -0
  58. data/spec/ruby1.8.6_rspec2.14/sheet_spec.rb +663 -0
  59. data/spec/ruby1.8.6_rspec2.14/spec_helper.rb +35 -0
  60. data/spec/sheet_spec.rb +2 -2
  61. metadata +28 -12
  62. data/README_detail.rdoc +0 -852
  63. data/README_development.rdoc +0 -5
@@ -0,0 +1,395 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.join(File.dirname(__FILE__), './../spec_helper')
4
+
5
+
6
+ $VERBOSE = nil
7
+
8
+ include RobustExcelOle
9
+ include General
10
+
11
+ describe Book do
12
+
13
+ before(:all) do
14
+ excel = Excel.new(:reuse => true)
15
+ open_books = excel == nil ? 0 : excel.Workbooks.Count
16
+ puts "*** open books *** : #{open_books}" if open_books > 0
17
+ Excel.kill_all
18
+ end
19
+
20
+ before do
21
+ @dir = create_tmpdir
22
+ @simple_file = @dir + '/workbook.xls'
23
+ @simple_save_file = @dir + '/workbook_save.xls'
24
+ @different_file = @dir + '/different_workbook.xls'
25
+ @simple_file_other_path = @dir + '/more_data/workbook.xls'
26
+ @another_simple_file = @dir + '/another_workbook.xls'
27
+ @linked_file = @dir + '/workbook_linked.xlsm'
28
+ @simple_file_xlsm = @dir + '/workbook.xls'
29
+ @simple_file_xlsx = @dir + '/workbook.xlsx'
30
+ end
31
+
32
+ after do
33
+ Excel.kill_all
34
+ #rm_tmp(@dir)
35
+ end
36
+
37
+ describe 'access sheet' do
38
+ before do
39
+ @book = Book.open(@simple_file)
40
+ end
41
+
42
+ after do
43
+ @book.close
44
+ end
45
+
46
+ it 'with sheet name' do
47
+ @book.sheet("Sheet1").should be_kind_of Sheet
48
+ @book.sheet("Sheet1").name.should == "Sheet1"
49
+ end
50
+
51
+ it 'with integer' do
52
+ @book.sheet(1).should be_kind_of Sheet
53
+ @book.sheet(1).name.should == "Sheet1"
54
+ end
55
+
56
+ it 'with block' do
57
+ @book.each do |sheet|
58
+ sheet.should be_kind_of Sheet
59
+ end
60
+ end
61
+
62
+ context 'open with block' do
63
+ it {
64
+ Book.open(@simple_file) do |book|
65
+ @book.sheet("Sheet1").should be_a Sheet
66
+ @book.sheet("Sheet1").name.should == "Sheet1"
67
+ end
68
+ }
69
+ end
70
+ end
71
+
72
+ describe 'access first and last sheet' do
73
+ before do
74
+ @book = Book.open(@simple_file)
75
+ end
76
+
77
+ it "should access the first sheet" do
78
+ first_sheet = @book.first_sheet
79
+ first_sheet.name.should == Sheet.new(@book.Worksheets.Item(1)).Name
80
+ first_sheet.name.should == @book.sheet(1).Name
81
+ end
82
+
83
+ it "should access the last sheet" do
84
+ last_sheet = @book.last_sheet
85
+ last_sheet.name.should == Sheet.new(@book.Worksheets.Item(3)).Name
86
+ last_sheet.name.should == @book.sheet(3).Name
87
+ end
88
+ end
89
+
90
+ describe "add_sheet" do
91
+
92
+ context "with no given sheet" do
93
+
94
+ before do
95
+ @book = Book.open(@simple_file)
96
+ @sheet = @book.sheet(1)
97
+ end
98
+
99
+ after do
100
+ @book.close(:if_unsaved => :forget)
101
+ end
102
+
103
+ it "should add empty sheet" do
104
+ @book.ole_workbook.Worksheets.Count.should == 3
105
+ @book.add_sheet
106
+ @book.ole_workbook.Worksheets.Count.should == 4
107
+ end
108
+
109
+ it "should add an empty sheet and return this added sheet" do
110
+ sheet = @book.add_sheet
111
+ copyed_sheet = @book.ole_workbook.Worksheets.Item(@book.ole_workbook.Worksheets.Count)
112
+ sheet.name.should eq copyed_sheet.name
113
+ end
114
+
115
+ it "should return new sheet" do
116
+ @book.add_sheet(:as => 'new sheet').name.should eq 'new sheet'
117
+ end
118
+
119
+ it "should add the first sheet" do
120
+ @book.add_sheet(:before => @sheet).name.should eq @book.sheet(1).name
121
+ end
122
+
123
+ it "should add the second sheet" do
124
+ @book.add_sheet(:after => @sheet).name.should eq @book.sheet(2).name
125
+ end
126
+
127
+ end
128
+
129
+ context "with copying a given sheet" do
130
+
131
+ before do
132
+ @book = Book.open(@simple_file)
133
+ @sheet = @book.sheet(1)
134
+ @another_book = Book.open(@another_simple_file)
135
+ end
136
+
137
+ after do
138
+ @book.close(:if_unsaved => :forget)
139
+ @another_book.close(:if_unsaved => :forget)
140
+ end
141
+
142
+ it "should copy and append a given sheet" do
143
+ @book.ole_workbook.Worksheets.Count.should == 3
144
+ @book.add_sheet @sheet
145
+ @book.ole_workbook.Worksheets.Count.should == 4
146
+ @book.ole_workbook.Worksheets(4).Name.should == @sheet.Name + " (2)"
147
+ end
148
+
149
+ it "should copy sheet from another book " do
150
+ @book.ole_workbook.Worksheets.Count.should == 3
151
+ @another_book.add_sheet @sheet
152
+ @another_book.ole_workbook.Worksheets.Count.should == 4
153
+ @another_book.ole_workbook.Worksheets(4).Name.should == @sheet.Name + " (2)"
154
+ end
155
+
156
+ it "should return copyed sheet" do
157
+ sheet = @book.add_sheet @sheet
158
+ copyed_sheet = @book.ole_workbook.Worksheets.Item(@book.ole_workbook.Worksheets.Count)
159
+ sheet.name.should eq copyed_sheet.name
160
+ end
161
+
162
+ it "should copy a given sheet and name the copyed sheet to 'copyed_name'" do
163
+ @book.add_sheet(@sheet, :as => 'copyed_name').name.should eq 'copyed_name'
164
+ end
165
+
166
+ it "should copy the first sheet and insert it before the first sheet" do
167
+ @book.add_sheet(@sheet, :before => @sheet).name.should eq @book.sheet(1).name
168
+ end
169
+
170
+ it "should copy the first sheet and insert it after the first sheet" do
171
+ @book.add_sheet(@sheet, :after => @sheet).name.should eq @book.sheet(2).name
172
+ end
173
+
174
+ it "should copy the first sheet before the third sheet and give 'before' the highest priority" do
175
+ @book.add_sheet(@sheet, :after => @sheet, :before => @book.sheet(3))
176
+ @book.Worksheets.Count.should == 4
177
+ end
178
+
179
+ it "should copy the first sheet before the third sheet and give 'before' the highest priority" do
180
+ @book.add_sheet(@sheet, :before => @book.sheet(3), :after => @sheet)
181
+ @book.Worksheets.Count.should == 4
182
+ end
183
+
184
+ it "should raise error with giving a name that already exists" do
185
+ @book.add_sheet(@sheet, :as => 'new_sheet')
186
+ expect{
187
+ @book.add_sheet(@sheet, :as => 'new_sheet')
188
+ }.to raise_error(NameAlreadyExists, /sheet name "new_sheet" already exists/)
189
+ end
190
+ end
191
+ end
192
+
193
+ describe "add_empty_sheet" do
194
+
195
+ before do
196
+ @book = Book.open(@simple_file)
197
+ @sheet = @book.sheet(1)
198
+ end
199
+
200
+ after do
201
+ @book.close(:if_unsaved => :forget)
202
+ end
203
+
204
+ it "should add empty sheet" do
205
+ @book.ole_workbook.Worksheets.Count.should == 3
206
+ @book.add_empty_sheet
207
+ @book.ole_workbook.Worksheets.Count.should == 4
208
+ end
209
+
210
+ it "should add an empty sheet and return this added sheet" do
211
+ sheet = @book.add_empty_sheet
212
+ copyed_sheet = @book.ole_workbook.Worksheets.Item(@book.ole_workbook.Worksheets.Count)
213
+ sheet.name.should eq copyed_sheet.name
214
+ end
215
+
216
+ it "should return new sheet" do
217
+ @book.add_empty_sheet(:as => 'new sheet').name.should eq 'new sheet'
218
+ end
219
+
220
+ it "should add the first sheet" do
221
+ @book.add_empty_sheet(:before => @sheet).name.should eq @book.sheet(1).name
222
+ end
223
+
224
+ it "should add the second sheet" do
225
+ @book.add_empty_sheet(:after => @sheet).name.should eq @book.sheet(2).name
226
+ end
227
+ end
228
+
229
+ describe "copy_sheet" do
230
+
231
+ before do
232
+ @book = Book.open(@simple_file)
233
+ @sheet = @book.sheet(1)
234
+ @another_book = Book.open(@another_simple_file)
235
+ end
236
+
237
+ after do
238
+ @book.close(:if_unsaved => :forget)
239
+ @another_book.close(:if_unsaved => :forget)
240
+ end
241
+
242
+ it "should copy and append a given sheet" do
243
+ @book.ole_workbook.Worksheets.Count.should == 3
244
+ @book.copy_sheet @sheet
245
+ @book.ole_workbook.Worksheets.Count.should == 4
246
+ @book.ole_workbook.Worksheets(4).Name.should == @sheet.Name + " (2)"
247
+ end
248
+
249
+ it "should copy sheet from another book " do
250
+ @book.ole_workbook.Worksheets.Count.should == 3
251
+ @another_book.copy_sheet @sheet
252
+ @another_book.ole_workbook.Worksheets.Count.should == 4
253
+ @another_book.ole_workbook.Worksheets(4).Name.should == @sheet.Name + " (2)"
254
+ end
255
+
256
+ it "should return copyed sheet" do
257
+ sheet = @book.add_sheet @sheet
258
+ copyed_sheet = @book.ole_workbook.Worksheets.Item(@book.ole_workbook.Worksheets.Count)
259
+ sheet.name.should eq copyed_sheet.name
260
+ end
261
+
262
+ it "should copy a given sheet and name the copyed sheet to 'copyed_name'" do
263
+ @book.copy_sheet(@sheet, :as => 'copyed_name').name.should eq 'copyed_name'
264
+ end
265
+
266
+ it "should copy the first sheet and insert it before the first sheet" do
267
+ @book.copy_sheet(@sheet, :before => @sheet).name.should eq @book.sheet(1).name
268
+ end
269
+
270
+ it "should copy the first sheet and insert it after the first sheet" do
271
+ @book.copy_sheet(@sheet, :after => @sheet).name.should eq @book.sheet(2).name
272
+ end
273
+
274
+ it "should copy the first sheet before the third sheet and give 'before' the highest priority" do
275
+ @book.copy_sheet(@sheet, :after => @sheet, :before => @book.sheet(3))
276
+ @book.Worksheets.Count.should == 4
277
+ end
278
+
279
+ it "should copy the first sheet before the third sheet and give 'before' the highest priority" do
280
+ @book.copy_sheet(@sheet, :before => @book.sheet(3), :after => @sheet)
281
+ @book.Worksheets.Count.should == 4
282
+ end
283
+
284
+ it "should raise error with giving a name that already exists" do
285
+ @book.copy_sheet(@sheet, :as => 'new_sheet')
286
+ expect{
287
+ @book.copy_sheet(@sheet, :as => 'new_sheet')
288
+ }.to raise_error(NameAlreadyExists, /sheet name "new_sheet" already exists/)
289
+ end
290
+ end
291
+
292
+ describe "add_or_copy_sheet" do
293
+
294
+ context "with no given sheet" do
295
+
296
+ before do
297
+ @book = Book.open(@simple_file)
298
+ @sheet = @book.sheet(1)
299
+ end
300
+
301
+ after do
302
+ @book.close(:if_unsaved => :forget)
303
+ end
304
+
305
+ it "should add empty sheet" do
306
+ @book.ole_workbook.Worksheets.Count.should == 3
307
+ @book.add_or_copy_sheet
308
+ @book.ole_workbook.Worksheets.Count.should == 4
309
+ end
310
+
311
+ it "should add an empty sheet and return this added sheet" do
312
+ sheet = @book.add_or_copy_sheet
313
+ copyed_sheet = @book.ole_workbook.Worksheets.Item(@book.ole_workbook.Worksheets.Count)
314
+ sheet.name.should eq copyed_sheet.name
315
+ end
316
+
317
+ it "should return new sheet" do
318
+ @book.add_or_copy_sheet(:as => 'new sheet').name.should eq 'new sheet'
319
+ end
320
+
321
+ it "should add the first sheet" do
322
+ @book.add_or_copy_sheet(:before => @sheet).name.should eq @book.sheet(1).name
323
+ end
324
+
325
+ it "should add the second sheet" do
326
+ @book.add_or_copy_sheet(:after => @sheet).name.should eq @book.sheet(2).name
327
+ end
328
+
329
+ end
330
+
331
+ context "with copying a given sheet" do
332
+
333
+ before do
334
+ @book = Book.open(@simple_file)
335
+ @sheet = @book.sheet(1)
336
+ @another_book = Book.open(@another_simple_file)
337
+ end
338
+
339
+ after do
340
+ @book.close(:if_unsaved => :forget)
341
+ @another_book.close(:if_unsaved => :forget)
342
+ end
343
+
344
+ it "should copy and append a given sheet" do
345
+ @book.ole_workbook.Worksheets.Count.should == 3
346
+ @book.add_or_copy_sheet @sheet
347
+ @book.ole_workbook.Worksheets.Count.should == 4
348
+ @book.ole_workbook.Worksheets(4).Name.should == @sheet.Name + " (2)"
349
+ end
350
+
351
+ it "should copy sheet from another book " do
352
+ @book.ole_workbook.Worksheets.Count.should == 3
353
+ @another_book.add_or_copy_sheet @sheet
354
+ @another_book.ole_workbook.Worksheets.Count.should == 4
355
+ @another_book.ole_workbook.Worksheets(4).Name.should == @sheet.Name + " (2)"
356
+ end
357
+
358
+ it "should return copyed sheet" do
359
+ sheet = @book.add_or_copy_sheet @sheet
360
+ copyed_sheet = @book.ole_workbook.Worksheets.Item(@book.ole_workbook.Worksheets.Count)
361
+ sheet.name.should eq copyed_sheet.name
362
+ end
363
+
364
+ it "should copy a given sheet and name the copyed sheet to 'copyed_name'" do
365
+ @book.add_or_copy_sheet(@sheet, :as => 'copyed_name').name.should eq 'copyed_name'
366
+ end
367
+
368
+ it "should copy the first sheet and insert it before the first sheet" do
369
+ @book.add_or_copy_sheet(@sheet, :before => @sheet).name.should eq @book.sheet(1).name
370
+ end
371
+
372
+ it "should copy the first sheet and insert it after the first sheet" do
373
+ @book.add_or_copy_sheet(@sheet, :after => @sheet).name.should eq @book.sheet(2).name
374
+ end
375
+
376
+ it "should copy the first sheet before the third sheet and give 'before' the highest priority" do
377
+ @book.add_or_copy_sheet(@sheet, :after => @sheet, :before => @book.sheet(3))
378
+ @book.Worksheets.Count.should == 4
379
+ end
380
+
381
+ it "should copy the first sheet before the third sheet and give 'before' the highest priority" do
382
+ @book.add_or_copy_sheet(@sheet, :before => @book.sheet(3), :after => @sheet)
383
+ @book.Worksheets.Count.should == 4
384
+ end
385
+
386
+ it "should raise error with giving a name that already exists" do
387
+ @book.add_or_copy_sheet(@sheet, :as => 'new_sheet')
388
+ expect{
389
+ @book.add_or_copy_sheet(@sheet, :as => 'new_sheet')
390
+ }.to raise_error(NameAlreadyExists, /sheet name "new_sheet" already exists/)
391
+ end
392
+ end
393
+ end
394
+
395
+ end
@@ -0,0 +1,51 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.join(File.dirname(__FILE__), './../spec_helper')
4
+
5
+
6
+ module My # :nodoc: #
7
+ class Excel < RobustExcelOle::Excel # :nodoc: #
8
+ end
9
+
10
+ class Book < RobustExcelOle::Book # :nodoc: #
11
+ end
12
+
13
+ end
14
+
15
+ describe "subclassed Book" do
16
+
17
+ before(:all) do
18
+ excel = RobustExcelOle::Excel.new(:reuse => true)
19
+ open_books = excel == nil ? 0 : excel.Workbooks.Count
20
+ puts "*** open books *** : #{open_books}" if open_books > 0
21
+ RobustExcelOle::Excel.kill_all
22
+ end
23
+
24
+ before do
25
+ @dir = create_tmpdir
26
+ @simple_file = @dir + '/workbook.xls'
27
+ end
28
+
29
+ after do
30
+ RobustExcelOle::Excel.kill_all
31
+ rm_tmp(@dir)
32
+ end
33
+
34
+
35
+ describe "open" do
36
+
37
+ it "should use the subclassed Excel" do
38
+ #REO::Book.open(@simple_file) do |book|
39
+ My::Book.open(@simple_file) do |book|
40
+ book.should be_a RobustExcelOle::Book
41
+ book.should be_a My::Book
42
+ book.excel.should be_a RobustExcelOle::Excel
43
+ book.excel.class.should == My::Excel
44
+ book.excel.should be_a My::Excel
45
+ end
46
+ end
47
+
48
+ end
49
+
50
+
51
+ end
@@ -0,0 +1,1737 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.join(File.dirname(__FILE__), './../spec_helper')
4
+
5
+
6
+ $VERBOSE = nil
7
+
8
+ include RobustExcelOle
9
+ include General
10
+
11
+ describe Book do
12
+
13
+ before(:all) do
14
+ excel = Excel.new(:reuse => true)
15
+ open_books = excel == nil ? 0 : excel.Workbooks.Count
16
+ puts "*** open books *** : #{open_books}" if open_books > 0
17
+ Excel.kill_all
18
+ end
19
+
20
+ before do
21
+ @dir = create_tmpdir
22
+ @simple_file = @dir + '/workbook.xls'
23
+ @simple_save_file = @dir + '/workbook_save.xls'
24
+ @different_file = @dir + '/different_workbook.xls'
25
+ @simple_file_other_path = @dir + '/more_data/workbook.xls'
26
+ @another_simple_file = @dir + '/another_workbook.xls'
27
+ @linked_file = @dir + '/workbook_linked.xlsm'
28
+ @simple_file_xlsm = @dir + '/workbook.xls'
29
+ @simple_file_xlsx = @dir + '/workbook.xlsx'
30
+ @simple_file1 = @simple_file
31
+ end
32
+
33
+ after do
34
+ Excel.kill_all
35
+ #rm_tmp(@dir)
36
+ end
37
+
38
+
39
+ describe "unobtrusively" do
40
+
41
+ def unobtrusively_ok? # :nodoc: #
42
+ Book.unobtrusively(@simple_file) do |book|
43
+ book.should be_a Book
44
+ sheet = book.sheet(1)
45
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
46
+ book.should be_alive
47
+ book.Saved.should be_false
48
+ end
49
+ end
50
+
51
+ it "should do simple unobtrusively" do
52
+ expect{unobtrusively_ok?}.to_not raise_error
53
+ end
54
+
55
+ describe "writability" do
56
+
57
+ context "with no book" do
58
+
59
+ it "should open read-write" do
60
+ Book.unobtrusively(@simple_file1) do |book|
61
+ book.ReadOnly.should be_false
62
+ @old_value = book.sheet(1)[1,1].Value
63
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
64
+ book.Saved.should be_false
65
+ end
66
+ Excel.current.Workbooks.Count.should == 0
67
+ b1 = Book.open(@simple_file1)
68
+ b1.sheet(1)[1,1].Value.should_not == @old_value
69
+ end
70
+
71
+ it "should open as read-write" do
72
+ Book.unobtrusively(@simple_file, :read_only => false) do |book|
73
+ book.ReadOnly.should be_false
74
+ @old_value = book.sheet(1)[1,1].Value
75
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
76
+ book.Saved.should be_false
77
+ end
78
+ Excel.current.Workbooks.Count.should == 0
79
+ b1 = Book.open(@simple_file1)
80
+ b1.sheet(1)[1,1].Value.should_not == @old_value
81
+ end
82
+
83
+ it "should open as read-write" do
84
+ Book.unobtrusively(@simple_file, :read_only => false, :writable => false) do |book|
85
+ book.ReadOnly.should be_false
86
+ @old_value = book.sheet(1)[1,1].Value
87
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
88
+ book.Saved.should be_false
89
+ end
90
+ Excel.current.Workbooks.Count.should == 0
91
+ b1 = Book.open(@simple_file1)
92
+ b1.sheet(1)[1,1].Value.should_not == @old_value
93
+ end
94
+
95
+ it "should open as read-write" do
96
+ Book.unobtrusively(@simple_file, :writable => true) do |book|
97
+ book.ReadOnly.should be_false
98
+ @old_value = book.sheet(1)[1,1].Value
99
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
100
+ book.Saved.should be_false
101
+ end
102
+ Excel.current.Workbooks.Count.should == 0
103
+ b1 = Book.open(@simple_file1)
104
+ b1.sheet(1)[1,1].Value.should_not == @old_value
105
+ end
106
+
107
+ it "should open as read-write" do
108
+ Book.unobtrusively(@simple_file, :writable => true, :read_only => false) do |book|
109
+ book.ReadOnly.should be_false
110
+ @old_value = book.sheet(1)[1,1].Value
111
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
112
+ book.Saved.should be_false
113
+ end
114
+ Excel.current.Workbooks.Count.should == 0
115
+ b1 = Book.open(@simple_file1)
116
+ b1.sheet(1)[1,1].Value.should_not == @old_value
117
+ end
118
+
119
+ it "should open read-only" do
120
+ Book.unobtrusively(@simple_file, :read_only => true) do |book|
121
+ book.ReadOnly.should be_true
122
+ @old_value = book.sheet(1)[1,1].Value
123
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
124
+ book.Saved.should be_false
125
+ end
126
+ Excel.current.Workbooks.Count.should == 0
127
+ b1 = Book.open(@simple_file1)
128
+ b1.sheet(1)[1,1].Value.should == @old_value
129
+ end
130
+
131
+ it "should open read-only" do
132
+ Book.unobtrusively(@simple_file, :read_only => true, :writable => false) do |book|
133
+ book.ReadOnly.should be_true
134
+ @old_value = book.sheet(1)[1,1].Value
135
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
136
+ book.Saved.should be_false
137
+ end
138
+ Excel.current.Workbooks.Count.should == 0
139
+ b1 = Book.open(@simple_file1)
140
+ b1.sheet(1)[1,1].Value.should == @old_value
141
+ end
142
+
143
+ it "should open not writable" do
144
+ Book.unobtrusively(@simple_file, :writable => false) do |book|
145
+ book.ReadOnly.should be_true
146
+ @old_value = book.sheet(1)[1,1].Value
147
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
148
+ book.Saved.should be_false
149
+ end
150
+ Excel.current.Workbooks.Count.should == 0
151
+ b1 = Book.open(@simple_file1)
152
+ b1.sheet(1)[1,1].Value.should == @old_value
153
+ end
154
+
155
+ it "should raise error if both options are true" do
156
+ expect{
157
+ Book.unobtrusively(@simple_file, :writable => true, :read_only => true) {|book|}
158
+ }.to raise_error(OptionInvalid, "contradicting options")
159
+ end
160
+ end
161
+
162
+ context "with open writable book" do
163
+
164
+ before do
165
+ @book = Book.open(@simple_file1)
166
+ @old_value = @book.sheet(1)[1,1].Value
167
+ end
168
+
169
+ it "should open as read-write by default" do
170
+ Book.unobtrusively(@simple_file1) do |book|
171
+ book.Readonly.should be_false
172
+ book.should == @book
173
+ book.filename.should == @book.filename
174
+ book.excel.should == @book.excel
175
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
176
+ end
177
+ @book.close
178
+ book2 = Book.open(@simple_file1)
179
+ book2.sheet(1)[1,1].Value.should_not == @old_value
180
+ end
181
+
182
+ it "should open as read-write" do
183
+ Book.unobtrusively(@simple_file1, :read_only => false) do |book|
184
+ book.Readonly.should be_false
185
+ book.should == @book
186
+ book.filename.should == @book.filename
187
+ book.excel.should == @book.excel
188
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
189
+ end
190
+ @book.close
191
+ book2 = Book.open(@simple_file1)
192
+ book2.sheet(1)[1,1].Value.should_not == @old_value
193
+ end
194
+
195
+ it "should open as read-write" do
196
+ Book.unobtrusively(@simple_file1, :read_only => false, :writable => false) do |book|
197
+ book.Readonly.should be_false
198
+ book.should == @book
199
+ book.filename.should == @book.filename
200
+ book.excel.should == @book.excel
201
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
202
+ end
203
+ @book.close
204
+ book2 = Book.open(@simple_file1)
205
+ book2.sheet(1)[1,1].Value.should_not == @old_value
206
+ end
207
+
208
+ it "should open as read-write" do
209
+ Book.unobtrusively(@simple_file1, :writable => true) do |book|
210
+ book.Readonly.should be_false
211
+ book.should == @book
212
+ book.filename.should == @book.filename
213
+ book.excel.should == @book.excel
214
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
215
+ end
216
+ @book.close
217
+ book2 = Book.open(@simple_file1)
218
+ book2.sheet(1)[1,1].Value.should_not == @old_value
219
+ end
220
+
221
+ it "should open as read-write" do
222
+ Book.unobtrusively(@simple_file1, :writable => true, :read_only => false) do |book|
223
+ book.Readonly.should be_false
224
+ book.should == @book
225
+ book.filename.should == @book.filename
226
+ book.excel.should == @book.excel
227
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
228
+ end
229
+ @book.close
230
+ book2 = Book.open(@simple_file1)
231
+ book2.sheet(1)[1,1].Value.should_not == @old_value
232
+ end
233
+
234
+ it "should force to read-only" do
235
+ Book.unobtrusively(@simple_file1, :read_only => true) do |book|
236
+ book.ReadOnly.should be_true
237
+ book.should == @book
238
+ book.filename.should == @book.filename
239
+ book.excel.should == @book.excel
240
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
241
+ end
242
+ @book.close
243
+ book2 = Book.open(@simple_file1)
244
+ book2.sheet(1)[1,1].Value.should == @old_value
245
+ end
246
+
247
+ it "should force to read-only" do
248
+ Book.unobtrusively(@simple_file1, :read_only => true, :writable => false) do |book|
249
+ book.ReadOnly.should be_true
250
+ book.should == @book
251
+ book.filename.should == @book.filename
252
+ book.excel.should == @book.excel
253
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
254
+ end
255
+ @book.close
256
+ book2 = Book.open(@simple_file1)
257
+ book2.sheet(1)[1,1].Value.should == @old_value
258
+ end
259
+
260
+ it "should open not writable" do
261
+ Book.unobtrusively(@simple_file1, :writable => false) do |book|
262
+ book.ReadOnly.should be_false
263
+ book.should == @book
264
+ book.filename.should == @book.filename
265
+ book.excel.should == @book.excel
266
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
267
+ end
268
+ @book.close
269
+ book2 = Book.open(@simple_file1)
270
+ book2.sheet(1)[1,1].Value.should == @old_value
271
+ end
272
+ end
273
+
274
+ context "with open read-only book" do
275
+
276
+ before do
277
+ @book = Book.open(@simple_file1, :read_only => true)
278
+ @old_value = @book.sheet(1)[1,1].Value
279
+ end
280
+
281
+ it "should not change the read_only mode" do
282
+ Book.unobtrusively(@simple_file1) do |book|
283
+ book.Readonly.should be_true
284
+ book.should == @book
285
+ book.filename.should == @book.filename
286
+ book.excel.should == @book.excel
287
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
288
+ end
289
+ @book.close
290
+ book2 = Book.open(@simple_file1)
291
+ book2.sheet(1)[1,1].Value.should == @old_value
292
+ end
293
+
294
+ it "should open as read-write" do
295
+ Book.unobtrusively(@simple_file1, :read_only => false) do |book|
296
+ book.Readonly.should be_true
297
+ book.should == @book
298
+ book.filename.should == @book.filename
299
+ book.excel.should == @book.excel
300
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
301
+ end
302
+ @book.close
303
+ book2 = Book.open(@simple_file1)
304
+ book2.sheet(1)[1,1].Value.should == @old_value
305
+ end
306
+
307
+ it "should open as read-write" do
308
+ Book.unobtrusively(@simple_file1, :read_only => false, :writable => false) do |book|
309
+ book.Readonly.should be_true
310
+ book.should == @book
311
+ book.filename.should == @book.filename
312
+ book.excel.should == @book.excel
313
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
314
+ end
315
+ @book.close
316
+ book2 = Book.open(@simple_file1)
317
+ book2.sheet(1)[1,1].Value.should == @old_value
318
+ end
319
+
320
+ it "should force to read-write" do
321
+ Book.unobtrusively(@simple_file1, :writable => true) do |book|
322
+ book.Readonly.should be_false
323
+ book.should == @book
324
+ book.filename.should == @book.filename
325
+ book.excel.should == @book.excel
326
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
327
+ end
328
+ @book.close
329
+ book2 = Book.open(@simple_file1)
330
+ book2.sheet(1)[1,1].Value.should_not == @old_value
331
+ end
332
+
333
+ it "should force to read-write" do
334
+ Book.unobtrusively(@simple_file1, :writable => true, :read_only => false) do |book|
335
+ book.Readonly.should be_false
336
+ book.should == @book
337
+ book.filename.should == @book.filename
338
+ book.excel.should == @book.excel
339
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
340
+ end
341
+ @book.close
342
+ book2 = Book.open(@simple_file1)
343
+ book2.sheet(1)[1,1].Value.should_not == @old_value
344
+ end
345
+
346
+ it "should force to read-write" do
347
+ e1 = Excel.create
348
+ Book.unobtrusively(@simple_file1, :writable => true, :rw_change_excel => e1) do |book|
349
+ book.Readonly.should be_false
350
+ book.filename.should == @book.filename
351
+ book.excel.should == e1
352
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
353
+ end
354
+ @book.close
355
+ book2 = Book.open(@simple_file1)
356
+ book2.sheet(1)[1,1].Value.should_not == @old_value
357
+ end
358
+
359
+ it "should force to read-write" do
360
+ Book.unobtrusively(@simple_file1, :writable => true, :rw_change_excel => :current) do |book|
361
+ book.Readonly.should be_false
362
+ book.should == @book
363
+ book.filename.should == @book.filename
364
+ book.excel.should == @book.excel
365
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
366
+ end
367
+ @book.close
368
+ book2 = Book.open(@simple_file1)
369
+ book2.sheet(1)[1,1].Value.should_not == @old_value
370
+ end
371
+
372
+ it "should force to read-write" do
373
+ Book.unobtrusively(@simple_file1, :writable => true, :rw_change_excel => :new) do |book|
374
+ book.Readonly.should be_false
375
+ book.filename.should == @book.filename
376
+ book.excel.should_not == @book.excel
377
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
378
+ end
379
+ @book.close
380
+ book2 = Book.open(@simple_file1)
381
+ book2.sheet(1)[1,1].Value.should_not == @old_value
382
+ end
383
+
384
+ it "should force to read-write" do
385
+ Book.unobtrusively(@simple_file1, :writable => true, :read_only => false) do |book|
386
+ book.Readonly.should be_false
387
+ book.should == @book
388
+ book.filename.should == @book.filename
389
+ book.excel.should == @book.excel
390
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
391
+ end
392
+ @book.close
393
+ book2 = Book.open(@simple_file1)
394
+ book2.sheet(1)[1,1].Value.should_not == @old_value
395
+ end
396
+
397
+ it "should force to read-only" do
398
+ Book.unobtrusively(@simple_file1, :read_only => true) do |book|
399
+ book.ReadOnly.should be_true
400
+ book.should == @book
401
+ book.filename.should == @book.filename
402
+ book.excel.should == @book.excel
403
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
404
+ end
405
+ @book.close
406
+ book2 = Book.open(@simple_file1)
407
+ book2.sheet(1)[1,1].Value.should == @old_value
408
+ end
409
+
410
+ it "should force to read-only" do
411
+ Book.unobtrusively(@simple_file1, :read_only => true, :writable => false) do |book|
412
+ book.ReadOnly.should be_true
413
+ book.should == @book
414
+ book.filename.should == @book.filename
415
+ book.excel.should == @book.excel
416
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
417
+ end
418
+ @book.close
419
+ book2 = Book.open(@simple_file1)
420
+ book2.sheet(1)[1,1].Value.should == @old_value
421
+ end
422
+
423
+ it "should open not writable" do
424
+ Book.unobtrusively(@simple_file1, :writable => false) do |book|
425
+ book.ReadOnly.should be_true
426
+ book.should == @book
427
+ book.filename.should == @book.filename
428
+ book.excel.should == @book.excel
429
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
430
+ end
431
+ @book.close
432
+ book2 = Book.open(@simple_file1)
433
+ book2.sheet(1)[1,1].Value.should == @old_value
434
+ end
435
+ end
436
+
437
+ context "with open unsaved writable book" do
438
+
439
+ before do
440
+ @book = Book.open(@simple_file1)
441
+ @book.sheet(1)[1,1] = @book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
442
+ @old_value = @book.sheet(1)[1,1].Value
443
+ end
444
+
445
+ it "should open as read-write by default" do
446
+ Book.unobtrusively(@simple_file1) do |book|
447
+ book.Readonly.should be_false
448
+ book.should == @book
449
+ book.filename.should == @book.filename
450
+ book.excel.should == @book.excel
451
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
452
+ end
453
+ @book.Saved.should be_false
454
+ @book.sheet(1)[1,1].Value.should_not == @old_value
455
+ end
456
+
457
+ it "should open as read-write" do
458
+ Book.unobtrusively(@simple_file1, :read_only => false) do |book|
459
+ book.Readonly.should be_false
460
+ book.should == @book
461
+ book.filename.should == @book.filename
462
+ book.excel.should == @book.excel
463
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
464
+ end
465
+ @book.Saved.should be_false
466
+ @book.sheet(1)[1,1].Value.should_not == @old_value
467
+ end
468
+
469
+ it "should open as read-write" do
470
+ Book.unobtrusively(@simple_file1, :read_only => false, :writable => false) do |book|
471
+ book.Readonly.should be_false
472
+ book.should == @book
473
+ book.filename.should == @book.filename
474
+ book.excel.should == @book.excel
475
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
476
+ end
477
+ @book.Saved.should be_false
478
+ @book.sheet(1)[1,1].Value.should_not == @old_value
479
+ end
480
+
481
+ it "should force to read-write" do
482
+ Book.unobtrusively(@simple_file1, :writable => true) do |book|
483
+ book.Readonly.should be_false
484
+ book.should == @book
485
+ book.filename.should == @book.filename
486
+ book.excel.should == @book.excel
487
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
488
+ end
489
+ @book.Saved.should be_false
490
+ @book.sheet(1)[1,1].Value.should_not == @old_value
491
+ end
492
+
493
+ it "should open as read-write" do
494
+ Book.unobtrusively(@simple_file1, :writable => true, :read_only => false) do |book|
495
+ book.Readonly.should be_false
496
+ book.should == @book
497
+ book.filename.should == @book.filename
498
+ book.excel.should == @book.excel
499
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
500
+ end
501
+ @book.Saved.should be_false
502
+ @book.sheet(1)[1,1].Value.should_not == @old_value
503
+ end
504
+
505
+ it "should force to read-only (not implemented)" do
506
+ expect{
507
+ Book.unobtrusively(@simple_file1, :read_only => true)
508
+ }.to raise_error(NotImplementedREOError)
509
+ end
510
+
511
+ it "should force to read-only (not implemented)" do
512
+ expect{
513
+ Book.unobtrusively(@simple_file1, :read_only => true, :writable => false)
514
+ }.to raise_error(NotImplementedREOError)
515
+ end
516
+
517
+ #it "should force to read-only (not implemented)" do
518
+ # expect{
519
+ # Book.unobtrusively(@simple_file1, :read_only => true) do |book|
520
+ # book.ReadOnly.should be_true
521
+ # book.should == @book
522
+ # book.filename.should == @book.filename
523
+ # book.excel.should == @book.excel
524
+ # book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
525
+ # end
526
+ # @book.Saved.should be_false
527
+ # @book.sheet(1)[1,1].Value.should == @old_value
528
+ # end
529
+ # }.to raise_error(NotImplementedREOError)
530
+ #end
531
+
532
+ #it "should force to read-only" do
533
+ # expect{
534
+ # Book.unobtrusively(@simple_file1, :read_only => true, :writable => false) do |book|
535
+ # book.ReadOnly.should be_true
536
+ # book.should == @book
537
+ # book.filename.should == @book.filename
538
+ # book.excel.should == @book.excel
539
+ # book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
540
+ # end
541
+ # @book.Saved.should be_false
542
+ # @book.sheet(1)[1,1].Value.should == @old_value
543
+ # end
544
+ # }.to raise_error(NotImplementedREOError)
545
+ #end
546
+
547
+ it "should open not writable" do
548
+ Book.unobtrusively(@simple_file1, :writable => false) do |book|
549
+ book.ReadOnly.should be_false
550
+ book.should == @book
551
+ book.filename.should == @book.filename
552
+ book.excel.should == @book.excel
553
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
554
+ end
555
+ @book.Saved.should be_false
556
+ @book.sheet(1)[1,1].Value.should_not == @old_value
557
+ @book.close(:if_unsaved => :forget)
558
+ book2 = Book.open(@simple_file1)
559
+ book2.sheet(1)[1,1].Value.should_not == @old_value
560
+ end
561
+ end
562
+
563
+ context "with open unsaved read-only book" do
564
+
565
+ before do
566
+ @book = Book.open(@simple_file1, :read_only => true)
567
+ @book.sheet(1)[1,1] = @book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
568
+ @old_value = @book.sheet(1)[1,1].Value
569
+ end
570
+
571
+ it "should open as read-only by default" do
572
+ Book.unobtrusively(@simple_file1) do |book|
573
+ book.Readonly.should be_true
574
+ book.should == @book
575
+ book.filename.should == @book.filename
576
+ book.excel.should == @book.excel
577
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
578
+ end
579
+ @book.Saved.should be_false
580
+ @book.ReadOnly.should be_true
581
+ @book.sheet(1)[1,1].Value.should_not == @old_value
582
+ @book.close
583
+ book2 = Book.open(@simple_file1)
584
+ book2.sheet(1)[1,1].Value.should_not == @old_value
585
+ end
586
+
587
+ it "should open as read-only" do
588
+ Book.unobtrusively(@simple_file1, :read_only => false) do |book|
589
+ book.Readonly.should be_true
590
+ book.should == @book
591
+ book.filename.should == @book.filename
592
+ book.excel.should == @book.excel
593
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
594
+ end
595
+ @book.Saved.should be_false
596
+ @book.ReadOnly.should be_true
597
+ @book.sheet(1)[1,1].Value.should_not == @old_value
598
+ @book.close
599
+ book2 = Book.open(@simple_file1)
600
+ book2.sheet(1)[1,1].Value.should_not == @old_value
601
+ end
602
+
603
+ it "should open as read-only" do
604
+ Book.unobtrusively(@simple_file1, :read_only => false, :writable => false) do |book|
605
+ book.Readonly.should be_true
606
+ book.should == @book
607
+ book.filename.should == @book.filename
608
+ book.excel.should == @book.excel
609
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
610
+ end
611
+ @book.Saved.should be_false
612
+ @book.ReadOnly.should be_true
613
+ @book.sheet(1)[1,1].Value.should_not == @old_value
614
+ @book.close
615
+ book2 = Book.open(@simple_file1)
616
+ book2.sheet(1)[1,1].Value.should_not == @old_value
617
+ end
618
+
619
+ it "should raise an error" do
620
+ expect{
621
+ Book.unobtrusively(@simple_file1, :writable => true)
622
+ }.to raise_error(NotImplementedREOError, "unsaved read-only workbook shall be written")
623
+ end
624
+
625
+ it "should raise an error" do
626
+ expect{
627
+ Book.unobtrusively(@simple_file1, :writable => true)
628
+ }.to raise_error(NotImplementedREOError, "unsaved read-only workbook shall be written")
629
+ end
630
+
631
+ it "should force to read-only" do
632
+ Book.unobtrusively(@simple_file1, :read_only => true) do |book|
633
+ book.ReadOnly.should be_true
634
+ book.should == @book
635
+ book.filename.should == @book.filename
636
+ book.excel.should == @book.excel
637
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
638
+ end
639
+ @book.Saved.should be_false
640
+ @book.ReadOnly.should be_true
641
+ @book.sheet(1)[1,1].Value.should_not == @old_value
642
+ @book.close
643
+ book2 = Book.open(@simple_file1)
644
+ book2.sheet(1)[1,1].Value.should_not == @old_value
645
+ end
646
+
647
+ it "should force to read-only" do
648
+ Book.unobtrusively(@simple_file1, :read_only => true, :writable => false) do |book|
649
+ book.ReadOnly.should be_true
650
+ book.should == @book
651
+ book.filename.should == @book.filename
652
+ book.excel.should == @book.excel
653
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
654
+ end
655
+ @book.Saved.should be_false
656
+ @book.ReadOnly.should be_true
657
+ @book.sheet(1)[1,1].Value.should_not == @old_value
658
+ @book.close
659
+ book2 = Book.open(@simple_file1)
660
+ book2.sheet(1)[1,1].Value.should_not == @old_value
661
+ end
662
+
663
+ it "should open not writable" do
664
+ Book.unobtrusively(@simple_file1, :writable => false) do |book|
665
+ book.ReadOnly.should be_true
666
+ book.should == @book
667
+ book.filename.should == @book.filename
668
+ book.excel.should == @book.excel
669
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
670
+ end
671
+ @book.Saved.should be_false
672
+ @book.ReadOnly.should be_true
673
+ @book.sheet(1)[1,1].Value.should_not == @old_value
674
+ @book.close
675
+ book2 = Book.open(@simple_file1)
676
+ book2.sheet(1)[1,1].Value.should_not == @old_value
677
+ end
678
+ end
679
+
680
+ end
681
+
682
+ describe "intertweened blocks" do
683
+
684
+ context "with no book" do
685
+
686
+ it "should not close the book in the outer block" do
687
+ Book.unobtrusively(@simple_file1) do |book|
688
+ Book.unobtrusively(@simple_file1) do |book2|
689
+ book2.should == book
690
+ end
691
+ book.should be_alive
692
+ Book.unobtrusively(@simple_file1) do |book3|
693
+ book3.should == book
694
+ end
695
+ book.should be_alive
696
+ end
697
+ end
698
+
699
+ it "should write in the outer and inner block" do
700
+ Book.unobtrusively(@simple_file1) do |book|
701
+ @old_value = book.sheet(1)[1,1].Value
702
+ book.ReadOnly.should be_false
703
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
704
+ book.Saved.should be_false
705
+ book.sheet(1)[1,1].Value.should_not == @old_value
706
+ Book.unobtrusively(@simple_file1) do |book2|
707
+ book2.should == book
708
+ book2.ReadOnly.should be_false
709
+ book2.Saved.should be_false
710
+ book2.sheet(1)[1,1].Value.should_not == @old_value
711
+ book2.sheet(1)[1,1] = book2.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
712
+ book2.sheet(1)[1,1].Value.should == @old_value
713
+ end
714
+ book.should be_alive
715
+ book.Saved.should be_false
716
+ book.sheet(1)[1,1].Value.should == @old_value
717
+ end
718
+ book = Book.open(@simple_file1)
719
+ book.sheet(1)[1,1].Value.should == @old_value
720
+ end
721
+
722
+ it "should write in the outer and not in the inner block" do
723
+ expect{
724
+ Book.unobtrusively(@simple_file1) do |book|
725
+ @old_value = book.sheet(1)[1,1].Value
726
+ book.ReadOnly.should be_false
727
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
728
+ book.Saved.should be_false
729
+ book.sheet(1)[1,1].Value.should_not == @old_value
730
+ Book.unobtrusively(@simple_file1, :read_only => true) do |book2|
731
+ book2.should == book
732
+ book2.ReadOnly.should be_true
733
+ #book2.Saved.should be_false
734
+ book2.sheet(1)[1,1].Value.should_not == @old_value
735
+ book2.sheet(1)[1,1] = book2.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
736
+ book2.sheet(1)[1,1].Value.should == @old_value
737
+ end
738
+ book.should be_alive
739
+ book.Saved.should be_false
740
+ book.sheet(1)[1,1].Value.should_not == @old_value
741
+ end
742
+ book = Book.open(@simple_file1)
743
+ book.sheet(1)[1,1].Value.should_not == @old_value
744
+ }.to raise_error(NotImplementedREOError)
745
+ end
746
+
747
+ it "should write in the outer and not in the inner block" do
748
+ Book.unobtrusively(@simple_file1) do |book|
749
+ @old_value = book.sheet(1)[1,1].Value
750
+ book.ReadOnly.should be_false
751
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
752
+ book.Saved.should be_false
753
+ book.sheet(1)[1,1].Value.should_not == @old_value
754
+ Book.unobtrusively(@simple_file1, :writable => false) do |book2|
755
+ book2.should == book
756
+ book2.ReadOnly.should be_false
757
+ book2.Saved.should be_false
758
+ book2.sheet(1)[1,1].Value.should_not == @old_value
759
+ book2.sheet(1)[1,1] = book2.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
760
+ book2.sheet(1)[1,1].Value.should == @old_value
761
+ end
762
+ book.should be_alive
763
+ book.Saved.should be_false
764
+ book.sheet(1)[1,1].Value.should == @old_value
765
+ end
766
+ book = Book.open(@simple_file1)
767
+ book.sheet(1)[1,1].Value.should == @old_value
768
+ end
769
+
770
+ it "should be read-only in the outer and write in the inner block" do
771
+ Book.unobtrusively(@simple_file1, :read_only => true) do |book|
772
+ @old_value = book.sheet(1)[1,1].Value
773
+ book.ReadOnly.should be_true
774
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
775
+ book.Saved.should be_false
776
+ book.sheet(1)[1,1].Value.should_not == @old_value
777
+ Book.unobtrusively(@simple_file1) do |book2|
778
+ book2.should == book
779
+ book2.ReadOnly.should be_true
780
+ book2.Saved.should be_false
781
+ book2.sheet(1)[1,1].Value.should_not == @old_value
782
+ book2.sheet(1)[1,1] = book2.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
783
+ book2.sheet(1)[1,1].Value.should == @old_value
784
+ end
785
+ book.should be_alive
786
+ book.Saved.should be_false
787
+ book.ReadOnly.should be_true
788
+ book.sheet(1)[1,1].Value.should == @old_value
789
+ end
790
+ book = Book.open(@simple_file1)
791
+ book.sheet(1)[1,1].Value.should == @old_value
792
+ end
793
+
794
+ end
795
+
796
+ end
797
+
798
+ describe "unchanging" do
799
+
800
+ context "with openess" do
801
+
802
+ it "should remain closed" do
803
+ Book.unobtrusively(@simple_file) do |book|
804
+ end
805
+ Excel.current.Workbooks.Count.should == 0
806
+ end
807
+
808
+ it "should remain open" do
809
+ book1 = Book.open(@simple_file1)
810
+ Book.unobtrusively(@simple_file1) do |book|
811
+ #book.should be_a Book
812
+ book.should be_alive
813
+ end
814
+ #book1.should be_alive
815
+ end
816
+
817
+ end
818
+
819
+ context "with writability" do
820
+
821
+ it "should remain read_only" do
822
+ book1 = Book.open(@simple_file1, :read_only => true)
823
+ Book.unobtrusively(@simple_file1) do |book|
824
+ end
825
+ book1.ReadOnly.should be_true
826
+ end
827
+
828
+ it "should remain writable" do
829
+ book1 = Book.open(@simple_file1, :read_only => false)
830
+ Book.unobtrusively(@simple_file1) do |book|
831
+ end
832
+ book1.ReadOnly.should be_false
833
+ end
834
+
835
+ it "should write and remain read_only and open the workbook in another Excel" do
836
+ book1 = Book.open(@simple_file1, :read_only => true)
837
+ old_value = book1.sheet(1)[1,1].Value
838
+ Book.unobtrusively(@simple_file1) do |book|
839
+ sheet = book.sheet(1)
840
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
841
+ book.excel.should == book1.excel
842
+ end
843
+ book1.ReadOnly.should be_true
844
+ book1.close
845
+ book2 = Book.open(@simple_file1)
846
+ book2.sheet(1)[1,1].Value.should == old_value
847
+ end
848
+
849
+ it "should write and remain read_only and open the workbook in the same Excel" do
850
+ book1 = Book.open(@simple_file1, :read_only => true)
851
+ old_value = book1.sheet(1)[1,1].Value
852
+ Book.unobtrusively(@simple_file1, :writable => true) do |book|
853
+ book.ReadOnly.should be_false
854
+ sheet = book.sheet(1)
855
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
856
+ book.excel.should == book1.excel
857
+ end
858
+ book1.ReadOnly.should be_true
859
+ book1.close
860
+ book2 = Book.open(@simple_file1)
861
+ book2.sheet(1)[1,1].Value.should_not == old_value
862
+ end
863
+
864
+ end
865
+
866
+ context "with visibility" do
867
+
868
+ it "should remain invisible" do
869
+ book1 = Book.open(@simple_file1, :visible => false)
870
+ Book.unobtrusively(@simple_file1) do |book|
871
+ end
872
+ book1.excel.Visible.should be_false
873
+ end
874
+
875
+ it "should remain visible" do
876
+ book1 = Book.open(@simple_file1, :visible => true)
877
+ Book.unobtrusively(@simple_file1) do |book|
878
+ end
879
+ book1.excel.Visible.should be_true
880
+ book1.Windows(book1.Name).Visible.should be_true
881
+ end
882
+
883
+ end
884
+
885
+ context "with check-compatibility" do
886
+
887
+ it "should remain check-compatibility false" do
888
+ book1 = Book.open(@simple_file1, :check_compatibility => false)
889
+ Book.unobtrusively(@simple_file1) do |book|
890
+ end
891
+ book1.CheckCompatibility.should be_false
892
+ end
893
+
894
+ it "should remain check-compatibility true" do
895
+ book1 = Book.open(@simple_file1, :check_compatibility => true)
896
+ Book.unobtrusively(@simple_file1) do |book|
897
+ end
898
+ book1.CheckCompatibility.should be_true
899
+ end
900
+
901
+ end
902
+
903
+ context "with calculation" do
904
+
905
+ it "should remain the calculation mode" do
906
+ book1 = Book.open(@simple_file1)
907
+ old_calculation = book1.excel.calculation
908
+ Book.unobtrusively(@simple_file1) do |book|
909
+ end
910
+ book1.excel.calculation.should == old_calculation
911
+ end
912
+
913
+ it "should remain calculation manual" do
914
+ book1 = Book.open(@simple_file1, :calculation => :manual)
915
+ Book.unobtrusively(@simple_file1) do |book|
916
+ end
917
+ book1.excel.Calculation.should == -4135
918
+ end
919
+
920
+ it "should remain calculation automatic" do
921
+ book1 = Book.open(@simple_file1, :calculation => :automatic)
922
+ Book.unobtrusively(@simple_file1) do |book|
923
+ end
924
+ book1.excel.Calculation.should == -4105
925
+ end
926
+
927
+ end
928
+ end
929
+
930
+ context "with no open book" do
931
+
932
+ it "should open unobtrusively if no Excel is open" do
933
+ Excel.close_all
934
+ Book.unobtrusively(@simple_file) do |book|
935
+ book.should be_a Book
936
+ book.excel.Visible.should be_false
937
+ book.CheckCompatibility.should be_false
938
+ book.ReadOnly.should be_false
939
+ end
940
+ end
941
+
942
+ it "should open unobtrusively in a new Excel" do
943
+ expect{ unobtrusively_ok? }.to_not raise_error
944
+ end
945
+ end
946
+
947
+ context "with two running excel instances" do
948
+
949
+ before :all do
950
+ Excel.close_all
951
+ end
952
+
953
+ before do
954
+ @excel1 = Excel.new(:reuse => false)
955
+ @excel2 = Excel.new(:reuse => false)
956
+ end
957
+
958
+ after do
959
+ Excel.kill_all
960
+ end
961
+
962
+ it "should open unobtrusively in the first opened Excel" do
963
+ Book.unobtrusively(@simple_file) do |book|
964
+ book.excel.should == @excel1
965
+ book.excel.should_not == @excel2
966
+ end
967
+ end
968
+
969
+ it "should open unobtrusively in a new Excel" do
970
+ Book.unobtrusively(@simple_file, :if_closed => :new) do |book|
971
+ book.should be_a Book
972
+ book.should be_alive
973
+ book.excel.should_not == @excel1
974
+ book.excel.should_not == @excel2
975
+ end
976
+ end
977
+
978
+ it "should open unobtrusively in a given Excel" do
979
+ Book.unobtrusively(@simple_file, :if_closed => @excel2) do |book|
980
+ book.should be_a Book
981
+ book.should be_alive
982
+ book.excel.should_not == @excel1
983
+ book.excel.should == @excel2
984
+ end
985
+ end
986
+
987
+ it "should open in another Excel instance if the given Excel instance is not alive" do
988
+ Excel.kill_all
989
+ sleep 2
990
+ expect{
991
+ Book.unobtrusively(@simple_file, :if_closed => @excel2) do |book|
992
+ book.should be_alive
993
+ end
994
+ }.to_not raise_error
995
+ end
996
+
997
+ it "should raise an error if the option is invalid" do
998
+ expect{
999
+ Book.unobtrusively(@simple_file, :if_closed => :invalid_option) do |book|
1000
+ end
1001
+ }.to raise_error(TypeREOError, "given object is neither an Excel, a Workbook, nor a Win32ole")
1002
+ end
1003
+
1004
+ end
1005
+
1006
+ context "with an open book" do
1007
+
1008
+ before do
1009
+ @book = Book.open(@simple_file1)
1010
+ end
1011
+
1012
+ after do
1013
+ @book.close(:if_unsaved => :forget)
1014
+ @book2.close(:if_unsaved => :forget) rescue nil
1015
+ end
1016
+
1017
+ it "should let an open Book open if it has been closed and opened again" do
1018
+ @book.close
1019
+ @book.reopen
1020
+ Book.unobtrusively(@simple_file1) do |book|
1021
+ book.should be_a Book
1022
+ book.should be_alive
1023
+ book.excel.should == @book.excel
1024
+ end
1025
+ @book.should be_alive
1026
+ @book.should be_a Book
1027
+ end
1028
+
1029
+ it "should let an open Book open if two books have been opened and one has been closed and opened again" do
1030
+ book2 = Book.open(@different_file, :force_excel => :new)
1031
+ @book.close
1032
+ book2.close
1033
+ @book.reopen
1034
+ Book.unobtrusively(@simple_file1) do |book|
1035
+ book.should be_a Book
1036
+ book.should be_alive
1037
+ book.excel.should == @book.excel
1038
+ end
1039
+ @book.should be_alive
1040
+ @book.should be_a Book
1041
+ end
1042
+
1043
+ it "should open in the Excel of the given Book" do
1044
+ #book1 = Book.open(@different_file)
1045
+ @book2 = Book.open(@another_simple_file, :force_excel => :new)
1046
+ Book.unobtrusively(@different_file, :if_closed => @book2) do |book|
1047
+ book.should be_a Book
1048
+ book.should be_alive
1049
+ book.excel.should_not == @book.excel
1050
+ book.excel.should == @book2.excel
1051
+ end
1052
+ end
1053
+
1054
+ it "should let a saved book saved" do
1055
+ @book.Saved.should be_true
1056
+ @book.should be_alive
1057
+ sheet = @book.sheet(1)
1058
+ old_cell_value = sheet[1,1].value
1059
+ unobtrusively_ok?
1060
+ @book.Saved.should be_true
1061
+ @book.should be_alive
1062
+ sheet = @book.sheet(1)
1063
+ sheet[1,1].value.should_not == old_cell_value
1064
+ end
1065
+
1066
+ it "should let the unsaved book unsaved" do
1067
+ sheet = @book.sheet(1)
1068
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1069
+ old_cell_value = sheet[1,1].value
1070
+ @book.Saved.should be_false
1071
+ unobtrusively_ok?
1072
+ @book.should be_alive
1073
+ @book.Saved.should be_false
1074
+ @book.close(:if_unsaved => :forget)
1075
+ @book2 = Book.open(@simple_file1)
1076
+ sheet2 = @book2.sheet(1)
1077
+ sheet2[1,1].value.should_not == old_cell_value
1078
+ end
1079
+
1080
+ it "should modify unobtrusively the second, writable book" do
1081
+ @book2 = Book.open(@simple_file1, :force_excel => :new)
1082
+ @book.ReadOnly.should be_false
1083
+ @book2.ReadOnly.should be_true
1084
+ sheet = @book2.sheet(1)
1085
+ old_cell_value = sheet[1,1].value
1086
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1087
+ unobtrusively_ok?
1088
+ @book2.should be_alive
1089
+ @book2.Saved.should be_false
1090
+ @book2.close(:if_unsaved => :forget)
1091
+ @book.close
1092
+ new_book = Book.open(@simple_file1)
1093
+ sheet2 = new_book.sheet(1)
1094
+ sheet2[1,1].value.should_not == old_cell_value
1095
+ end
1096
+ end
1097
+
1098
+
1099
+ context "with a closed book" do
1100
+
1101
+ before do
1102
+ @book = Book.open(@simple_file)
1103
+ end
1104
+
1105
+ after do
1106
+ @book.close(:if_unsaved => :forget)
1107
+ end
1108
+
1109
+ it "should let the closed book closed by default" do
1110
+ sheet = @book.sheet(1)
1111
+ old_cell_value = sheet[1,1].value
1112
+ @book.close
1113
+ @book.should_not be_alive
1114
+ unobtrusively_ok?
1115
+ @book.should_not be_alive
1116
+ new_book = Book.open(@simple_file1)
1117
+ sheet = new_book.sheet(1)
1118
+ sheet[1,1].Value.should_not == old_cell_value
1119
+ end
1120
+
1121
+ # The bold reanimation of the @book
1122
+ it "should use the excel of the book and keep open the book" do
1123
+ excel = Excel.new(:reuse => false)
1124
+ sheet = @book.sheet(1)
1125
+ old_cell_value = sheet[1,1].value
1126
+ @book.close
1127
+ @book.should_not be_alive
1128
+ Book.unobtrusively(@simple_file, :keep_open => true) do |book|
1129
+ book.should be_a Book
1130
+ book.excel.should == @book.excel
1131
+ book.excel.should_not == excel
1132
+ sheet = book.sheet(1)
1133
+ cell = sheet[1,1]
1134
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
1135
+ book.Saved.should be_false
1136
+ end
1137
+ @book.should be_alive
1138
+ @book.close
1139
+ new_book = Book.open(@simple_file1)
1140
+ sheet = new_book.sheet(1)
1141
+ sheet[1,1].value.should_not == old_cell_value
1142
+ end
1143
+
1144
+ # book shall be reanimated
1145
+ it "should use the excel of the book and keep open the book" do
1146
+ excel = Excel.new(:reuse => false)
1147
+ sheet = @book.sheet(1)
1148
+ old_cell_value = sheet[1,1].value
1149
+ @book.close
1150
+ @book.should_not be_alive
1151
+ Book.unobtrusively(@simple_file, :if_closed => :new) do |book|
1152
+ book.should be_a Book
1153
+ book.should be_alive
1154
+ book.excel.should_not == @book.excel
1155
+ book.excel.should_not == excel
1156
+ sheet = book.sheet(1)
1157
+ cell = sheet[1,1]
1158
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
1159
+ book.Saved.should be_false
1160
+ end
1161
+ @book.should_not be_alive
1162
+ new_book = Book.open(@simple_file1)
1163
+ sheet = new_book.sheet(1)
1164
+ sheet[1,1].value.should_not == old_cell_value
1165
+ end
1166
+
1167
+ it "should use another excel if the Excels are closed" do
1168
+ sheet = @book.sheet(1)
1169
+ old_cell_value = sheet[1,1].value
1170
+ @book.close
1171
+ @book.should_not be_alive
1172
+ Book.unobtrusively(@simple_file, :keep_open => true) do |book|
1173
+ book.should be_a Book
1174
+ sheet = book.sheet(1)
1175
+ cell = sheet[1,1]
1176
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
1177
+ book.Saved.should be_false
1178
+ end
1179
+ @book.should be_alive
1180
+ @book.close
1181
+ new_book = Book.open(@simple_file1)
1182
+ sheet = new_book.sheet(1)
1183
+ sheet[1,1].value.should_not == old_cell_value
1184
+ end
1185
+
1186
+ it "should use another excel if the Excels are closed" do
1187
+ excel = Excel.new(:reuse => false)
1188
+ sheet = @book.sheet(1)
1189
+ old_cell_value = sheet[1,1].value
1190
+ @book.close
1191
+ @book.should_not be_alive
1192
+ Excel.kill_all
1193
+ Book.unobtrusively(@simple_file, :if_closed => :new, :keep_open => true) do |book|
1194
+ book.should be_a Book
1195
+ book.excel.should_not == @book.excel
1196
+ book.excel.should_not == excel
1197
+ sheet = book.sheet(1)
1198
+ cell = sheet[1,1]
1199
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
1200
+ book.Saved.should be_false
1201
+ end
1202
+ @book.should_not be_alive
1203
+ new_book = Book.open(@simple_file1)
1204
+ sheet = new_book.sheet(1)
1205
+ sheet[1,1].value.should_not == old_cell_value
1206
+ end
1207
+
1208
+ it "should modify unobtrusively the copied file" do
1209
+ sheet = @book.sheet(1)
1210
+ old_cell_value = sheet[1,1].value
1211
+ File.delete simple_save_file rescue nil
1212
+ @book.save_as(@simple_save_file)
1213
+ @book.close
1214
+ Book.unobtrusively(@simple_save_file) do |book|
1215
+ sheet = book.sheet(1)
1216
+ cell = sheet[1,1]
1217
+ sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
1218
+ end
1219
+ old_book = Book.open(@simple_file1)
1220
+ old_sheet = old_book.sheet(1)
1221
+ old_sheet[1,1].Value.should == old_cell_value
1222
+ old_book.close
1223
+ new_book = Book.open(@simple_save_file)
1224
+ new_sheet = new_book.sheet(1)
1225
+ new_sheet[1,1].Value.should_not == old_cell_value
1226
+ new_book.close
1227
+ end
1228
+ end
1229
+
1230
+ context "with a visible book" do
1231
+
1232
+ before do
1233
+ @book = Book.open(@simple_file1, :visible => true)
1234
+ end
1235
+
1236
+ after do
1237
+ @book.close(:if_unsaved => :forget)
1238
+ @book2.close(:if_unsaved => :forget) rescue nil
1239
+ end
1240
+
1241
+ it "should let an open Book open" do
1242
+ Book.unobtrusively(@simple_file1) do |book|
1243
+ book.should be_a Book
1244
+ book.should be_alive
1245
+ book.excel.should == @book.excel
1246
+ book.excel.Visible.should be_true
1247
+ end
1248
+ @book.should be_alive
1249
+ @book.should be_a Book
1250
+ @book.excel.Visible.should be_true
1251
+ end
1252
+
1253
+ end
1254
+
1255
+ context "with various options for an Excel instance in which to open a closed book" do
1256
+
1257
+ before do
1258
+ @book = Book.open(@simple_file1)
1259
+ @book.close
1260
+ end
1261
+
1262
+ it "should use a given Excel" do
1263
+ new_excel = Excel.new(:reuse => false)
1264
+ another_excel = Excel.new(:reuse => false)
1265
+ Book.unobtrusively(@simple_file1, :if_closed => another_excel) do |book|
1266
+ book.excel.should_not == @book.excel
1267
+ book.excel.should_not == new_excel
1268
+ book.excel.should == another_excel
1269
+ end
1270
+ end
1271
+
1272
+ it "should use another Excel" do
1273
+ new_excel = Excel.new(:reuse => false)
1274
+ Book.unobtrusively(@simple_file1, :if_closed => :new) do |book|
1275
+ book.excel.should_not == @book.excel
1276
+ book.excel.should_not == new_excel
1277
+ book.excel.visible.should be_false
1278
+ book.excel.displayalerts.should == :if_visible
1279
+ @another_excel = book.excel
1280
+ end
1281
+ Book.unobtrusively(@simple_file1, :if_closed => :reuse) do |book|
1282
+ book.excel.should == @book.excel
1283
+ book.excel.should_not == new_excel
1284
+ book.excel.visible.should be_false
1285
+ book.excel.displayalerts.should == :if_visible
1286
+ book.excel.should_not == @another_excel
1287
+ end
1288
+ end
1289
+
1290
+ it "should reuse Excel" do
1291
+ new_excel = Excel.new(:reuse => false)
1292
+ Book.unobtrusively(@simple_file1, :if_closed => :reuse) do |book|
1293
+ book.excel.should == @book.excel
1294
+ book.excel.should_not == new_excel
1295
+ end
1296
+ end
1297
+
1298
+ it "should reuse Excel by default" do
1299
+ new_excel = Excel.new(:reuse => false)
1300
+ Book.unobtrusively(@simple_file1) do |book|
1301
+ book.excel.should == @book.excel
1302
+ book.excel.should_not == new_excel
1303
+ end
1304
+ end
1305
+
1306
+ end
1307
+
1308
+ context "with a read_only book" do
1309
+
1310
+ before do
1311
+ @book = Book.open(@simple_file1, :read_only => true)
1312
+ end
1313
+
1314
+ after do
1315
+ @book.close
1316
+ end
1317
+
1318
+ it "should let the saved book saved" do
1319
+ @book.ReadOnly.should be_true
1320
+ @book.Saved.should be_true
1321
+ sheet = @book.sheet(1)
1322
+ old_cell_value = sheet[1,1].value
1323
+ unobtrusively_ok?
1324
+ @book.should be_alive
1325
+ @book.Saved.should be_true
1326
+ @book.ReadOnly.should be_true
1327
+ @book.close
1328
+ book2 = Book.open(@simple_file1)
1329
+ sheet2 = book2.sheet(1)
1330
+ sheet2[1,1].value.should == old_cell_value
1331
+ end
1332
+
1333
+ it "should let the unsaved book unsaved" do
1334
+ @book.ReadOnly.should be_true
1335
+ sheet = @book.sheet(1)
1336
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1337
+ @book.Saved.should be_false
1338
+ @book.should be_alive
1339
+ Book.unobtrusively(@simple_file1) do |book|
1340
+ book.should be_a Book
1341
+ sheet = book.sheet(1)
1342
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1343
+ @cell_value = sheet[1,1].Value
1344
+ book.should be_alive
1345
+ book.Saved.should be_false
1346
+ end
1347
+ @book.should be_alive
1348
+ @book.Saved.should be_false
1349
+ @book.ReadOnly.should be_true
1350
+ @book.close
1351
+ book2 = Book.open(@simple_file1)
1352
+ sheet2 = book2.sheet(1)
1353
+ # modifies unobtrusively the saved version, not the unsaved version
1354
+ sheet2[1,1].value.should == @cell_value
1355
+ end
1356
+
1357
+ it "should open unobtrusively by default the writable book" do
1358
+ book2 = Book.open(@simple_file1, :force_excel => :new, :read_only => false)
1359
+ @book.ReadOnly.should be_true
1360
+ book2.Readonly.should be_false
1361
+ sheet = @book.sheet(1)
1362
+ cell_value = sheet[1,1].value
1363
+ Book.unobtrusively(@simple_file1, :if_closed => :new) do |book|
1364
+ book.should be_a Book
1365
+ book.excel.should == book2.excel
1366
+ book.excel.should_not == @book.excel
1367
+ sheet = book.sheet(1)
1368
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1369
+ book.should be_alive
1370
+ book.Saved.should be_false
1371
+ end
1372
+ @book.Saved.should be_true
1373
+ @book.ReadOnly.should be_true
1374
+ @book.close
1375
+ book2.close
1376
+ book3 = Book.open(@simple_file1)
1377
+ new_sheet = book3.sheet(1)
1378
+ new_sheet[1,1].value.should_not == cell_value
1379
+ book3.close
1380
+ end
1381
+
1382
+ it "should open unobtrusively the book in a new Excel to open the book writable" do
1383
+ excel1 = Excel.new(:reuse => false)
1384
+ excel2 = Excel.new(:reuse => false)
1385
+ book2 = Book.open(@simple_file1, :force_excel => :new, :read_only => true)
1386
+ @book.ReadOnly.should be_true
1387
+ book2.Readonly.should be_true
1388
+ sheet = @book.sheet(1)
1389
+ cell_value = sheet[1,1].value
1390
+ Book.unobtrusively(@simple_file1, :writable => true, :if_closed => :new, :rw_change_excel => :new) do |book|
1391
+ book.should be_a Book
1392
+ book.ReadOnly.should be_false
1393
+ book.excel.should_not == book2.excel
1394
+ book.excel.should_not == @book.excel
1395
+ book.excel.should_not == excel1
1396
+ book.excel.should_not == excel2
1397
+ sheet = book.sheet(1)
1398
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1399
+ book.should be_alive
1400
+ book.Saved.should be_false
1401
+ end
1402
+ @book.Saved.should be_true
1403
+ @book.ReadOnly.should be_true
1404
+ @book.close
1405
+ book2.close
1406
+ book3 = Book.open(@simple_file1)
1407
+ new_sheet = book3.sheet(1)
1408
+ new_sheet[1,1].value.should_not == cell_value
1409
+ book3.close
1410
+ end
1411
+
1412
+ it "should open unobtrusively the book in the same Excel to open the book writable" do
1413
+ excel1 = Excel.new(:reuse => false)
1414
+ excel2 = Excel.new(:reuse => false)
1415
+ book2 = Book.open(@simple_file1, :force_excel => :new, :read_only => true)
1416
+ @book.ReadOnly.should be_true
1417
+ book2.Readonly.should be_true
1418
+ sheet = @book.sheet(1)
1419
+ cell_value = sheet[1,1].value
1420
+ Book.unobtrusively(@simple_file1, :writable => true, :if_closed => :new, :rw_change_excel => :current) do |book|
1421
+ book.should be_a Book
1422
+ book.excel.should == book2.excel
1423
+ book.ReadOnly.should be_false
1424
+ sheet = book.sheet(1)
1425
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1426
+ book.should be_alive
1427
+ book.Saved.should be_false
1428
+ end
1429
+ book2.Saved.should be_true
1430
+ book2.ReadOnly.should be_true
1431
+ @book.close
1432
+ book2.close
1433
+ book3 = Book.open(@simple_file1)
1434
+ new_sheet = book3.sheet(1)
1435
+ new_sheet[1,1].value.should_not == cell_value
1436
+ book3.close
1437
+ end
1438
+
1439
+ it "should open unobtrusively the book in the Excel where it was opened most recently" do
1440
+ book2 = Book.open(@simple_file1, :force_excel => :new, :read_only => true)
1441
+ @book.ReadOnly.should be_true
1442
+ book2.Readonly.should be_true
1443
+ sheet = @book.sheet(1)
1444
+ cell_value = sheet[1,1].value
1445
+ Book.unobtrusively(@simple_file1, :if_closed => :new, :read_only => true) do |book|
1446
+ book.should be_a Book
1447
+ book.excel.should == book2.excel
1448
+ book.excel.should_not == @book.excel
1449
+ book.should be_alive
1450
+ book.Saved.should be_true
1451
+ end
1452
+ @book.Saved.should be_true
1453
+ @book.ReadOnly.should be_true
1454
+ @book.close
1455
+ book2.close
1456
+ end
1457
+
1458
+ end
1459
+
1460
+ context "with a virgin Book class" do
1461
+ before do
1462
+ class Book
1463
+ @@bookstore = nil
1464
+ end
1465
+ end
1466
+ it "should work" do
1467
+ expect{ unobtrusively_ok? }.to_not raise_error
1468
+ end
1469
+ end
1470
+
1471
+ context "with a book never opened before" do
1472
+ before do
1473
+ class Book
1474
+ @@bookstore = nil
1475
+ end
1476
+ other_book = Book.open(@different_file)
1477
+ end
1478
+ it "should open the book" do
1479
+ expect{ unobtrusively_ok? }.to_not raise_error
1480
+ end
1481
+ end
1482
+
1483
+ context "with a saved book" do
1484
+
1485
+ before do
1486
+ @book1 = Book.open(@simple_file1)
1487
+ end
1488
+
1489
+ after do
1490
+ @book1.close(:if_unsaved => :forget)
1491
+ end
1492
+
1493
+ it "should save if the book was modified during unobtrusively" do
1494
+ m_time = File.mtime(@book1.stored_filename)
1495
+ Book.unobtrusively(@simple_file1, :if_closed => :new) do |book|
1496
+ @book1.Saved.should be_true
1497
+ book.Saved.should be_true
1498
+ sheet = book.sheet(1)
1499
+ cell = sheet[1,1]
1500
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
1501
+ @book1.Saved.should be_false
1502
+ book.Saved.should be_false
1503
+ sleep 1
1504
+ end
1505
+ @book1.Saved.should be_true
1506
+ m_time2 = File.mtime(@book1.stored_filename)
1507
+ m_time2.should_not == m_time
1508
+ end
1509
+
1510
+ it "should not save the book if it was not modified during unobtrusively" do
1511
+ m_time = File.mtime(@book1.stored_filename)
1512
+ Book.unobtrusively(@simple_file1) do |book|
1513
+ @book1.Saved.should be_true
1514
+ book.Saved.should be_true
1515
+ sleep 1
1516
+ end
1517
+ m_time2 = File.mtime(@book1.stored_filename)
1518
+ m_time2.should == m_time
1519
+ end
1520
+ end
1521
+
1522
+ context "with block result" do
1523
+ before do
1524
+ @book1 = Book.open(@simple_file1)
1525
+ end
1526
+
1527
+ after do
1528
+ @book1.close(:if_unsaved => :forget)
1529
+ end
1530
+
1531
+ it "should yield the block result true" do
1532
+ result =
1533
+ Book.unobtrusively(@simple_file1) do |book|
1534
+ @book1.Saved.should be_true
1535
+ end
1536
+ result.should == true
1537
+ end
1538
+
1539
+ it "should yield the block result nil" do
1540
+ result =
1541
+ Book.unobtrusively(@simple_file1) do |book|
1542
+ end
1543
+ result.should == nil
1544
+ end
1545
+
1546
+ it "should yield the block result with an unmodified book" do
1547
+ sheet1 = @book1.sheet(1)
1548
+ cell1 = sheet1[1,1].value
1549
+ result =
1550
+ Book.unobtrusively(@simple_file1) do |book|
1551
+ sheet = book.sheet(1)
1552
+ cell = sheet[1,1].value
1553
+ end
1554
+ result.should == cell1
1555
+ end
1556
+
1557
+ it "should yield the block result even if the book gets saved" do
1558
+ sheet1 = @book1.sheet(1)
1559
+ @book1.save
1560
+ result =
1561
+ Book.unobtrusively(@simple_file1) do |book|
1562
+ sheet = book.sheet(1)
1563
+ sheet[1,1] = 22
1564
+ @book1.Saved.should be_false
1565
+ 42
1566
+ end
1567
+ result.should == 42
1568
+ @book1.Saved.should be_true
1569
+ end
1570
+ end
1571
+
1572
+ context "with several Excel instances" do
1573
+
1574
+ before do
1575
+ @book1 = Book.open(@simple_file1)
1576
+ @book2 = Book.open(@simple_file1, :force_excel => :new)
1577
+ @book1.Readonly.should == false
1578
+ @book2.Readonly.should == true
1579
+ old_sheet = @book1.sheet(1)
1580
+ @old_cell_value = old_sheet[1,1].value
1581
+ @book1.close
1582
+ @book2.close
1583
+ @book1.should_not be_alive
1584
+ @book2.should_not be_alive
1585
+ end
1586
+
1587
+ it "should open unobtrusively the closed book in the most recent Excel where it was open before" do
1588
+ Book.unobtrusively(@simple_file1) do |book|
1589
+ book.excel.should_not == @book2.excel
1590
+ book.excel.should == @book1.excel
1591
+ book.ReadOnly.should == false
1592
+ sheet = book.sheet(1)
1593
+ cell = sheet[1,1]
1594
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
1595
+ book.Saved.should be_false
1596
+ end
1597
+ new_book = Book.open(@simple_file1)
1598
+ sheet = new_book.sheet(1)
1599
+ sheet[1,1].value.should_not == @old_cell_value
1600
+ end
1601
+
1602
+ it "should open unobtrusively the closed book in the new Excel" do
1603
+ Book.unobtrusively(@simple_file1, :if_closed => :new) do |book|
1604
+ book.excel.should_not == @book2.excel
1605
+ book.excel.should_not == @book1.excel
1606
+ book.ReadOnly.should == false
1607
+ sheet = book.sheet(1)
1608
+ cell = sheet[1,1]
1609
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
1610
+ book.Saved.should be_false
1611
+ end
1612
+ new_book = Book.open(@simple_file1)
1613
+ sheet = new_book.sheet(1)
1614
+ sheet[1,1].Value.should_not == @old_cell_value
1615
+ end
1616
+
1617
+ it "should open unobtrusively the closed book in a new Excel if the Excel is not alive anymore" do
1618
+ Excel.kill_all
1619
+ Book.unobtrusively(@simple_file1, :if_closed => :new) do |book|
1620
+ book.ReadOnly.should == false
1621
+ book.excel.should_not == @book1.excel
1622
+ book.excel.should_not == @book2.excel
1623
+ sheet = book.sheet(1)
1624
+ cell = sheet[1,1]
1625
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
1626
+ book.Saved.should be_false
1627
+ end
1628
+ new_book = Book.open(@simple_file1)
1629
+ sheet = new_book.sheet(1)
1630
+ sheet[1,1].Value.should_not == @old_cell_value
1631
+ end
1632
+ end
1633
+ end
1634
+
1635
+ describe "for_reading, for_modifying" do
1636
+
1637
+ context "open unobtrusively for reading and modifying" do
1638
+
1639
+ before do
1640
+ @book = Book.open(@simple_file1)
1641
+ sheet = @book.sheet(1)
1642
+ @old_cell_value = sheet[1,1].value
1643
+ @book.close
1644
+ end
1645
+
1646
+ it "should not change the value" do
1647
+ Book.for_reading(@simple_file1) do |book|
1648
+ book.should be_a Book
1649
+ book.should be_alive
1650
+ book.Saved.should be_true
1651
+ sheet = book.sheet(1)
1652
+ cell = sheet[1,1]
1653
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
1654
+ book.Saved.should be_false
1655
+ book.excel.should == @book.excel
1656
+ end
1657
+ new_book = Book.open(@simple_file1, :visible => true)
1658
+ sheet = new_book.sheet(1)
1659
+ sheet[1,1].Value.should == @old_cell_value
1660
+ end
1661
+
1662
+ it "should not change the value and use a given Excel" do
1663
+ new_excel = Excel.new(:reuse => false)
1664
+ another_excel = Excel.new(:reuse => false)
1665
+ Book.for_reading(@simple_file1, :if_closed => another_excel) do |book|
1666
+ sheet = book.sheet(1)
1667
+ cell = sheet[1,1]
1668
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
1669
+ book.excel.should == another_excel
1670
+ end
1671
+ new_book = Book.open(@simple_file1, :visible => true)
1672
+ sheet = new_book.sheet(1)
1673
+ sheet[1,1].Value.should == @old_cell_value
1674
+ end
1675
+
1676
+ it "should not change the value and use the new Excel instance" do
1677
+ new_excel = Excel.new(:reuse => false)
1678
+ Book.for_reading(@simple_file1, :if_closed => :new) do |book|
1679
+ sheet = book.sheet(1)
1680
+ cell = sheet[1,1]
1681
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
1682
+ book.excel.should_not == @book.excel
1683
+ book.excel.should_not == new_excel
1684
+ book.excel.visible.should be_false
1685
+ book.excel.displayalerts.should == :if_visible
1686
+ end
1687
+ new_book = Book.open(@simple_file1, :visible => true)
1688
+ sheet = new_book.sheet(1)
1689
+ sheet[1,1].Value.should == @old_cell_value
1690
+ end
1691
+
1692
+ it "should change the value" do
1693
+ Book.for_modifying(@simple_file1) do |book|
1694
+ sheet = book.sheet(1)
1695
+ cell = sheet[1,1]
1696
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
1697
+ book.excel.should == @book.excel
1698
+ end
1699
+ new_book = Book.open(@simple_file, :visible => true)
1700
+ sheet = new_book.sheet(1)
1701
+ sheet[1,1].Value.should_not == @old_cell_value
1702
+ end
1703
+
1704
+ it "should change the value and use a given Excel" do
1705
+ new_excel = Excel.new(:reuse => false)
1706
+ another_excel = Excel.new(:reuse => false)
1707
+ Book.for_modifying(@simple_file1, :if_closed => another_excel) do |book|
1708
+ sheet = book.sheet(1)
1709
+ cell = sheet[1,1]
1710
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
1711
+ book.excel.should == another_excel
1712
+ end
1713
+ new_book = Book.open(@simple_file1, :visible => true)
1714
+ sheet = new_book.sheet(1)
1715
+ sheet[1,1].Value.should_not == @old_cell_value
1716
+ end
1717
+
1718
+ it "should change the value and use the new Excel instance" do
1719
+ new_excel = Excel.new(:reuse => false)
1720
+ Book.for_modifying(@simple_file1, :if_closed => :new) do |book|
1721
+ sheet = book.sheet(1)
1722
+ cell = sheet[1,1]
1723
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
1724
+ book.excel.should_not == @book.excel
1725
+ book.excel.should_not == new_excel
1726
+ book.excel.visible.should be_false
1727
+ book.excel.displayalerts.should == :if_visible
1728
+ end
1729
+ new_book = Book.open(@simple_file1, :visible => true)
1730
+ sheet = new_book.sheet(1)
1731
+ sheet[1,1].Value.should_not == @old_cell_value
1732
+ end
1733
+ end
1734
+ end
1735
+
1736
+ end
1737
+