robust_excel_ole 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require File.join(File.dirname(__FILE__), './spec_helper')
3
+ require File.join(File.dirname(__FILE__), './../spec_helper')
4
4
 
5
5
 
6
6
  $VERBOSE = nil
@@ -70,8 +70,8 @@ describe Book do
70
70
  it {
71
71
  expect {
72
72
  @book.save_as(@simple_file)
73
- }.to raise_error(IOError,
74
- "Not opened for writing(open with :read_only option)")
73
+ }.to raise_error(ExcelErrorSave,
74
+ "Not opened for writing (opened with :read_only option)")
75
75
  }
76
76
  end
77
77
 
@@ -110,6 +110,155 @@ describe Book do
110
110
  end
111
111
  end
112
112
 
113
+ context "with blocked by another file" do
114
+
115
+ before do
116
+ @book = Book.open(@simple_file)
117
+ @book2 = Book.open(@another_simple_file)
118
+ end
119
+
120
+ after do
121
+ @book.close(:if_unsaved => :forget)
122
+ @book2.close
123
+ end
124
+
125
+ it "should raise an error with :obstructed => :raise" do
126
+ File.delete @simple_file_other_path rescue nil
127
+ File.open(@simple_file_other_path,"w") do | file |
128
+ file.puts "garbage"
129
+ end
130
+ File.exist?(@simple_file_other_path).should be_true
131
+ expect{
132
+ @book2.save_as(@simple_file_other_path, :if_exists => :overwrite, :if_obstructed => :raise)
133
+ }.to raise_error(ExcelErrorSave, /blocked by another workbook: "workbook.xls"/)
134
+ end
135
+
136
+ it "should close the blocking workbook without saving, and save the current workbook with :if_obstructed => :forget" do
137
+ File.delete @simple_file_other_path rescue nil
138
+ File.open(@simple_file_other_path,"w") do | file |
139
+ file.puts "garbage"
140
+ end
141
+ @book2.save_as(@simple_file_other_path, :if_exists => :overwrite, :if_obstructed => :forget)
142
+ @book.should_not be_alive
143
+ File.exist?(@simple_file_other_path).should be_true
144
+ new_book = Book.open(@simple_file_other_path)
145
+ new_book.should be_a Book
146
+ new_book.close
147
+ end
148
+
149
+ it "should close the blocking workbook without saving even if it is unsaved with :if_obstructed => :forget" do
150
+ File.delete @simple_file_other_path rescue nil
151
+ File.open(@simple_file_other_path,"w") do | file |
152
+ file.puts "garbage"
153
+ end
154
+ sheet = @book[0]
155
+ cell_value = sheet[1,1].value
156
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
157
+ @book.Saved.should be_false
158
+ @book2.save_as(@simple_file_other_path, :if_exists => :overwrite, :if_obstructed => :forget)
159
+ @book.should_not be_alive
160
+ @book2.should be_alive
161
+ File.exist?(@simple_file_other_path).should be_true
162
+ new_book = Book.open(@simple_file_other_path)
163
+ new_book.should be_a Book
164
+ new_book.close
165
+ old_book = Book.open(@simple_file)
166
+ old_sheet = old_book[0]
167
+ old_sheet[1,1].value.should == cell_value
168
+ old_book.close
169
+ end
170
+
171
+ it "should save and close the blocking workbook, and save the current workbook with :if_obstructed => :save" do
172
+ File.delete @simple_file_other_path rescue nil
173
+ File.open(@simple_file_other_path,"w") do | file |
174
+ file.puts "garbage"
175
+ end
176
+ sheet = @book[0]
177
+ cell_value = sheet[1,1].value
178
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
179
+ @book.Saved.should be_false
180
+ @book2.save_as(@simple_file_other_path, :if_exists => :overwrite, :if_obstructed => :save)
181
+ @book.should_not be_alive
182
+ @book2.should be_alive
183
+ File.exist?(@simple_file_other_path).should be_true
184
+ new_book = Book.open(@simple_file_other_path)
185
+ new_book.should be_a Book
186
+ new_book.close
187
+ old_book = Book.open(@simple_file)
188
+ old_sheet = old_book[0]
189
+ old_sheet[1,1].value.should_not == cell_value
190
+ old_book.close
191
+ end
192
+
193
+ it "should close the blocking workbook if it was saved, and save the current workbook with :if_obstructed => :close_if_saved" do
194
+ File.delete @simple_file_other_path rescue nil
195
+ File.open(@simple_file_other_path,"w") do | file |
196
+ file.puts "garbage"
197
+ end
198
+ @book.Saved.should be_true
199
+ @book2.save_as(@simple_file_other_path, :if_exists => :overwrite, :if_obstructed => :close_if_saved)
200
+ @book.should_not be_alive
201
+ @book2.should be_alive
202
+ File.exist?(@simple_file_other_path).should be_true
203
+ new_book = Book.open(@simple_file_other_path)
204
+ new_book.should be_a Book
205
+ new_book.close
206
+ end
207
+
208
+ it "should raise an error if the blocking workbook was unsaved with :if_obstructed => :close_if_saved" do
209
+ sheet = @book[0]
210
+ cell_value = sheet[1,1].value
211
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
212
+ @book.Saved.should be_false
213
+ expect{
214
+ @book2.save_as(@simple_file_other_path, :if_exists => :overwrite, :if_obstructed => :close_if_saved)
215
+ }.to raise_error(ExcelErrorSave, /blocking workbook is unsaved: "workbook.xls"/)
216
+ end
217
+
218
+ it "should raise an error with an invalid option" do
219
+ File.delete @simple_file_other_path rescue nil
220
+ File.open(@simple_file_other_path,"w") do | file |
221
+ file.puts "garbage"
222
+ end
223
+ File.exist?(@simple_file_other_path).should be_true
224
+ expect{
225
+ @book2.save_as(@simple_file_other_path, :if_exists => :overwrite, :if_obstructed => :invalid)
226
+ }.to raise_error(ExcelErrorSave, ":if_obstructed: invalid option: :invalid")
227
+ end
228
+
229
+ it "should raise an error by default" do
230
+ File.delete @simple_file_other_path rescue nil
231
+ File.open(@simple_file_other_path,"w") do | file |
232
+ file.puts "garbage"
233
+ end
234
+ File.exist?(@simple_file_other_path).should be_true
235
+ expect{
236
+ @book2.save_as(@simple_file_other_path, :if_exists => :overwrite)
237
+ }.to raise_error(ExcelErrorSave, /blocked by another workbook: "workbook.xls"/)
238
+ end
239
+
240
+ it "should raise an error if the file does not exist and an workbook with the same name and other path exists" do
241
+ File.delete @simple_file_other_path rescue nil
242
+ File.exist?(@simple_file_other_path).should be_false
243
+ expect{
244
+ @book2.save_as(@simple_file_other_path, :if_exists => :overwrite, :if_obstructed => :raise)
245
+ }.to raise_error(ExcelErrorSave, /blocked by another workbook: "workbook.xls"/)
246
+ end
247
+
248
+ it "should raise an error if the file exists and an workbook with the same name and other path exists" do
249
+ File.delete @simple_file_other_path rescue nil
250
+ File.open(@simple_file_other_path,"w") do | file |
251
+ file.puts "garbage"
252
+ end
253
+ File.exist?(@simple_file_other_path).should be_true
254
+ expect{
255
+ @book.save_as(@simple_file_other_path, :if_exists => :raise, :if_obstructed => :raise)
256
+ }.to raise_error(ExcelErrorSave, /file already exists: "workbook.xls"/)
257
+ end
258
+
259
+ end
260
+
261
+
113
262
  # options :overwrite, :raise, :excel, no option, invalid option
