spex 0.5.0 → 0.5.1
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/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/spex/assertions/modified_assertion.rb +43 -0
- data/test/helper.rb +15 -0
- data/test/test_modified_assertion.rb +140 -0
- metadata +17 -3
    
        data/Rakefile
    CHANGED
    
    
    
        data/VERSION
    CHANGED
    
    | @@ -1 +1 @@ | |
| 1 | 
            -
            0.5. | 
| 1 | 
            +
            0.5.1
         | 
| @@ -1,13 +1,18 @@ | |
| 1 1 | 
             
            require 'digest/md5'
         | 
| 2 | 
            +
            require 'diff/lcs'
         | 
| 3 | 
            +
            require 'diff/lcs/array'
         | 
| 2 4 |  | 
| 3 5 | 
             
            module Spex
         | 
| 4 6 |  | 
| 5 7 | 
             
              # With no option, just verifies a change occurs
         | 
| 6 8 | 
             
              class ModifiedAssertion < FileAssertion
         | 
| 7 9 | 
             
                as :modified, 'file modification'
         | 
| 10 | 
            +
                option :added, "Added content (string or regexp)"
         | 
| 11 | 
            +
                option :removed, "Removed content (string or regexp)"
         | 
| 8 12 |  | 
| 9 13 | 
             
                def prepare
         | 
| 10 14 | 
             
                  track_checksum!
         | 
| 15 | 
            +
                  track_contents! if options[:added] || options[:removed]
         | 
| 11 16 | 
             
                end
         | 
| 12 17 |  | 
| 13 18 | 
             
                def before
         | 
| @@ -19,6 +24,7 @@ module Spex | |
| 19 24 | 
             
                  checksum = current_checksum
         | 
| 20 25 | 
             
                  if active?
         | 
| 21 26 | 
             
                    assert_not_equal @before_checksum, checksum, "Checksum did not change"
         | 
| 27 | 
            +
                    check_added_and_removed if options[:added] || options[:removed]
         | 
| 22 28 | 
             
                  else
         | 
| 23 29 | 
             
                    assert_equal @before_checksum, checksum, "Checksum changed"
         | 
| 24 30 | 
             
                  end
         | 
| @@ -26,10 +32,47 @@ module Spex | |
| 26 32 |  | 
| 27 33 | 
             
                private
         | 
| 28 34 |  | 
| 35 | 
            +
                def check_added_and_removed
         | 
| 36 | 
            +
                  diff = generate_diff
         | 
| 37 | 
            +
                  if options[:added]
         | 
| 38 | 
            +
                    assert_diff_match(diff,
         | 
| 39 | 
            +
                                      '+', options[:added],
         | 
| 40 | 
            +
                                      "Did not add: #{options[:added]}")
         | 
| 41 | 
            +
                  end
         | 
| 42 | 
            +
                  if options[:removed]
         | 
| 43 | 
            +
                    assert_diff_match(diff,
         | 
| 44 | 
            +
                                      '-', options[:removed],
         | 
| 45 | 
            +
                                      "Did not remove: #{options[:removed]}")
         | 
| 46 | 
            +
                  end          
         | 
| 47 | 
            +
                end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                def assert_diff_match(diff, action, content, message)
         | 
| 50 | 
            +
                  found = diff.any? do |change|
         | 
| 51 | 
            +
                    if change.action == action
         | 
| 52 | 
            +
                      case content
         | 
| 53 | 
            +
                      when String
         | 
| 54 | 
            +
                        change.element.include?(content)
         | 
| 55 | 
            +
                      when Regexp
         | 
| 56 | 
            +
                        change.element.match(content)
         | 
| 57 | 
            +
                      end
         | 
| 58 | 
            +
                    end
         | 
| 59 | 
            +
                  end
         | 
| 60 | 
            +
                  assert found, message
         | 
| 61 | 
            +
                end
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                def generate_diff
         | 
| 64 | 
            +
                  contents = File.readlines(target)
         | 
| 65 | 
            +
                  @before_contents.diff(contents).flatten
         | 
| 66 | 
            +
                end
         | 
| 67 | 
            +
             | 
| 29 68 | 
             
                def track_checksum!
         | 
| 30 69 | 
             
                  @before_checksum = current_checksum
         | 
