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

Commit

Permalink
added tests for myself
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbetz committed Sep 11, 2008
1 parent dbe6cfe commit 704f6ae
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .cvsignore
@@ -0,0 +1,2 @@
.gitignore
.git
4 changes: 2 additions & 2 deletions flogger.gemspec
@@ -1,11 +1,11 @@
Gem::Specification.new do |s|
s.name = %q{flogger}
s.version = "0.0.1"
s.version = "0.0.2"
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.homepage = %q{http://simplificator.com/en/lab}
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
41 changes: 36 additions & 5 deletions lib/flogger/assertions.rb
@@ -1,11 +1,30 @@
module Flogger
module InstanceMethods
#
# assert_flog([files_or_dir], options)
#
# assert the flog score of file(s) is below a treshold.
#
# = Samples:
# == Flog a file or all ruby files in a directory
# assert_flog(file_or_dir)
# == Flog several files or directories
# assert_flog(file_or_dir_1, file_or_dir_2, file_or_fir_n)
# == set your own flog threshold
# assert_flog(file_or_dir, :threshold => 20)
# == set your own flog threshold for a specific method
# assert_flog(file_or_dir, :thresholds => {'FooClass#bar_method'})
#
# == options
# * :treshold what flog score do we allow at most. __default__ is 20.
# * :tresholds customize tresholds on a per class/method base, overrides :treshold option
#
# == Message
# This assertion does not support passing in your own message like
# other assertions do (optional last parameter) because the error message
# lists all failures from flog.
#
def assert_flog(*args)
files, options = extract_files_and_options(args)
options = {:limit => 20}.merge(options)
options = {:treshold => 20}.merge(options)
flogger = Flog.new()
flogger.flog_files(files)
failures = reject_success(flogger.totals, options)
Expand All @@ -15,6 +34,10 @@ def assert_flog(*args)
end


def assert_floq_rails(*args)
assert_flog([RAILS_ROOT] + args)
end


private

Expand All @@ -35,7 +58,8 @@ def extract_files_and_options(args)
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]})"
limit = treshold_for_key(key, options)
message << "#{key.ljust(40, ' ')} has a flog score of #{value} (exceeding treshold of #{limit} by #{value - limit})"
end
message.join("\n")
end
Expand All @@ -45,9 +69,16 @@ def build_flog_message(failures, options)
#
def reject_success(totals, options)
totals.reject do |key, value|
value < options[:limit]
value < treshold_for_key(key, options)
end
end


def treshold_for_key(key, options)
options.has_key?(:tresholds) && options[:tresholds].has_key?(key) ?
options[:tresholds][key] : options[:treshold]
end

end
end

Expand Down
28 changes: 20 additions & 8 deletions test/test_assertions.rb
Expand Up @@ -9,18 +9,26 @@ def public_extract_files_and_options(*args)
end
end

class TestChronic < Test::Unit::TestCase

def setup

class TestAssertions < Test::Unit::TestCase
def test_assert_flog_raises
assert_raise(Test::Unit::AssertionFailedError) do
assert_flog(__FILE__)
end
end


def test_assert_flog
assert_flog(__FILE__, :limit => 0)
# flog score of > 9
def test_tresholds
assert_raise(Test::Unit::AssertionFailedError) do
assert_flog(__FILE__, :treshold => 10)
end
assert_raise(Test::Unit::AssertionFailedError) do
assert_flog(__FILE__, :treshold => 30, :tresholds => {'TestAssertions#test_extract_files_and_options' => 20})
end

assert_flog(__FILE__, :treshold => 10, :tresholds => {'TestAssertions#test_extract_files_and_options' => 23})
end


# flog score of > 22
def test_extract_files_and_options
files, options = public_extract_files_and_options('some')
assert_equal(['some'], files)
Expand All @@ -34,6 +42,10 @@ def test_extract_files_and_options
files, options = public_extract_files_and_options('some', 'other', :key => :value)
assert_equal(['some', 'other'], files)
assert_equal({:key => :value}, options)

files, options = public_extract_files_and_options('some', 'other', {})
assert_equal(['some', 'other'], files)
assert_equal({}, options)
end
end

Expand Down

0 comments on commit 704f6ae

Please sign in to comment.