114
263
  possible_displayalerts = [true, false]
115
264
  possible_displayalerts.each do |displayalert_value|
@@ -128,9 +277,9 @@ describe Book do
128
277
  book_save = Book.open(@simple_save_file, :excel => :new)
129
278
  expect{
130
279
  @book.save_as(@simple_save_file, :if_exists => :overwrite)
131
- }.to raise_error(ExcelErrorSave, "book is open and used in Excel")
280
+ }.to raise_error(ExcelErrorSave, "workbook is open and used in Excel")
132
281
  book_save.close
133
- end
282
+ end
134
283
 
135
284
  it "should save to simple_save_file.xls with :if_exists => :overwrite" do
136
285
  File.delete @simple_save_file rescue nil
@@ -143,6 +292,18 @@ describe Book do
143
292
  new_book.should be_a Book
144
293
  new_book.close
145
294
  end
295
+
296
+ it "should simple save if file name is equal to the old one with :if_exists => :overwrite" do
297
+ sheet = @book[0]
298
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
299
+ new_value = sheet[1,1].value
300
+ @book.save_as(@simple_file, :if_exists => :overwrite)
301
+ new_book = Book.open(@simple_file)
302
+ new_sheet = new_book[0]
303
+ new_sheet[1,1].value.should == new_value
304
+ new_book.close
305
+ end
306
+
146
307
  it "should save to 'simple_save_file.xls' with :if_exists => :raise" do
147
308
  dirname, basename = File.split(@simple_save_file)
148
309
  File.delete @simple_save_file rescue nil
@@ -153,7 +314,7 @@ describe Book do
153
314
  booklength = File.size?(@simple_save_file)
154
315
  expect {
155
316
  @book.save_as(@simple_save_file, :if_exists => :raise)
156
- }.to raise_error(ExcelErrorSave, 'book already exists: ' + basename)
317
+ }.to raise_error(ExcelErrorSave, /file already exists: "workbook_save.xls"/)
157
318
  File.exist?(@simple_save_file).should be_true
158
319
  File.size?(@simple_save_file).should == booklength
159
320
  end
@@ -165,7 +326,7 @@ describe Book do
165
326
  file.puts "garbage"
166
327
  end
167
328
  @garbage_length = File.size?(@simple_save_file)
168
- @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
329
+ @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '../helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
169
330
  end
170
331
 
171
332
  after do
@@ -220,7 +381,7 @@ describe Book do
220
381
  @book.workbook.Close
221
382
  expect{
222
383
  @book.save_as(@simple_save_file, :if_exists => :alert)
223
- }.to raise_error(ExcelErrorSaveUnknown)
384
+ }.to raise_error(ExcelErrorSave, "Workbook is not alive")
224
385
  File.exist?(@simple_save_file).should be_true
