test_equality_with_lcs 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Scott Noel-Hemming
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,67 @@
1
+ = test_equality_with_lcs
2
+
3
+ Compare two strings, or arrays, and get failed results back in an easy to distinguish LCS output.
4
+
5
+ Let's say you want to test two strings:
6
+ @string1 = "Hello World!"
7
+ @string2 = "Hello Bob!"
8
+
9
+ *Unit tests*:
10
+ class ClassName < Test::Unit::TestCase
11
+ include TestWithLCS::Unit
12
+
13
+ def test_something
14
+ assert_equal_with_lcs @string1, @string2
15
+ end
16
+ end
17
+
18
+ and
19
+
20
+ *Spec tests*:
21
+ define "Something" do
22
+ include TestEqualityWithLCS::Spec
23
+
24
+ it "matches" do
25
+ @string1.should be_equal_with_lcs(@string2)
26
+ end
27
+ end
28
+
29
+ result in the output:
30
+
31
+ ' ! ! '
32
+ 'Hello World!'
33
+ 'Hello Bob !'
34
+
35
+ * without the quotes though.
36
+ I added them to bring attention
37
+ to the included whitespace.
38
+
39
+ == Dependencies
40
+
41
+ diff-lcs ( http://rubyforge.org/projects/ruwiki/ )
42
+ colored ( http://errtheblog.com/ )
43
+
44
+ == Installation
45
+
46
+ 1. Require the gem
47
+ 2. Install the dependant gems
48
+ 2. Require the necessary lib for your framework
49
+ 3. include the appropriate module for your testing framework
50
+
51
+ * TestWithLCS::Spec for Rspec
52
+ * TestWithLCS::Unit for Test/Unit
53
+
54
+ == Note on Patches/Pull Requests
55
+
56
+ * Fork the project.
57
+ * Make your feature addition or bug fix.
58
+ * Add tests for it. This is important so I don't break it in a
59
+ future version unintentionally.
60
+ * Commit, do not mess with rakefile, version, or history.
61
+ (if you want to have your own version, that is fine but
62
+ bump version in a commit by itself I can ignore when I pull)
63
+ * Send me a pull request. Bonus points for topic branches.
64
+
65
+ == Copyright
66
+
67
+ Copyright (c) 2009 Scott Noel-Hemming. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,71 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "test_equality_with_lcs"
8
+ gem.summary = %Q{Compare two strings, or arrays, and get failed results back in an easy to distinguish LCS output.}
9
+ gem.description = %Q{Compare two strings, or arrays, and get failed results back in an easy to distinguish LCS output.}
10
+ gem.email = "frogstarr78@gmail.com"
11
+ gem.homepage = "http://github.com/frogstarr78/test_equality_with_lcs"
12
+ gem.authors = ["Scott Noel-Hemming"]
13
+ gem.add_development_dependency "rspec"
14
+ gem.add_development_dependency "diff-lcs"
15
+ gem.add_development_dependency "colored"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/*_test.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ require 'spec/rake/spectask'
31
+ Spec::Rake::SpecTask.new(:spec) do |spec|
32
+ spec.libs << 'lib' << 'spec'
33
+ spec.spec_files = FileList['spec/**/*_spec.rb']
34
+ end
35
+
36
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
37
+ spec.libs << 'lib' << 'spec'
38
+ spec.pattern = 'spec/**/*_spec.rb'
39
+ spec.rcov = true
40
+ end
41
+
42
+ task :spec => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ if File.exist?('VERSION')
49
+ version = File.read('VERSION')
50
+ else
51
+ version = ""
52
+ end
53
+
54
+ rdoc.rdoc_dir = 'rdoc'
55
+ rdoc.title = "test_equality_with_lcs #{version}"
56
+ rdoc.rdoc_files.include('README*')
57
+ rdoc.rdoc_files.include('lib/**/*.rb')
58
+ end
59
+
60
+ begin
61
+ require 'rcov/rcovtask'
62
+ Rcov::RcovTask.new do |test|
63
+ test.libs << 'test'
64
+ test.pattern = 'test/**/*_test.rb'
65
+ test.verbose = true
66
+ end
67
+ rescue LoadError
68
+ task :rcov do
69
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
70
+ end
71
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,58 @@
1
+ module TestEqualityWithLCS
2
+ module SharedMethods
3
+ @@colorize = true
4
+ def self.included in_class
5
+ in_class.class.send :define_method, :colorize do
6
+ @@colorize
7
+ end
8
+
9
+ in_class.class.send :define_method, :colorize= do |color|
10
+ @@colorize = color
11
+ end
12
+ end
13
+
14
+ private
15
+ def message_for_argument_types string1, string2
16
+ if string1.is_a? String and string2.is_a? String
17
+ delegating_to_string_generating_method string1, string2
18
+ elsif string1.is_a? Array and string2.is_a? Array
19
+ lcs_message(string1, string2)
20
+ else
21
+ raise Test::Unit::AssertionFailedError.new("Comparing different types.")
22
+ end
23
+ end
24
+
25
+ def delegating_to_string_generating_method string1, string2
26
+ seq1 = string1.split "\n"
27
+ seq2 = string2.split "\n"
28
+ messages = []
29
+ (['']*seq1.size).zip(seq1, seq2).each do |junk, line1, line2|
30
+ messages << lcs_message("#{line1}", "#{line2}")
31
+ end
32
+ messages.join "\n"
33
+ end
34
+
35
+ def lcs_message string1, string2
36
+ message_array = []
37
+ Diff::LCS.sdiff string1, string2 do |diff|
38
+ action = diff.action == '=' ? ' ' : diff.action
39
+ case action
40
+ when '-'
41
+ color = :red
42
+ when '+'
43
+ color = :green
44
+ when '!'
45
+ color = :magenta
46
+ else
47
+ color = :white
48
+ end
49
+ left = diff.new_element || ' '
50
+ right = diff.old_element || ' '
51
+ message_array << [action, left, right].map {|thing| thing.send(@@colorize ? color : :to_s) }
52
+ end
53
+ horizontal_diff_array = message_array.transpose
54
+ horizontal_messages = horizontal_diff_array.map {|diff| diff.join '' }
55
+ horizontal_messages.join "\n"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,28 @@
1
+ module TestEqualityWithLCS
2
+ module Spec
3
+
4
+ class BeEqualWithLCS
5
+ include SharedMethods
6
+ def initialize expected
7
+ @expected = expected
8
+ end
9
+
10
+ def matches? target
11
+ @target = target
12
+ @expected == @target
13
+ end
14
+
15
+ def failure_message_for_should
16
+ message_for_argument_types @expected, @target
17
+ end
18
+
19
+ def failure_message_for_should_not
20
+ message_for_argument_types @expected, @target
21
+ end
22
+ end
23
+
24
+ def be_equal_with_lcs expected
25
+ BeEqualWithLCS.new expected
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ require 'test/unit/assertions'
2
+
3
+ module TestEqualityWithLCS
4
+ module Unit
5
+ include SharedMethods
6
+ def assert_equal_with_lcs string1, string2
7
+ # depending on the size of the strings
8
+ # and since we generate the error message
9
+ # before running the assertion
10
+ # we want to bypass this behavior for the sake of speed
11
+ return if string1 == string2
12
+
13
+ message = message_for_argument_types string1, string2
14
+ assert_block message do
15
+ string1 == string2
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ require 'diff/lcs'
2
+ require 'colored'
3
+ require 'test_equality_with_lcs/shared'
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'test_equality_with_lcs'
4
+ require 'test_equality_with_lcs/spec'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ Spec::Runner.configure do |config|
9
+ config.include TestEqualityWithLCS::Spec
10
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "TestEqualityWithLcs" do
4
+ it "passes" do
5
+ 'actual'.should be_equal_with_lcs('actual')
6
+ end
7
+
8
+ it "doesn't pass" do
9
+ 'actual'.should_not be_equal_with_lcs('something else so this test will fail and we can test the resulting string')
10
+ end
11
+
12
+ it "get's correct output" do
13
+ self.class.colorize = false
14
+ # careful of the whitespace required here.
15
+ expected_error = "---------------------------------- ---------- --- ----------- + -----------
16
+ a c t ual
17
+ something else so this test will fail and we can test the resu lting string"
18
+ lambda{
19
+ 'actual'.should be_equal_with_lcs('something else so this test will fail and we can test the resulting string')
20
+ }.should raise_error(Spec::Expectations::ExpectationNotMetError, expected_error)
21
+ end
22
+
23
+ it "get's correct output with multi-line string comparison" do
24
+ self.class.colorize = false
25
+ # careful of the whitespace required here.
26
+ seq1 = "actual
27
+ ly
28
+ I
29
+ didn't want it all
30
+ on one
31
+ line"
32
+ seq2 = "actually I did want it on ...
33
+ well a couple lines."
34
+
35
+ expected_error = " -----------------------
36
+ actual
37
+ actually I did want it on ...
38
+ -- !----------------
39
+ ly
40
+ well a couple lines."
41
+ lambda{
42
+ seq1.should be_equal_with_lcs(seq2)
43
+ }.should raise_error(Spec::Expectations::ExpectationNotMetError, expected_error)
44
+ end
45
+ end
@@ -0,0 +1,99 @@
1
+ require 'test_helper'
2
+
3
+ class AssertEqualityWithLCSTest < Test::Unit::TestCase
4
+ def test_my_failing_message
5
+ seq1 = %w(a b c e h j l \ m n p)
6
+ seq2 = %w(b c d e f j k l m r s t)
7
+ begin
8
+ self.class.colorize = true
9
+ assert_equal_with_lcs seq1, seq2
10
+ rescue Test::Unit::AssertionFailedError => received
11
+ expected = '-'.red << ' '.white << ' '.white << '+'.green << ' '.white << '!'.magenta << ' '.white << '+'.green << ' '.white << '-'.red << ' '.white << '!'.magenta << '!'.magenta << '+'.green << "\n" <<
12
+ ' '.red << 'b'.white << 'c'.white << 'd'.green << 'e'.white << 'f'.magenta << 'j'.white << 'k'.green << 'l'.white << ' '.red << 'm'.white << 'r'.magenta << 's'.magenta << 't'.green << "\n" <<
13
+ 'a'.red << 'b'.white << 'c'.white << ' '.green << 'e'.white << 'h'.magenta << 'j'.white << ' '.green << 'l'.white << ' '.red << 'm'.white << 'n'.magenta << 'p'.magenta << ' '.green
14
+ assert_equal expected, received.message
15
+ end
16
+ end
17
+
18
+ def test_my_failing_message_on_multi_line_strings
19
+ seq1 = %Q|abce
20
+ hjl
21
+ \ mnp
22
+ q\tu
23
+ v
24
+ y
25
+ xz|
26
+ seq2 = %Q|bcd
27
+ efjklmr
28
+ st|
29
+ begin
30
+ self.class.colorize = false
31
+ assert_equal_with_lcs seq1, seq2
32
+ rescue Test::Unit::AssertionFailedError => received
33
+ received_messages = received.message.split "\n"
34
+
35
+ assert_equal '- !', received_messages[0]
36
+ assert_equal ' bcd', received_messages[1]
37
+ assert_equal 'abce', received_messages[2]
38
+
39
+ assert_equal '!+ + ++', received_messages[3]
40
+ assert_equal 'efjklmr', received_messages[4]
41
+ assert_equal 'h j l ', received_messages[5]
42
+
43
+ assert_equal '!!--', received_messages[6]
44
+ assert_equal 'st ', received_messages[7]
45
+ assert_equal ' mnp', received_messages[8]
46
+
47
+ assert_equal "---", received_messages[9]
48
+ assert_equal " ", received_messages[10]
49
+ assert_equal "q\tu", received_messages[11]
50
+
51
+ assert_equal '-', received_messages[12]
52
+ assert_equal ' ', received_messages[13]
53
+ assert_equal 'v', received_messages[14]
54
+
55
+ assert_equal '-', received_messages[15]
56
+ assert_equal ' ', received_messages[16]
57
+ assert_equal 'y', received_messages[17]
58
+
59
+ assert_equal '--', received_messages[18]
60
+ assert_equal ' ', received_messages[19]
61
+ assert_equal 'xz', received_messages[20]
62
+ ensure
63
+ self.class.colorize = true
64
+ end
65
+ end
66
+
67
+ def test_me_failing_again
68
+ seq1 = %w(a b c e h j l \ m n p)
69
+ seq2 = %w(b c d e f j k l m r s t)
70
+ assert_raises Test::Unit::AssertionFailedError do
71
+ assert_equal_with_lcs seq1, seq2
72
+ end
73
+ end
74
+
75
+ def test_no_passing
76
+ seq1 = %w(a b c e h j l \ m n p)
77
+ seq2 = %w(a b c e h j l \ m n p)
78
+ assert_nothing_raised {
79
+ assert_equal_with_lcs seq1, seq2
80
+ }
81
+ end
82
+
83
+ def test_string_lcs_array
84
+ # though the underlying library supports it,
85
+ # we don't want to allow that kind of messiness
86
+ # in our testing framework.
87
+ seq1 = %q(abcehjl mnp)
88
+ seq2 = %w(a b c e h j l \ m n p)
89
+ assert_raises Test::Unit::AssertionFailedError do
90
+ assert_equal_with_lcs seq1, seq2
91
+ end
92
+ end
93
+
94
+ def test_colorize
95
+ assert_equal true, self.class.colorize
96
+ self.class.colorize = false
97
+ assert_equal false, self.class.colorize
98
+ end
99
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'test_equality_with_lcs'
4
+ require 'test_equality_with_lcs/unit'
5
+ require 'test/unit'
6
+
7
+ class Test::Unit::TestCase
8
+ include TestEqualityWithLCS::Unit
9
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: test_equality_with_lcs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Scott Noel-Hemming
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-08 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: diff-lcs
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: colored
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description: Compare two strings, or arrays, and get failed results back in an easy to distinguish LCS output.
46
+ email: frogstarr78@gmail.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README
54
+ files:
55
+ - .document
56
+ - .gitignore
57
+ - LICENSE
58
+ - README
59
+ - Rakefile
60
+ - VERSION
61
+ - lib/test_equality_with_lcs.rb
62
+ - lib/test_equality_with_lcs/shared.rb
63
+ - lib/test_equality_with_lcs/spec.rb
64
+ - lib/test_equality_with_lcs/unit.rb
65
+ - spec/spec_helper.rb
66
+ - spec/test_equality_with_lcs_spec.rb
67
+ - test/assert_equality_with_lcs_test.rb
68
+ - test/test_helper.rb
69
+ has_rdoc: true
70
+ homepage: http://github.com/frogstarr78/test_equality_with_lcs
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options:
75
+ - --charset=UTF-8
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ requirements: []
91
+
92
+ rubyforge_project:
93
+ rubygems_version: 1.3.5
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Compare two strings, or arrays, and get failed results back in an easy to distinguish LCS output.
97
+ test_files:
98
+ - spec/test_equality_with_lcs_spec.rb
99
+ - spec/spec_helper.rb
100
+ - test/test_helper.rb
101
+ - test/assert_equality_with_lcs_test.rb