lib/simulacrum/component.rb in simulacrum-0.1.1 vs lib/simulacrum/component.rb in simulacrum-0.3.0
- old
+ new
@@ -1,57 +1,65 @@
+# encoding: UTF-8
require 'capybara/dsl'
require 'fileutils'
require 'RMagick'
require_relative 'renderer'
module Simulacrum
+ # Component Class is responsible for managing the testing of a component
+ # defined in a test suite
class Component
- attr_reader :name, :browser
attr_accessor :options
+ attr_reader :name
def initialize(name, options = {})
@name = name
@options = options
@renderer = Simulacrum::Renderer.new(options.url)
end
- # Load up the component url and capture an image, returns a File object
def render
ensure_example_path
- save_candidate(@renderer.render)
+ tmp_path = @renderer.render
+ save_candidate(tmp_path)
crop_candidate_to_selector
+ true
ensure
cleanup
end
def reference?
- File.exists?(reference_path)
+ File.exist?(reference_path)
end
def candidate?
- File.exists?(candidate_path)
+ File.exist?(candidate_path)
end
def diff?
- File.exists?(diff_path)
+ File.exist?(diff_path)
end
- def acceptable_delta
- options.acceptable_delta || Simulacrum.configuration.acceptable_delta
+ def delta_threshold
+ # TODO: These should probably be `configuration.defaults....`
+ options.delta_threshold || Simulacrum.configuration.delta_threshold
end
def reference_path
+ # TODO: These should probably be `configuration.defaults....`
filename = Simulacrum.configuration.reference_filename
File.join(root_path, "#{filename}.png")
end
def candidate_path
+ # TODO: These should probably be `configuration.defaults....`
filename = Simulacrum.configuration.candidate_filename
File.join(root_path, "#{filename}.png")
end
def diff_path
+ # TODO: These should probably be `configuration.defaults....`
filename = Simulacrum.configuration.diff_filename
File.join(root_path, "#{filename}.png")
end
def remove_candidate
@@ -64,27 +72,30 @@
private
def cleanup
@renderer.cleanup
- # FileUtils.remove_entry(root_path) unless reference? || candidate? || diff?
end
def save_candidate(tmp_image_path)
FileUtils.mv(tmp_image_path, candidate_path)
end
def crop_candidate_to_selector
unless capture_selector.nil?
- candidate_image = Magick::Image::read(candidate_path).first
+ candidate_image = Magick::Image.read(candidate_path).first
bounds = @renderer.get_bounds_for_selector(capture_selector)
candidate_image.crop!(*bounds)
candidate_image.write(candidate_path)
end
end
def root_path
- File.join(Simulacrum.configuration.references_path, name.to_s, driver_path)
+ File.join(
+ Simulacrum.configuration.references_path,
+ name.to_s,
+ driver_path
+ )
end
def driver_path
Capybara.current_driver == :default ? '' : Capybara.current_driver.to_s
end