225
386
  File.size?(@simple_save_file).should == @garbage_length
226
387
  @book.excel.DisplayAlerts.should == displayalert_value
@@ -238,17 +399,17 @@ describe Book do
238
399
  booklength = File.size?(@simple_save_file)
239
400
  expect {
240
401
  @book.save_as(@simple_save_file)
241
- }.to raise_error(ExcelErrorSave, 'book already exists: ' + basename)
402
+ }.to raise_error(ExcelErrorSave, /file already exists: "workbook_save.xls"/)
242
403
  File.exist?(@simple_save_file).should be_true
243
404
  File.size?(@simple_save_file).should == booklength
244
405
  end
245
406
 
246
- it "should save to 'simple_save_file.xls' with :if_exists => :invalid_option" do
407
+ it "should save to 'simple_save_file.xls' with :if_exists => :invalid" do
247
408
  File.delete @simple_save_file rescue nil
248
409
  @book.save_as(@simple_save_file)
249
410
  expect {
250
- @book.save_as(@simple_save_file, :if_exists => :invalid_option)
251
- }.to raise_error(ExcelErrorSave, ':if_exists: invalid option: invalid_option')
411
+ @book.save_as(@simple_save_file, :if_exists => :invalid)
412
+ }.to raise_error(ExcelErrorSave, ':if_exists: invalid option: :invalid')
252
413
  end
253
414
  end
254
415
  end
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require File.join(File.dirname(__FILE__), './spec_helper')
3
+ require File.join(File.dirname(__FILE__), './../spec_helper')
4
4
 
5
5
 
6
6
  $VERBOSE = nil
@@ -45,7 +45,9 @@ describe Book do
45
45
 
46
46
  context "only first argument" do
47
47
  it "should add worksheet" do
48
- expect { @book.add_sheet @sheet }.to change{ @book.workbook.Worksheets.Count }.from(3).to(4)
48
+ @book.workbook.Worksheets.Count == 3
49
+ @book.add_sheet @sheet
50
+ @book.workbook.Worksheets.Count == 4
49
51
  end
50
52
 
51
53
  it "should return copyed sheet" do
@@ -104,7 +106,9 @@ describe Book do
104
106
 
105
107
  context "without argument" do
106
108
  it "should add empty sheet" do
107
- expect { @book.add_sheet }.to change{ @book.workbook.Worksheets.Count }.from(3).to(4)
109
+ @book.workbook.Worksheets.Count.should == 3
110
+ @book.add_sheet
111
+ @book.workbook.Worksheets.Count.should == 4
108
112
  end
109
113
 
110
114
  it "should return copyed sheet" do
@@ -119,7 +123,7 @@ describe Book do
119
123
  @book.add_sheet(@sheet, :as => 'new_sheet')
120
124
  expect{
121
125
  @book.add_sheet(@sheet, :as => 'new_sheet')
122
- }.to raise_error(ExcelErrorSheet, "sheet name already exists")
126
+ }.to raise_error(ExcelErrorSheet, /sheet name "new_sheet" already exists/)
123
127
  end
124
128
  end
125
129
  end
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require File.join(File.dirname(__FILE__), './spec_helper')
3
+ require File.join(File.dirname(__FILE__), './../spec_helper')
4
4
 
5
5
 
6
6
  $VERBOSE = nil
@@ -47,17 +47,35 @@ describe Book do
47
47
 
48
48
  describe "open" do
49
49
 
50
+ context "lift a workbook to a Book object" do
51
+
52
+ before do
53
+ @book = Book.open(@simple_file)
54
+ end
55
+
56
+ after do
57
+ @book.close
58
+ end
59
+
60
+ it "should yield an identical Book" do
61
+ workbook = @book.workbook
62
+ new_book = Book.new(workbook)
63
+ new_book.should be_a Book
64
+ new_book.should be_alive
65
+ new_book.should == @book
66
+ new_book.filename.should == @book.filename
67
+ new_book.excel.should == @book.excel
68
+ new_book.should === @book
69
+ new_book.close
70
+ end
71
+ end
72
+
50
73
  context "with various file formats" do
51
74
 
52
75
  it "should open linked workbook" do
53
76
  book = Book.open(@linked_file, :visible => true)
54
77
  book.close
55
78
  end
56
-
57
- it "should open xlsm file" do
58
- book = Book.open(@simple_file_xlsm, :visible => true)
59
- book.close
60
- end
61
79
  end
62
80
 
63
81
  context "with standard options" do
@@ -208,7 +226,7 @@ describe Book do
208
226
  it "should raise an error, if :if_unsaved is :raise" do
209
227
  expect {
210
228
  @new_book = Book.open(@simple_file, :if_unsaved => :raise)
211
- }.to raise_error(ExcelErrorOpen, "book is already open but not saved (#{File.basename(@simple_file)})")
229
+ }.to raise_error(ExcelErrorOpen, /workbook is already open but not saved: "workbook.xls"/)
212
230
  end
213
231
 
214
232
  it "should let the book open, if :if_unsaved is :accept" do
@@ -222,7 +240,7 @@ describe Book do
222
240
 
223
241
  context "with :if_unsaved => :alert" do
224
242
  before do
