robust_excel_ole 0.3.5 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,13 @@
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
- module My
7
- class Excel < RobustExcelOle::Excel
6
+ module My # :nodoc: #
7
+ class Excel < RobustExcelOle::Excel # :nodoc: #
8
8
  end
9
9
 
10
- class Book < RobustExcelOle::Book
10
+ class Book < RobustExcelOle::Book # :nodoc: #
11
11
  end
12
12
  end
13
13
 
@@ -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
@@ -74,8 +74,9 @@ describe Book do
74
74
  begin
75
75
  @excel1.close
76
76
  @excel2.close
77
- rescue ExcelErrorOpen => msg
78
- puts "ExcelError: #{msg.message}" if msg.message =~ /Excel instance not alive or damaged/
77
+ rescue RuntimeError => msg
78
+ #puts "RuntimeError: #{msg.message}" if msg.message =~ /Excel instance not alive or damaged/
79
+ nil
79
80
  end
80
81
  end
81
82
 
@@ -119,7 +120,7 @@ describe Book do
119
120
  expect{
120
121
  Book.unobtrusively(@simple_file, :invalid_option) do |book|
121
122
  end
122
- }.to raise_error(ExcelErrorOpen, "provided instance is neither an Excel nor a Book")
123
+ }.to raise_error(ExcelError, "receiver instance is neither an Excel nor a Book")
123
124
  end
124
125
 
125
126
  end
@@ -135,6 +136,42 @@ describe Book do
135
136
  @book2.close(:if_unsaved => :forget) rescue nil
136
137
  end
137
138
 
139
+ #it "should let an open Book open" do
140
+ # Book.unobtrusively(@simple_file) do |book|
141
+ # book.should be_a Book
142
+ # book.should be_alive
143
+ # book.excel.should == @book.excel
144
+ # end
145
+ # @book.should be_alive
146
+ # @book.should be_a Book
147
+ #end
148
+
149
+ it "should let an open Book open if it has been closed and opened again" do
150
+ @book.close
151
+ @book.reopen
152
+ Book.unobtrusively(@simple_file) do |book|
153
+ book.should be_a Book
154
+ book.should be_alive
155
+ book.excel.should == @book.excel
156
+ end
157
+ @book.should be_alive
158
+ @book.should be_a Book
159
+ end
160
+
161
+ it "should let an open Book open if two books have been opened and one has been closed and opened again" do
162
+ book2 = Book.open(@different_file, :force_excel => :new)
163
+ @book.close
164
+ book2.close
165
+ @book.reopen
166
+ Book.unobtrusively(@simple_file) do |book|
167
+ book.should be_a Book
168
+ book.should be_alive
169
+ book.excel.should == @book.excel
170
+ end
171
+ @book.should be_alive
172
+ @book.should be_a Book
173
+ end
174
+
138
175
  it "should open in the Excel of the given Book" do
139
176
  #book1 = Book.open(@different_file)
140
177
  @book2 = Book.open(@another_simple_file, :force_excel => :new)
@@ -323,8 +360,31 @@ describe Book do
323
360
  new_sheet[1,1].Value.should_not == old_cell_value
324
361
  new_book.close
325
362
  end
363
+ end
364
+
365
+ context "with a visible book" do
326
366
 
367
+ before do
368
+ @book = Book.open(@simple_file, :visible => true)
369
+ end
327
370
 
371
+ after do
372
+ @book.close(:if_unsaved => :forget)
373
+ @book2.close(:if_unsaved => :forget) rescue nil
374
+ end
375
+
376
+ it "should let an open Book open" do
377
+ Book.unobtrusively(@simple_file) do |book|
378
+ book.should be_a Book
379
+ book.should be_alive
380
+ book.excel.should == @book.excel
381
+ book.excel.Visible.should be_true
382
+ end
383
+ @book.should be_alive
384
+ @book.should be_a Book
385
+ @book.excel.Visible.should be_true
386
+ end
387
+
328
388
  end
