Skip to content

Commit 0270ab7

Browse files
committed
test unit
1 parent 3ceafab commit 0270ab7

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
See stuff about a Rails app.
44

55
```bash
6-
$ bundle exec rake:stats[/users/me/path/to/app/]
6+
$ bundle exec rake:stats[/path/to/app/]
77

8-
Directory: /users/me/path/to/app/
8+
Directory: /path/to/app/
99

1010
+----------------------+-------+-------+---------+---------+-----+-------+
1111
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
@@ -37,7 +37,7 @@ Directory: /users/me/path/to/app/
3737
* Library files
3838
* Gems that you've embedded in the project
3939
* Engines and their code
40-
* RSpec and Cucumber Tests
40+
* RSpec/Unit/Cucumber Tests
4141

4242
### TODO
4343

@@ -46,5 +46,4 @@ Directory: /users/me/path/to/app/
4646
* Support JS for projects that have it in public (but not compiled)
4747
* Add CSS but don't count towards ratios
4848
* Output other metrics like number of tables and columns
49-
* Test unit support
5049
* Different output formatters

lib/rails_stats.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module RailsStats
1010
require 'rails_stats/code_statistics_calculator'
1111
require 'rails_stats/util'
1212
require 'rails_stats/app_statistics'
13+
require 'rails_stats/test_statistics'
1314
require 'rails_stats/spec_statistics'
1415
require 'rails_stats/cucumber_statistics'
1516
require 'rails_stats/root_statistics'

lib/rails_stats/code_statistics.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ def calculate_spec_projects
8383
end
8484

8585
def calculate_test_projects
86-
[] # TODO: test unit
86+
specs = Util.calculate_projects(@root_directory, "**", "test", "test_helper.rb")
87+
specs.collect do |root_path|
88+
TestStatistics.new(root_path, @key_concepts)
89+
end
8790
end
8891

8992
def calculate_root_projects

lib/rails_stats/test_statistics.rb

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
module RailsStats
2+
class TestStatistics
3+
attr_reader :statistics, :total, :test
4+
5+
SPEC_FOLDERS = ['controllers',
6+
'functional',
7+
'helpers',
8+
'models',
9+
'requests',
10+
'unit',
11+
'integrations',
12+
'integration',
13+
'mailers',
14+
'lib']
15+
16+
def initialize(directory, key_concepts)
17+
@test = true
18+
@directory = directory
19+
@key_concepts = key_concepts
20+
@statistics = calculate_statistics
21+
@total = calculate_total
22+
end
23+
24+
private
25+
26+
def calculate_total
27+
out = CodeStatisticsCalculator.new(true)
28+
@statistics.each do |key, stats|
29+
out.add(stats)
30+
end
31+
out
32+
end
33+
34+
def calculate_statistics
35+
out = {}
36+
categorize_files.each do |key, list|
37+
out[key] = Util.calculate_file_statistics(list)
38+
end
39+
out
40+
end
41+
42+
def categorize_files
43+
out = {}
44+
Dir[File.join(@directory, "**", "*.rb")].each do |file_path|
45+
if file_path =~ /.*_test.rb$/
46+
key = categorize_file(file_path)
47+
else
48+
key = "Test Support"
49+
end
50+
51+
out[key] ||= []
52+
out[key] << file_path
53+
end
54+
55+
out
56+
end
57+
58+
def categorize_file(file_path)
59+
types = (@key_concepts + SPEC_FOLDERS).uniq
60+
types.each do |folder|
61+
if file_path =~ /\/#{folder}\//
62+
folder = Inflector.humanize(folder)
63+
folder = Inflector.titleize(folder)
64+
folder = Inflector.singularize(folder)
65+
return "#{folder} Tests"
66+
end
67+
end
68+
69+
# something else
70+
return "Other Tests"
71+
end
72+
end
73+
74+
end

0 commit comments

Comments
 (0)