225
- @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
243
+ @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '../helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
226
244
  end
227
245
 
228
246
  after do
@@ -289,7 +307,7 @@ describe Book do
289
307
  if :if_obstructed is :close_if_saved" do
290
308
  expect{
291
309
  @new_book = Book.open(@simple_file, :if_obstructed => :close_if_saved)
292
- }.to raise_error(ExcelErrorOpen, "book with the same name in a different path is unsaved: workbook.xls")
310
+ }.to raise_error(ExcelErrorOpen, /workbook with the same name in a different path is unsaved: "workbook.xls"/)
293
311
  @book.save
294
312
  @new_book = Book.open(@simple_file, :if_obstructed => :close_if_saved)
295
313
  @book.should_not be_alive
@@ -317,7 +335,7 @@ describe Book do
317
335
  File.delete @simple_save_file rescue nil
318
336
  expect {
319
337
  Book.open(@simple_save_file)
320
- }.to raise_error(ExcelErrorOpen, "file #{@simple_save_file} not found")
338
+ }.to raise_error(ExcelErrorOpen, /file "#{@simple_save_file}" not found/)
321
339
  end
322
340
  end
323
341
 
@@ -375,9 +393,10 @@ describe Book do
375
393
  end
376
394
  end
377
395
 
378
- describe "send methods to workbook" do
396
+ describe "uplifting" do
379
397
 
380
398
  context "with standard" do
399
+
381
400
  before do
382
401
  @book = Book.open(@simple_file)
383
402
  end
@@ -386,20 +405,429 @@ describe Book do
386
405
  @book.close
387
406
  end
388
407
 
389
- it "should send Saved to workbook" do
408
+ it "should uplift a workbook to a book with an open book" do
409
+ workbook = @book.workbook
410
+ book1 = Book.new(workbook)
411
+ book1.should be_a Book
412
+ book1.should be_alive
413
+ book1.should == @book
414
+ end
415
+ end
416
+ end
417
+
418
+ describe "unobtrusively" do
419
+
420
+ def unobtrusively_ok? # :nodoc: #
421
+ Book.unobtrusively(@simple_file) do |book|
422
+ book.should be_a Book
423
+ sheet = book[0]
424
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
425
+ book.should be_alive
426
+ book.Saved.should be_false
427
+ end
428
+ end
429
+
430
+ context "with no open book" do
431
+
432
+ it "should open unobtrusively in a new Excel" do
433
+ expect{ unobtrusively_ok? }.to_not raise_error
434
+ end
435
+ end
436
+
437
+ context "with two running excel instances" do
438
+ before :all do
439
+ Excel.close_all
440
+ end
441
+
442
+ before do
443
+ @excel1 = Excel.new(:reuse => false)
444
+ @excel2 = Excel.new(:reuse => false)
445
+ end
446
+
447
+ after do
448
+ #Excel.close_all
449
+ begin
450
+ @excel1.close
451
+ @excel2.close
452
+ rescue ExcelErrorOpen => msg
453
+ puts "ExcelError: #{msg.message}" if msg.message =~ /Excel instance not alive or damaged/
454
+ end
455
+ end
456
+
457
+ it "should open unobtrusively in a new Excel" do
458
+ Book.unobtrusively(@simple_file, :hidden) do |book|
459
+ book.should be_a Book
460
+ book.should be_alive
461
+ book.excel.should_not == @excel1
462
+ book.excel.should_not == @excel2
463
+ end
464
+ end
465
+
466
+ it "should open unobtrusively in a given Excel" do
467
+ Book.unobtrusively(@simple_file, @excel2) do |book|
468
+ book.should be_a Book
469
+ book.should be_alive
470
+ book.excel.should_not == @excel1
471
+ book.excel.should == @excel2
472
+ end
473
+ end
474
+ end
475
+
476
+ context "with an open book" do
477
+
478
+ before do
479
+ @book = Book.open(@simple_file)
480
+ end
481
+
482
+ after do
483
+ @book.close(:if_unsaved => :forget)
484
+ @book2.close(:if_unsaved => :forget) rescue nil
485
+ end
486
+
487
+ it "should let an open Book open if two books have been opened and one has been closed and opened again" do
488
+ book2 = Book.open(@different_file, :force_excel => :new)
489
+ @book.close
490
+ book2.close
491
+ @book.reopen
492
+ Book.unobtrusively(@simple_file) do |book|
493
+ book.should be_a Book
494
+ book.should be_alive
495
+ book.excel.should == @book.excel
496
+ end
497
+ @book.should be_alive
498
+ @book.should be_a Book
499
+ end
500
+
501
+ it "should let a saved book saved" do
502
+ @book.Saved.should be_true
503
+ @book.should be_alive
504
+ sheet = @book[0]
505
+ old_cell_value = sheet[1,1].value
506
+ unobtrusively_ok?
390
507
  @book.Saved.should be_true
508
+ @book.should be_alive
509
+ sheet = @book[0]
510
+ sheet[1,1].value.should_not == old_cell_value
391
511
  end
392
512
 