329
389
 
330
390
  context "with various options for an Excel instance in which to open a closed book" do
@@ -2,13 +2,14 @@
2
2
 
3
3
  require File.join(File.dirname(__FILE__), './spec_helper')
4
4
 
5
+ =begin
5
6
  RSpec.configure do |config|
6
7
 
7
8
  config.mock_with :rspec do |mocks|
8
9
  mocks.syntax = :should
9
10
  end
10
11
  end
11
-
12
+ =end
12
13
 
13
14
  $VERBOSE = nil
14
15
 
@@ -27,7 +28,6 @@ module RobustExcelOle
27
28
  end
28
29
  end
29
30
 
30
-
31
31
  $mock_bookstore = MockBookstore.new
32
32
 
33
33
  class Book
@@ -54,8 +54,15 @@ describe Bookstore do
54
54
  end
55
55
 
56
56
  after do
57
- Excel.close_all
58
- rm_tmp(@dir)
57
+ begin
58
+ Excel.close_all
59
+ rescue WeakRef::RefError => msg
60
+ puts "#{msg.message}"
61
+ Excel.kill_all
62
+ end
63
+ begin
64
+ rm_tmp(@dir) rescue nil
65
+ end
59
66
  end
60
67
 
61
68
  describe "create bookstore" do
data/spec/cell_spec.rb CHANGED
@@ -1,13 +1,15 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require File.join(File.dirname(__FILE__), './spec_helper')
3
3
 
4
- describe RobustExcelOle::Cell do
4
+ include RobustExcelOle
5
+
6
+ describe Cell do
5
7
 
6
8
  before(:all) do
7
- excel = RobustExcelOle::Excel.new(:reuse => true)
9
+ excel = Excel.new(:reuse => true)
8
10
  open_books = excel == nil ? 0 : excel.Workbooks.Count
9
11
  puts "*** open books *** : #{open_books}" if open_books > 0
10
- RobustExcelOle::Excel.close_all
12
+ Excel.close_all
11
13
  end
12
14
 
13
15
  before do
@@ -20,7 +22,7 @@ describe RobustExcelOle::Cell do
20
22
 
21
23
  context "open simple.xls" do
22
24
  before do
23
- @book = RobustExcelOle::Book.open(@dir + '/workbook.xls', :read_only => true)
25
+ @book = Book.open(@dir + '/workbook.xls', :read_only => true)
24
26
  @sheet = @book[1]
25
27
  @cell = @sheet[1, 1]
26
28
  end
@@ -52,7 +54,7 @@ describe RobustExcelOle::Cell do
52
54
 
53
55
  context "open merge_cells.xls" do
54
56
  before do
55
- @book = RobustExcelOle::Book.open(@dir + '/merge_cells.xls', :read_only => true)
57
+ @book = Book.open(@dir + '/merge_cells.xls', :read_only => true)
56
58
  @sheet = @book[0]
57
59
  end
58
60
 
Binary file
Binary file
Binary file
Binary file
data/spec/excel_spec.rb CHANGED
@@ -4,12 +4,15 @@ require File.join(File.dirname(__FILE__), './spec_helper')
4
4
 
5
5
  $VERBOSE = nil
6
6
 
7
+ include RobustExcelOle
8
+
7
9
  module RobustExcelOle
8
10
 
9
11
  describe Excel do
10
12
 
11
13
  before(:all) do
12
- Excel.close_all
14
+ Excel.kill_all
15
+ #Excel.close_all
13
16
  end
14
17
 
15
18
  before do
@@ -22,7 +25,8 @@ module RobustExcelOle
22
25
  end
23
26
 
24
27
  after do
25
- Excel.close_all
28
+ #Excel.close_all
29
+ Excel.kill_all
26
30
  rm_tmp(@dir)
27
31
  end
28
32
 
