spec_converter 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt ADDED
@@ -0,0 +1,6 @@
1
+ Manifest.txt
2
+ README.txt
3
+ Rakefile
4
+ bin/spec_converter
5
+ lib/spec_converter.rb
6
+ test/spec_converter_test.rb
data/README.txt ADDED
@@ -0,0 +1,47 @@
1
+ = spec_converter
2
+
3
+ == DESCRIPTION
4
+ Simple converter to go from test-unit or dust style tests to test-spec (http://rubyforge.org/projects/test-spec) specs.
5
+
6
+ == FEATURES/PROBLEMS
7
+ This will change all files in place!! Make sure you are properly backed up and/or committed to SVN before running.
8
+
9
+ == INSTALL
10
+ sudo gem install spec-converter
11
+
12
+ == USAGE
13
+ Assuming your project is at ~/work/project/:
14
+
15
+ cd ~/work/project
16
+ spec_converter
17
+
18
+ == URLS:
19
+ * test/spec: http://rubyforge.org/projects/test-spec
20
+ * trac: http://opensource.thinkrelevance.com/wiki/spec-converter
21
+ * svn trunk: https://opensource.thinkrelevance.com/svn/spec_converter/trunk
22
+ * rdoc: http://spec-converter.rubyforge.org/rdoc
23
+
24
+ == LICENSE:
25
+
26
+ (The MIT License)
27
+
28
+ Copyright (c) 2007-08 Relevance
29
+
30
+ Permission is hereby granted, free of charge, to any person obtaining
31
+ a copy of this software and associated documentation files (the
32
+ 'Software'), to deal in the Software without restriction, including
33
+ without limitation the rights to use, copy, modify, merge, publish,
34
+ distribute, sublicense, and/or sell copies of the Software, and to
35
+ permit persons to whom the Software is furnished to do so, subject to
36
+ the following conditions:
37
+
38
+ The above copyright notice and this permission notice shall be
39
+ included in all copies or substantial portions of the Software.
40
+
41
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
42
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
43
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
44
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
45
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
46
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
47
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require 'lib/spec_converter'
6
+ require 'rcov/rcovtask'
7
+
8
+ Hoe.new('SpecConverter', SpecConverter::VERSION) do |p|
9
+ p.rubyforge_name = "thinkrelevance"
10
+ p.description = "Convert your tests to test/spec specs. See http://opensource.thinkrelevance.com/wiki/spec-converter for details."
11
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
12
+ p.name = 'spec_converter'
13
+ p.remote_rdoc_dir = 'rdoc'
14
+ p.summary = "Convert your tests to test/spec specs"
15
+ p.author = "Relevance"
16
+ p.email = "opensource@thinkrelevance.com"
17
+ p.url = "http://opensource.thinkrelevance.com/wiki/spec-converter"
18
+ end
19
+
20
+ desc 'Default: run unit tests.'
21
+ task :default => :test
22
+
23
+ desc 'Default for CruiseControl'
24
+ task :cruise => ['test', 'test:flog']
25
+
26
+ desc 'Test for flog failure'
27
+ namespace :test do
28
+ task :flog do
29
+ threshold = (ENV['FLOG_THRESHOLD'] || 120).to_i
30
+ dirs = %w(lib tasks test)
31
+ result = IO.popen("flog #{dirs.join(' ')} 2>/dev/null | grep \"(\" | grep -v \"main#none\" | head -n 1").readlines.join('')
32
+ result =~ /\((.*)\)/
33
+ flog = $1.to_i
34
+ result =~ /^(.*):/
35
+ method = $1
36
+ if flog > threshold
37
+ raise "FLOG failed for #{method} with score of #{flog} (threshold is #{threshold})."
38
+ end
39
+ puts "FLOG passed, with highest score being #{flog} for #{method}."
40
+ end
41
+ end
42
+
43
+ desc 'Run all tests.'
44
+ Rake::TestTask.new(:test) do |t|
45
+ t.libs << 'lib'
46
+ t.pattern = 'test/**/*_test.rb'
47
+ t.verbose = true
48
+ end
49
+
50
+ Rcov::RcovTask.new("rcov") do |t|
51
+ rcov_output = "tmp/coverage"
52
+ FileUtils.mkdir_p rcov_output unless File.exist? rcov_output
53
+ t.libs << "lib"
54
+ t.test_files = FileList["test/**/*_test.rb"]
55
+ t.output_dir = "#{rcov_output}/"
56
+ t.verbose = true
57
+ t.rcov_opts = ["-x", "^/Library", "--sort coverage"]
58
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'spec_converter'
4
+
5
+ SpecConverter.start
@@ -0,0 +1,74 @@
1
+ # Simple converter to go to test/spec style
2
+ # This will change all files in place, so make sure you are properly backed up and/or committed to SVN!
3
+ class SpecConverter
4
+ VERSION = "0.0.3"
5
+
6
+ def self.start
7
+ spec_converter = SpecConverter.new
8
+ spec_converter.convert
9
+ end
10
+
11
+ # Convert tests from old spec style to new style -- assumes you are in your project root and globs all tests
12
+ # in your test directory.
13
+ def convert
14
+ raise "No test diretory - you must run this script from your project root, which should also contain a test directory." unless File.directory?("test")
15
+ tests = Dir.glob('test/**/*_test.rb')
16
+ tests.each do |test_file|
17
+ translate_file(test_file)
18
+ end
19
+ end
20
+
21
+ def translate_file(file)
22
+ translation = ""
23
+ File.open(file) do |io|
24
+ io.each_line do |line|
25
+ translation << convert_line(line)
26
+ end
27
+ end
28
+ File.open(file, "w") do |io|
29
+ io.write(translation)
30
+ end
31
+ end
32
+
33
+ def convert_line(line)
34
+ convert_rspec_old_style_names(line)
35
+ convert_dust_style(line)
36
+ convert_test_unit_class_name(line)
37
+ convert_test_unit_methods(line)
38
+ convert_def_setup(line)
39
+ convert_assert(line)
40
+ line
41
+ end
42
+
43
+ private
44
+
45
+ def convert_def_setup(line)
46
+ line.gsub!(/(^\s*)def setup(\s*)$/, '\1before do\2')
47
+ end
48
+
49
+ def convert_rspec_old_style_names(line)
50
+ line.gsub!(/(^\s*)context(\s.*do)/, '\1describe\2')
51
+ line.gsub!(/(^\s*)specify(\s.*do)/, '\1it\2')
52
+ end
53
+
54
+ def convert_test_unit_class_name(line)
55
+ line.gsub!(/^class\s*([\w:]+)Test\s*<\s*Test::Unit::TestCase/, 'describe "\1" do')
56
+ line.gsub!(/^class\s*([\w:]+)Test\s*<\s*(ActiveSupport|ActionController)::(IntegrationTest|TestCase)/, 'describe "\1", \2::\3 do')
57
+ end
58
+
59
+ def convert_test_unit_methods(line)
60
+ line.gsub!(/(^\s*)def\s*test_([\w_!?,$]+)/) { %{#{$1}it "#{$2.split('_').join(' ')}" do} }
61
+ end
62
+
63
+ def convert_dust_style(line)
64
+ line.gsub!(/(^\s*)test(\s.*do)/, '\1it\2')
65
+ end
66
+
67
+ def convert_assert(line)
68
+ line.gsub!(/(^\s*)assert\s+([^\s]*)\s*([<=>~]+)\s*(.*)$/, '\1\2.should \3 \4' )
69
+ line.gsub!(/(^\s*)assert\s+\!(.*)$/, '\1\2.should.not == true' )
70
+ line.gsub!(/(^\s*)assert(_not_nil){0,1}\s+(.*)$/, '\1\3.should.not == nil' )
71
+ line.gsub!(/(^\s*)assert_(nil|true|false)\s+(.*)$/, '\1\3.should == \2' )
72
+ line.gsub!(/(^\s*)assert_equal\s+("[^"]+"|[^,]+),\s*("[^"]+"|[^,\n]+)$/, '\1\3.should == \2' )
73
+ end
74
+ end
@@ -0,0 +1,203 @@
1
+ require 'rubygems'
2
+ require 'test/spec'
3
+ require 'mocha'
4
+ require 'tempfile'
5
+ require File.dirname(__FILE__) + '/../lib/spec_converter'
6
+
7
+ describe "converting from test/spec old style to new style names" do
8
+ before do
9
+ @converter = SpecConverter.new
10
+ end
11
+
12
+ it "replaces spec context with describe" do
13
+ @converter.convert_line(%[context "should foo bar" do]).should == %[describe "should foo bar" do]
14
+ @converter.convert_line(%[ context "should foo bar" do]).should == %[ describe "should foo bar" do]
15
+ end
16
+
17
+ it "ignores unrelated uses of 'context'" do
18
+ @converter.convert_line(%[# here is a comment with context]).should == %[# here is a comment with context]
19
+ @converter.convert_line(%[contexts.each do |context|]).should == %[contexts.each do |context|]
20
+ end
21
+
22
+ it "replaces spec specify with it" do
23
+ @converter.convert_line(%[ specify "remember me saves the user" do]).should == %[ it "remember me saves the user" do]
24
+ end
25
+
26
+ it "ignores unrelated uses of 'specify'" do
27
+ @converter.convert_line(%[# I like to specify things]).should == %[# I like to specify things]
28
+ @converter.convert_line(%[ @user.should.specify.stuff]).should == %[ @user.should.specify.stuff]
29
+ end
30
+
31
+ end
32
+
33
+ describe "SpecConverter.start" do
34
+ it "creates an instance and calls convert" do
35
+ SpecConverter.expects(:new).returns(converter = stub)
36
+ converter.expects(:convert)
37
+ SpecConverter.start
38
+ end
39
+ end
40
+
41
+ describe "translate file overwrites old file with translated stuff" do
42
+ it "works" do
43
+ file = Tempfile.open("some_file.rb")
44
+ file << "Here is some stuff that is cool!"
45
+ file.close
46
+
47
+ converter = SpecConverter.new
48
+ converter.expects(:convert_line).with(anything).returns("Translated to be super awesome")
49
+ converter.translate_file(file.path)
50
+
51
+ File.open(file.path).read.should == "Translated to be super awesome"
52
+ end
53
+ end
54
+
55
+ describe "converting dust style to test/spec style" do
56
+ before do
57
+ @converter = SpecConverter.new
58
+ end
59
+
60
+ it "changes test...do to it...do" do
61
+ @converter.convert_line(%[test "should do something cool and fun!!" do]).should == %[it "should do something cool and fun!!" do]
62
+ end
63
+
64
+ it "ignores unrelated lines" do
65
+ @converter.convert_line(%[# test that this class can do something right]).should == %[# test that this class can do something right]
66
+ end
67
+
68
+ end
69
+
70
+ describe "converting ActionController tests to describe blocks" do
71
+ before do
72
+ @converter = SpecConverter.new
73
+ end
74
+
75
+ it "replaces class FooTest < ActionController::IntegrationTest with describe foo do" do
76
+ @converter.convert_line(%[class ResearchTest < ActionController::IntegrationTest]).should == %[describe "Research", ActionController::IntegrationTest do]
77
+ end
78
+
79
+ it "replaces class FooControllerTest < ActionController::TestCase with describe foo do" do
80
+ @converter.convert_line(%[class ResearchControllerTest < ActionController::TestCase]).should == %[describe "ResearchController", ActionController::TestCase do]
81
+ end
82
+
83
+ it "replaces class FooControllerTest < ActiveSupport::TestCase with describe foo do" do
84
+ @converter.convert_line(%[class ResearchControllerTest < ActiveSupport::TestCase]).should == %[describe "ResearchController", ActiveSupport::TestCase do]
85
+ end
86
+ end
87
+
88
+ describe "converting Test::Unit style to describe blocks" do
89
+ before do
90
+ @converter = SpecConverter.new
91
+ end
92
+
93
+ it "replaces class FooTest < Test::Unit::TestCase with describe foo do" do
94
+ @converter.convert_line(%[class ResearchTest < Test::Unit::TestCase]).should == %[describe "Research" do]
95
+ end
96
+
97
+ it "replaces class FooTest < Test::Unit::TestCase with silly whitespacing with describe foo do" do
98
+ @converter.convert_line(%[class ResearchTest < Test::Unit::TestCase]).should == %[describe "Research" do]
99
+ end
100
+
101
+ it "converts namespaced test unit classes" do
102
+ @converter.convert_line(%[class Admin::DashboardControllerTest < Test::Unit::TestCase]).should == %[describe "Admin::DashboardController" do]
103
+ end
104
+
105
+ it "ignores unrelated classes" do
106
+ @converter.convert_line(%[class Foo]).should == %[class Foo]
107
+ @converter.convert_line(%[class Bar < ActiveRecord::Base]).should == %[class Bar < ActiveRecord::Base]
108
+ end
109
+
110
+ end
111
+
112
+ describe "converting Test::Unit methods to it blocks" do
113
+ before do
114
+ @converter = SpecConverter.new
115
+ end
116
+
117
+ it "replaces def setup with before do" do
118
+ @converter.convert_line(%[def setup\n]).should == %[before do\n]
119
+ end
120
+
121
+ it "ignores method definitions that only start with setup" do
122
+ @converter.convert_line(%[def setup_my_object]).should == %[def setup_my_object]
123
+ end
124
+
125
+ it "replaces class def test_something? with it something? do" do
126
+ @converter.convert_line(%[def test_something?]).should == %[it "something?" do]
127
+ end
128
+
129
+ it "replaces class def test_something? with it something? do" do
130
+ @converter.convert_line(%[def test_something?]).should == %[it "something?" do]
131
+ end
132
+
133
+ it "replaces class def test_something with it something do" do
134
+ @converter.convert_line(%[def test_something]).should == %[it "something" do]
135
+ end
136
+
137
+ it "replaces class def test_something with it something do when it has leading whitespace" do
138
+ @converter.convert_line(%[ def test_something_here]).should == %[ it "something here" do]
139
+ end
140
+
141
+ it "ignores unrelated lines" do
142
+ @converter.convert_line(%[def foo]).should == %[def foo]
143
+ end
144
+
145
+ it "ignores unrelated lines with leading whitespace" do
146
+ @converter.convert_line(%[ def foo]).should == %[ def foo]
147
+ end
148
+
149
+ end
150
+
151
+ describe "converting assertions" do
152
+ before do
153
+ @converter = SpecConverter.new
154
+ end
155
+
156
+ it "replaces assert !foo to foo.should == false" do
157
+ @converter.convert_line(%[ assert !foo]).should == %[ foo.should.not == true]
158
+ end
159
+
160
+ it "replaces assert_equal y, x, bar to x.should == y" do
161
+ @converter.convert_line(%[ assert_equal "$1,490.00", x["price"]\n]).should == %[ x["price"].should == "$1,490.00"\n]
162
+ end
163
+
164
+ it "replaces assert foo to foo.should == true" do
165
+ @converter.convert_line(%[ assert foo]).should == %[ foo.should.not == nil]
166
+ end
167
+
168
+ it "replaces assert_nil foo to foo.should == nil" do
169
+ @converter.convert_line(%[ assert_nil foo]).should == %[ foo.should == nil]
170
+ end
171
+
172
+ it "replaces assert_true foo to foo.should == true" do
173
+ @converter.convert_line(%[ assert_true foo]).should == %[ foo.should == true]
174
+ end
175
+
176
+ it "replaces assert_false foo to foo.should == false" do
177
+ @converter.convert_line(%[ assert_false foo]).should == %[ foo.should == false]
178
+ end
179
+
180
+ it "replaces assert_not_nil foo to foo.should.not == nil" do
181
+ @converter.convert_line(%[ assert_not_nil foo]).should == %[ foo.should.not == nil]
182
+ end
183
+ it "replaces assert foo > 20 to foo.should > 20" do
184
+ @converter.convert_line(%[ assert foo > 20]).should == %[ foo.should > 20]
185
+ end
186
+ end
187
+
188
+ describe "converting things in batch" do
189
+ before do
190
+ @converter = SpecConverter.new
191
+ end
192
+
193
+ it "takes convert all test files in the test subdirectory" do
194
+ @convert.expects(:convert_line).never
195
+ files = %w[test/foo_test.rb test/models/another_test.rb]
196
+ files.each do |file|
197
+ @converter.expects(:translate_file).with(file)
198
+ end
199
+ Dir.expects(:glob).with(anything()).returns(files)
200
+ File.stubs(:directory?).with("test").returns(true)
201
+ @converter.convert
202
+ end
203
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spec_converter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Relevance
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-02-15 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.5.0
23
+ version:
24
+ description: Convert your tests to test/spec specs. See http://opensource.thinkrelevance.com/wiki/spec-converter for details.
25
+ email: opensource@thinkrelevance.com
26
+ executables:
27
+ - spec_converter
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - Manifest.txt
32
+ - README.txt
33
+ files:
34
+ - Manifest.txt
35
+ - README.txt
36
+ - Rakefile
37
+ - bin/spec_converter
38
+ - lib/spec_converter.rb
39
+ - test/spec_converter_test.rb
40
+ has_rdoc: true
41
+ homepage: http://opensource.thinkrelevance.com/wiki/spec-converter
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --main
45
+ - README.txt
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project: thinkrelevance
63
+ rubygems_version: 1.0.1
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: Convert your tests to test/spec specs
67
+ test_files: []
68
+