393
- it "should send Fullname to workbook" do
394
- @book.Fullname.tr('\\','/').should == @simple_file
513
+ it "should let the unsaved book unsaved" do
514
+ sheet = @book[0]
515
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
516
+ old_cell_value = sheet[1,1].value
517
+ @book.Saved.should be_false
518
+ unobtrusively_ok?
519
+ @book.should be_alive
520
+ @book.Saved.should be_false
521
+ @book.close(:if_unsaved => :forget)
522
+ @book2 = Book.open(@simple_file)
523
+ sheet2 = @book2[0]
524
+ sheet2[1,1].value.should_not == old_cell_value
525
+ end
526
+ end
527
+
528
+ context "with a closed book" do
529
+
530
+ before do
531
+ @book = Book.open(@simple_file)
532
+ end
533
+
534
+ after do
535
+ @book.close(:if_unsaved => :forget)
536
+ end
537
+
538
+ it "should let the closed book closed by default" do
539
+ sheet = @book[0]
540
+ old_cell_value = sheet[1,1].value
541
+ @book.close
542
+ @book.should_not be_alive
543
+ unobtrusively_ok?
544
+ @book.should_not be_alive
545
+ @book = Book.open(@simple_file)
546
+ sheet = @book[0]
547
+ sheet[1,1].Value.should_not == old_cell_value
548
+ end
549
+
550
+ # The bold reanimation of the @book
551
+ it "should use the excel of the book and keep open the book" do
552
+ excel = Excel.new(:reuse => false)
553
+ sheet = @book[0]
554
+ old_cell_value = sheet[1,1].value
555
+ @book.close
556
+ @book.should_not be_alive
557
+ Book.unobtrusively(@simple_file, :keep_open => true) do |book|
558
+ book.should be_a Book
559
+ book.excel.should == @book.excel
560
+ book.excel.should_not == excel
561
+ sheet = book[0]
562
+ cell = sheet[1,1]
563
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
564
+ book.Saved.should be_false
565
+ end
566
+ @book.should be_alive
567
+ @book.close
568
+ new_book = Book.open(@simple_file)
569
+ sheet = new_book[0]
570
+ sheet[1,1].value.should_not == old_cell_value
571
+ end
572
+
573
+ # book shall be reanimated even with :hidden
574
+ it "should use the excel of the book and keep open the book" do
575
+ excel = Excel.new(:reuse => false)
576
+ sheet = @book[0]
577
+ old_cell_value = sheet[1,1].value
578
+ @book.close
579
+ @book.should_not be_alive
580
+ Book.unobtrusively(@simple_file, :hidden) do |book|
581
+ book.should be_a Book
582
+ book.should be_alive
583
+ book.excel.should_not == @book.excel
584
+ book.excel.should_not == excel
585
+ sheet = book[0]
586
+ cell = sheet[1,1]
587
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
588
+ book.Saved.should be_false
589
+ end
590
+ @book.should_not be_alive
591
+ new_book = Book.open(@simple_file)
592
+ sheet = new_book[0]
593
+ sheet[1,1].value.should_not == old_cell_value
594
+ end
595
+ end
596
+
597
+ context "with a visible book" do
598
+
599
+ before do
600
+ @book = Book.open(@simple_file, :visible => true)
601
+ end
602
+
603
+ after do
604
+ @book.close(:if_unsaved => :forget)
605
+ @book2.close(:if_unsaved => :forget) rescue nil
606
+ end
607
+
608
+ it "should let an open Book open" do
609
+ Book.unobtrusively(@simple_file) do |book|
610
+ book.should be_a Book
611
+ book.should be_alive
612
+ book.excel.should == @book.excel
613
+ book.excel.Visible.should be_true
614
+ end
615
+ @book.should be_alive
616
+ @book.should be_a Book
617
+ @book.excel.Visible.should be_true
618
+ end
619
+ end
620
+
621
+ context "with a read_only book" do
622
+
623
+ before do
624
+ @book = Book.open(@simple_file, :read_only => true)
625
+ end
626
+
627
+ after do
628
+ @book.close
629
+ end
630
+
631
+ it "should open unobtrusively the book in a new Excel such that the book is writable" do
632
+ book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
633
+ @book.ReadOnly.should be_true
634
+ book2.Readonly.should be_true
635
+ sheet = @book[0]
636
+ cell_value = sheet[1,1].value
637
+ Book.unobtrusively(@simple_file, :hidden) do |book|
638
+ book.should be_a Book
639
+ book.excel.should_not == book2.excel
640
+ book.excel.should_not == @book.excel
641
+ sheet = book[0]
642
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
643
+ book.should be_alive
644
+ book.Saved.should be_false
645
+ end
646
+ @book.Saved.should be_true
647
+ @book.ReadOnly.should be_true
648
+ @book.close
649
+ book2.close
650
+ book3 = Book.open(@simple_file)
651
+ new_sheet = book3[0]
652
+ new_sheet[1,1].value.should_not == cell_value
653
+ book3.close
654
+ end
655
+ end
656
+
657
+ context "with a virgin Book class" do
658
+ before do
659
+ class Book
660
+ @@bookstore = nil
661
+ end
662
+ end
663
+ it "should work" do
664
+ expect{ unobtrusively_ok? }.to_not raise_error
665
+ end
666
+ end
667
+
668
+ context "with a book never opened before" do
669
+ before do
670
+ class Book
671
+ @@bookstore = nil
672
+ end
673
+ other_book = Book.open(@different_file)
674
+ end
675
+ it "should open the book" do
676
+ expect{ unobtrusively_ok? }.to_not raise_error
677
+ end
678
+ end
679
+
680
+ context "with block result" do
681
+ before do
682
+ @book1 = Book.open(@simple_file)
683
+ end
684
+
685
+ after do
686
+ @book1.close(:if_unsaved => :forget)
687
+ end
688
+
689
+ it "should yield the block result true" do
690
+ result =
691
+ Book.unobtrusively(@simple_file) do |book|
692
+ @book1.Saved.should be_true
693
+ end
694
+ result.should == true
695
+ end
696
+ end
697
+
698
+ context "with several Excel instances" do
699
+
700
+ before do
701
+ @book1 = Book.open(@simple_file)
702
+ @book2 = Book.open(@simple_file, :force_excel => :new)
703
+ @book1.Readonly.should == false
704
+ @book2.Readonly.should == true
705
+ old_sheet = @book1[0]
706
+ @old_cell_value = old_sheet[1,1].value
707
+ @book1.close
708
+ @book2.close
709
+ @book1.should_not be_alive
710
+ @book2.should_not be_alive
711
+ end
712
+
713
+ it "should open unobtrusively the closed book in the most recent Excel where it was open before" do
714
+ Book.unobtrusively(@simple_file) do |book|
715
+ book.excel.should == @book2.excel
716
+ book.excel.should_not == @book1.excel
717
+ book.ReadOnly.should == false
718
+ sheet = book[0]
719
+ cell = sheet[1,1]
720
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
721
+ book.Saved.should be_false
722
+ end
723
+ new_book = Book.open(@simple_file)
724
+ sheet = new_book[0]
725
+ sheet[1,1].value.should_not == @old_cell_value
726
+ end
727
+
728
+ it "should open unobtrusively the closed book in the new hidden Excel" do
729
+ Book.unobtrusively(@simple_file, :hidden) do |book|
730
+ book.excel.should_not == @book2.excel
731
+ book.excel.should_not == @book1.excel
732
+ book.ReadOnly.should == false
733
+ sheet = book[0]
734
+ cell = sheet[1,1]
735
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
736
+ book.Saved.should be_false
737
+ end
738
+ new_book = Book.open(@simple_file)
739
+ sheet = new_book[0]
740
+ sheet[1,1].Value.should_not == @old_cell_value
395
741
  end