@@ -51,37 +55,147 @@ module RobustExcelOle
51
55
  creation_ok?
52
56
  end
53
57
 
58
+ it "should work with 'reuse' " do
59
+ excel = Excel.create
60
+ @excel = Excel.new(:reuse => true)
61
+ creation_ok?
62
+ @excel.should === excel
63
+ @excel = Excel.current
64
+ creation_ok?
65
+ @excel.should === excel
66
+ end
67
+
54
68
  it "should work with 'create' " do
69
+ excel = Excel.create
70
+ @excel = Excel.new(:reuse => false)
71
+ creation_ok?
72
+ @excel.should_not == excel
55
73
  @excel = Excel.create
56
74
  creation_ok?
75
+ @excel.should_not == excel
57
76
  end
58
77
 
78
+ it "should work with reusing (uplifting) an Excel given as WIN32Ole object" do
79
+ excel0 = Excel.create
80
+ book = Book.open(@simple_file)
81
+ excel = book.excel
82
+ win32ole_excel = WIN32OLE.connect(book.workbook.Fullname).Application
83
+ @excel = Excel.new(:reuse => win32ole_excel)
84
+ creation_ok?
85
+ @excel.should be_a Excel
86
+ @excel.should be_alive
87
+ @excel.should == excel
88
+ end
59
89
  end
60
90
 
61
- context "with existing excel" do
91
+ context "with identity transparence" do
62
92
 
63
93
  before do
64
94
  @excel1 = Excel.create
65
95
  end
66
96
 
67
- it "should create different excel" do
97
+ it "should create different Excel instances" do
68
98
  excel2 = Excel.create
99
+ excel2.should_not == @excel1
69
100
  excel2.Hwnd.should_not == @excel1.Hwnd
70
101
  end
71
102
 
72
- it "should reuse existing excel" do
103
+ it "should reuse the existing Excel instances" do
73
104
  excel2 = Excel.current
105
+ excel2.should === @excel1
74
106
  excel2.Hwnd.should == @excel1.Hwnd
75
107
  end
76
108
 
77
- it "should reuse existing excel with default options for 'new'" do
109
+ it "should reuse existing Excel instance with default options for 'new'" do
78
110
  excel2 = Excel.new
79
- excel2.should be_a Excel
111
+ excel2.should === @excel1
80
112
  excel2.Hwnd.should == @excel1.Hwnd
81
113
  end
82
114
 
115
+ it "should yield the same Excel instances for the same Excel objects" do
116
+ excel2 = @excel1
117
+ excel2.Hwnd.should == @excel1.Hwnd
118
+ excel2.should === @excel1
119
+ end
120
+ end
121
+
122
+ context "excel_processes" do
123
+
124
+ before do
125
+ @excel1 = Excel.create
126
+ @excel2 = Excel.create
127
+ end
128
+
129
+ it "should yield Excel objects" do
130
+ excels = Excel.excel_processes
131
+ excels[0].should == @excel1
132
+ excels[1].should == @excel2
133
+ end
134
+
135
+ end
136
+
137
+ context "kill Excel processes hard" do
138
+
139
+ before do
140
+ @excel1 = Excel.create
141
+ @excel2 = Excel.create
142
+ end
143
+
144
+ it "should kill Excel processes" do
145
+ Excel.kill_all
146
+ @excel1.alive?.should be_false
147
+ @excel2.alive?.should be_false
148
+ end
149
+
83
150
  end
84
151
 
