Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbetz committed Sep 11, 2008
0 parents commit dbe6cfe
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
nbproject/**
Empty file added Rakefile
Empty file.
11 changes: 11 additions & 0 deletions flogger.gemspec
@@ -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
8 changes: 8 additions & 0 deletions lib/flogger.rb
@@ -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'
54 changes: 54 additions & 0 deletions lib/flogger/assertions.rb
@@ -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


40 changes: 40 additions & 0 deletions test/test_assertions.rb
@@ -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


0 comments on commit dbe6cfe

Please sign in to comment.