396
742
  end
397
- end
398
743
 
399
- describe "hidden_excel" do
744
+ context "with :hidden" do
745
+
746
+ before do
747
+ @book1 = Book.open(@simple_file)
748
+ @book1.close
749
+ end
400
750
 
401
- context "with some open book" do
751
+ it "should create a new hidden Excel instance and use this afterwards" do
752
+ hidden_excel = nil
753
+ Book.unobtrusively(@simple_file, :hidden) do |book|
754
+ book.should be_a Book
755
+ book.should be_alive
756
+ book.excel.Visible.should be_false
757
+ book.excel.DisplayAlerts.should be_false
758
+ hidden_excel = book.excel
759
+ end
760
+ Book.unobtrusively(@different_file, :hidden) do |book|
761
+ book.should be_a Book
762
+ book.should be_alive
763
+ book.excel.Visible.should be_false
764
+ book.excel.DisplayAlerts.should be_false
765
+ book.excel.should == hidden_excel
766
+ end
767
+ end
768
+ end
769
+ end
770
+
771
+ describe "for_reading, for_modifying" do
772
+
773
+ context "open unobtrusively for reading and modifying" do
774
+
775
+ before do
776
+ @book = Book.open(@simple_file)
777
+ sheet = @book[0]
778
+ @old_cell_value = sheet[1,1].value
779
+ @book.close
780
+ end
781
+
782
+ it "should not change the value" do
783
+ Book.for_reading(@simple_file) do |book|
784
+ book.should be_a Book
785
+ book.should be_alive
786
+ book.Saved.should be_true
787
+ sheet = book[0]
788
+ cell = sheet[1,1]
789
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
790
+ book.Saved.should be_false
791
+ book.excel.should == @book.excel
792
+ end
793
+ new_book = Book.open(@simple_file, :visible => true)
794
+ sheet = new_book[0]
795
+ sheet[1,1].Value.should == @old_cell_value
796
+ end
797
+
798
+ it "should not change the value and use the hidden Excel instance" do
799
+ new_excel = Excel.new(:reuse => false)
800
+ Book.for_reading(@simple_file, :hidden) do |book|
801
+ sheet = book[0]
802
+ cell = sheet[1,1]
803
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
804
+ book.excel.should_not == @book.excel
805
+ book.excel.should_not == new_excel
806
+ book.excel.visible.should be_false
807
+ book.excel.displayalerts.should be_false
808
+ end
809
+ new_book = Book.open(@simple_file, :visible => true)
810
+ sheet = new_book[0]
811
+ sheet[1,1].Value.should == @old_cell_value
812
+ end
813
+
814
+ it "should change the value" do
815
+ Book.for_modifying(@simple_file) do |book|
816
+ sheet = book[0]
817
+ cell = sheet[1,1]
818
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
819
+ book.excel.should == @book.excel
820
+ end
821
+ new_book = Book.open(@simple_file, :visible => true)
822
+ sheet = new_book[0]
823
+ sheet[1,1].Value.should_not == @old_cell_value
824
+ end
825
+ end
826
+ end
402
827
 
