assert 0.2.1 → 0.3.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/Gemfile.lock +1 -1
- data/lib/assert/context.rb +14 -2
- data/lib/assert/test.rb +37 -16
- data/lib/assert/version.rb +1 -1
- data/lib/assert/view/terminal.rb +18 -3
- data/test/context/class_methods_test.rb +47 -1
- data/test/test_test.rb +56 -1
- metadata +9 -9
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/lib/assert/context.rb
    CHANGED
    
    | @@ -80,12 +80,12 @@ module Assert | |
| 80 80 | 
             
                    end
         | 
| 81 81 | 
             
                  end
         | 
| 82 82 |  | 
| 83 | 
            -
                  def  | 
| 83 | 
            +
                  def test(desc_or_macro, &block)
         | 
| 84 84 | 
             
                    if desc_or_macro.kind_of?(Macro)
         | 
| 85 85 | 
             
                      instance_eval(&desc_or_macro)
         | 
| 86 86 | 
             
                    else
         | 
| 87 87 | 
             
                      raise ArgumentError, "please provide a test block" unless block_given?
         | 
| 88 | 
            -
                      method_name = "test:  | 
| 88 | 
            +
                      method_name = "test: #{desc_or_macro}"
         | 
| 89 89 |  | 
| 90 90 | 
             
                      # instead of using the typical 'method_defined?' pattern (which) checks
         | 
| 91 91 | 
             
                      # all parent contexts, we really just need to make sure the method_name
         | 
| @@ -101,6 +101,18 @@ module Assert | |
| 101 101 | 
             
                    end
         | 
| 102 102 | 
             
                  end
         | 
| 103 103 |  | 
| 104 | 
            +
                  def should(desc_or_macro, &block)
         | 
| 105 | 
            +
                    if !desc_or_macro.kind_of?(Macro)
         | 
| 106 | 
            +
                      desc_or_macro = "should #{desc_or_macro}"
         | 
| 107 | 
            +
                    end
         | 
| 108 | 
            +
                    test(desc_or_macro, &block)
         | 
| 109 | 
            +
                  end
         | 
| 110 | 
            +
             | 
| 111 | 
            +
                  def test_eventually(desc, &block)
         | 
| 112 | 
            +
                    test(desc) { skip }
         | 
| 113 | 
            +
                  end
         | 
| 114 | 
            +
                  alias_method :test_skip, :test_eventually
         | 
| 115 | 
            +
             | 
| 104 116 | 
             
                  def should_eventually(desc, &block)
         | 
| 105 117 | 
             
                    should(desc) { skip }
         | 
| 106 118 | 
             
                  end
         | 
    
        data/lib/assert/test.rb
    CHANGED
    
    | @@ -1,41 +1,51 @@ | |
| 1 1 | 
             
            require 'assert/result'
         | 
| 2 2 | 
             
            require 'assert/result_set'
         | 
| 3 | 
            +
            require 'assert/options'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            require 'stringio'
         | 
| 3 6 |  | 
| 4 7 | 
             
            module Assert
         | 
| 5 8 | 
             
              class Test
         | 
| 9 | 
            +
                include Assert::Options
         | 
| 10 | 
            +
                options do
         | 
| 11 | 
            +
                  default_capture_out true
         | 
| 12 | 
            +
                end
         | 
| 6 13 |  | 
| 7 14 | 
             
                # a Test is some code/method to run in the scope of a Context.  After a
         | 
| 8 15 | 
             
                # a test runs, it should have some assertions which are its results.
         | 
| 9 16 |  | 
| 10 17 | 
             
                attr_reader :name, :code, :context_class
         | 
| 11 | 
            -
                attr_accessor :results
         | 
| 18 | 
            +
                attr_accessor :results, :output
         | 
| 12 19 |  | 
| 13 20 | 
             
                def initialize(name, context_class, code = nil, &block)
         | 
| 14 21 | 
             
                  @context_class = context_class
         | 
| 15 22 | 
             
                  @name = name_from_context(name)
         | 
| 16 23 | 
             
                  @code = (code || block)
         | 
| 17 24 | 
             
                  @results = ResultSet.new
         | 
| 25 | 
            +
                  @output = ""
         | 
| 18 26 | 
             
                end
         | 
