<commit_before>require 'test_helper'
require 'minitest/autorun'
require 'coaster/core_ext/standard_error/raven'

module Coaster
  class TestStandardError < Minitest::Test
    class SampleError < StandardError
      def self.status; 10 end
    end

    class ExampleError < StandardError
      def self.status; 20 end
    end

    class UntitledError < StandardError; end

    def setup
      I18n.backend = I18n::Backend::Simple.new
      I18n.load_path += [File.expand_path('../locales/en.yml', __FILE__)]
      I18n.enforce_available_locales = false
    end

    def test_message
      raise SampleError, {m: 'beer is proof'}
    rescue => e
      assert_equal 'beer is proof', e.message
    end

    def test_attributes
      raise SampleError, {blah: 'foo'}
    rescue => e
      assert_equal 'foo', e.attr[:blah]
    end

    def test_hash
      begin
        raise SampleError, {frog: 'rams'}
      rescue => e
        raise ExampleError, {wat: 'cha'}
      end
    rescue => e
      assert_equal e.to_hash['wat'], 'cha'
      assert_equal e.to_hash['type'], 'Coaster::TestStandardError::ExampleError'
      assert_equal e.to_hash['status'], 20
      assert_equal e.to_hash['http_status'], 500
      assert_equal e.to_hash['message'], 'Test sample error'
      assert_equal e.to_hash['cause']['frog'], 'rams'
      assert_equal e.to_hash['cause']['type'], 'Coaster::TestStandardError::SampleError'
      assert_equal e.to_hash['cause']['status'], 10
      assert_equal e.to_hash['cause']['http_status'], 500
      assert_equal e.to_hash['cause']['message'], 'Test sample error'
    end

    def test_cause_attributes
      begin
        raise SampleError, {frog: 'rams'}
      rescue => e
        raise ExampleError, {wat: 'cha'}
      end
    rescue => e
      assert_equal e.cause.attr['frog'], 'rams'
      assert_equal e.attr['frog'], 'rams'
      assert_equal e.attr['wat'], 'cha'
    end

    def test_to_detail
      begin
        raise SampleError, {frog: 'rams'}
      rescue => e
        raise ExampleError, {wat: 'cha'}
      end
    rescue => e
      detail = <<-LOG
