etest-unit 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # etest-unit
2
+
3
+ ## Installation
4
+
5
+ gem install etest-unit
6
+
7
+ ## Intro
8
+
9
+ ~/projects/ruby/scratch > irb
10
+ irb> class X
11
+ irb> def self.foo; "baz"; end
12
+ irb> end
13
+ irb> module X::Etest
14
+ irb> def test_abc
15
+ irb> assert_equal X.foo, "bar"
16
+ irb> end
17
+ irb> end
18
+ irb* require "etest-unit"
19
+ => true
20
+ irb> X.etest
21
+ Warning: Cannot reload module X
22
+ Loaded suite X::Etest
23
+ Started
24
+ F
25
+ ===============================================================================
26
+ Failure:
27
+ test_abc()
28
+ (irb):7:in `test_abc'
29
+ <"baz"> expected but was
30
+ <"bar">
31
+
32
+ [ ... ]
33
+ 57.30 tests/s, 57.30 assertions/s
34
+ => 1 tests, 1 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
35
+
36
+
37
+ irb> # fix the typo
38
+ irb* class X
39
+ irb> def self.foo; "bar"; end
40
+ irb> end
41
+ => nil
42
+ irb> # run test again
43
+ irb* X.etest
44
+ Warning: Cannot reload module X
45
+ Loaded suite X::Etest
46
+ Started
47
+ .
48
+
49
+ Finished in 0.00111 seconds.
50
+
51
+ 1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
52
+ 100% passed
53
+
54
+ irb>
55
+
56
+ ## License
57
+
58
+ The etest-unit gem is distributed under the terms of the Modified BSD License, see LICENSE.BSD for details.
@@ -0,0 +1,3 @@
1
+ module Etest
2
+ VERSION = "0.7.0"
3
+ end
data/lib/etest-unit.rb ADDED
@@ -0,0 +1,76 @@
1
+ require "test/unit"
2
+
3
+ require File.dirname(__FILE__) + "/string_ext"
4
+ require File.dirname(__FILE__) + "/module_ext"
5
+
6
+ require "test/unit/ui/console/testrunner"
7
+
8
+ module Etest
9
+ class TestCase < Test::Unit::TestCase
10
+ def self.etest=(etest)
11
+ include @etest = etest
12
+ end
13
+
14
+ def self.suite
15
+ suite = super
16
+ suite.name = @etest.name
17
+ suite
18
+ end
19
+ end
20
+
21
+ def self.run(etest)
22
+ test_case_klass = Class.new(TestCase)
23
+ test_case_klass.etest = etest
24
+ Test::Unit::UI::Console::TestRunner.new(test_case_klass).start
25
+ end
26
+ end
27
+
28
+ class Test::Unit::TestSuite
29
+ attr :name, true
30
+ end
31
+
32
+ __END__
33
+
34
+ def self.autorun
35
+ auto_run
36
+ end
37
+
38
+ def self.auto_run
39
+ #
40
+ # find all modules that are not named /::Etest$/, and try to load
41
+ # the respective Etest module.
42
+ etests = Module.instances.map { |mod|
43
+ #next if mod.name =~ /\bEtest$/
44
+ next if mod.name == "Object"
45
+
46
+ Module.by_name "#{mod.name}::Etest"
47
+ }.compact.uniq.sort_by(&:name)
48
+
49
+ run(*etests)
50
+ end
51
+
52
+ def self.run(*etests)
53
+ #
54
+ # convert all Etest modules into a test case
55
+ test_cases = etests.map { |etest|
56
+ STDERR.puts "Running: #{etest}"
57
+ to_test_case etest
58
+ }
59
+
60
+ MiniTest::Test.run_etests(*test_cases)
61
+ end
62
+
63
+ #
64
+ # convert an Etest moodule into a MiniTest testcase
65
+ def self.to_test_case(mod)
66
+ klass = Class.new TestCase
67
+ klass.send :include, mod
68
+ klass.send :include, Assertions
69
+
70
+ Kernel.silent do
71
+ mod.const_set("TestCase", klass)
72
+ end
73
+ klass
74
+ end
75
+ end
76
+
data/lib/module_ext.rb ADDED
@@ -0,0 +1,65 @@
1
+ module Kernel
2
+ def self.silent(&block)
3
+ old_verbose, $VERBOSE = $VERBOSE, nil
4
+ yield
5
+ ensure
6
+ $VERBOSE = old_verbose
7
+ end
8
+ end
9
+
10
+ #
11
+ # TDD helpers for modules.
12
+ class Module
13
+ #
14
+ # reloads the module, and runs the module's etests.
15
+ def etest
16
+ reload if respond_to?(:reload)
17
+ ::Etest.run self.const_get("Etest")
18
+ end
19
+
20
+ #
21
+ # returns all instances of a module
22
+ def instances #:nodoc:
23
+ r = []
24
+ ObjectSpace.each_object(self) { |mod| r << mod }
25
+ r
26
+ end
27
+
28
+ #
29
+ # load a module by name.
30
+ def self.by_name(name) #:nodoc:
31
+ Kernel.silent do
32
+ r = eval(name, nil, __FILE__, __LINE__)
33
+ r if r.is_a?(Module) && r.name == name
34
+ end
35
+ rescue NameError, LoadError
36
+ nil
37
+ end
38
+
39
+ #
40
+ # tries to reload the source file for this module. THIS IS A DEVELOPMENT
41
+ # helper, don't try to use it in production mode!
42
+ #
43
+ # Limitations:
44
+ #
45
+ # To reload a module with a name of "X::Y" we try to load (in that order)
46
+ # "x/y.rb", "x.rb"
47
+ #
48
+ def reload
49
+ Module::Reloader.reload_file("#{to_s.underscore}.rb") || begin
50
+ STDERR.puts("Warning: Cannot reload module #{self}")
51
+ false
52
+ end
53
+ end
54
+
55
+ module Reloader #:nodoc:
56
+ def self.reload_file(file)
57
+ begin
58
+ load(file) && file
59
+ rescue LoadError
60
+ nfile = file.gsub(/\/[^\/]+\.rb/, ".rb")
61
+ nfile != file && reload_file(nfile)
62
+ end
63
+ end
64
+ end
65
+ end
data/lib/string_ext.rb ADDED
@@ -0,0 +1,14 @@
1
+ class String
2
+ # taken from active-support
3
+ def underscore
4
+ gsub(/::/, '/').
5
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
6
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
7
+ tr("-", "_").
8
+ downcase
9
+ end
10
+
11
+ def camelize
12
+ gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
13
+ end
14
+ end
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+ DIRNAME = File.expand_path File.dirname(__FILE__)
3
+ Dir.chdir(DIRNAME)
4
+
5
+ ETEST_TEST=true
6
+
7
+ require "etest-unit"
8
+
9
+ # ---------------------------------------------------------------------
10
+
11
+ module String::Etest
12
+ def test_camelize
13
+ assert_equal "x", "X".underscore
14
+ assert_equal "xa_la_nder", "XaLaNder".underscore
15
+ end
16
+
17
+ def test_underscore
18
+ assert_equal "X", "x".camelize
19
+ assert_equal "XaLaNder", "xa_la_nder".camelize
20
+ end
21
+ end
22
+
23
+ module Fixnum::Etest
24
+ def test_success
25
+ $etests_did_run = true
26
+ assert true
27
+ end
28
+ end
29
+
30
+ String.etest
31
+
32
+ $etests_did_run = false
33
+ Fixnum.etest
34
+ exit(0) if $etests_did_run
35
+
36
+ STDERR.puts "Etests didn't run"
37
+ exit(1)
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: etest-unit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - radiospiel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: test-unit
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Build your tests alongside your code.
31
+ email: eno@radiospiel.org
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/etest-unit/version.rb
37
+ - lib/etest-unit.rb
38
+ - lib/module_ext.rb
39
+ - lib/string_ext.rb
40
+ - README.md
41
+ - test/etest_test.rb
42
+ homepage: http://github.com/radiospiel/etest
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ segments:
55
+ - 0
56
+ hash: 1268382848584802831
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.24
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Build your tests alongside your code.
69
+ test_files: []