esumitra-test_off 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/features/data/DocWithMacros.doc +0 -0
- data/features/data/DocWithoutMacros.doc +0 -0
- data/features/step_definitions/word_app_steps.rb +121 -0
- data/features/step_definitions/word_doc_steps.rb +67 -0
- data/features/word_app.feature +34 -0
- data/features/word_doc.feature +30 -0
- data/lib/msword.rb +79 -0
- data/test/rspec/document_spec.rb +52 -0
- data/test/rspec/word_spec.rb +59 -0
- metadata +61 -0
Binary file
|
Binary file
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../lib/msword.rb'
|
2
|
+
|
3
|
+
include MSWordUtils
|
4
|
+
|
5
|
+
#-----------------------------------------
|
6
|
+
# Scenario: Instantiate a word application
|
7
|
+
#-----------------------------------------
|
8
|
+
Given /^I have Word installed$/ do
|
9
|
+
@wdApp = Word.new
|
10
|
+
end
|
11
|
+
|
12
|
+
When /^I ask for the word version$/ do
|
13
|
+
@version = @wdApp.Version
|
14
|
+
end
|
15
|
+
|
16
|
+
Then /^I should get back the word version$/ do
|
17
|
+
@version.should_not == nil
|
18
|
+
@wdApp.Quit(0)
|
19
|
+
end
|
20
|
+
#-----------------------------------------
|
21
|
+
# Scenario: Get correct template count for word application
|
22
|
+
#-----------------------------------------
|
23
|
+
Given /^a new Word object$/ do
|
24
|
+
@wdApp = Word.new
|
25
|
+
end
|
26
|
+
|
27
|
+
When /^I ask for a multiple level member$/ do
|
28
|
+
@tCount = @wdApp.Templates.Count
|
29
|
+
end
|
30
|
+
|
31
|
+
Then /^I should get back the correct member value$/ do
|
32
|
+
@tCount.should == 2
|
33
|
+
@wdApp.Quit(0)
|
34
|
+
end
|
35
|
+
#-----------------------------------------
|
36
|
+
# Scenario: Call method with arguments
|
37
|
+
#-----------------------------------------
|
38
|
+
When /^I call a method with an argument$/ do
|
39
|
+
@points = @wdApp.CentimetersToPoints(1)
|
40
|
+
end
|
41
|
+
|
42
|
+
Then /^I should get the correct method value$/ do
|
43
|
+
@points.should be_close(28.34646,0.0001)
|
44
|
+
@wdApp.Quit(0)
|
45
|
+
end
|
46
|
+
#-----------------------------------------
|
47
|
+
# Scenario: Call static method with a code block
|
48
|
+
#-----------------------------------------
|
49
|
+
Given /^nothing$/ do
|
50
|
+
end
|
51
|
+
|
52
|
+
When /^I call the Word.open method with a block$/ do
|
53
|
+
@version = Word.open{|app| app.Version}
|
54
|
+
end
|
55
|
+
|
56
|
+
Then /^I should get the correct result from the block$/ do
|
57
|
+
@version.should eql("11.0")
|
58
|
+
end
|
59
|
+
#-----------------------------------------
|
60
|
+
# Scenario: Call a function with no arguments in a doc
|
61
|
+
#-----------------------------------------
|
62
|
+
When /^I pass a block to execute macro (.+) in document at (.+)$/ do |macro_name, filename|
|
63
|
+
@result = Word.open{ |app|
|
64
|
+
filepath = File.expand_path(File.dirname(__FILE__) + '/' + filename)
|
65
|
+
macroDoc = app.Documents.Open(filepath)
|
66
|
+
rs = app.Run(macro_name)
|
67
|
+
macroDoc.Close(0)
|
68
|
+
rs
|
69
|
+
}
|
70
|
+
end
|
71
|
+
|
72
|
+
Then /^I should get the no arg return value (.+)$/ do |value|
|
73
|
+
@result.to_s.should eql(value)
|
74
|
+
end
|
75
|
+
#-----------------------------------------
|
76
|
+
# Scenario: Call a function with multiple arguments in a doc
|
77
|
+
#-----------------------------------------
|
78
|
+
When /^I pass a block to execute macro (.+) with arguments (.+) and (.+) in document at (.+)$/ do |macro_name, arg1,arg2, filename|
|
79
|
+
@result = Word.open{ |app|
|
80
|
+
filepath = File.expand_path(File.dirname(__FILE__) + '/' + filename)
|
81
|
+
macroDoc = app.Documents.Open(filepath)
|
82
|
+
rs = app.Run(macro_name,arg1,arg2)
|
83
|
+
macroDoc.Close(0)
|
84
|
+
rs
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
Then /^I should get the multiple args return value (.+)$/ do |value|
|
89
|
+
@result.should eql(value)
|
90
|
+
end
|
91
|
+
#-----------------------------------------
|
92
|
+
# Scenario: Open an existing document
|
93
|
+
#-----------------------------------------
|
94
|
+
Given /^I open an existing document at (.+)$/ do |filename|
|
95
|
+
@doc = Document.new(filename)
|
96
|
+
end
|
97
|
+
|
98
|
+
When /^I read the document text$/ do
|
99
|
+
@text = @doc.Range.Text
|
100
|
+
end
|
101
|
+
|
102
|
+
Then /^I should be able to write the text$/ do
|
103
|
+
!@text.nil?
|
104
|
+
@doc.close
|
105
|
+
end
|
106
|
+
#-----------------------------------------
|
107
|
+
# Scenario: Execute a simple macro in a document
|
108
|
+
#-----------------------------------------
|
109
|
+
Given /^I open a document with a simple macro at (.+)$/ do |filename|
|
110
|
+
@doc = Document.new(filename)
|
111
|
+
end
|
112
|
+
|
113
|
+
When /^I call the simple macro named (.+)$/ do |macro_name|
|
114
|
+
puts "#{@doc.Name}!#{macro_name}"
|
115
|
+
@fResp = @doc.Application.Run("#{@doc.Name}!#{macro_name}")
|
116
|
+
end
|
117
|
+
|
118
|
+
Then /^the simple macro should return truet$/ do
|
119
|
+
@doc.close
|
120
|
+
@fResp
|
121
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../lib/msword.rb'
|
2
|
+
|
3
|
+
include MSWordUtils
|
4
|
+
|
5
|
+
NormalDocPath = File.expand_path(File.dirname(__FILE__) + "/../data/DocWithoutMacros.doc")
|
6
|
+
MacroDocPath= File.expand_path(File.dirname(__FILE__) + "/../data/DocWithMacros.doc")
|
7
|
+
NewDocPath=File.expand_path(File.dirname(__FILE__) + "/../data")
|
8
|
+
|
9
|
+
Given /^an existing word document$/ do
|
10
|
+
File.exists?(NormalDocPath).should be_true
|
11
|
+
end
|
12
|
+
|
13
|
+
When /^I open the existing document$/ do
|
14
|
+
@doc = Document.new(NormalDocPath)
|
15
|
+
end
|
16
|
+
|
17
|
+
Then /^I should get the text successfully$/ do
|
18
|
+
@doc.Range.Text.size.should_not equal(0)
|
19
|
+
@doc.close
|
20
|
+
end
|
21
|
+
|
22
|
+
Given /^a random document name$/ do
|
23
|
+
@new_file_path = "#{NewDocPath}/#{Time.now.to_i}.doc"
|
24
|
+
end
|
25
|
+
|
26
|
+
When /^I create a new document$/ do
|
27
|
+
@doc = Document.new
|
28
|
+
end
|
29
|
+
|
30
|
+
Then /^I should be able to save the new document correctly$/ do
|
31
|
+
@doc.SaveAs(@new_file_path)
|
32
|
+
@doc.close
|
33
|
+
File.exists?(@new_file_path).should be_true
|
34
|
+
File.delete(@new_file_path)
|
35
|
+
end
|
36
|
+
|
37
|
+
When /^I pass a code block to get the paragraph count$/ do
|
38
|
+
Document.open(NormalDocPath) { |doc|
|
39
|
+
@pars = doc.Paragraphs.Count
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
Then /^I should get the paragraph count correctly$/ do
|
44
|
+
@pars.should_not equal(0)
|
45
|
+
end
|
46
|
+
|
47
|
+
Given /^an existing document with macros$/ do
|
48
|
+
@doc = Document.new(MacroDocPath)
|
49
|
+
end
|
50
|
+
|
51
|
+
When /^I try to execute a simple macro$/ do
|
52
|
+
@macro_result = @doc.macro_SimpleMacroNoParams
|
53
|
+
end
|
54
|
+
|
55
|
+
Then /^I should get the correct result from the simple macro$/ do
|
56
|
+
@macro_result.should be_true
|
57
|
+
@doc.close
|
58
|
+
end
|
59
|
+
|
60
|
+
When /^I try to execute a document macro with arguments$/ do
|
61
|
+
@macro_result = @doc.macro_SimpleMacroMultipleParams("string1","string2")
|
62
|
+
end
|
63
|
+
|
64
|
+
Then /^I should get the correct result from the document macro with arguments$/ do
|
65
|
+
@macro_result.should == "string1+string2"
|
66
|
+
@doc.close
|
67
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
@word
|
2
|
+
Feature: Word Application Feature
|
3
|
+
Provide easy interface to the
|
4
|
+
"Word.Application" COM object
|
5
|
+
|
6
|
+
Scenario: Instantiate a word application
|
7
|
+
Given I have Word installed
|
8
|
+
When I ask for the word version
|
9
|
+
Then I should get back the word version
|
10
|
+
|
11
|
+
Scenario: Call application multiple level member method without arguments
|
12
|
+
Given a new Word object
|
13
|
+
When I ask for a multiple level member
|
14
|
+
Then I should get back the correct member value
|
15
|
+
|
16
|
+
Scenario: Call application method with arguments
|
17
|
+
Given a new Word object
|
18
|
+
When I call a method with an argument
|
19
|
+
Then I should get the correct method value
|
20
|
+
|
21
|
+
Scenario: Call application static method with a code block
|
22
|
+
Given nothing
|
23
|
+
When I call the Word.open method with a block
|
24
|
+
Then I should get the correct result from the block
|
25
|
+
|
26
|
+
Scenario: Use application to call function with no arguments
|
27
|
+
Given nothing
|
28
|
+
When I pass a block to execute macro Module1.SimpleMacroNoParams in document at ../data/DocWithMacros.doc
|
29
|
+
Then I should get the no arg return value true
|
30
|
+
|
31
|
+
Scenario: Use application to call a function with multiple arguments
|
32
|
+
Given nothing
|
33
|
+
When I pass a block to execute macro Module1.SimpleMacroMultipleParams with arguments string1 and string2 in document at ../data/DocWithMacros.doc
|
34
|
+
Then I should get the multiple args return value string1+string2
|
@@ -0,0 +1,30 @@
|
|
1
|
+
@document
|
2
|
+
Feature:
|
3
|
+
Provide easy interface to the
|
4
|
+
"Word.Document" COM object
|
5
|
+
|
6
|
+
Scenario: Open existing document
|
7
|
+
Given an existing word document
|
8
|
+
When I open the existing document
|
9
|
+
Then I should get the text successfully
|
10
|
+
|
11
|
+
Scenario: Create new document
|
12
|
+
Given a random document name
|
13
|
+
When I create a new document
|
14
|
+
Then I should be able to save the new document correctly
|
15
|
+
|
16
|
+
Scenario: Open existing document and execute code block
|
17
|
+
Given nothing
|
18
|
+
When I pass a code block to get the paragraph count
|
19
|
+
Then I should get the paragraph count correctly
|
20
|
+
|
21
|
+
Scenario: Open existing document and run macro
|
22
|
+
Given an existing document with macros
|
23
|
+
When I try to execute a simple macro
|
24
|
+
Then I should get the correct result from the simple macro
|
25
|
+
|
26
|
+
Scenario: Open existing document and run a macro with arguments
|
27
|
+
Given an existing document with macros
|
28
|
+
When I try to execute a document macro with arguments
|
29
|
+
Then I should get the correct result from the document macro with arguments
|
30
|
+
|
data/lib/msword.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# word utilities
|
2
|
+
|
3
|
+
require 'win32ole'
|
4
|
+
require 'Win32API'
|
5
|
+
|
6
|
+
module MSWordUtils
|
7
|
+
class WdSaveOptions
|
8
|
+
WdDoNotSaveChanges = 0
|
9
|
+
WdSaveChanges = -1
|
10
|
+
end
|
11
|
+
|
12
|
+
# Document
|
13
|
+
# create a new document, if path specified, opens the document at the path
|
14
|
+
# if no path specified, creates a new document
|
15
|
+
# options :read_only => ReadOnly, :visible => Visible
|
16
|
+
class Document
|
17
|
+
def initialize(path = nil,opts = {})
|
18
|
+
@app = WIN32OLE.new('Word.Application')
|
19
|
+
read_opt = opts[:read_only]?true:false
|
20
|
+
if path.nil?
|
21
|
+
@doc = @app.Documents.Add
|
22
|
+
else
|
23
|
+
@doc = @app.Documents.Open(path,true,read_opt)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def close(saveDoc = false)
|
28
|
+
saveOpt = (saveDoc)?(WdSaveOptions::WdSaveChanges):(WdSaveOptions::WdDoNotSaveChanges)
|
29
|
+
@doc.Close(saveOpt)
|
30
|
+
@app.Quit(WdSaveOptions::WdDoNotSaveChanges)
|
31
|
+
end
|
32
|
+
|
33
|
+
def method_missing(msg,*args)
|
34
|
+
if ((msg.id2name=~/^macro_/) == 0)
|
35
|
+
macro_name = $'
|
36
|
+
@app.Run(macro_name,*args)
|
37
|
+
else
|
38
|
+
@doc.send(msg,*args)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.open(path,read_only=true)
|
43
|
+
app = WIN32OLE.new('Word.Application')
|
44
|
+
doc = app.Documents.Open(path,true,read_only)
|
45
|
+
begin
|
46
|
+
yield(doc) if block_given?
|
47
|
+
rescue => e
|
48
|
+
puts e.message
|
49
|
+
ensure
|
50
|
+
save_flag=(read_only)?(WdSaveOptions::WdDoNotSaveChanges):(WdSaveOptions::WdSaveChanges)
|
51
|
+
doc.Close(save_flag)
|
52
|
+
app.Quit(WdSaveOptions::WdDoNotSaveChanges)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
# Application
|
59
|
+
class Word
|
60
|
+
def initialize
|
61
|
+
@app = WIN32OLE.new('Word.Application')
|
62
|
+
end
|
63
|
+
|
64
|
+
def method_missing(msg,*args)
|
65
|
+
@app.send(msg,*args)
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.open()
|
69
|
+
app = WIN32OLE.new('Word.Application')
|
70
|
+
begin
|
71
|
+
yield(app) if block_given?
|
72
|
+
rescue => e
|
73
|
+
puts e.message
|
74
|
+
ensure
|
75
|
+
app.Quit(WdSaveOptions::WdDoNotSaveChanges)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# rspec tests for document class
|
2
|
+
# run with 'spec -c -f specdoc document_spec.rb' for BDD process
|
3
|
+
# run with 'rake rspec_tests' to run as standard process'
|
4
|
+
|
5
|
+
require File.dirname(__FILE__) + '/../../lib/msword.rb'
|
6
|
+
|
7
|
+
include MSWordUtils
|
8
|
+
|
9
|
+
describe Document do
|
10
|
+
NewDocPath=File.expand_path(File.dirname(__FILE__) + "/../../features/data")
|
11
|
+
NormalDocPath = NewDocPath + '/DocWithoutMacros.doc'
|
12
|
+
MacroDocPath = NewDocPath + '/DocWithMacros.doc'
|
13
|
+
|
14
|
+
it "should open an existing document" do
|
15
|
+
doc = Document.new(NormalDocPath)
|
16
|
+
doc.Range.Text.size.should_not equal(0)
|
17
|
+
doc.close
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should open a new document" do
|
21
|
+
new_file_path = "#{NewDocPath}/#{Time.now.to_i}.doc"
|
22
|
+
doc = Document.new
|
23
|
+
doc.SaveAs(new_file_path)
|
24
|
+
doc.close
|
25
|
+
File.exists?(new_file_path).should be_true
|
26
|
+
File.delete(new_file_path)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should open an existing document with a code block" do
|
30
|
+
result = Document.open(NormalDocPath) {|doc|
|
31
|
+
doc.Paragraphs.Count
|
32
|
+
}
|
33
|
+
result.should_not equal(0)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should run a macro with no arguments in an existing document" do
|
37
|
+
doc = Document.new(MacroDocPath)
|
38
|
+
result = doc.macro_SimpleMacroNoParams
|
39
|
+
result.should be_true
|
40
|
+
doc.close
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should run a macro with multiple arguments in an existing document" do
|
44
|
+
arg1='string1'
|
45
|
+
arg2='string2'
|
46
|
+
doc = Document.new(MacroDocPath)
|
47
|
+
result = doc.macro_SimpleMacroMultipleParams(arg1,arg2)
|
48
|
+
result.should == "#{arg1}+#{arg2}"
|
49
|
+
doc.close
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# rspec tests for word class
|
2
|
+
require File.dirname(__FILE__) + '/../../lib/msword.rb'
|
3
|
+
|
4
|
+
include MSWordUtils
|
5
|
+
|
6
|
+
describe Word do
|
7
|
+
DocWithMacro = File.expand_path(File.dirname(__FILE__) + "/../../features/data/DocWithMacros.doc")
|
8
|
+
Macro1 = "Module1.SimpleMacroNoParams"
|
9
|
+
Macro2 = "Module1.SimpleMacroMultipleParams"
|
10
|
+
|
11
|
+
it "should instantiate as an object" do
|
12
|
+
word = Word.new
|
13
|
+
version = word.Version
|
14
|
+
version.should == ("11.0" || "12.0")
|
15
|
+
word.Quit(0)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should return a correct multiple level member value" do
|
19
|
+
word = Word.new
|
20
|
+
templates = word.Templates.Count
|
21
|
+
word.Quit(0)
|
22
|
+
templates.should > 0
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should return the correct value for a method with an argument" do
|
26
|
+
word = Word.new
|
27
|
+
points = word.CentimetersToPoints(1)
|
28
|
+
word.Quit(0)
|
29
|
+
points.should be_close(28.34646,0.0001)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return the correct value for code block" do
|
33
|
+
version = Word.open{|app| app.Version}
|
34
|
+
version.should == ("11.0" || "12.0")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return the correct value for a macro function with no arguments" do
|
38
|
+
result = Word.open{|app|
|
39
|
+
macroDoc = app.Documents.Open(DocWithMacro)
|
40
|
+
r = app.Run(Macro1)
|
41
|
+
macroDoc.Close(0)
|
42
|
+
r
|
43
|
+
}
|
44
|
+
result.should be_true
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return the correct value for a macro function with multiple arguments" do
|
48
|
+
arg1 = 'string1'
|
49
|
+
arg2 = 'string2'
|
50
|
+
result = Word.open{|app|
|
51
|
+
macroDoc = app.Documents.Open(DocWithMacro)
|
52
|
+
r = app.Run(Macro2,arg1,arg2)
|
53
|
+
macroDoc.Close(0)
|
54
|
+
r
|
55
|
+
}
|
56
|
+
result.should == "#{arg1}+#{arg2}"
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: esumitra-test_off
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.2"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Edward Sumitra
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-13 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A library for Behavior Driven Development of VBA code
|
17
|
+
email: ed_sumitra@yahoo.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/msword.rb
|
26
|
+
- features/word_app.feature
|
27
|
+
- features/word_doc.feature
|
28
|
+
- features/data/DocWithMacros.doc
|
29
|
+
- features/data/DocWithoutMacros.doc
|
30
|
+
- features/step_definitions/word_app_steps.rb
|
31
|
+
- features/step_definitions/word_doc_steps.rb
|
32
|
+
- test/rspec/document_spec.rb
|
33
|
+
- test/rspec/word_spec.rb
|
34
|
+
has_rdoc: false
|
35
|
+
homepage: http://github.com/esumitra/test_off
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements:
|
54
|
+
- MS Office, rspec and cucumber to run tests
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.2.0
|
57
|
+
signing_key:
|
58
|
+
specification_version: 2
|
59
|
+
summary: A library for Behavior Driven Development of VBA code
|
60
|
+
test_files: []
|
61
|
+
|