require 'base64' require 'yaml' require 'uri' def calculate_resized_image_b64(resize_str) base_image = MiniMagick::Image.open(File.expand_path("../../fixtures/basic-app/source/images/original.jpg", __dir__)) base_image.resize(resize_str) Base64.strict_encode64(base_image.to_blob) end image10x10 = "")+"\" class=\"image-resized-to10x10\" alt=\"Original.10x10gt\" />" image5x5 = "\"Original.5x5\"" srcset20x20 = "" srcset15x15 = "" Then(/^I should see base64ed thumbnails of the images$/) do step %Q{I should see '#{image10x10}'} step %Q{I should see '#{image5x5}'} end Then(/^I should see base64ed thumbnails and srcset of the images$/) do step %Q{I should see '#{image10x10}'} step %Q{I should see '#{image5x5}'} step %Q{I should see '#{srcset20x20}'} step %Q{I should see '#{srcset15x15}'} end Given(/^there is no cache directory yet$/) do if directory? MiddlemanSimpleThumbnailer::Extension.config.cache_dir remove MiddlemanSimpleThumbnailer::Extension.config.cache_dir end end Then(/^the cache directory should exist (?:at "([^"]+)" )?with the following files$/) do |cache_dir, files| cache_dir ||= MiddlemanSimpleThumbnailer::Extension.config.cache_dir expect(cache_dir).to be_an_existing_directory cd cache_dir files.raw.map{|file_row| file_row[0]}.each { |f| expect(f).to be_an_existing_file } end Then(/^I should see base64ed data of the cached thumbnails with image path$/) do # data that won't ever be generated by resizing an actual image cached_10x10_data = "foo" cached_5x5_data = "bar" cached_15x15_data = "hello" cached_20x20_data = "world" cachedimage10x10 = "" cachedimage5x5 = "" cachedsrcset20x20 = "" cachedsrcset15x15 = "" step %Q{I should see '#{cachedimage10x10}'} step %Q{I should see '#{cachedimage5x5}'} step %Q{I should see '#{cachedsrcset20x20}'} step %Q{I should see '#{cachedsrcset15x15}'} end Then(/^I should see base64ed data of the cached thumbnails$/) do # data that won't ever be generated by resizing an actual image cached_10x10_data = "foo" cached_5x5_data = "bar" cachedimage10x10 = "\"\"" cachedimage5x5 = "\"\"" step %Q{I should see '#{cachedimage10x10}'} step %Q{I should see '#{cachedimage5x5}'} end Then(/^I should see urls for the following cached thumbnails:$/) do |table| table.hashes.each do |row| isRelative = (row['relative'].to_s == "true") urlPrefix = isRelative ? '' : '/' if(row['type'] == "img") imagedef = "\"#{row['alt']}\"" else imagedef = "" end step %Q{I should see '#{imagedef}'} end end Then(/^the following images should exist:$/) do |table| table.hashes.each do |row| cd('.') do image = MiniMagick::Image.open(row["filename"]) expect(image.dimensions).to eq row["dimensions"].split("x").map(&:to_i) end end end Then(/^file "([^"]+)" should contain the following data:$/) do |file, data| expect(file).to be_an_existing_file cd('.') do specs = YAML.load_file(file) expect(specs.length).to eq(data.hashes.length) data.hashes.each do |hash| expect(specs).to include(hash) end end end Then(/^the file "([^"]*)" should not contain '([^']*)'$/) do |file, partial_content| expect(file).not_to have_file_content(Regexp.new(Regexp.escape(partial_content)), true) end When(/^I visit all images$/) do page.body.scan(/\]*\>/) do |m| visit(m[0]) if !m[0].start_with?('data:') end end