152
+ context "with recreating Excel instances" do
153
+
154
+ it "should recreate a single Excel instance" do
155
+ book = Book.open(@simple_file)
156
+ excel = book.excel
157
+ excel.close
158
+ excel.should_not be_alive
159
+ excel.recreate
160
+ excel.should be_a Excel
161
+ excel.should be_alive
162
+ book.should be_alive
163
+ excel.close
164
+ excel.should_not be_alive
165
+ end
166
+
167
+ it "should recreate several Excel instances" do
168
+ book = Book.open(@simple_file)
169
+ book2 = Book.open(@another_simple_file, :force_excel => book)
170
+ book3 = Book.open(@different_file, :force_excel => :new)
171
+ excel = book.excel
172
+ excel3 = book3.excel
173
+ excel.close
174
+ excel3.close
175
+ excel.should_not be_alive
176
+ excel3.should_not be_alive
177
+ excel.recreate(:visible => true)
178
+ excel.should be_alive
179
+ excel.should be_a Excel
180
+ excel.visible.should be_true
181
+ excel.displayalerts.should be_false
182
+ book.should be_alive
183
+ book2.should be_alive
184
+ book.excel.should == excel
185
+ book2.excel.should == excel
186
+ excel3.recreate(:visible => true, :displayalerts => true)
187
+ excel3.should be_alive
188
+ excel3.should be_a Excel
189
+ excel3.visible.should be_true
190
+ excel3.displayalerts.should be_true
191
+ book3.should be_alive
192
+ excel.close
193
+ excel.should_not be_alive
194
+ excel3.close
195
+ excel3.should_not be_alive
196
+ end
197
+ end
198
+
85
199
  context "close excel instances" do
86
200
  def direct_excel_creation_helper # :nodoc: #
87
201
  expect { WIN32OLE.connect("Excel.Application") }.to raise_error
@@ -102,50 +216,162 @@ module RobustExcelOle
102
216
  end
103
217
  end
104
218
 
105
- =begin
106
- # testing private methods
107
- context "close_excel" do
219
+ describe "close_all" do
220
+
221
+ context "with saved workbooks" do
108
222
 
109
223
  before do
110
- @book = Book.open(@simple_file, :visible => true)
111
- @excel = @book.excel
112
- @book2 = Book.open(@simple_file, :force_excel => :new, :visible => true)
113
- @excel2 = @book2.excel
224
+ book = Book.open(@simple_file)
225
+ book2 = Book.open(@simple_file, :force_excel => :new)
226
+ @excel = book.excel
227
+ @excel2 = book2.excel
114
228
  end
115
229
 
116
- it "should close one Excel" do
230
+ it "should close the Excel instances" do
117
231
  @excel.should be_alive
118
232
  @excel2.should be_alive
119
- @book.should be_alive
120
- @book2.should be_alive
121
- @excel.close_excel(:hard => false)
233
+ Excel.close_all
122
234
  @excel.should_not be_alive
123
- @book.should_not be_alive
124
- @excel2.should be_alive
125
- @book2.should be_alive
235
+ @excel2.should_not be_alive
126
236
  end
127
237
  end
128
238
 
129
- =end
239
+ context "with unsaved_workbooks" do
240
+
241
+ before do
242
+ book1 = Book.open(@simple_file, :read_only => true)
243
+ book2 = Book.open(@simple_file, :force_excel => :new)
244
+ book3 = Book.open(@another_simple_file, :force_excel => book2.excel)
245
+ book4 = Book.open(@different_file, :force_excel => :new)
246
+ @excel1 = book1.excel
247
+ @excel2 = book2.excel
248
+ @excel4 = book4.excel
249
+ sheet2 = book2[0]
250
+ @old_cell_value2 = sheet2[1,1].value
251
+ sheet2[1,1] = sheet2[1,1].value == "foo" ? "bar" : "foo"
252
+ sheet3 = book3[0]
253
+ @old_cell_value3 = sheet3[1,1].value
254
+ sheet3[1,1] = sheet3[1,1].value == "foo" ? "bar" : "foo"
255
+ end
256
+
257
+ it "should close the first Excel without unsaved workbooks and then raise an error" do
258
+ expect{
259
+ Excel.close_all
260
+ }.to raise_error(ExcelErrorClose, "Excel contains unsaved workbooks")
261
+ @excel1.should_not be_alive
262
+ end
130
263
 
