This repository has been archived by the owner on Aug 23, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pascalbetz
committed
Sep 11, 2008
0 parents
commit dbe6cfe
Showing
6 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nbproject/** |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Gem::Specification.new do |s| | ||
s.name = %q{flogger} | ||
s.version = "0.0.1" | ||
s.date = %q{2008-09-11} | ||
s.authors = ["Simplificator GmbH"] | ||
s.email = %q{info@simplificator.com} | ||
s.summary = %q{flog assertions for unit tests} | ||
s.homepage = %q{http://simplificator.com/} | ||
s.description = %q{adds custom assertions to test/unit so you can test for your flog score} | ||
s.files = ['lib/flogger.rb', 'lib/flogger/assertions.rb'] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#$:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed | ||
require 'flogger/assertions' | ||
require 'test/unit' | ||
require 'rubygems' | ||
require 'flog' | ||
Test::Unit::TestCase.send(:include, Flogger::InstanceMethods) | ||
|
||
puts 'loaded flogger' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module Flogger | ||
module InstanceMethods | ||
# | ||
# assert_flog([files_or_dir], options) | ||
# | ||
def assert_flog(*args) | ||
files, options = extract_files_and_options(args) | ||
options = {:limit => 20}.merge(options) | ||
flogger = Flog.new() | ||
flogger.flog_files(files) | ||
failures = reject_success(flogger.totals, options) | ||
assert_block(build_flog_message(failures, options)) do | ||
failures.size == 0 | ||
end | ||
end | ||
|
||
|
||
|
||
private | ||
|
||
# | ||
# Extracts an optional Hash (options) from an Array of Strings | ||
# | ||
def extract_files_and_options(args) | ||
raise ArgumentError.new('To few argumnets') if args.size < 1 | ||
if args.size == 1 || args.last.is_a?(String) | ||
[args, {}] | ||
else | ||
[args[0...-1], args.last] | ||
end | ||
end | ||
# | ||
# Builds a 'nice' error message listing all failed methods | ||
# | ||
def build_flog_message(failures, options) | ||
message = ['Error when flogging your files:'] | ||
failures.each do |key, value| | ||
message << "#{key.ljust(40, ' ')} has a flog score of #{value} (exceeding limit of #{options[:limit]} by #{value - options[:limit]})" | ||
end | ||
message.join("\n") | ||
end | ||
|
||
# | ||
# Remove all values which are not exceeding the limit | ||
# | ||
def reject_success(totals, options) | ||
totals.reject do |key, value| | ||
value < options[:limit] | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'flogger' | ||
require 'test/unit' | ||
# | ||
# Hackhack. Making a private method public (without using send() | ||
# | ||
module Flogger::InstanceMethods | ||
def public_extract_files_and_options(*args) | ||
extract_files_and_options(args) | ||
end | ||
end | ||
|
||
class TestChronic < Test::Unit::TestCase | ||
|
||
def setup | ||
|
||
end | ||
|
||
|
||
def test_assert_flog | ||
assert_flog(__FILE__, :limit => 0) | ||
end | ||
|
||
|
||
def test_extract_files_and_options | ||
files, options = public_extract_files_and_options('some') | ||
assert_equal(['some'], files) | ||
assert_equal({}, options) | ||
|
||
files, options = public_extract_files_and_options('some', 'other') | ||
assert_equal(['some', 'other'], files) | ||
assert_equal({}, options) | ||
|
||
|
||
files, options = public_extract_files_and_options('some', 'other', :key => :value) | ||
assert_equal(['some', 'other'], files) | ||
assert_equal({:key => :value}, options) | ||
end | ||
end | ||
|
||
|