<commit_before># encoding: utf-8

require 'spec_helper'

describe Bugsnag::Cleaner do
  subject { described_class.new(nil) }

  describe "#clean_object" do
    it "cleans up recursive hashes" do
      a = {:a => {}}
      a[:a][:b] = a
      expect(subject.clean_object(a)).to eq({:a => {:b => "[RECURSION]"}})
    end

    it "cleans up hashes when keys infinitely recurse in to_s" do
      class RecursiveHashKey
        def to_s
          to_s
        end
      end

      key = RecursiveHashKey.new

      a = {}
      a[key] = 1

      expect(subject.clean_object(a)).to eq({ key => "[FILTERED]" })
    end

    it "cleans up hashes when keys raise in to_s" do
      class RaisingHashKey
        def to_s
          raise "hey!"
        end
      end

      key = RaisingHashKey.new

      a = {}
      a[key] = 1

      expect(subject.clean_object(a)).to eq({ key => "[FILTERED]" })
    end

    it "cleans up recursive arrays" do
      a = []
      a << a
      a << "hello"
      expect(subject.clean_object(a)).to eq(["[RECURSION]", "hello"])
    end

    it "doesn't remove nil from arrays" do
      a = ["b", nil, "c"]
      expect(subject.clean_object(a)).to eq(["b", nil, "c"])
    end

    it "allows multiple copies of the same string" do
      a = {:name => "bugsnag"}
      a[:second] = a[:name]
      expect(subject.clean_object(a)).to eq({:name => "bugsnag", :second => "bugsnag"})
    end

    it "allows multiple copies of the same object" do
      a = []
      b = ["hello"]
      a << b; a << b
      expect(subject.clean_object(a)).to eq([["hello"], ["hello"]])
    end

    it "cleans up UTF8 strings properly" do
      obj = "André"
      expect(subject.clean_object(obj)).to eq("André")
    end

    it "cleans custom objects" do
      class Macaron; end
      a = Macaron.new
      expect(subject.clean_object(a)).to eq('[OBJECT]')
    end

    it "cleans custom objects when they infinitely recurse" do
      class RecursiveObject
        def to_s
          to_s
        end
      end

      object = RecursiveObject.new

      expect(subject.clean_object(object)).to eq("[RECURSION]")
    end

    it "cleans up binary strings properly" do
      if RUBY_VERSION > "1.9"<commit_msg>Skip infinite recursion tests on JRuby<commit_after># encoding: utf-8

require 'spec_helper'

describe Bugsnag::Cleaner do
  subject { described_class.new(nil) }

  describe "#clean_object" do
    is_jruby = defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'

    it "cleans up recursive hashes" do
      a = {:a => {}}
      a[:a][:b] = a
      expect(subject.clean_object(a)).to eq({:a => {:b => "[RECURSION]"}})
    end

    it "cleans up hashes when keys infinitely recurse in to_s" do
      skip "JRuby doesn't allow recovery from SystemStackErrors" if is_jruby

      class RecursiveHashKey
        def to_s
          to_s
        end
      end

      key = RecursiveHashKey.new

      a = {}
      a[key] = 1

      expect(subject.clean_object(a)).to eq({ key => "[FILTERED]" })
    end

    it "cleans up hashes when keys raise in to_s" do
      class RaisingHashKey
        def to_s
          raise "hey!"
        end
      end

      key = RaisingHashKey.new

      a = {}
      a[key] = 1

      expect(subject.clean_object(a)).to eq({ key => "[FILTERED]" })
    end

    it "cleans up recursive arrays" do
      a = []
      a << a
      a << "hello"
      expect(subject.clean_object(a)).to eq(["[RECURSION]", "hello"])
    end

    it "doesn't remove nil from arrays" do
      a = ["b", nil, "c"]
      expect(subject.clean_object(a)).to eq(["b", nil, "c"])
    end

    it "allows multiple copies of the same string" do
      a = {:name => "bugsnag"}
      a[:second] = a[:name]
      expect(subject.clean_object(a)).to eq({:name => "bugsnag", :second => "bugsnag"})
    end

    it "allows multiple copies of the same object" do
      a = []
      b = ["hello"]
      a << b; a << b
      expect(subject.clean_object(a)).to eq([["hello"], ["hello"]])
    end

    it "cleans up UTF8 strings properly" do
      obj = "André"
      expect(subject.clean_object(obj)).to eq("André")
    end

    it "cleans custom objects" do
      class Macaron; end
      a = Macaron.new
      expect(subject.clean_object(a)).to eq('[OBJECT]')
    end

    it "cleans custom objects when they infinitely recurse" do
      skip "JRuby doesn't allow recovery from SystemStackErrors" if is_jruby

      class RecursiveObject
        def to_s
          to_s
        end
      end

      object = RecursiveObject.new

      expect(subject.clean_object(object)).to eq("[RECURSION]")
    end

    it "cleans up binary strings properly" do
      if RUBY_VERSION > "1.9"
{'fraction_non_alphanumeric': 0.1294748124330118, 'fraction_numerical': 0.002572347266881029, 'mean_word_length': 2.2583798882681565, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, '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': '3997042', 'n_tokens_mistral': 1619, 'n_tokens_neox': 1521, 'n_words': 448}