131
- describe "close_all" do
264
+ it "should close the Excel instances without saving the unsaved workbooks" do
265
+ Excel.close_all(:if_unsaved => :forget)
266
+ @excel1.should_not be_alive
267
+ @excel2.should_not be_alive
268
+ @excel4.should_not be_alive
269
+ new_book2 = Book.open(@simple_file)
270
+ new_sheet2 = new_book2[0]
271
+ new_sheet2[1,1].value.should == @old_cell_value2
272
+ new_book2.close
273
+ new_book3 = Book.open(@another_simple_file)
274
+ new_sheet3 = new_book3[0]
275
+ new_sheet3[1,1].value.should == @old_cell_value3
276
+ new_book3.close
277
+ end
132
278
 
133
- context "with saved workbooks" do
279
+ it "should close the Excel instances with saving the unsaved workbooks" do
280
+ Excel.close_all(:if_unsaved => :save)
281
+ @excel1.should_not be_alive
282
+ @excel2.should_not be_alive
283
+ @excel4.should_not be_alive
284
+ new_book2 = Book.open(@simple_file)
285
+ new_sheet2 = new_book2[0]
286
+ new_sheet2[1,1].value.should_not == @old_cell_value2
287
+ new_book2.close
288
+ new_book3 = Book.open(@another_simple_file)
289
+ new_sheet3 = new_book3[0]
290
+ new_sheet3[1,1].value.should_not == @old_cell_value3
291
+ new_book3.close
292
+ end
293
+
294
+ it "should raise an error for invalid option" do
295
+ expect {
296
+ Excel.close_all(:if_unsaved => :invalid_option)
297
+ }.to raise_error(ExcelErrorClose, ":if_unsaved: invalid option: :invalid_option")
298
+ end
299
+
300
+ it "should raise an error by default" do
301
+ expect{
302
+ Excel.close_all
303
+ }.to raise_error(ExcelErrorClose, "Excel contains unsaved workbooks")
304
+ @excel1.should_not be_alive
305
+ end
306
+ end
134
307
 
308
+ context "with :if_unsaved => :alert" do
135
309
  before do
136
- book = Book.open(@simple_file, :visible => true)
137
- book2 = Book.open(@simple_file, :force_excel => :new, :visible => true)
138
- @excel = book.excel
310
+ @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
311
+ #book1 = Book.open(@simple_file, :read_only => true)
312
+ book2 = Book.open(@simple_file, :force_excel => :new)
313
+ #book3 = Book.open(@another_simple_file, :force_excel => book2.excel)
314
+ #book4 = Book.open(@different_file, :force_excel => :new)
315
+ #@excel1 = book1.excel
139
316
  @excel2 = book2.excel
317
+ #@excel4 = book4.excel
318
+ sheet2 = book2[0]
319
+ @old_cell_value2 = sheet2[1,1].value
320
+ sheet2[1,1] = sheet2[1,1].value == "foo" ? "bar" : "foo"
321
+ #sheet3 = book3[0]
322
+ #@old_cell_value3 = sheet3[1,1].value
323
+ #sheet3[1,1] = sheet3[1,1].value == "foo" ? "bar" : "foo"
140
324
  end
141
325
 
142
- it "should close the Excel instances" do
143
- @excel.should be_alive
144
- @excel2.should be_alive
145
- Excel.close_all
146
- @excel.should_not be_alive
326
+ after do
327
+ @key_sender.close
328
+ end
329
+
330
+
331
+ it "should save if user answers 'yes'" do
332
+ @key_sender.puts "{enter}"
333
+ Excel.close_all(:if_unsaved => :alert)
334
+ #@excel1.should_not be_alive
147
335
  @excel2.should_not be_alive
