<commit_before>require 'fileutils'
require 'logger'
require 'test/unit'

require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/ext/String+Extensions')
require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/pretty-io')
require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/logger-delegator')
require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/messenging')

class TestMessenging < Test::Unit::TestCase
  include CLIUtils::Messenging
  
  def setup
    @file1path = '/tmp/file1.txt'
  end

  def teardown
    FileUtils.rm(@file1path) if File.exists?(@file1path)
  end
  
  def test_stdout_output
    assert_output('# This is error'.red + "\n") { messenger.send(:error, 'This is error') }
    assert_output('# This is info'.blue + "\n") { messenger.send(:info, 'This is info') }
    assert_output('---> This is section'.purple + "\n") { messenger.send(:section, 'This is section') }
    assert_output('# This is success'.green + "\n") { messenger.send(:success, 'This is success') }
    assert_output('# This is warn'.yellow + "\n") { messenger.send(:warn, 'This is warn') }
  end
  
  def test_wrapping
    CLIUtils::PrettyIO.wrap_at(35)
    
    long_str = 'This is a really long string that should wrap itself at some point, okay?'
    expected_str = long_str.gsub(/\n/, ' ').gsub(/(.{1,#{CLIUtils::PrettyIO.wrap_limit - 2}})(\s+|$)/, "# \\1\n").strip
    assert_output(expected_str.blue + "\n") { messenger.send(:info, long_str) }
  end

  def test_attach_detach
    file_logger = Logger.new(@file1path)
    file_logger.formatter = proc do |severity, datetime, progname, msg|
      "#{ severity }: #{ msg }\n"
    end
    
    messenger.attach(file_logger)
    messenger.send(:info, 'Info test')
    messenger.send(:error, 'Error test')
    messenger.detach(file_logger)
    messenger.send(:warn, 'Warn test')
    
    File.open(@file1path, 'r') do |f|
      assert_output("INFO: Info test\nERROR: Error test\n") { puts f.read.lines[1..-1].join }
    end
  end
end<commit_msg>Backwards compatibility<commit_after>require 'fileutils'
require 'logger'
require 'test/unit'

require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/ext/String+Extensions')
require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/pretty-io')
require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/logger-delegator')
require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/messenging')

class TestMessenging < Test::Unit::TestCase
  include CLIUtils::Messenging
  
  def setup
    @file1path = '/tmp/file1.txt'
  end

  def teardown
    FileUtils.rm(@file1path) if File.exists?(@file1path)
  end
  
  def test_stdout_output
    assert_output('# This is error'.red + "\n") { messenger.send(:error, 'This is error') }
    assert_output('# This is info'.blue + "\n") { messenger.send(:info, 'This is info') }
    assert_output('---> This is section'.purple + "\n") { messenger.send(:section, 'This is section') }
    assert_output('# This is success'.green + "\n") { messenger.send(:success, 'This is success') }
    assert_output('# This is warn'.yellow + "\n") { messenger.send(:warn, 'This is warn') }
  end
  
  def test_wrapping
    CLIUtils::PrettyIO.wrap_at(35)
    
    long_str = 'This is a really long string that should wrap itself at some point, okay?'
    expected_str = long_str.gsub(/\n/, ' ').gsub(/(.{1,#{CLIUtils::PrettyIO.wrap_limit - 2}})(\s+|$)/, "# \\1\n").strip
    assert_output(expected_str.blue + "\n") { messenger.send(:info, long_str) }
  end

  def test_attach_detach
    file_logger = Logger.new(@file1path)
    file_logger.formatter = proc do |severity, datetime, progname, msg|
      "#{ severity }: #{ msg }\n"
    end
    
    messenger.attach(file_logger)
    messenger.send(:info, 'Info test')
    messenger.send(:error, 'Error test')
    messenger.detach(file_logger)
    messenger.send(:warn, 'Warn test')
    
    File.open(@file1path, 'r') do |f|
      assert_output("INFO: Info test\nERROR: Error test\n") { puts f.read.lines.to_a[1..-1].join }
    end
  end
end
{'fraction_non_alphanumeric': 0.16926889714993804, 'fraction_numerical': 0.00644361833952912, 'mean_word_length': 3.810488676996424, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6165968', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1372, 'n_words': 300}