828
+ describe "send methods to workbook" do
829
+
830
+ context "with standard" do
403
831
  before do
404
832
  @book = Book.open(@simple_file)
405
833
  end
@@ -408,12 +836,8 @@ describe Book do
408
836
  @book.close
409
837
  end
410
838
 
411
- it "should create and use a hidden Excel instance" do
412
- book2 = Book.open(@simple_file, :force_excel => @book.bookstore.hidden_excel)
413
- book2.excel.should_not == @book.excel
414
- book2.excel.visible.should be_false
415
- book2.excel.displayalerts.should be_false
416
- book2.close
839
+ it "should send Saved to workbook" do
840
+ @book.Saved.should be_true
417
841
  end
418
842
  end
419
843
  end
@@ -455,12 +879,6 @@ describe Book do
455
879
  @book1.close(:if_unsaved => :forget)
456
880
  end
457
881
 
458
- it "should set value of a range" do
459
- @book1.nvalue("new").should == "foo"
460
- @book1.set_nvalue("new","bar")
461
- @book1.nvalue("new").should == "bar"
462
- end
463
-
464
882
  it "should set value of a range" do
465
883
  @book1.nvalue("new").should == "foo"
466
884
  @book1["new"] = "bar"
@@ -503,15 +921,15 @@ describe Book do
503
921
  it "should raise error by default" do
504
922
  expect{
505
923
  @book.close(:if_unsaved => :raise)
506
- }.to raise_error(ExcelErrorClose, "book is unsaved (#{File.basename(@simple_file)})")
924
+ }.to raise_error(ExcelErrorClose, /workbook is unsaved: "workbook.xls"/)
507
925
  end
508
926
 
509
927
  it "should save the book before close with option :save" do
510
928
  ole_workbook = @book.workbook
511
929
  excel = @book.excel
512
- expect {
513
- @book.close(:if_unsaved => :save)
514
- }.to change {excel.Workbooks.Count }.by(-1)
930
+ excel.Workbooks.Count.should == 1
931
+ @book.close(:if_unsaved => :save)
932
+ excel.Workbooks.Count.should == 0
515
933
  @book.workbook.should == nil
516
934
  @book.should_not be_alive