336
+ #@excel4.should_not be_alive
337
+ #@excel5.should_not be_alive
338
+ new_book2 = Book.open(@simple_file)
339
+ new_sheet2 = new_book2[0]
340
+ new_sheet2[1,1].value.should_not == @old_cell_value2
341
+ new_book2.close
342
+ #new_book3 = Book.open(@another_simple_file)
343
+ #new_sheet3 = new_book3[0]
344
+ #new_sheet3[1,1].value.should == @old_cell_value3
345
+ #new_book3.close
148
346
  end
347
+
348
+ it "should not save if user answers 'no'" do
349
+ @key_sender.puts "{right}{enter}"
350
+ @key_sender.puts "{right}{enter}"
351
+ Excel.close_all(:if_unsaved => :alert)
352
+ #@excel1.should_not be_alive
353
+ @excel2.should_not be_alive
354
+ #@excel4.should_not be_alive
355
+ #@excel5.should_not be_alive
356
+ new_book2 = Book.open(@simple_file)
357
+ new_sheet2 = new_book2[0]
358
+ new_sheet2[1,1].value.should == @old_cell_value2
359
+ new_book2.close
360
+ #new_book4 = Book.open(@different_file)
361
+ #new_sheet4 = new_book4[0]
362
+ #new_sheet4[1,1].value.should_not == @old_cell_value4
363
+ #new_book4.close
364
+ end
365
+
366
+ # it "should not save if user answers 'cancel'" do
367
+ # @key_sender.puts "{left}{enter}"
368
+ # @key_sender.puts "{left}{enter}"
369
+ # @key_sender.puts "{left}{enter}"
370
+ # @key_sender.puts "{left}{enter}"
371
+ # expect{
372
+ # Excel.close_all(:if_unsaved => :alert)
373
+ # }.to raise_error(ExcelUserCanceled, "close: canceled by user")
374
+ # end
149
375
  end
150
376
  end
151
377
 
@@ -167,7 +393,7 @@ module RobustExcelOle
167
393
  end
168
394
  end
169
395
 
170
- context "with an unsaved workbook" do
396
+ context "with unsaved workbooks" do
171
397
 
172
398
  before do
173
399
  @excel = Excel.create
@@ -175,82 +401,155 @@ module RobustExcelOle
175
401
  sheet = @book[0]
176
402
  @old_cell_value = sheet[1,1].value
177
403
  sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
404
+ @book2 = Book.open(@another_simple_file)
405
+ sheet2 = @book2[0]
406
+ @old_cell_value2 = sheet2[1,1].value
407
+ sheet2[1,1] = sheet2[1,1].value == "foo" ? "bar" : "foo"
178
408
  end
179
409
 
180
410
  it "should raise an error" do
181
411
  @excel.should be_alive
182
412
  @book.should be_alive
183
413
  @book.saved.should be_false
414
+ @book2.should be_alive
415
+ @book2.saved.should be_false
184
416
  expect{
185
417
  @excel.close(:if_unsaved => :raise)
186
418
  }.to raise_error(ExcelErrorClose, "Excel contains unsaved workbooks")
187
- @excel.should be_alive
188
- @book.should be_alive
189
419
  end
190
420
 
191
421
  it "should close the Excel without saving the workbook" do
192
422
  @excel.should be_alive
193
- @book.should be_alive
194
- @book.saved.should be_false
195
423
  @excel.close(:if_unsaved => :forget)
196
424
  @excel.should_not be_alive
197
- @book.should_not be_alive
198
425
  new_book = Book.open(@simple_file)
199
426
  new_sheet = new_book[0]
200
427
  new_sheet[1,1].value.should == @old_cell_value
201
428
  new_book.close
429
+ new_book2 = Book.open(@another_simple_file)
430
+ new_sheet2 = new_book2[0]
431
+ new_sheet2[1,1].value.should == @old_cell_value2
432
+ new_book2.close
202
433
  end
203
434
 
204
435
  it "should close the Excel with saving the workbook" do