| 31 70 | 
             
                end
         | 
| 32 71 |  | 
| 72 | 
            +
                def track_contents!
         | 
| 73 | 
            +
                  @before_contents = File.readlines(target)
         | 
| 74 | 
            +
                end
         | 
| 75 | 
            +
             | 
| 33 76 | 
             
                def current_checksum
         | 
| 34 77 | 
             
                  if File.exist?(target)
         | 
| 35 78 | 
             
                    generate_checksum
         | 
    
        data/test/helper.rb
    CHANGED
    
    | @@ -1,10 +1,12 @@ | |
| 1 1 | 
             
            require 'rubygems'
         | 
| 2 2 | 
             
            require 'test/unit'
         | 
| 3 3 | 
             
            require 'shoulda'
         | 
| 4 | 
            +
            require 'stringio'
         | 
| 4 5 |  | 
| 5 6 | 
             
            $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
         | 
| 6 7 | 
             
            $LOAD_PATH.unshift(File.dirname(__FILE__))
         | 
| 7 8 | 
             
            require 'spex'
         | 
| 9 | 
            +
            require 'fakefs/safe'
         | 
| 8 10 |  | 
| 9 11 | 
             
            class Test::Unit::TestCase
         | 
| 10 12 |  | 
| @@ -13,5 +15,18 @@ class Test::Unit::TestCase | |
| 13 15 | 
             
              def script(&block)
         | 
| 14 16 | 
             
                @script = Spex::Script.evaluate(&block)
         | 
| 15 17 | 
             
              end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
              def assertion_passes(&block)
         | 
| 20 | 
            +
                @assertion.prepare
         | 
| 21 | 
            +
                @assertion.before
         | 
| 22 | 
            +
                yield if block_given?
         | 
| 23 | 
            +
                @assertion.after
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              def assertion_fails(&block)
         | 
| 27 | 
            +
                assert_raises Test::Unit::AssertionFailedError do
         | 
| 28 | 
            +
                  assertion_passes(&block)
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
              end
         | 
| 16 31 |  | 
| 17 32 | 
             
            end
         | 
| @@ -0,0 +1,140 @@ | |
| 1 | 
            +
            require 'helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class TestAssertion < Test::Unit::TestCase
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              def set_assertion(options = {})
         | 
| 6 | 
            +
                @assertion = Spex::ModifiedAssertion.new(@filename, options)
         | 
| 7 | 
            +
              end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              def non_blank_file!(text = 'test')
         | 
| 10 | 
            +
                File.open(@filename, 'w') { |f| f.puts text }
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              def blank_file!
         | 
| 14 | 
            +
                File.open(@filename, 'w') { |f| f.puts }
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
              
         | 
| 17 | 
            +
              context "Modified Assertion" do
         | 
| 18 | 
            +
                setup do
         | 
| 19 | 
            +
                  FakeFS.activate!
         | 
| 20 | 
            +
                  @filename = '/tmp/modified-test'
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                teardown do
         | 
| 24 | 
            +
                  FakeFS.deactivate!
         | 
| 25 | 
            +
                end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                context "instances" do
         | 
| 28 | 
            +
                  context "without options" do
         | 
| 29 | 
            +
                    setup do
         | 
| 30 | 
            +
                      set_assertion
         | 
| 31 | 
            +
                      blank_file!
         | 
| 32 | 
            +
                    end
         | 
| 33 | 
            +
                    should "pass when modifications happen" do
         | 
| 34 | 
            +
                      assertion_passes { non_blank_file! }
         | 
| 35 | 
            +
                    end
         | 
| 36 | 
            +
                    should "when modifications don't happen" do
         | 
| 37 | 
            +
                      assertion_fails
         | 
| 38 | 
            +
                    end
         | 
| 39 | 
            +
                  end
         | 
| 40 | 
            +
                  context "with :added" do
         | 
| 41 | 
            +
                    context "as a string" do
         | 
| 42 | 
            +
                      setup do
         | 
| 43 | 
            +
                        set_assertion(:added => 'test')
         | 
| 44 | 
            +
                        blank_file!
         | 
| 45 | 
            +
                      end
         | 
| 46 | 
            +
                      context "when the text is added" do
         | 
| 47 | 
            +
                        should "pass" do
         | 