| 19 27 |  | 
| 20 28 | 
             
                def run(view=nil)
         | 
| 21 29 | 
             
                  @results.view = view
         | 
| 22 30 | 
             
                  run_scope = @context_class.new(self)
         | 
| 23 | 
            -
                   | 
| 24 | 
            -
                    @context_class.setup(run_scope)
         | 
| 25 | 
            -
                    if @code.kind_of?(::Proc)
         | 
| 26 | 
            -
                      run_scope.instance_eval(&@code)
         | 
| 27 | 
            -
                    elsif run_scope.respond_to?(@code.to_s)
         | 
| 28 | 
            -
                      run_scope.send(@code.to_s)
         | 
| 29 | 
            -
                    end
         | 
| 30 | 
            -
                  rescue Result::TestSkipped => err
         | 
| 31 | 
            -
                    @results << Result::Skip.new(self.name, err)
         | 
| 32 | 
            -
                  rescue Exception => err
         | 
| 33 | 
            -
                    @results << Result::Error.new(self.name, err)
         | 
| 34 | 
            -
                  ensure
         | 
| 31 | 
            +
                  capture_out(StringIO.new(@output, "w+")) do
         | 
| 35 32 | 
             
                    begin
         | 
| 36 | 
            -
                      @context_class. | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 33 | 
            +
                      @context_class.setup(run_scope)
         | 
| 34 | 
            +
                      if @code.kind_of?(::Proc)
         | 
| 35 | 
            +
                        run_scope.instance_eval(&@code)
         | 
| 36 | 
            +
                      elsif run_scope.respond_to?(@code.to_s)
         | 
| 37 | 
            +
                        run_scope.send(@code.to_s)
         | 
| 38 | 
            +
                      end
         | 
| 39 | 
            +
                    rescue Result::TestSkipped => err
         | 
| 40 | 
            +
                      @results << Result::Skip.new(self.name, err)
         | 
| 41 | 
            +
                    rescue Exception => err
         | 
| 42 | 
            +
                      @results << Result::Error.new(self.name, err)
         | 
| 43 | 
            +
                    ensure
         | 
| 44 | 
            +
                      begin
         | 
| 45 | 
            +
                        @context_class.teardown(run_scope)
         | 
| 46 | 
            +
                      rescue Exception => teardown_err
         | 
| 47 | 
            +
                        @results << Result::Error.new(self.name, teardown_err)
         | 
| 48 | 
            +
                      end
         | 
| 39 49 | 
             
                    end
         | 
| 40 50 | 
             
                  end
         | 
| 41 51 | 
             
                  @results.view = nil
         | 
| @@ -69,6 +79,17 @@ module Assert | |
| 69 79 |  | 
| 70 80 | 
             
                protected
         | 
| 71 81 |  | 
| 82 | 
            +
                def capture_out(io, &block)
         | 
| 83 | 
            +
                  if self.class.options.capture_out && io
         | 
| 84 | 
            +
                    orig_stdout = $stdout.clone
         | 
| 85 | 
            +
                    $stdout = io
         | 
| 86 | 
            +
                    block.call
         | 
| 87 | 
            +
                    $stdout = orig_stdout
         | 
| 88 | 
            +
                  else
         | 
| 89 | 
            +
                    block.call
         | 
| 90 | 
            +
                  end
         | 
| 91 | 
            +
                end
         | 
| 92 | 
            +
             | 
| 72 93 | 
             
                def name_from_context(name)
         | 
