<commit_before>require 'spec_helper'

describe 'PNG testuite' do
  
  context 'Decoding broken images' do
    png_suite_files(:broken).each do |file|
      it "should report #{File.basename(file)} as broken" do
        lambda { ChunkyPNG::Image.from_file(file) }.should raise_error
      end

      it "should not report #{File.basename(file)} as unsupported" do
        lambda { ChunkyPNG::Image.from_file(file) }.should_not raise_error(ChunkyPNG::NotSupported)
      end
    end
  end
  
  context 'Decoding unsupported images' do

    # TODO: we eventually want to support these!
    
    png_suite_files(:basic_not_supported).each do |file|
      color_mode = file.match(/[in](\d)[apgc](\d\d)\.png$/)[1].to_i
      bit_depth  = file.match(/[in](\d)[apgc](\d\d)\.png$/)[2].to_i
      
      it "should report #{File.basename(file)} (color mode: #{color_mode}, bit depth: #{bit_depth}) as unsupported" do
        lambda { ChunkyPNG::Image.from_file(file) }.should raise_error(ChunkyPNG::NotSupported)
      end
    end
  end
  
  context 'Decoding supported images' do
    png_suite_files(:basic, '*.png').each do |file|

      reference  = file.sub(/\.png$/, '.rgba')
      color_mode = file.match(/[in](\d)[apgc](\d\d)\.png$/)[1].to_i
      bit_depth  = file.match(/[in](\d)[apgc](\d\d)\.png$/)[2].to_i
      
      it "should decode #{File.basename(file)} (color mode: #{color_mode}, bit depth: #{bit_depth}) exactly the same as the reference image" do
        decoded = ChunkyPNG::Canvas.from_file(file)
        File.open(reference, 'rb:binary') { |f| decoded.to_rgba_stream.should == f.read }
      end
    end
  end
  
  context 'Decoding text chunks' do
    
    it "should not find metadata in a file without text chunks" do
      image = ChunkyPNG::Image.from_file(png_suite_file(:metadata, 'cm0n0g04.png'))
      image.metadata.should be_empty
    end
    
    # it "should find metadata in a file with uncompressed text chunks" do
    #   image = ChunkyPNG::Image.from_file(png_suite_file(:metadata, 'cm7n0g04.png'))
    #   image.metadata.should_not be_empty
    # end
    # 
    # it "should find metadata in a file with compressed text chunks" do
    #   image = ChunkyPNG::Image.from_file(png_suite_file(:metadata, 'cm9n0g04.png'))
    #   image.metadata.should_not be_empty
    # end<commit_msg>Changed File.open() mode to fix failure in png_suite_spec<commit_after>require 'spec_helper'

describe 'PNG testuite' do
  
  context 'Decoding broken images' do
    png_suite_files(:broken).each do |file|
      it "should report #{File.basename(file)} as broken" do
        lambda { ChunkyPNG::Image.from_file(file) }.should raise_error
      end

      it "should not report #{File.basename(file)} as unsupported" do
        lambda { ChunkyPNG::Image.from_file(file) }.should_not raise_error(ChunkyPNG::NotSupported)
      end
    end
  end
  
  context 'Decoding unsupported images' do

    # TODO: we eventually want to support these!
    
    png_suite_files(:basic_not_supported).each do |file|
      color_mode = file.match(/[in](\d)[apgc](\d\d)\.png$/)[1].to_i
      bit_depth  = file.match(/[in](\d)[apgc](\d\d)\.png$/)[2].to_i
      
      it "should report #{File.basename(file)} (color mode: #{color_mode}, bit depth: #{bit_depth}) as unsupported" do
        lambda { ChunkyPNG::Image.from_file(file) }.should raise_error(ChunkyPNG::NotSupported)
      end
    end
  end
  
  context 'Decoding supported images' do
    png_suite_files(:basic, '*.png').each do |file|

      reference  = file.sub(/\.png$/, '.rgba')
      color_mode = file.match(/[in](\d)[apgc](\d\d)\.png$/)[1].to_i
      bit_depth  = file.match(/[in](\d)[apgc](\d\d)\.png$/)[2].to_i
      
      it "should decode #{File.basename(file)} (color mode: #{color_mode}, bit depth: #{bit_depth}) exactly the same as the reference image" do
        decoded = ChunkyPNG::Canvas.from_file(file)
        File.open(reference, 'rb') { |f| decoded.to_rgba_stream.should == f.read }
      end
    end
  end
  
  context 'Decoding text chunks' do
    
    it "should not find metadata in a file without text chunks" do
      image = ChunkyPNG::Image.from_file(png_suite_file(:metadata, 'cm0n0g04.png'))
      image.metadata.should be_empty
    end
    
    # it "should find metadata in a file with uncompressed text chunks" do
    #   image = ChunkyPNG::Image.from_file(png_suite_file(:metadata, 'cm7n0g04.png'))
    #   image.metadata.should_not be_empty
    # end
    # 
    # it "should find metadata in a file with compressed text chunks" do
    #   image = ChunkyPNG::Image.from_file(png_suite_file(:metadata, 'cm9n0g04.png'))
    #   image.metadata.should_not be_empty
    # end
{'fraction_non_alphanumeric': 0.13510615483594252, 'fraction_numerical': 0.006862534848809779, 'mean_word_length': 3.1829596412556054, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1035640', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1584, 'n_words': 372}