<commit_before>require 'RMagick'
require_relative 'gatling/capture_element'
require 'capybara'
require 'capybara/dsl'
require 'gatling/config'
require 'gatling/file_helper'
require 'gatling/image'


module Gatling
  class Comparison
    include Capybara::DSL

    #TODO: Training mode
    #TODO: Diff with reports
    #TODO: Fuzz matches
    #TODO: Helpers for cucumber

    #TODO: Listen to Gabe and diferentiate between different types of image sources
    #TODO: Lazy evaluation of images so that it only happens at compare time
    #TODO: rename Gatling::Image to something more meaningful
    #TODO: Make directories as needed

    def initialize expected_filename, actual_element  
      Gatling::FileHelper.make_required_directories 

      #Image taken from the web element
      @actual_image = Gatling::Image.new
      @actual_image.base_on_element(actual_element)
      @actual_image.file_name = expected_filename

      #reference image that may exist on the file system
      @expected_image = Gatling::Image.new
      @expected_image.file_name = expected_filename

      #an image of diferences between compared images
      @diff_image = Gatling::Image.new
      @diff_image.file_name = expected_filename
    end

    def matches?
      if Gatling::Configuration.trainer_toggle
        save_image_as_reference(@actual_image)
        return true
      end

      if !@expected_image.exists?
        @actual_image.save :as => :candidate
        raise "The design reference #{@actual_image.file_name} does not exist, #{@actual_image.path} is now available to be used as a reference. Copy candidate to root reference_image_path to use as reference"
      else
        @expected_image.base_on_file @expected_image.file_name
        compare @expected_image, @actual_image
      end
    end

    def compare(expected_image, actual_image)    
      diff_metric = Gatling::ImageWrangler.compare(expected_image, actual_image)
      matches = diff_metric[1] == 0.0
      save_diff(diff_metric) unless matches
      matches
    end

    private

    def save_diff diff_metric
      @diff_image.rmagick_image = diff_metric.first
      @diff_image.save(:as => :diff)
     
      @actual_image.save(:as => :candidate)
      raise "element did not match #{@diff_image.file_name}. A diff image: #{@diff_image.file_name} was created in #{@diff_image.path}. A new reference #{@actual_image.path} can be used to fix the test"
    end

    def save_image_as_reference(image)
      if image.exists? == false
        image.save(:as => :reference)
        puts "Saved #{image.path} as reference"
      else
        puts "#{image.path} ALREADY EXISTS. REFERENCE IMAGE WAS NOT OVERWRITTEN. PLEASE DELETE THE OLD FILE TO UPDATE USING TRAINER"
      end
    end
  end
end




<commit_msg>remove create diff method<commit_after>require 'RMagick'
require_relative 'gatling/capture_element'
require 'capybara'
require 'capybara/dsl'
require 'gatling/config'
require 'gatling/file_helper'
require 'gatling/image'


module Gatling
  class Comparison
    include Capybara::DSL

    #TODO: Training mode
    #TODO: Diff with reports
    #TODO: Fuzz matches
    #TODO: Helpers for cucumber

    #TODO: Listen to Gabe and diferentiate between different types of image sources
    #TODO: Lazy evaluation of images so that it only happens at compare time
    #TODO: rename Gatling::Image to something more meaningful
    #TODO: Make directories as needed

    def initialize expected_filename, actual_element  
      Gatling::FileHelper.make_required_directories 

      #image taken from the web element
      @actual_image = Gatling::Image.new
      @actual_image.base_on_element(actual_element)
      @actual_image.file_name = expected_filename

      #reference image that may exist on the file system
      @expected_image = Gatling::Image.new
      @expected_image.file_name = expected_filename
    end

    def matches?
      if Gatling::Configuration.trainer_toggle
        save_image_as_reference(@actual_image)
        return true
      end

      if !@expected_image.exists?
        @actual_image.save :as => :candidate
        raise "The design reference #{@actual_image.file_name} does not exist, #{@actual_image.path} is now available to be used as a reference. Copy candidate to root reference_image_path to use as reference"
      else
        @expected_image.base_on_file @expected_image.file_name
        compare @expected_image, @actual_image
      end
    end

    def compare(expected_image, actual_image)         
      diff_metric = Gatling::ImageWrangler.compare(expected_image, actual_image)

      matches = diff_metric[1] == 0.0

      if !matches
        diff_image = Gatling::Image.new
        diff_image.file_name = actual_image.file_name
        diff_image.rmagick_image = diff_metric.first
        diff_image.save(:as => :diff)

        actual_image.save(:as => :candidate) 
        raise "element did not match #{actual_image.file_name}. A diff image: #{diff_image.file_name} was created in #{diff_image.path}. A new reference #{actual_image.path} can be used to fix the test"
      
      end
      matches   
    end

    private

    def save_image_as_reference(image)
      if image.exists?
        puts "#{image.path} already exists. reference image was not overwritten. please delete the old file to update using trainer"
      else
        image.save(:as => :reference)
        puts "Saved #{image.path} as reference"     
      end
    end
  end
end





{'fraction_non_alphanumeric': 0.07274721257539755, 'fraction_numerical': 0.0010966916468652897, 'mean_word_length': 3.18348623853211, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '567245', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1597, 'n_words': 481}