image_spec 0.0.2 → 0.0.3

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/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/README.md CHANGED
@@ -0,0 +1,5 @@
1
+ # Usage
2
+
3
+ ~~~~ { ruby }
4
+ require 'image_spec/matchers'
5
+ ~~~~
data/image_spec.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  Gem::Specification.new do |s|
3
3
  s.name = 'image_spec'
4
- s.version = '0.0.2'
4
+ s.version = '0.0.3'
5
5
  s.authors = ["Matt Wynne, Mike Howson"]
6
6
  s.description = 'A library of Rspec matchers to allow comparison of similar images'
7
7
  s.summary = "image_spec-#{s.version}"
@@ -0,0 +1,102 @@
1
+ module ImageSpec
2
+
3
+ require 'open3'
4
+ class Comparison
5
+ def initialize(expected, actual, max_acceptable_score = 0.01)
6
+ @expected, @actual = expected, actual
7
+ @max_acceptable_score = max_acceptable_score
8
+ end
9
+
10
+ def look_similar?
11
+ score and (score < @max_acceptable_score)
12
+ end
13
+
14
+ def score
15
+ raise "Expected image path is blank" unless @expected
16
+ raise "Actual image path is blank" unless @actual
17
+
18
+ [@expected, @actual].each do |path|
19
+ raise "No such file! (#{path})" unless File.exists?(path.to_s)
20
+ end
21
+
22
+ raise "Files are not the same type!" unless same_type?
23
+
24
+ raise "Files are not the same size" unless same_size?
25
+
26
+ cmd = "compare -verbose -metric mae #{@expected} #{@actual} #{Rails.root}/tmp/diff"
27
+ Open3.popen3(cmd) do |stdin, stdout, stderr|
28
+ output = stderr.read
29
+ return false if output =~ /images too dissimilar/
30
+
31
+ output.match /^\s*all:.*\((.*)\)$/
32
+ $1.to_f
33
+ end
34
+ end
35
+
36
+ def same_type?
37
+ File.extname(@expected) == File.extname(@actual)
38
+ end
39
+
40
+ def same_size?
41
+ size_of(@expected) == size_of(@actual)
42
+ end
43
+
44
+ def size_of(image)
45
+ `identify #{image}`.match /\b(\d+x\d+)\b/
46
+ $1.split("x").map(&:to_i)
47
+ end
48
+ end
49
+ end
50
+
51
+ RSpec::Matchers.define(:look_like) do |exected_file_path|
52
+ def max_acceptable_score
53
+ 0.01
54
+ end
55
+
56
+ match do |actual_file_path|
57
+ @expected = exected_file_path
58
+ @actual = actual_file_path
59
+ comparison.look_similar?
60
+ end
61
+
62
+ def comparison
63
+ ImageSpec::Comparison.new(@expected, @actual, max_acceptable_score)
64
+ end
65
+
66
+ failure_message_for_should do
67
+ "Expected #{@actual} to look like #{@expected}. Comparison score should be less than #{max_acceptable_score} but was #{@score}"
68
+ end
69
+ end
70
+
71
+ RSpec::Matchers.define(:have_image_that_looks_like) do |expected_file_path|
72
+ match do |page|
73
+ @page = page
74
+ @expected_file_path = expected_file_path
75
+ any_images_look_similar?
76
+ end
77
+
78
+ failure_message_for_should do
79
+ "Expected one of the images on the page to look similar to #{expected_file_path}"
80
+ end
81
+
82
+ def any_images_look_similar?
83
+ urls = page.all(:xpath, '//img/@src').map(&:text)
84
+ clean_urls = urls.map { |url| url.gsub(/\?\d+$/, '') }
85
+ actual_paths = clean_urls.map { |url| File.join(Rails.root, 'public', url) }
86
+
87
+ actual_paths.any? do |actual_path|
88
+ looks_like?(actual_path)
89
+ end
90
+ end
91
+
92
+ def looks_like?(other_file)
93
+ comparison = comparison_with(other_file)
94
+ return false unless comparison.same_type? && comparison.same_size?
95
+ comparison.look_similar?
96
+ end
97
+
98
+ def comparison_with(other_file)
99
+ ImageSpec::Comparison.new(@expected_file_path, other_file)
100
+ end
101
+ end
102
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: image_spec
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matt Wynne, Mike Howson
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-09 00:00:00 +01:00
19
- default_executable:
18
+ date: 2011-06-09 00:00:00 Z
20
19
  dependencies: []
21
20
 
22
21
  description: A library of Rspec matchers to allow comparison of similar images
@@ -29,10 +28,11 @@ extra_rdoc_files:
29
28
  - LICENSE
30
29
  - README.md
31
30
  files:
31
+ - .gitignore
32
32
  - LICENSE
33
33
  - README.md
34
34
  - image_spec.gemspec
35
- has_rdoc: true
35
+ - lib/image_spec/matchers.rb
36
36
  homepage: http://github.com/mikehowson/image_spec
37
37
  licenses: []
38
38
 
@@ -62,9 +62,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
62
  requirements: []
63
63
 
64
64
  rubyforge_project:
65
- rubygems_version: 1.6.2
65
+ rubygems_version: 1.8.5
66
66
  signing_key:
67
67
  specification_version: 3
68
- summary: image_spec-0.0.2
68
+ summary: image_spec-0.0.3
69
69
  test_files: []
70
70