205
436
  @excel.should be_alive
206
- @book.should be_alive
207
- @book.saved.should be_false
208
437
  @excel.close(:if_unsaved => :save)
209
438
  @excel.should_not be_alive
210
- @book.should_not be_alive
211
439
  new_book = Book.open(@simple_file)
212
440
  new_sheet = new_book[0]
213
441
  new_sheet[1,1].value.should_not == @old_cell_value
214
442
  new_book.close
443
+ new_book2 = Book.open(@another_simple_file)
444
+ new_sheet2 = new_book2[0]
445
+ new_sheet2[1,1].value.should_not == @old_cell_value2
446
+ new_book2.close
215
447
  end
216
448
 
217
449
  it "should raise an error for invalid option" do
218
450
  expect {
219
451
  @excel.close(:if_unsaved => :invalid_option)
220
- }.to raise_error(ExcelErrorClose, ":if_unsaved: invalid option: invalid_option")
452
+ }.to raise_error(ExcelErrorClose, ":if_unsaved: invalid option: :invalid_option")
221
453
  end
222
454
 
223
455
  it "should raise an error by default" do
224
456
  @excel.should be_alive
225
- @book.should be_alive
226
- @book.saved.should be_false
227
457
  expect{
228
458
  @excel.close
229
459
  }.to raise_error(ExcelErrorClose, "Excel contains unsaved workbooks")
460
+ end
461
+
462
+ it "should close the Excel without saving the workbook hard" do
230
463
  @excel.should be_alive
231
464
  @book.should be_alive
465
+ @book.saved.should be_false
466
+ @excel.close(:if_unsaved => :forget, :hard => true)
467
+ @excel.should_not be_alive
468
+ @book.should_not be_alive
469
+ new_book = Book.open(@simple_file)
470
+ new_sheet = new_book[0]
471
+ new_sheet[1,1].value.should == @old_cell_value
472
+ new_book.close
473
+ new_book.excel.close(:hard => true)
474
+ procs = WIN32OLE.connect("winmgmts:\\\\.")
475
+ processes = procs.InstancesOf("win32_process")
476
+ result = []
477
+ processes.each do |p|
478
+ result << p if p.name == "EXCEL.EXE"
479
+ end
480
+ result.should be_empty
481
+ end
482
+ end
483
+
484
+ context "with :if_unsaved => :alert" do
485
+
486
+ before do
487
+ @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
488
+ @excel = Excel.create
489
+ @book = Book.open(@simple_file)
490
+ sheet = @book[0]
491
+ @old_cell_value = sheet[1,1].value
492
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
493
+ end
494
+
495
+ after do
496
+ @key_sender.close
497
+ end
498
+
499
+ it "should save if user answers 'yes'" do
500
+ # "Yes" is to the left of "No", which is the default. --> language independent
501
+ @excel.should be_alive
502
+ @key_sender.puts "{enter}"
503
+ @excel.close(:if_unsaved => :alert)
504
+ @excel.should_not be_alive
505
+ new_book = Book.open(@simple_file)
506
+ new_sheet = new_book[0]
507
+ new_sheet[1,1].value.should_not == @old_cell_value
508
+ new_book.close
509
+ end
510
+
511
+ it "should not save if user answers 'no'" do
512
+ @excel.should be_alive
513
+ @book.should be_alive
514
+ @book.saved.should be_false
515
+ @key_sender.puts "{right}{enter}"
516
+ @excel.close(:if_unsaved => :alert)
517
+ @excel.should_not be_alive
518
+ @book.should_not be_alive
519
+ new_book = Book.open(@simple_file)
520
+ new_sheet = new_book[0]
521
+ new_sheet[1,1].value.should == @old_cell_value
522
+ new_book.close
523
+ end
524
+
525
+ it "should not save if user answers 'cancel'" do
526
+ # strangely, in the "cancel" case, the question will sometimes be repeated twice
527
+ @excel.should be_alive
528
+ @book.should be_alive
529
+ @book.saved.should be_false
530
+ @key_sender.puts "{left}{enter}"
531
+ @key_sender.puts "{left}{enter}"
532
+ expect{
533
+ @excel.close(:if_unsaved => :alert)
534
+ }.to raise_error(ExcelUserCanceled, "close: canceled by user")
232
535
  end