517
935
  expect{
@@ -542,23 +960,6 @@ describe Book do
542
960
  end
543
961
  end
544
962
 
545
- context "with open with read only" do
546
- before do
547
- @book = Book.open(@simple_file, :read_only => true)
548
- end
549
-
550
- after do
551
- @book.close
552
- end
553
-
554
- it {
555
- expect {
556
- @book.save_as(@simple_file)
557
- }.to raise_error(IOError,
558
- "Not opened for writing(open with :read_only option)")
559
- }
560
- end
561
-
562
963
  context "with argument" do
563
964
  before do
564
965
  Book.open(@simple_file) do |book|
@@ -570,111 +971,6 @@ describe Book do
570
971
  File.exist?(@simple_save_file).should be_true
571
972
  end
572
973
  end
573
-
574
- context "with different extensions" do
575
- before do
576
- @book = Book.open(@simple_file)
577
- end
578
-
579
- after do
580
- @book.close
581
- end
582
-
583
- possible_extensions = ['xls', 'xlsm', 'xlsx']
584
- possible_extensions.each do |extensions_value|
585
- it "should save to 'simple_save_file.#{extensions_value}'" do
586
- simple_save_file = @dir + '/simple_save_file.' + extensions_value
587
- File.delete simple_save_file rescue nil
588
- @book.save_as(simple_save_file, :if_exists => :overwrite)
589
- File.exist?(simple_save_file).should be_true
590
- new_book = Book.open(simple_save_file)
591
- new_book.should be_a Book
592
- new_book.close
593
- end
594
- end
595
- end
596
-
597
- # options :overwrite, :raise, :excel, no option, invalid option
598
- possible_displayalerts = [true, false]
599
- possible_displayalerts.each do |displayalert_value|
600
- context "with displayalerts=#{displayalert_value}" do
601
- before do
602
- @book = Book.open(@simple_file, :displayalerts => displayalert_value)
603
- end
604
-
605
- after do
606
- @book.close
607
- end
608
-
609
- it "should raise an error if the book is open" do
610
- File.delete @simple_save_file rescue nil
611
- FileUtils.copy @simple_file, @simple_save_file
612
- book_save = Book.open(@simple_save_file, :excel => :new)
613
- expect{
614
- @book.save_as(@simple_save_file, :if_exists => :overwrite)
615
- }.to raise_error(ExcelErrorSave, "book is open and used in Excel")
616
- book_save.close
617
- end
618
-
619
- context "with :if_exists => :alert" do
620
- before do
621
- File.delete @simple_save_file rescue nil
622
- File.open(@simple_save_file,"w") do | file |
623
- file.puts "garbage"
624
- end
625
- @garbage_length = File.size?(@simple_save_file)
626
- @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
627
- end
628
-
629
- after do
630
- @key_sender.close
631
- end
632
-
633
- it "should save if user answers 'yes'" do
634
- # "Yes" is to the left of "No", which is the default. --> language independent
635
- @key_sender.puts "{left}{enter}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
636
- @book.save_as(@simple_save_file, :if_exists => :alert)
637
- File.exist?(@simple_save_file).should be_true
638
- File.size?(@simple_save_file).should > @garbage_length
639
- @book.excel.DisplayAlerts.should == displayalert_value
640
- new_book = Book.open(@simple_save_file, :excel => :new)
641
- new_book.should be_a Book
642
- new_book.close
643
- @book.excel.DisplayAlerts.should == displayalert_value
644
- end
645
-
646
- it "should not save if user answers 'no'" do
647
- # Just give the "Enter" key, because "No" is the default. --> language independent
648
- # strangely, in the "no" case, the question will sometimes be repeated three times
649
- @key_sender.puts "{enter}"
650
- @key_sender.puts "{enter}"
651
- @key_sender.puts "{enter}"
652
- #@key_sender.puts "%{n}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
653
- expect{
654
- @book.save_as(@simple_save_file, :if_exists => :alert)
655
- }.to raise_error(ExcelErrorSave, "not saved or canceled by user")
656
- File.exist?(@simple_save_file).should be_true
657
- File.size?(@simple_save_file).should == @garbage_length
658
- @book.excel.DisplayAlerts.should == displayalert_value
659
- end
660
-
661
- it "should not save if user answers 'cancel'" do
662
- # 'Cancel' is right from 'yes'
663
- # strangely, in the "no" case, the question will sometimes be repeated three times
664
- @key_sender.puts "{right}{enter}"
665
- @key_sender.puts "{right}{enter}"
666
- @key_sender.puts "{right}{enter}"
667
- #@key_sender.puts "%{n}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
668
- expect{
669
- @book.save_as(@simple_save_file, :if_exists => :alert)
670
- }.to raise_error(ExcelErrorSave, "not saved or canceled by user")
671
- File.exist?(@simple_save_file).should be_true
672
- File.size?(@simple_save_file).should == @garbage_length
673
- @book.excel.DisplayAlerts.should == displayalert_value
674
- end
675
- end
676
- end
677
- end
678
974
  end
679
975
 
680
976
  describe "alive?, filename, ==, visible, displayalerts, activate, saved" do
@@ -731,22 +1027,12 @@ describe Book do
731
1027
  @new_book = Book.open(@simple_file)
732
1028
  @new_book.should == @book
733
1029
  end
734
-
735
- it "should be false with two different books" do
736
- @new_book = Book.new(@different_file)
737
- @new_book.should_not == @book
738
- end
739
-
740
- it "should be false with same book names but different paths" do
741
- @new_book = Book.new(@simple_file_other_path, :excel => :new)
742
- @new_book.should_not == @book
743
- end
744
1030
  end
745
1031
 
746
1032
  context "with activate" do
747
1033
 
748
1034
  before do
749
- @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
1035
+ @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '../helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
750
1036
  @book = Book.open(@simple_file, :visible => true)
751
1037
  @book2 = Book.open(@another_simple_file, :force_excel => :new, :visible => true)
752
1038
  end
@@ -791,7 +1077,10 @@ describe Book do
791
1077
 
792
1078
  context "only first argument" do
793
1079
  it "should add worksheet" do
794
- expect { @book.add_sheet @sheet }.to change{ @book.workbook.Worksheets.Count }.from(3).to(4)
1080
+ @book.workbook.Worksheets.Count.should == 3
1081
+ @book.add_sheet @sheet
1082
+ @book.workbook.Worksheets.Count.should == 4
1083
+ #expect { @book.add_sheet @sheet }.to change{ @book.workbook.Worksheets.Count }.from(3).to(4)
795
1084
  end
796
1085
 
797
1086
  it "should return copyed sheet" do
@@ -802,23 +1091,6 @@ describe Book do
802
1091
  end
803
1092
 
804
1093
  context "with first argument" do
805
- context "with second argument is {:as => 'copyed_name'}" do
806
- it "copyed sheet name should be 'copyed_name'" do
807
- @book.add_sheet(@sheet, :as => 'copyed_name').name.should eq 'copyed_name'
808
- end
809
- end
810
-
811
- context "with second argument is {:before => @sheet}" do
812
- it "should add the first sheet" do
813
- @book.add_sheet(@sheet, :before => @sheet).name.should eq @book[0].name
814
- end
815
- end
816
-
817
- context "with second argument is {:after => @sheet}" do
818
- it "should add the first sheet" do
819
- @book.add_sheet(@sheet, :after => @sheet).name.should eq @book[1].name
820
- end
821
- end
822
1094
 
823
1095
  context "with second argument is {:before => @book[2], :after => @sheet}" do
824
1096
  it "should arguments in the first is given priority" do
@@ -848,9 +1120,6 @@ describe Book do
848
1120
  end
849
1121
 
850
1122
  context "without argument" do
851
- it "should add empty sheet" do
852
- expect { @book.add_sheet }.to change{ @book.workbook.Worksheets.Count }.from(3).to(4)
853
- end
854
1123
 
855
1124
  it "should return copyed sheet" do
856
1125
  sheet = @book.add_sheet