| 48 | 
            +
                          assertion_passes { non_blank_file! }
         | 
| 49 | 
            +
                        end
         | 
| 50 | 
            +
                      end
         | 
| 51 | 
            +
                      context "when different text is added" do
         | 
| 52 | 
            +
                        should "fail" do
         | 
| 53 | 
            +
                          assertion_fails { non_blank_file!('other') }
         | 
| 54 | 
            +
                        end
         | 
| 55 | 
            +
                      end
         | 
| 56 | 
            +
                      context "when modifications don't happen" do
         | 
| 57 | 
            +
                        should "fail when modifications don't happen" do
         | 
| 58 | 
            +
                          assertion_fails
         | 
| 59 | 
            +
                        end
         | 
| 60 | 
            +
                      end
         | 
| 61 | 
            +
                    end
         | 
| 62 | 
            +
                    context "as a regexp" do
         | 
| 63 | 
            +
                      setup do
         | 
| 64 | 
            +
                        set_assertion(:added => /t.st/)
         | 
| 65 | 
            +
                        blank_file!
         | 
| 66 | 
            +
                      end
         | 
| 67 | 
            +
                      context "when the text is added" do
         | 
| 68 | 
            +
                        should "pass" do
         | 
| 69 | 
            +
                          assertion_passes { non_blank_file! }
         | 
| 70 | 
            +
                        end
         | 
| 71 | 
            +
                      end
         | 
| 72 | 
            +
                      context "when different text is added" do
         | 
| 73 | 
            +
                        should "fail" do
         | 
| 74 | 
            +
                          assertion_fails { non_blank_file!('other') }
         | 
| 75 | 
            +
                        end
         | 
| 76 | 
            +
                      end
         | 
| 77 | 
            +
                      context "when modifications don't happen" do
         | 
| 78 | 
            +
                        should "fail when modifications don't happen" do
         | 
| 79 | 
            +
                          assertion_fails
         | 
| 80 | 
            +
                        end
         | 
| 81 | 
            +
                      end
         | 
| 82 | 
            +
                    end
         | 
| 83 | 
            +
                  end
         | 
| 84 | 
            +
                  context "with :removed" do
         | 
| 85 | 
            +
                    context "as a string" do
         | 
| 86 | 
            +
                      setup do
         | 
| 87 | 
            +
                        set_assertion(:removed => 'test')
         | 
| 88 | 
            +
                      end
         | 
| 89 | 
            +
                      context "when the text is removed" do
         | 
| 90 | 
            +
                        setup do
         | 
| 91 | 
            +
                          non_blank_file!
         | 
| 92 | 
            +
                        end
         | 
| 93 | 
            +
                        should "pass" do
         | 
| 94 | 
            +
                          assertion_passes { blank_file! }
         | 
| 95 | 
            +
                        end
         | 
| 96 | 
            +
                      end
         | 
| 97 | 
            +
                      context "when different text is removed" do
         | 
| 98 | 
            +
                        setup do
         | 
| 99 | 
            +
                          non_blank_file!('other')
         | 
| 100 | 
            +
                        end
         | 
| 101 | 
            +
                        should "fail" do
         | 
| 102 | 
            +
                          assertion_fails { blank_file! }
         | 
| 103 | 
            +
                        end
         | 
| 104 | 
            +
                      end
         | 
| 105 | 
            +
                      context "when modifications don't happen" do
         | 
| 106 | 
            +
                        should "fail when modifications don't happen" do
         | 
| 107 | 
            +
                          assertion_fails
         | 
| 108 | 
            +
                        end
         | 
| 109 | 
            +
                      end
         | 
| 110 | 
            +
                    end
         | 
| 111 | 
            +
                    context "as a regexp" do
         | 
| 112 | 
            +
                      setup do
         | 
| 113 | 
            +
                        set_assertion(:removed => /t.st/)
         | 
| 114 | 
            +
                      end
         | 
| 115 | 
            +
                      context "when the text is removed" do
         | 
| 116 | 
            +
                        setup do
         | 
| 117 | 
            +
                          non_blank_file!
         | 
| 118 | 
            +
                        end
         | 
| 119 | 
            +
                        should "pass" do
         | 
| 120 | 
            +
                          assertion_passes { blank_file! }
         | 