| 73 94 | 
             
                  [ @context_class.description,
         | 
| 74 95 | 
             
                    name.gsub(/^test:\s+should/, "should")
         | 
    
        data/lib/assert/version.rb
    CHANGED
    
    
    
        data/lib/assert/view/terminal.rb
    CHANGED
    
    | @@ -45,9 +45,15 @@ module Assert::View | |
| 45 45 | 
             
                end
         | 
| 46 46 |  | 
| 47 47 | 
             
                def detailed_results
         | 
| 48 | 
            -
                  details = self.suite. | 
| 49 | 
            -
                     | 
| 50 | 
            -
             | 
| 48 | 
            +
                  details = self.suite.ordered_tests.reverse.collect do |test|
         | 
| 49 | 
            +
                    test.results.collect do |result|
         | 
| 50 | 
            +
                      if show_result_details?(result)
         | 
| 51 | 
            +
                        [ result_io_msg(result.to_s, result.to_sym),
         | 
| 52 | 
            +
                          output_io_msg(test.output.to_s)
         | 
| 53 | 
            +
                        ].join("\n")
         | 
| 54 | 
            +
                      end
         | 
| 55 | 
            +
                    end
         | 
| 56 | 
            +
                  end.flatten.compact
         | 
| 51 57 | 
             
                  "\n\n" + details.join("\n\n") if !details.empty?
         | 
| 52 58 | 
             
                end
         | 
| 53 59 |  | 
| @@ -93,6 +99,15 @@ module Assert::View | |
| 93 99 | 
             
                  io_msg(msg, :term_styles => term_styles)
         | 
| 94 100 | 
             
                end
         | 
| 95 101 |  | 
| 102 | 
            +
                def output_io_msg(output)
         | 
| 103 | 
            +
                  if output && !output.empty?
         | 
| 104 | 
            +
                    [ "--- stdout ---",
         | 
| 105 | 
            +
                      io_msg(output),
         | 
| 106 | 
            +
                      "--------------"
         | 
| 107 | 
            +
                    ].collect{|i| i.strip}.join("\n")
         | 
| 108 | 
            +
                  end
         | 
| 109 | 
            +
                end
         | 
| 110 | 
            +
             | 
| 96 111 | 
             
                def io_msg(msg, opts={})
         | 
| 97 112 | 
             
                  val = super
         | 
| 98 113 | 
             
                  if !(style = term_style(*opts[:term_styles])).empty?
         | 
| @@ -15,7 +15,9 @@ class Assert::Context | |
| 15 15 |  | 
| 16 16 | 
             
                should have_instance_methods :setup_once, :before_once, :setup, :before, :setups
         | 
| 17 17 | 
             
                should have_instance_methods :teardown_once, :after_once, :teardown, :after, :teardowns
         | 
| 18 | 
            -
                should have_instance_methods :description, :desc, :subject | 
| 18 | 
            +
                should have_instance_methods :description, :desc, :subject
         | 
| 19 | 
            +
                should have_instance_methods :test, :test_eventually, :test_skip
         | 
| 20 | 
            +
                should have_instance_methods :should, :should_eventually, :should_skip
         | 
| 19 21 | 
             
              end
         | 
| 20 22 |  | 
| 21 23 |  | 
| @@ -236,6 +238,50 @@ class Assert::Context | |
| 236 238 |  | 
| 237 239 |  | 
| 238 240 |  | 
| 241 | 
            +
              class TestMethTest < ClassMethodsTest
         | 
| 242 | 
            +
                desc "test method"
         | 
| 243 | 
            +
                setup do
         | 
| 244 | 
            +
                  should_desc = "be true"
         | 
| 245 | 
            +
                  should_block = @should_block = ::Proc.new{ assert(true) }
         | 
| 246 | 
            +
                  @context_class = Factory.context_class do
         | 
| 247 | 
            +
                    test(should_desc, &should_block)
         | 
| 248 | 
            +
                  end
         | 
| 249 | 
            +
                  @method_name = "test: #{should_desc}"
         | 
| 250 | 
            +
                  @context = @context_class.new(Factory.test("something", @context_class))
         | 
| 251 | 
            +
                end
         | 
| 252 | 
            +
                subject{ @context }
         | 
| 253 | 
            +
             | 
| 254 | 
            +
                should "define a test method named after the should desc" do
         | 
| 255 | 
            +
                  assert_respond_to @method_name, subject
         | 
| 256 | 
            +
                  assert_equal subject.instance_eval(&@should_block), subject.send(@method_name)
         | 
| 257 | 
            +
                end
         | 
| 258 | 
            +
             | 
| 259 | 
            +
              end
         | 
| 260 | 
            +
             | 
| 261 | 
            +
              class TestEventuallyTest < ClassMethodsTest
         | 
| 262 | 
            +
                desc "test_eventually method"
         | 
| 263 | 
            +
                setup do
         | 
| 264 | 
            +
                  should_desc = @should_desc = "be true"
         | 
| 265 | 
            +
                  should_block = @should_block = ::Proc.new{ assert(true) }
         | 
| 266 | 
            +
                  @context_class = Factory.context_class do
         | 
| 267 | 
            +
                    test_eventually(should_desc, &should_block)
         | 
| 268 | 
            +
                  end
         | 
| 269 | 
            +
                  @method_name = "test: #{@should_desc}"
         | 
| 270 | 
            +
                  @context = @context_class.new(Factory.test("something", @context_class))
         | 
| 271 | 
            +
                end
         | 
| 272 | 
            +
                subject{ @context }
         | 
| 273 | 
            +
             | 
| 274 | 
            +
                should "define a test method named after the should desc that raises a test skipped" do
         | 
| 275 | 
            +
                  assert_respond_to @method_name, subject
         | 
| 276 | 
            +
                  assert_raises(Assert::Result::TestSkipped) do
         | 
| 277 | 
            +
                    subject.send(@method_name)
         | 
| 278 | 
            +
                  end
         | 
| 279 | 
            +
                end
         | 
| 280 | 
            +
             | 
| 281 | 
            +
              end
         | 
| 282 | 
            +
             | 
| 283 | 
            +
             | 
| 284 | 
            +
             | 
| 239 285 | 
             
              class ShouldTest < ClassMethodsTest
         | 
| 240 286 | 
             
                desc "should method"
         | 
| 241 287 | 
             
                setup do
         | 
    
        data/test/test_test.rb
    CHANGED
    
    | @@ -20,7 +20,7 @@ class Assert::Test | |
| 20 20 | 
             
                subject{ @test }
         | 
| 21 21 |  | 
| 22 22 | 
             
                should have_readers :name, :code, :context_class
         | 
| 23 | 
            -
                should have_accessor :results
         | 
| 23 | 
            +
                should have_accessor :results, :output
         | 
| 24 24 | 
             
                should have_instance_methods :run, :result_count
         | 
| 25 25 | 
             
                should have_instance_methods *Assert::Result.types.keys.collect{|k| "#{k}_results".to_sym }
         | 
| 26 26 |  | 
| @@ -183,4 +183,59 @@ class Assert::Test | |
| 183 183 |  | 
| 184 184 | 
             
              end
         | 
| 185 185 |  | 
| 186 | 
            +
              class CaptureOutTest < BasicTest
         | 
| 187 | 
            +
                desc "when capturing std out"
         | 
| 188 | 
            +
                setup {
         | 
| 189 | 
            +
                  @test = Factory.test("stdout") {
         | 
| 190 | 
            +
                    puts "std out from the test"
         | 
| 191 | 
            +
                    assert true
         | 
| 192 | 
            +
                  }
         | 
| 193 | 
            +
                  @orig_capture = @test.class.options.capture_out
         | 
| 194 | 
            +
                  @test.class.options.capture_out(true)
         | 
| 195 | 
            +
                  @test.run
         | 
| 196 | 
            +
                }
         | 
| 197 | 
            +
                teardown {
         | 
| 198 | 
            +
                  @test.class.options.capture_out(@orig_capture)
         | 
| 199 | 
            +
                }
         | 
| 200 | 
            +
             | 
| 201 | 
            +
                should "capture any io from the test" do
         | 
| 202 | 
            +
                  assert_equal "std out from the test\n", @test.output
         | 
| 203 | 
            +
                end
         | 
| 204 | 
            +
             | 
| 205 | 
            +
              end
         | 
| 206 | 
            +
             | 
| 207 | 
            +
             | 
| 208 | 
            +
             | 
| 209 | 
            +
              class FullOutputCaptureTest < BasicTest
         | 
| 210 | 
            +
                desc "when collecting std out across setup, teardown, and meth calls"
         | 
| 211 | 
            +
                setup do
         | 
| 212 | 
            +
                  @test = Factory.test("fullstdouttest") {
         | 
| 213 | 
            +
                    puts "std out from the test"
         | 
| 214 | 
            +
                    assert a_method_an_assert_calls.inspect
         | 
| 215 | 
            +
                  }
         | 
| 216 | 
            +
                  @test.context_class.setup { puts "std out from the setup" }
         | 
| 217 | 
            +
                  @test.context_class.teardown { puts "std out from the teardown" }
         | 
| 218 | 
            +
                  @test.context_class.send(:define_method, "a_method_an_assert_calls") do
         | 
| 219 | 
            +
                    puts "std out from a method an assert called"
         | 
| 220 | 
            +
                    true
         | 
| 221 | 
            +
                  end
         | 
| 222 | 
            +
             | 
| 223 | 
            +
                  @orig_capture = @test.class.options.capture_out
         | 
| 224 | 
            +
                  @test.class.options.capture_out(true)
         | 
| 225 | 
            +
                  @test.run
         | 
| 226 | 
            +
                end
         | 
| 227 | 
            +
                teardown {
         | 
| 228 | 
            +
                  @test.class.options.capture_out(@orig_capture)
         | 
| 229 | 
            +
                }
         | 
| 230 | 
            +
             | 
| 231 | 
            +
                should "collect it on itself in the output accessor" do
         | 
| 232 | 
            +
                  assert_equal([
         | 
| 233 | 
            +
                    "std out from the setup",
         | 
| 234 | 
            +
                    "std out from the test",
         | 
| 235 | 
            +
                    "std out from a method an assert called",
         | 
| 236 | 
            +
                    "std out from the teardown\n"
         | 
| 237 | 
            +
                  ].join("\n"), @test.output)
         | 
| 238 | 
            +
                end
         | 
| 239 | 
            +
              end
         | 
| 240 | 
            +
             | 
| 186 241 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,13 +1,13 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: assert
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              hash:  | 
| 4 | 
            +
              hash: 19
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
              segments: 
         | 
| 7 7 | 
             
              - 0
         | 
| 8 | 
            -
              -  | 
| 9 | 
            -
              -  | 
| 10 | 
            -
              version: 0. | 
| 8 | 
            +
              - 3
         | 
| 9 | 
            +
              - 0
         | 
| 10 | 
            +
              version: 0.3.0
         | 
| 11 11 | 
             
            platform: ruby
         | 
| 12 12 | 
             
            authors: 
         | 
| 13 13 | 
             
            - Kelly Redding
         | 
| @@ -16,12 +16,11 @@ autorequire: | |
| 16 16 | 
             
            bindir: bin
         | 
| 17 17 | 
             
            cert_chain: []
         | 
| 18 18 |  | 
| 19 | 
            -
            date: 2011-08- | 
| 19 | 
            +
            date: 2011-08-23 00:00:00 Z
         | 
| 20 20 | 
             
            dependencies: 
         | 
| 21 21 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| 22 | 
            -
              name: bundler
         | 
| 23 | 
            -
              type: :development
         | 
| 24 22 | 
             
              prerelease: false
         | 
| 23 | 
            +
              type: :development
         | 
| 25 24 | 
             
              requirement: &id001 !ruby/object:Gem::Requirement 
         | 
| 26 25 | 
             
                none: false
         | 
| 27 26 | 
             
                requirements: 
         | 
| @@ -32,11 +31,11 @@ dependencies: | |
| 32 31 | 
             
                    - 1
         | 
| 33 32 | 
             
                    - 0
         | 
| 34 33 | 
             
                    version: "1.0"
         | 
| 34 | 
            +
              name: bundler
         | 
| 35 35 | 
             
              version_requirements: *id001
         | 
| 36 36 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| 37 | 
            -
              name: ansi
         | 
| 38 | 
            -
              type: :runtime
         | 
| 39 37 | 
             
              prerelease: false
         | 
| 38 | 
            +
              type: :runtime
         | 
| 40 39 | 
             
              requirement: &id002 !ruby/object:Gem::Requirement 
         | 
| 41 40 | 
             
                none: false
         | 
| 42 41 | 
             
                requirements: 
         | 
| @@ -47,6 +46,7 @@ dependencies: | |
| 47 46 | 
             
                    - 1
         | 
| 48 47 | 
             
                    - 3
         | 
| 49 48 | 
             
                    version: "1.3"
         | 
| 49 | 
            +
              name: ansi
         | 
| 50 50 | 
             
              version_requirements: *id002
         | 
| 51 51 | 
             
            description: Test::Unit style testing framework, just better than Test::Unit.
         | 
| 52 52 | 
             
            email: 
         |