233
536
  end
234
537
  end
235
538
 
236
- # context "with :if_unsaved => :alert" do
237
- # before do
238
- # @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
239
- # end
240
- #
241
- # after do
242
- # @key_sender.close
243
- # end
244
- #
245
- # possible_answers = [:yes, :no, :cancel]
246
- # possible_answers.each_with_index do |answer, position|
247
- # it "should" + (answer == :yes ? "" : " not") + " the unsaved book and" + (answer == :cancel ? " not" : "") + " close it" + "if user answers '#{answer}'" do
248
- # # "Yes" is the default. "No" is right of "Yes", "Cancel" is right of "No" --> language independent
249
- # @key_sender.puts "{right}" * position + "{enter}"
250
- # end
251
- # end
252
- # end
539
+ describe "alive" do
253
540
 
541
+ it "should yield alive" do
542
+ excel = Excel.create
543
+ excel.alive?.should be_true
544
+ end
545
+
546
+ it "should yield not alive" do
547
+ excel = Excel.create
548
+ excel.close
549
+ excel.alive?.should be_false
550
+ end
551
+
552
+ end
254
553
 
255
554
  describe "==" do
256
555
  before do
@@ -365,12 +664,18 @@ module RobustExcelOle
365
664
  end
366
665
 
367
666
  it "should raise WIN32OLERuntimeError" do
368
- expect{ @excel1.NonexistingMethod }.to raise_error(VBAMethodMissingError)
667
+ expect{ @excel1.NonexistingMethod }.to raise_error(VBAMethodMissingError, /unknown VBA property or method :NonexistingMethod/)
369
668
  end
370
669
 
371
670
  it "should raise NoMethodError for uncapitalized methods" do
372
671
  expect{ @excel1.nonexisting_method }.to raise_error(NoMethodError)
373
672
  end
673
+
674
+ it "should report that Excel is not alive" do
675
+ @excel1.close
676
+ expect{ @excel1.Nonexisting_method }.to raise_error(ExcelError, "method missing: Excel not alive")
677
+ end
678
+
374
679
  end
375
680
 
376
681
  context "with hwnd and hwnd2excel" do
@@ -485,31 +790,6 @@ module RobustExcelOle
485
790
  end
486
791
  end
487
792
  end
488
-
489
- describe "RobustExcelOle" do
490
- context "#absolute_path" do
491
- before do
492
- @previous_dir = Dir.getwd
493
- end
494
-
495
- after do
496
- Dir.chdir @previous_dir
497
- end
498
-
499
- it "should work" do
500
- RobustExcelOle::absolute_path("C:/abc").should == "C:\\abc"
501
- RobustExcelOle::absolute_path("C:\\abc").should == "C:\\abc"
502
- Dir.chdir "C:/windows"
503
- RobustExcelOle::absolute_path("C:abc").downcase.should == Dir.pwd.gsub("/","\\").downcase + "\\abc"
504
- RobustExcelOle::absolute_path("C:abc").upcase.should == File.expand_path("abc").gsub("/","\\").upcase
505
- end
506
-
507
- it "should return right absolute path name" do
508
- @filename = 'C:/Dokumente und Einstellungen/Zauberthomas/Eigene Dateien/robust_excel_ole/spec/book_spec.rb'
509
- RobustExcelOle::absolute_path(@filename).gsub("\\","/").should == @filename
510
- end
511
- end
512
- end
513
793
  end
514
794
 
515
795
  class TestError < RuntimeError # :nodoc: #