| 121 | 
            +
                        end
         | 
| 122 | 
            +
                      end
         | 
| 123 | 
            +
                      context "when different text is removed" do
         | 
| 124 | 
            +
                        setup do
         | 
| 125 | 
            +
                          non_blank_file!('other')
         | 
| 126 | 
            +
                        end
         | 
| 127 | 
            +
                        should "fail" do
         | 
| 128 | 
            +
                          assertion_fails { blank_file! }
         | 
| 129 | 
            +
                        end
         | 
| 130 | 
            +
                      end
         | 
| 131 | 
            +
                      context "when modifications don't happen" do
         | 
| 132 | 
            +
                        should "fail when modifications don't happen" do
         | 
| 133 | 
            +
                          assertion_fails
         | 
| 134 | 
            +
                        end
         | 
| 135 | 
            +
                      end
         | 
| 136 | 
            +
                    end
         | 
| 137 | 
            +
                  end
         | 
| 138 | 
            +
                end
         | 
| 139 | 
            +
              end
         | 
| 140 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version | |
| 5 5 | 
             
              segments: 
         | 
| 6 6 | 
             
              - 0
         | 
| 7 7 | 
             
              - 5
         | 
| 8 | 
            -
              -  | 
| 9 | 
            -
              version: 0.5. | 
| 8 | 
            +
              - 1
         | 
| 9 | 
            +
              version: 0.5.1
         | 
| 10 10 | 
             
            platform: ruby
         | 
| 11 11 | 
             
            authors: 
         | 
| 12 12 | 
             
            - Bruce Williams
         | 
| @@ -14,7 +14,7 @@ autorequire: | |
| 14 14 | 
             
            bindir: bin
         | 
| 15 15 | 
             
            cert_chain: []
         | 
| 16 16 |  | 
| 17 | 
            -
            date: 2010-04- | 
| 17 | 
            +
            date: 2010-04-14 00:00:00 -07:00
         | 
| 18 18 | 
             
            default_executable: spex
         | 
| 19 19 | 
             
            dependencies: 
         | 
| 20 20 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| @@ -29,6 +29,18 @@ dependencies: | |
| 29 29 | 
             
                    version: "0"
         | 
| 30 30 | 
             
              type: :runtime
         | 
| 31 31 | 
             
              version_requirements: *id001
         | 
| 32 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 33 | 
            +
              name: diff-lcs
         | 
| 34 | 
            +
              prerelease: false
         | 
| 35 | 
            +
              requirement: &id002 !ruby/object:Gem::Requirement 
         | 
| 36 | 
            +
                requirements: 
         | 
| 37 | 
            +
                - - ">="
         | 
| 38 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 39 | 
            +
                    segments: 
         | 
| 40 | 
            +
                    - 0
         | 
| 41 | 
            +
                    version: "0"
         | 
| 42 | 
            +
              type: :runtime
         | 
| 43 | 
            +
              version_requirements: *id002
         | 
| 32 44 | 
             
            description: An easy-to-use test harness that runs assertions before and after and executable is run
         | 
| 33 45 | 
             
            email: bruce@codefluency.com
         | 
| 34 46 | 
             
            executables: 
         | 
| @@ -68,6 +80,7 @@ files: | |
| 68 80 | 
             
            - lib/spex/script.rb
         | 
| 69 81 | 
             
            - test/helper.rb
         | 
| 70 82 | 
             
            - test/test_assertion.rb
         | 
| 83 | 
            +
            - test/test_modified_assertion.rb
         | 
| 71 84 | 
             
            - test/test_script.rb
         | 
| 72 85 | 
             
            has_rdoc: true
         | 
| 73 86 | 
             
            homepage: http://github.com/bruce/spex
         | 
| @@ -102,6 +115,7 @@ summary: A test harness for executables | |
| 102 115 | 
             
            test_files: 
         | 
| 103 116 | 
             
            - test/helper.rb
         | 
| 104 117 | 
             
            - test/test_assertion.rb
         | 
| 118 | 
            +
            - test/test_modified_assertion.rb
         | 
| 105 119 | 
             
            - test/test_script.rb
         | 
| 106 120 | 
             
            - examples/chgrp.rb
         | 
| 107 121 | 
             
            - examples/chmod.rb
         |