[Coaster::TestStandardError::ExampleError] status:20
	MESSAGE: Test sample error
	@fingerprint: []
	@tags: {}
	@level: \"error\"
	@attributes: {\"frog\"=>\"rams\", \"wat\"=>\"cha\"}
	@tkey: nil
	@raven: {}
	CAUSE: [Coaster::TestStandardError::SampleError] status:10
		MESSAGE: Test sample error
		@fingerprint: []
		@tags: {}
		@level: \"error\"
		@attributes: {\"frog\"=>\"rams\"}
		@tkey: nil
		@raven: {}
LOG
      assert_equal(detail, e.to_detail)
    end

    def test_translation
      raise SampleError, {tkey: '.test'}
    rescue => e
      assert_equal e._translate, 'Test this translation'
    end

    def test_title
      raise SampleError, 'foobar'
    rescue => e
      assert_equal e.title, 'Test this title'
    end

    def test_title_missing
      raise UntitledError, 'untitled'
    rescue => e
      assert_nil e.title
    end

    def root_cause_sample1
      raise StandardError, 'a'
    end

    def root_cause_sample2
      root_cause_sample1
    rescue
      raise NoMethodError, 'b'
    end

    def root_cause_sample3
      root_cause_sample2
    rescue
      raise ArgumentError, 'c'
    end

    def test_root_cause
      begin
        root_cause_sample3
      rescue => e
        assert_equal e.root_cause.message, 'a'
      end
    end

    def test_raven_notes
      raise SampleError, m: 'foofoo', something: 'other'
    rescue => e
      assert_equal e.notes(the_other: 'something')[:extra][:something], 'other'
      assert_equal e.notes(the_other: 'something')[:extra][:the_other], 'something'
    end

    def test_to_hash
      aa # raise NameError
    rescue => e
      assert_equal e.to_hash['type'], 'NameError'
      assert_equal e.to_hash['status'], 999999
      assert_equal e.to_hash['http_status'], 500
      assert e.to_hash['message'] =~ /undefined local variable or method `aa'/
    end

    def test_descriptions
      raise SampleError
    rescue => e
      e.descriptions.merge!(a: 1)
      assert_equal e.descriptions['a'], 1
    end

    class SampleErrorSub < SampleError; end
    class SampleErrorSubSub < SampleErrorSub
      def it_might_happen?; true end
    end
    SampleError.after_logging(:blah) do 
      self.attributes[:abc] = 100
      @blah = 101
    end
    def test_before_logging
      e = SampleErrorSubSub.new(m: 'foo')
      assert !e.after_logging_blocks[:blah].nil?
      e.logging
      assert_equal e.attributes[:abc], 100
      assert_equal e.instance_variable_get(:@blah), 101
    end
    class SampleErrorMightHappen < SampleErrorSub
      def it_might_happen?; true end
    end
    def test_might_happen
      e = SampleErrorMightHappen.new('fbar')
      assert !e.report?
    end
  end
end<commit_msg>fix test<commit_after>require 'test_helper'
require 'minitest/autorun'
require 'coaster/core_ext/standard_error/raven'

module Coaster
  class TestStandardError < Minitest::Test
    class SampleError < StandardError
      def self.status; 10 end
    end

    class ExampleError < StandardError
      def self.status; 20 end
    end

    class UntitledError < StandardError; end

    def setup
      I18n.backend = I18n::Backend::Simple.new
      I18n.load_path += [File.expand_path('../locales/en.yml', __FILE__)]
      I18n.enforce_available_locales = false
    end

    def test_message
      raise SampleError, {m: 'beer is proof'}
    rescue => e
      assert_equal 'beer is proof', e.message
    end

    def test_attributes
      raise SampleError, {blah: 'foo'}
    rescue => e
      assert_equal 'foo', e.attr[:blah]
    end

    def test_hash
      begin
        raise SampleError, {frog: 'rams'}
      rescue => e
        raise ExampleError, {wat: 'cha'}
      end
    rescue => e
      assert_equal 'cha', e.to_hash['wat']
      assert_equal 'Coaster::TestStandardError::ExampleError', e.to_hash['type']
      assert_equal 20, e.to_hash['status']
      assert_equal 500, e.to_hash['http_status']
      assert_equal 'Test sample error', e.to_hash['message']
      assert_equal 'rams', e.to_hash['cause']['frog']
      assert_equal 'Coaster::TestStandardError::SampleError', e.to_hash['cause']['type']
      assert_equal 10, e.to_hash['cause']['status']
      assert_equal 500, e.to_hash['cause']['http_status']
      assert_equal 'Test sample error', e.to_hash['cause']['message']
    end

    def test_cause_attributes
      begin
        raise SampleError, {frog: 'rams'}
      rescue => e
        raise ExampleError, {wat: 'cha'}
      end
    rescue => e
      assert_equal 'rams', e.cause.attr['frog']
      assert_equal 'rams', e.attr['frog']
      assert_equal 'cha', e.attr['wat']
    end

    def test_to_detail
      begin
        raise SampleError, {frog: 'rams'}
      rescue => e
        raise ExampleError, {wat: 'cha'}
      end
    rescue => e
      detail = <<-LOG
[Coaster::TestStandardError::ExampleError] status:20
	MESSAGE: Test sample error
	@fingerprint: []
	@tags: {}
	@level: \"error\"
	@attributes: {\"frog\"=>\"rams\", \"wat\"=>\"cha\"}
	@tkey: nil
	@raven: {}
	CAUSE: [Coaster::TestStandardError::SampleError] status:10
		MESSAGE: Test sample error
		@fingerprint: []
		@tags: {}
		@level: \"error\"
		@attributes: {\"frog\"=>\"rams\"}
		@tkey: nil
		@raven: {}
LOG
      assert_equal(detail, e.to_detail)
    end

    def test_translation
      raise SampleError, {tkey: '.test'}
    rescue => e
      assert_equal 'Test this translation', e._translate
    end

    def test_title
      raise SampleError, 'foobar'
    rescue => e
      assert_equal 'Test this title', e.title
    end

    def test_title_missing
      raise UntitledError, 'untitled'
    rescue => e
      assert_nil e.title
    end

    def root_cause_sample1
      raise StandardError, 'a'
    end

    def root_cause_sample2
      root_cause_sample1
    rescue
      raise NoMethodError, 'b'
    end

    def root_cause_sample3
      root_cause_sample2
    rescue
      raise ArgumentError, 'c'
    end

    def test_root_cause
      begin
        root_cause_sample3
      rescue => e
        assert_equal 'a', e.root_cause.message
      end
    end

    def test_raven_notes
      raise SampleError, m: 'foofoo', something: 'other'
    rescue => e
      assert_equal 'other', e.notes(the_other: 'something')[:extra][:something]
      assert_equal 'something', e.notes(the_other: 'something')[:extra][:the_other]
    end

    def test_to_hash
      aa # raise NameError
    rescue => e
      assert_equal 'NameError', e.to_hash['type']
      assert_equal 999999, e.to_hash['status']
      assert_equal 500, e.to_hash['http_status']
      assert_match /undefined local variable or method `aa'/, e.to_hash['message']
    end

    def test_descriptions
      raise SampleError
    rescue => e
      e.descriptions.merge!(a: 1)
      assert_equal 1, e.descriptions['a']
    end

    class SampleErrorSub < SampleError; end
    class SampleErrorSubSub < SampleErrorSub
      def it_might_happen?; true end
    end
    SampleError.after_logging(:blah) do 
      self.attributes[:abc] = 100
      @blah = 101
    end
    def test_before_logging
      e = SampleErrorSubSub.new(m: 'foo')
      assert !e.after_logging_blocks[:blah].nil?
      e.logging
      assert_equal 100, e.attributes[:abc]
      assert_equal 101, e.instance_variable_get(:@blah)
    end
    class SampleErrorMightHappen < SampleErrorSub
      def it_might_happen?; true end
    end
    def test_might_happen
      e = SampleErrorMightHappen.new('fbar')
      assert !e.report?
    end
  end
end
{'fraction_non_alphanumeric': 0.11292859389745202, 'fraction_numerical': 0.011534025374855825, 'mean_word_length': 2.7834192780642604, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 39, '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': '6172640', 'n_tokens_mistral': 3258, 'n_tokens_neox': 3094, 'n_words': 754}