├── var ├── name ├── title ├── version ├── organization ├── authors ├── copyrights ├── summary ├── repositories ├── requirements ├── resources └── description ├── try ├── .test ├── test_unit_example.rb └── test_example.rb ├── .gitignore ├── lib ├── microtest │ ├── testunit.rb │ └── assertions.rb └── microtest.rb ├── Manifest.txt ├── test └── test_case.rb ├── .test ├── Assembly ├── History.md ├── .index ├── License.txt ├── README.md └── .gemspec /var/name: -------------------------------------------------------------------------------- 1 | microtest 2 | -------------------------------------------------------------------------------- /var/title: -------------------------------------------------------------------------------- 1 | MicroTest 2 | -------------------------------------------------------------------------------- /var/version: -------------------------------------------------------------------------------- 1 | 0.2.1 2 | -------------------------------------------------------------------------------- /var/organization: -------------------------------------------------------------------------------- 1 | Rubyworks 2 | -------------------------------------------------------------------------------- /var/authors: -------------------------------------------------------------------------------- 1 | --- 2 | - Trans 3 | -------------------------------------------------------------------------------- /var/copyrights: -------------------------------------------------------------------------------- 1 | --- 2 | - 2011 Rubyworks (BSD-2-Clause) 3 | -------------------------------------------------------------------------------- /var/summary: -------------------------------------------------------------------------------- 1 | Microminal TestUnit-style Test Framework 2 | -------------------------------------------------------------------------------- /try/.test: -------------------------------------------------------------------------------- 1 | $:.unshift(File.dirname(__FILE__)+'/../lib') 2 | 3 | -------------------------------------------------------------------------------- /var/repositories: -------------------------------------------------------------------------------- 1 | --- 2 | upstream: git://github.com/rubyworks/microtest.git 3 | 4 | -------------------------------------------------------------------------------- /var/requirements: -------------------------------------------------------------------------------- 1 | --- 2 | - rubytest 3 | - detroit (build) 4 | - ergo (build) 5 | - qed (test) 6 | 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.lock 3 | .ergo/digest 4 | .yardoc 5 | doc/ 6 | log/ 7 | pkg/ 8 | tmp/ 9 | web/ 10 | -------------------------------------------------------------------------------- /var/resources: -------------------------------------------------------------------------------- 1 | --- 2 | home: http://rubyworks.github.com/microtest 3 | code: http://github.com/rubyworks/microtest 4 | 5 | -------------------------------------------------------------------------------- /lib/microtest/testunit.rb: -------------------------------------------------------------------------------- 1 | require 'microtest' 2 | 3 | module Test 4 | module Unit 5 | TestCase = MicroTest::TestCase 6 | end 7 | end 8 | 9 | -------------------------------------------------------------------------------- /var/description: -------------------------------------------------------------------------------- 1 | MicroTest is a very small Test::Unit/MiniTest compatbile 2 | test framework that runs on top of RubyTest, a Universal 3 | Test Harness for Ruby. 4 | -------------------------------------------------------------------------------- /Manifest.txt: -------------------------------------------------------------------------------- 1 | #!mast .ruby .yaropts bin lib spec test [A-Z]*.* 2 | .ruby 3 | lib/microtest/assertions.rb 4 | lib/microtest/testunit.rb 5 | lib/microtest.rb 6 | test/test_case.rb 7 | README.md 8 | History.md 9 | License.txt 10 | -------------------------------------------------------------------------------- /try/test_unit_example.rb: -------------------------------------------------------------------------------- 1 | require 'microtest/unit' 2 | require 'microtest/assertions' 3 | 4 | class ExampleTest < Test::Unit::TestCase 5 | 6 | def setup 7 | @a = 1 8 | end 9 | 10 | def test_example_of_passing_test 11 | assert_equal(1, @a) 12 | end 13 | 14 | end 15 | 16 | -------------------------------------------------------------------------------- /test/test_case.rb: -------------------------------------------------------------------------------- 1 | class ExampleTest < MicroTest::TestCase 2 | 3 | # 4 | def setup 5 | @a = 1 6 | end 7 | 8 | # 9 | def test_alpha 10 | assert_equal(1, @a) 11 | @a = 2 12 | end 13 | 14 | # 15 | def test_alpha_again 16 | assert_equal(1, @a) 17 | @a = 2 18 | end 19 | 20 | end 21 | 22 | -------------------------------------------------------------------------------- /try/test_example.rb: -------------------------------------------------------------------------------- 1 | require 'microtest' 2 | require 'microtest/assertions' 3 | 4 | class ExampleTest < MicroTest::TestCase 5 | 6 | def setup 7 | @a = 1 8 | end 9 | 10 | def test_example_of_passing_test 11 | assert_equal(1, @a) 12 | end 13 | 14 | def test_another_example 15 | assert_kind_of(Integer, @a) 16 | end 17 | 18 | end 19 | -------------------------------------------------------------------------------- /.test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | Test.run :default do |run| 4 | run.files << 'test/test_*.rb' 5 | 6 | require 'microtest' 7 | require 'microtest/assertions' 8 | end 9 | 10 | Test.run(:cov) do |run| 11 | run.files << 'test/test_*.rb' 12 | 13 | require 'simplecov' 14 | 15 | SimpleCov.start do 16 | require 'microtest' 17 | require 'microtest/assertions' 18 | 19 | coverage_dir 'log/coverage' 20 | end 21 | end 22 | 23 | -------------------------------------------------------------------------------- /Assembly: -------------------------------------------------------------------------------- 1 | --- 2 | github: 3 | gh_pages: web 4 | 5 | gem: 6 | active: true 7 | 8 | dnote: 9 | title : Source Notes 10 | labels: ~ 11 | output: log/notes.html 12 | 13 | yard: 14 | yardopts: true 15 | 16 | #qed: 17 | # files : ~ 18 | # #exclude : ~ 19 | # #loadpath: ~ 20 | # #requires: ~ 21 | # #live : false 22 | # active : false 23 | 24 | #qedoc: 25 | # files : spec/ 26 | # output: QED.rdoc 27 | # active: false 28 | 29 | vclog: 30 | output: 31 | - log/changes.html 32 | - log/history.html 33 | active: false 34 | 35 | email: 36 | mailto: 37 | - ruby-talk@ruby-lang.org 38 | - rubyworks-mailinglist@googlegroups.com 39 | 40 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | # Release History 2 | 3 | ## 0.2.1 / 2012-03-04 4 | 5 | This release simply fixes assert_raises assertion method. 6 | 7 | Changes: 8 | 9 | * Fix assert_raises assertion in assertions.rb. 10 | 11 | 12 | ## 0.2.0 / 2012-03-03 13 | 14 | Fix setup and teardown procedures os they run before and after each test, 15 | not all tests. 16 | 17 | Changes: 18 | 19 | * Fix when setup and teardown procedures run. 20 | 21 | 22 | ## 0.1.1 / 2012-03-02 23 | 24 | This release consists of just minor tweaks --no functional changes. 25 | 26 | Changes: 27 | 28 | * Various admin adjustments. 29 | 30 | 31 | ## 0.1.0 / 2011-08-11 32 | 33 | This first release covers basic compatability with Test::Unit and MiniTest. 34 | 35 | Changes: 36 | 37 | * Happy Birthday. 38 | -------------------------------------------------------------------------------- /.index: -------------------------------------------------------------------------------- 1 | --- 2 | revision: 2013 3 | type: ruby 4 | sources: 5 | - var 6 | authors: 7 | - name: Trans 8 | email: transfire@gmail.com 9 | organizations: [] 10 | requirements: 11 | - name: rubytest 12 | - groups: 13 | - build 14 | development: true 15 | name: detroit 16 | - groups: 17 | - build 18 | development: true 19 | name: ergo 20 | - groups: 21 | - test 22 | development: true 23 | name: qed 24 | conflicts: [] 25 | alternatives: [] 26 | resources: 27 | - type: home 28 | uri: http://rubyworks.github.com/microtest 29 | label: Homepage 30 | - type: code 31 | uri: http://github.com/rubyworks/microtest 32 | label: Source Code 33 | repositories: 34 | - name: upstream 35 | scm: git 36 | uri: git://github.com/rubyworks/microtest.git 37 | categories: [] 38 | copyrights: 39 | - holder: Rubyworks 40 | year: '2011' 41 | license: BSD-2-Clause 42 | customs: [] 43 | paths: 44 | lib: 45 | - lib 46 | summary: Microminal TestUnit-style Test Framework 47 | title: MicroTest 48 | version: 0.2.1 49 | name: microtest 50 | description: ! 'MicroTest is a very small Test::Unit/MiniTest compatbile 51 | 52 | test framework that runs on top of RubyTest, a Universal 53 | 54 | Test Harness for Ruby.' 55 | date: '2013-03-10' 56 | -------------------------------------------------------------------------------- /License.txt: -------------------------------------------------------------------------------- 1 | MicroTest - Traditional Style Test Framework 2 | http://rubyworks.github.com/microtest 3 | 4 | Copyright (c) 2011 RubyWorks. All rights reserved. 5 | 6 | BSD-2-Clause (aka FreeBSD) License 7 | 8 | Redistribution and use in source and binary forms, with or without 9 | modification, are permitted provided that the following conditions are met: 10 | 11 | 1. Redistributions of source code must retain the above copyright notice, 12 | this list of conditions and the following disclaimer. 13 | 14 | 2. Redistributions in binary form must reproduce the above copyright 15 | notice, this list of conditions and the following disclaimer in the 16 | documentation and/or other materials provided with the distribution. 17 | 18 | THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 19 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 20 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 21 | COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 22 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 26 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 27 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MicroTest 2 | 3 | [![Gem Version](https://badge.fury.io/rb/microtest.png)](http://badge.fury.io/rb/microtest) 4 | [![Build Status](https://secure.travis-ci.org/rubyworks/microtest.png)](http://travis-ci.org/rubyworks/microtest)     5 | [![Flattr Me](http://api.flattr.com/button/flattr-badge-large.png)](http://flattr.com/thing/324911/Rubyworks-Ruby-Development-Fund) 6 | 7 | [Website](http://rubyworks.github.com/microtest) · 8 | [API](http://rubydoc.info/gems/microtest) · 9 | [Report Issue](http://github.com/rubyworks/microtest/issues) · 10 | [Source Code](http://github.com/rubyworks/microtest) 11 | 12 | 13 | ## Description 14 | 15 | MicroTest is a minimal Test::Unit and MiniTest compatible 16 | test framework that runs on top of Ruby Test. 17 | 18 | 19 | ## Installation 20 | 21 | Using Rubygems: 22 | 23 | $ gem install microtest 24 | 25 | 26 | ## Instruction 27 | 28 | Tests are written in the same manner as they are for Ruby's 29 | traditional test framework(s). The only significant difference 30 | is that an assertions framework library needs to be required 31 | along with the test library itself. MicroTest comes with a 32 | traditional assertions system for backward compatability 33 | with TestUnit and MiniTest. Simply require `microtest/assertion` 34 | to get it. Alternatively any BRASS compliant assertion framework 35 | can be used. 36 | 37 | ```ruby 38 | require 'microtest' 39 | require 'microtest/assertions' 40 | 41 | class ExampleTest < MicroTest::TestCase 42 | 43 | # 44 | def setup 45 | @a = 1 46 | end 47 | 48 | # 49 | def test_alpha_is_one 50 | assert_equal(1, @a) 51 | end 52 | 53 | end 54 | ``` 55 | 56 | For drop in compatibility with Test::Unit, load `microtest/testunit`. 57 | 58 | ```ruby 59 | require 'microtest/testunit' 60 | require 'microtest/assertions' 61 | 62 | class ExampleTest < Test::Unit::TestCase 63 | ... 64 | end 65 | ``` 66 | 67 | To run tests use the `rubytest` command line utility. 68 | 69 | 70 | $ rubytest -Ilib test/test_example.rb 71 | 72 | 73 | See [RubyTest](http://rubyworks.github.com/rubytest) for more details on this. 74 | 75 | 76 | ## License 77 | 78 | Copyright (c) 2011 Rubyworks 79 | 80 | MicroTest is distributes under the terms of the **FreeBSD** license. 81 | 82 | See License.txt for details. 83 | 84 | -------------------------------------------------------------------------------- /lib/microtest.rb: -------------------------------------------------------------------------------- 1 | module MicroTest 2 | 3 | # 4 | # Output naturalized test names instead of just method names. 5 | # 6 | def self.natural_names 7 | @natural_names ||= nil 8 | end 9 | 10 | # 11 | # Set flag to output naturalized test names instead of just method names. 12 | # 13 | def self.natural_names=(boolean) 14 | @natural_names = !!boolean 15 | end 16 | 17 | # The World serves as a base class in which the end-tester can 18 | # add univerally available test helpers for all test cases. 19 | class World 20 | end 21 | 22 | # 23 | class TestCase < World 24 | 25 | # 26 | # When the TestCase class is inherited, a new instance is 27 | # automatically created. 28 | # 29 | def self.inherited(subclass) 30 | subclass.new 31 | end 32 | 33 | # 34 | # Create a new test and add it the the $TEST_SUITE global variable. 35 | # 36 | def self.new(*a,&b) 37 | $TEST_SUITE << super(*a,&b) 38 | end 39 | 40 | # 41 | # Returns name of testcase class. 42 | # 43 | def to_s 44 | self.class.name 45 | end 46 | 47 | # 48 | # Wrap test case run. 49 | # 50 | # @todo: Support setup-all and teardown-all in future ? 51 | # 52 | def call(&cont) 53 | #setup_all 54 | cont.call 55 | #teardown_all 56 | end 57 | 58 | # 59 | # Iterate over each test. 60 | # 61 | def each 62 | methods.each do |m| 63 | next unless m.to_s.start_with?('test_') 64 | yield(TestMethod.new(self, method(m))) 65 | end 66 | end 67 | 68 | # 69 | # No-op for test setup routine. 70 | # 71 | def setup 72 | end 73 | 74 | # 75 | # No-op for test teardown routine. 76 | # 77 | def teardown 78 | end 79 | 80 | end 81 | 82 | # 83 | # Encapsualtes test method for execution by RubyTest. Mainly 84 | # this separate encapsulation allows the test description to 85 | # be something other than just the method name, e.g. if the 86 | # `MicroTest.natural_names` flag is set to true. 87 | # 88 | class TestMethod 89 | def initialize(testcase, method) 90 | @testcase = testcase 91 | @method = method 92 | end 93 | 94 | def call 95 | @testcase.setup 96 | @method.call 97 | @testcase.teardown 98 | end 99 | 100 | def to_s 101 | name = @method.name.to_s 102 | name.gsub!('_', ' ') if MicroTest.natural_names 103 | return name 104 | end 105 | end 106 | 107 | end 108 | -------------------------------------------------------------------------------- /.gemspec: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | 3 | require 'yaml' 4 | require 'pathname' 5 | 6 | module Indexer 7 | 8 | # Convert index data into a gemspec. 9 | # 10 | # Notes: 11 | # * Assumes all executables are in bin/. 12 | # * Does not yet handle default_executable setting. 13 | # * Does not yet handle platform setting. 14 | # * Does not yet handle required_ruby_version. 15 | # * Support for rdoc entries is weak. 16 | # 17 | class GemspecExporter 18 | 19 | # File globs to include in package --unless a manifest file exists. 20 | FILES = ".index .yardopts alt bin data demo ext features lib man spec test try* [A-Z]*.*" unless defined?(FILES) 21 | 22 | # File globs to omit from FILES. 23 | OMIT = "Config.rb" unless defined?(OMIT) 24 | 25 | # Standard file patterns. 26 | PATTERNS = { 27 | :root => '{.index,Gemfile}', 28 | :bin => 'bin/*', 29 | :lib => 'lib/{**/}*', #.rb', 30 | :ext => 'ext/{**/}extconf.rb', 31 | :doc => '*.{txt,rdoc,md,markdown,tt,textile}', 32 | :test => '{test,spec}/{**/}*.rb' 33 | } unless defined?(PATTERNS) 34 | 35 | # For which revision of indexer spec is this converter intended? 36 | REVISION = 2013 unless defined?(REVISION) 37 | 38 | # 39 | def self.gemspec 40 | new.to_gemspec 41 | end 42 | 43 | # 44 | attr :metadata 45 | 46 | # 47 | def initialize(metadata=nil) 48 | @root_check = false 49 | 50 | if metadata 51 | root_dir = metadata.delete(:root) 52 | if root_dir 53 | @root = root_dir 54 | @root_check = true 55 | end 56 | metadata = nil if metadata.empty? 57 | end 58 | 59 | @metadata = metadata || YAML.load_file(root + '.index') 60 | 61 | if @metadata['revision'].to_i != REVISION 62 | warn "This gemspec exporter was not designed for this revision of index metadata." 63 | end 64 | end 65 | 66 | # 67 | def has_root? 68 | root ? true : false 69 | end 70 | 71 | # 72 | def root 73 | return @root if @root || @root_check 74 | @root_check = true 75 | @root = find_root 76 | end 77 | 78 | # 79 | def manifest 80 | return nil unless root 81 | @manifest ||= Dir.glob(root + 'manifest{,.txt}', File::FNM_CASEFOLD).first 82 | end 83 | 84 | # 85 | def scm 86 | return nil unless root 87 | @scm ||= %w{git hg}.find{ |m| (root + ".#{m}").directory? }.to_sym 88 | end 89 | 90 | # 91 | def files 92 | return [] unless root 93 | @files ||= \ 94 | if manifest 95 | File.readlines(manifest). 96 | map{ |line| line.strip }. 97 | reject{ |line| line.empty? || line[0,1] == '#' } 98 | else 99 | list = [] 100 | Dir.chdir(root) do 101 | FILES.split(/\s+/).each do |pattern| 102 | list.concat(glob(pattern)) 103 | end 104 | OMIT.split(/\s+/).each do |pattern| 105 | list = list - glob(pattern) 106 | end 107 | end 108 | list 109 | end.select{ |path| File.file?(path) }.uniq 110 | end 111 | 112 | # 113 | def glob_files(pattern) 114 | return [] unless root 115 | Dir.chdir(root) do 116 | Dir.glob(pattern).select do |path| 117 | File.file?(path) && files.include?(path) 118 | end 119 | end 120 | end 121 | 122 | def patterns 123 | PATTERNS 124 | end 125 | 126 | def executables 127 | @executables ||= \ 128 | glob_files(patterns[:bin]).map do |path| 129 | File.basename(path) 130 | end 131 | end 132 | 133 | def extensions 134 | @extensions ||= \ 135 | glob_files(patterns[:ext]).map do |path| 136 | File.basename(path) 137 | end 138 | end 139 | 140 | def name 141 | metadata['name'] || metadata['title'].downcase.gsub(/\W+/,'_') 142 | end 143 | 144 | def homepage 145 | page = ( 146 | metadata['resources'].find{ |r| r['type'] =~ /^home/i } || 147 | metadata['resources'].find{ |r| r['name'] =~ /^home/i } || 148 | metadata['resources'].find{ |r| r['name'] =~ /^web/i } 149 | ) 150 | page ? page['uri'] : false 151 | end 152 | 153 | def licenses 154 | metadata['copyrights'].map{ |c| c['license'] }.compact 155 | end 156 | 157 | def require_paths 158 | paths = metadata['paths'] || {} 159 | paths['load'] || ['lib'] 160 | end 161 | 162 | # 163 | # Convert to gemnspec. 164 | # 165 | def to_gemspec 166 | if has_root? 167 | Gem::Specification.new do |gemspec| 168 | to_gemspec_data(gemspec) 169 | to_gemspec_paths(gemspec) 170 | end 171 | else 172 | Gem::Specification.new do |gemspec| 173 | to_gemspec_data(gemspec) 174 | to_gemspec_paths(gemspec) 175 | end 176 | end 177 | end 178 | 179 | # 180 | # Convert pure data settings. 181 | # 182 | def to_gemspec_data(gemspec) 183 | gemspec.name = name 184 | gemspec.version = metadata['version'] 185 | gemspec.summary = metadata['summary'] 186 | gemspec.description = metadata['description'] 187 | 188 | metadata['authors'].each do |author| 189 | gemspec.authors << author['name'] 190 | 191 | if author.has_key?('email') 192 | if gemspec.email 193 | gemspec.email << author['email'] 194 | else 195 | gemspec.email = [author['email']] 196 | end 197 | end 198 | end 199 | 200 | gemspec.licenses = licenses 201 | 202 | requirements = metadata['requirements'] || [] 203 | requirements.each do |req| 204 | next if req['optional'] 205 | next if req['external'] 206 | 207 | name = req['name'] 208 | groups = req['groups'] || [] 209 | 210 | version = gemify_version(req['version']) 211 | 212 | if groups.empty? or groups.include?('runtime') 213 | # populate runtime dependencies 214 | if gemspec.respond_to?(:add_runtime_dependency) 215 | gemspec.add_runtime_dependency(name,*version) 216 | else 217 | gemspec.add_dependency(name,*version) 218 | end 219 | else 220 | # populate development dependencies 221 | if gemspec.respond_to?(:add_development_dependency) 222 | gemspec.add_development_dependency(name,*version) 223 | else 224 | gemspec.add_dependency(name,*version) 225 | end 226 | end 227 | end 228 | 229 | # convert external dependencies into gemspec requirements 230 | requirements.each do |req| 231 | next unless req['external'] 232 | gemspec.requirements << ("%s-%s" % req.values_at('name', 'version')) 233 | end 234 | 235 | gemspec.homepage = homepage 236 | gemspec.require_paths = require_paths 237 | gemspec.post_install_message = metadata['install_message'] 238 | end 239 | 240 | # 241 | # Set gemspec settings that require a root directory path. 242 | # 243 | def to_gemspec_paths(gemspec) 244 | gemspec.files = files 245 | gemspec.extensions = extensions 246 | gemspec.executables = executables 247 | 248 | if Gem::VERSION < '1.7.' 249 | gemspec.default_executable = gemspec.executables.first 250 | end 251 | 252 | gemspec.test_files = glob_files(patterns[:test]) 253 | 254 | unless gemspec.files.include?('.document') 255 | gemspec.extra_rdoc_files = glob_files(patterns[:doc]) 256 | end 257 | end 258 | 259 | # 260 | # Return a copy of this file. This is used to generate a local 261 | # .gemspec file that can automatically read the index file. 262 | # 263 | def self.source_code 264 | File.read(__FILE__) 265 | end 266 | 267 | private 268 | 269 | def find_root 270 | root_files = patterns[:root] 271 | if Dir.glob(root_files).first 272 | Pathname.new(Dir.pwd) 273 | elsif Dir.glob("../#{root_files}").first 274 | Pathname.new(Dir.pwd).parent 275 | else 276 | #raise "Can't find root of project containing `#{root_files}'." 277 | warn "Can't find root of project containing `#{root_files}'." 278 | nil 279 | end 280 | end 281 | 282 | def glob(pattern) 283 | if File.directory?(pattern) 284 | Dir.glob(File.join(pattern, '**', '*')) 285 | else 286 | Dir.glob(pattern) 287 | end 288 | end 289 | 290 | def gemify_version(version) 291 | case version 292 | when /^(.*?)\+$/ 293 | ">= #{$1}" 294 | when /^(.*?)\-$/ 295 | "< #{$1}" 296 | when /^(.*?)\~$/ 297 | "~> #{$1}" 298 | else 299 | version 300 | end 301 | end 302 | 303 | end 304 | 305 | end 306 | 307 | Indexer::GemspecExporter.gemspec -------------------------------------------------------------------------------- /lib/microtest/assertions.rb: -------------------------------------------------------------------------------- 1 | module MicroTest 2 | 3 | # Legacy Assertions 4 | # 5 | # This module provides a compatibility layer for Test::Unit/MiniTest. 6 | # This is an optional module and must be loaded on it's own. 7 | # 8 | # Note that two methods are not provided, +#assert_nothing_raised+, 9 | # and +#assert_nothing_thrown+. 10 | # 11 | module Assertions 12 | 13 | # Private method upon which all of the legacy assertions are based 14 | # (except for #assert itself). 15 | # 16 | # @raise [Assertion] If test fails. 17 | # 18 | # @return nothing 19 | def __assert__(test, msg=nil) 20 | msg = "failed assertion (no message given)" unless msg 21 | raise Assertion.new(msg, :backtrace=>caller[1..-1]) unless test 22 | end 23 | 24 | private :__assert__ 25 | 26 | 27 | # The assertion upon which all other assertions are based. 28 | # 29 | # @example 30 | # assert [1, 2].include?(5) 31 | # 32 | # @raise [Assertion] if `test` is true. 33 | def assert(test, msg=nil) 34 | msg = "failed assertion (no message given)" unless msg 35 | raise Assertion.new(msg, :backtrace=>caller) unless test 36 | end 37 | 38 | # The opposite of assert. 39 | # 40 | # @example 41 | # assert [1, 2].include?(1) 42 | # 43 | # @raise [Assertion] if `test` is false. 44 | def refute(test, msg=nil) 45 | msg = "failed assertion (no message given)" unless msg 46 | raise Assertion.new(msg, :backtrace=>caller) if test 47 | end 48 | 49 | 50 | # Passes if the block yields true. 51 | # 52 | # @example 53 | # assert_block "Couldn't do the thing" do 54 | # do_the_thing 55 | # end 56 | # 57 | # @raise [Assertion] if test fails 58 | # 59 | # @return nothing 60 | def assert_block(msg=nil) # :yields: 61 | test = ! yield 62 | msg = "assertion failed" unless msg 63 | __assert__(test, msg) 64 | end 65 | 66 | # Passes if the block yields false. 67 | # 68 | # @example 69 | # refute_block "Could do the thing" do 70 | # do_the_thing 71 | # end 72 | # 73 | # @raise [Assertion] if test fails 74 | # 75 | # @return nothing 76 | def refute_block(msg=nil) # :yields: 77 | test = ! yield 78 | msg = "refutation failed" unless msg 79 | __assert__(!test, msg) 80 | end 81 | 82 | 83 | # Passes if expected equals actual (using #== operator). 84 | # 85 | # Note that the ordering of arguments is important, 86 | # since a helpful error message is generated when this 87 | # one fails that tells you the values of expected and actual. 88 | # 89 | # @example 90 | # assert_equal 'MY STRING', 'my string'.upcase 91 | # 92 | # @raise [Assertion] if test fails 93 | # 94 | # @return nothing 95 | def assert_equal(exp, act, msg=nil) 96 | test = (exp == act) 97 | msg = "Expected #{act.inspect} to be equal to #{exp.inspect}" unless msg 98 | __assert__(test, msg) 99 | end 100 | 101 | # Passes if expected does not equal actual (using #== operator). 102 | # 103 | # @example 104 | # refute_equal 'some string', 5 105 | # 106 | # @raise [Assertion] if test fails 107 | # 108 | # @return nothing 109 | def refute_equal(exp, act, msg=nil) 110 | test = (exp == act) 111 | msg = "Expected #{act.inspect} to not be equal to #{exp.inspect}" unless msg 112 | __assert__(!test, msg) 113 | end 114 | 115 | # Passes if expected does not equal actual (using #!= operator). 116 | # 117 | # @example 118 | # assert_not_equal 'some string', 5 119 | # 120 | # @raise [Assertion] if test fails 121 | # 122 | # @return nothing 123 | def assert_not_equal(exp, act, msg=nil) 124 | test = (exp != act) 125 | msg = "Expected #{act.inspect} to not be equal to #{exp.inspect}" unless msg 126 | __assert__(test, msg) 127 | end 128 | 129 | 130 | # Passes if expected_float and actual_float are equal within delta tolerance. 131 | # 132 | # @example 133 | # assert_in_delta 0.05, (50000.0 / 10**6), 0.00001 134 | # 135 | # @raise [Assertion] if test fails 136 | # 137 | # @return nothing 138 | def assert_in_delta(exp, act, delta, msg=nil) 139 | test = (exp.to_f - act.to_f).abs <= delta.to_f 140 | msg = "Expected #{exp} to be within #{delta} of #{act}" unless msg 141 | __assert__(test, msg) 142 | end 143 | 144 | # Passes if expected_float and actual_float are equal within delta tolerance. 145 | # 146 | # @example 147 | # refute_in_delta 0.05, (50000.0 / 10**6), 0.01 148 | # 149 | # @raise [Assertion] if test fails 150 | # 151 | # @return nothing 152 | def refute_in_delta(exp, act, delta, msg=nil) 153 | test = (exp.to_f - act.to_f).abs <= delta.to_f 154 | msg = "Expected #{exp} to not be within #{delta} of #{act}" unless msg 155 | __assert__(!test, msg) 156 | end 157 | 158 | 159 | # Passes if object is an instance of a class. 160 | # 161 | # @example 162 | # assert_instance_of String, 'foo' 163 | # 164 | # @raise [Assertion] if test fails 165 | # 166 | # @return nothing 167 | def assert_instance_of(cls, obj, msg=nil) 168 | test = (cls === obj) 169 | msg = "Expected #{obj} to be a #{cls}" unless msg 170 | __assert__(test, msg) 171 | end 172 | 173 | # Passes if object is not an instance of a class. 174 | # 175 | # @example 176 | # refute_instance_of(String, :foo) 177 | # 178 | # @raise [Assertion] if test fails 179 | # 180 | # @return nothing 181 | def refute_instance_of(cls, obj, msg=nil) 182 | test = (cls === obj) 183 | msg = "Expected #{obj} to not be a #{cls}" unless msg 184 | __assert__(!test, msg) 185 | end 186 | 187 | alias_method :assert_not_instance_of, :refute_instance_of 188 | 189 | 190 | # Passes if object is a kind of a class. 191 | # 192 | # @example 193 | # assert_kind_of Object, 'foo' 194 | # 195 | # @raise [Assertion] if test fails 196 | # 197 | # @return nothing 198 | def assert_kind_of(cls, obj, msg=nil) 199 | test = obj.kind_of?(cls) 200 | msg = "Expected #{obj.inspect} to be a kind of #{cls}" unless msg 201 | __assert__(test, msg) 202 | end 203 | 204 | # Passes if object is not a kind of a class. 205 | # 206 | # @example 207 | # refute_kind_of(Numeric, 'foo') 208 | # 209 | # @raise [Assertion] if test fails 210 | # 211 | # @return nothing 212 | def refute_kind_of(cls, obj, msg=nil) 213 | test = obj.kind_of?(cls) 214 | msg = "Expected #{obj.inspect} to not be a kind of #{cls}" unless msg 215 | __assert__(!test, msg) 216 | end 217 | 218 | alias_method :assert_not_kind_of, :refute_kind_of 219 | 220 | 221 | # Passes if regexp matches a string (using `#=~`). 222 | # 223 | # @example 224 | # assert_match(/\d+/, 'five, 6, seven') 225 | # 226 | # @raise [Assertion] if test fails 227 | # 228 | # @return nothing 229 | def assert_match(exp, act, msg=nil) 230 | test = (act =~ exp) 231 | msg = "Expected #{act.inspect} to match #{exp.inspect}" unless msg 232 | __assert__(test, msg) 233 | end 234 | 235 | # Passes if regexp does not match a string (using `#=~`). 236 | # 237 | # @example 238 | # refute_match(/two/, 'one 2 three') 239 | # 240 | # @raise [Assertion] if test fails 241 | # 242 | # @return nothing 243 | def refute_match(exp, act, msg=nil) 244 | test = (act =~ exp) 245 | msg = "Expected #{act.inspect} to match #{exp.inspect}" unless msg 246 | __assert__(!test, msg) 247 | end 248 | 249 | # Passes if regexp does not match a string (using `#!~`). 250 | # 251 | # @example 252 | # refute_match(/two/, 'one 2 three') 253 | # 254 | # @raise [Assertion] if test fails 255 | # 256 | # @return nothing 257 | def assert_no_match(exp, act, msg=nil) 258 | test = (act !~ exp) 259 | msg = "Expected #{act.inspect} to match #{exp.inspect}" unless msg 260 | __assert__(test, msg) 261 | end 262 | 263 | 264 | # Passes if object is nil. 265 | # 266 | # @example 267 | # assert_nil [1, 2].uniq! 268 | # 269 | # @raise [Assertion] if test fails 270 | # 271 | # @return nothing 272 | def assert_nil(obj, msg=nil) 273 | test = obj.nil? 274 | msg = "Expected #{obj.inspect} to be nil" unless msg 275 | __assert__(test, msg) 276 | end 277 | 278 | # Passes if object is not nil. 279 | # 280 | # @example 281 | # refute_nil [1, 2].first 282 | # 283 | # @raise [Assertion] if test fails 284 | # 285 | # @return nothing 286 | def refute_nil(obj, msg=nil) 287 | test = obj.nil? 288 | msg = "Expected #{obj.inspect} to not be nil" unless msg 289 | __assert__(!test, msg) 290 | end 291 | 292 | alias_method :assert_not_nil, :refute_nil 293 | 294 | 295 | # Compares the +object1+ with +object2+ using operator. 296 | # 297 | # Passes if object1.send(operator, object2) is true. 298 | # 299 | # @example 300 | # assert_operator 5, :>=, 4 301 | # 302 | # @raise [Assertion] if test fails 303 | # 304 | # @return nothing 305 | def assert_operator(o1, op, o2, msg="") 306 | test = o1.__send__(op, o2) 307 | msg = "Expected #{o1}.#{op}(#{o2}) to be true" unless msg 308 | __assert__(test, msg) 309 | end 310 | 311 | # Compares the +object1+ with +object2+ using operator. 312 | # 313 | # Passes if object1.send(operator, object2) is false. 314 | # 315 | # @example 316 | # assert_operator 5, :>=, 4 317 | # 318 | # @raise [Assertion] if test fails 319 | # 320 | # @return nothing 321 | def refute_operator(o1, op, o2, msg="") 322 | test = o1.__send__(op, o2) 323 | msg = "Expected #{o1}.#{op}(#{o2}) to be false" unless msg 324 | __assert__(!test, msg) 325 | end 326 | 327 | alias_method :assert_not_operator, :refute_operator 328 | 329 | 330 | # Passes if the block raises one of the given exceptions. 331 | # 332 | # @example 333 | # assert_raise RuntimeError, LoadError do 334 | # raise 'Boom!!!' 335 | # end 336 | # 337 | # @raise [Assertion] if test fails 338 | # 339 | # @return nothing 340 | def assert_raises(*args) 341 | msg = (Module === args.last ? nil : args.pop) 342 | begin 343 | yield 344 | msg = "Expected one of #{args.join(', ')} to be raised" unless msg 345 | __assert__(false, msg) 346 | rescue Exception => err 347 | test = args.any?{ |e| e === err } 348 | msg = "Expected one of #{args.join(', ')} to be raised, but got #{err.class}" unless msg 349 | __assert__(test, msg) 350 | return err 351 | end 352 | end 353 | 354 | alias_method :assert_raise, :assert_raises 355 | 356 | # Provides a way to assert that a procedure 357 | # does not raise an exception. 358 | # 359 | # @example 360 | # refute_raises(StandardError){ raise } 361 | # 362 | #def refute_raises(exception, &block) 363 | # begin 364 | # block.call(*a) 365 | # rescue exception 366 | # raise Assertion 367 | # end 368 | #end 369 | 370 | #alias_method :assert_not_raises, :refute_raises 371 | 372 | 373 | # Passes if +object+ respond_to? +method+. 374 | # 375 | # @example 376 | # assert_respond_to 'bugbear', :slice 377 | # 378 | # @raise [Assertion] if test fails 379 | # 380 | # @return nothing 381 | def assert_respond_to(obj, meth, msg=nil) 382 | msg = "Expected #{obj} (#{obj.class}) to respond to ##{meth}" unless msg 383 | #flip = (Symbol === obj) && ! (Symbol === meth) # HACK for specs 384 | #obj, meth = meth, obj if flip 385 | test = obj.respond_to?(meth) 386 | __assert__(test, msg) 387 | end 388 | 389 | # Passes if `object` does not respond to `method`. 390 | # 391 | # @example 392 | # refute_respond_to 'bugbear', :slice 393 | # 394 | # @raise [Assertion] if test fails 395 | # 396 | # @return nothing 397 | def refute_respond_to(obj, meth, msg=nil) 398 | msg = "Expected #{obj} (#{obj.class}) to not respond to ##{meth}" unless msg 399 | #flip = (Symbol === obj) && ! (Symbol === meth) # HACK for specs 400 | #obj, meth = meth, obj if flip 401 | test = obj.respond_to?(meth) 402 | __assert__(!test, msg) 403 | end 404 | 405 | alias_method :assert_not_respond_to, :refute_respond_to 406 | 407 | 408 | # Passes if +actual+ .equal? +expected+ (i.e. they are the same instance). 409 | # 410 | # @example 411 | # o = Object.new 412 | # assert_same(o, o) 413 | # 414 | # @raise [Assertion] if test fails 415 | # 416 | # @return nothing 417 | def assert_same(exp, act, msg=nil) 418 | msg = "Expected #{act.inspect} to be the same as #{exp.inspect}" unless msg 419 | test = exp.equal?(act) 420 | __assert__(test, msg) 421 | end 422 | 423 | # Passes if ! actual .equal? expected (i.e. they are the same instance). 424 | # 425 | # @example 426 | # refute_same Object.new, Object.new 427 | # 428 | # @raise [Assertion] if test fails 429 | # 430 | # @return nothing 431 | def refute_same(exp, act, msg=nil) 432 | test = exp.equal?(act) 433 | msg = "Expected #{act.inspect} to not be the same as #{exp.inspect}" unless msg 434 | __assert__(!test, msg) 435 | end 436 | 437 | alias_method :assert_not_same, :refute_same 438 | 439 | 440 | # Passes if the method send returns a true value. 441 | # The parameter +send_array+ is composed of: 442 | # 443 | # * A receiver 444 | # * A method 445 | # * Arguments to the method 446 | # 447 | # @example 448 | # assert_send [[1, 2], :include?, 2] 449 | # 450 | # @raise [Assertion] if test fails 451 | # 452 | # @return nothing 453 | def assert_send(send_array, msg=nil) 454 | r, m, *args = *send_array 455 | test = r.__send__(m, *args) 456 | msg = "Expected #{r}.#{m}(*#{args.inspect}) to return true" unless msg 457 | __assert__(test, msg) 458 | end 459 | 460 | # Passes if the method send returns a true value. 461 | # The parameter +send_array+ is composed of: 462 | # 463 | # * A receiver 464 | # * A method 465 | # * Arguments to the method 466 | # 467 | # @example 468 | # refute_send [[1, 2], :include?, 4] 469 | # 470 | # @raise [Assertion] if test fails 471 | # 472 | # @return nothing 473 | def refute_send(send_array, msg=nil) 474 | r, m, *args = *send_array 475 | test = r.__send__(m, *args) 476 | msg = "Expected #{r}.#{m}(*#{args.inspect}) to return false" unless msg 477 | __assert__(!test, msg) 478 | end 479 | 480 | alias_method :assert_not_send, :refute_send 481 | 482 | 483 | # Passes if the block throws expected symbol. 484 | # 485 | # @example 486 | # assert_throws :done do 487 | # throw :done 488 | # end 489 | # 490 | # @raise [Assertion] if test fails 491 | # 492 | # @return nothing 493 | def assert_throws(sym, msg=nil) 494 | msg = "Expected #{sym} to have been thrown" unless msg 495 | test = true 496 | catch(sym) do 497 | begin 498 | yield 499 | rescue ArgumentError => e # 1.9 exception 500 | default += ", not #{e.message.split(/ /).last}" 501 | rescue NameError => e # 1.8 exception 502 | default += ", not #{e.name.inspect}" 503 | end 504 | test = false 505 | end 506 | __assert__(test, msg) 507 | end 508 | 509 | # Passes if the block does not throw expected symbol. 510 | # 511 | # @example 512 | # refute_throws :done do 513 | # throw :another 514 | # end 515 | # 516 | # @todo This implementation may well be incorrect. 517 | # 518 | # @raise [Assertion] if test fails 519 | # 520 | # @return nothing 521 | def refute_throws(sym, msg=nil) 522 | msg = "Expected #{sym} to not have been thrown" unless msg 523 | test = true 524 | catch(sym) do 525 | begin 526 | yield 527 | rescue ArgumentError => e # 1.9 exception 528 | default += ", not #{e.message.split(/ /).last}" 529 | rescue NameError => e # 1.8 exception 530 | default += ", not #{e.name.inspect}" 531 | end 532 | test = false 533 | end 534 | __assert__(!test, msg) 535 | end 536 | 537 | 538 | # Flunk always fails. 539 | # 540 | # @example 541 | # flunk 'Not done testing yet.' 542 | # 543 | # @raise [Assertion] always 544 | # 545 | # @return nothing 546 | def flunk(msg=nil) 547 | __assert__(false, msg) 548 | end 549 | 550 | end 551 | 552 | # 553 | class World 554 | include Assertions 555 | end 556 | 557 | end 558 | --------------------------------------------------------------------------------