<commit_before># encoding: UTF-8
require 'nokogiri'
require 'minitest/autorun'
require_relative '../lib/prettyprint'

class PrettyPrintTests < Minitest::Test

  OP1 = {
    :block => %w(root block structure div),
    :compact => %w(p),
    :inline => %w(i),
    :preserve_whitespace => true,
    :tab => '  '
  }

  OP2 = {
    :block => %w(root),
    :compact => %w(p),
    :inline => %w(i),
    :preserve_whitespace => false,
    :tab => '  '
  }

  OP3 = {
    :block => %w(root),
    :compact => %w(p),
    :inline => %w(i),
    :tab => '  '
  }

  def setup_and_exercise options
    @parsed = Nokogiri.XML @input
    @pp = PrettyPrint.new(options).pp(@parsed)
  end

  def test_strips_basic_whitespace_from_block_when_ws_is_true
    @input = "<root>  <p>stuff</p>  </root>"
    setup_and_exercise OP1
    assert @pp.match /\n  <p>/
  end

  def test_leaves_inline_and_compact_space_when_ws_is_true
    @input = "<root>  <p> </p><p>stuff<i> </i></p>  </root>"
    setup_and_exercise OP1
    assert @pp =~ /<p> <\/p>/
    assert @pp =~ /<i> <\/i>/
    assert @pp !~ /<root>  <p>/
  end

  def test_strips_inline_and_compact_space_when_ws_is_false
    @input = "<root>  <p> </p><p>stuff<i> </i></p>  </root>"
    setup_and_exercise OP2
    assert @pp =~ /<p\/>/
    assert @pp =~ /<i\/>/
    assert @pp !~ /<root>  <p>/
  end

  def test_ws_preserve_defaults_to_true_if_not_given
    @input = "<root>  <p> </p><p>stuff<i> </i></p>  </root>"
    setup_and_exercise OP3
    assert @pp =~ /<p> <\/p>/
    assert @pp =~ /<i> <\/i>/
    assert @pp !~ /<root>  <p>/
  end

  def test_pp_blocks
    @input = "<root>  <block><p> </p></block><p>stuff<i> </i></p>  </root>"
    setup_and_exercise OP1
    assert @pp.match /<root>\n  <block>\n    <p>/
  end

  def test_more_complex_example
    @input = "<root>  <block>
 <p> </p>
    
    </block><p>stuff<i> </i></p>  
    
    <structure>
                       <div>
                       <p>yo yo<i/></p>
</div>
</structure> <structure>
                       <div>
                       <p>yo yo<i/></p>
</div>
</structure></root>"
    setup_and_exercise OP1
expected = '<?xml version="1.0"?>
<root>
  <block>
    <p> </p>
  </block>
  <p>stuff<i> </i></p>
  <structure>
    <div>
      <p>yo yo<i/></p>
    </div>
  </structure>
  <structure>
    <div>
      <p>yo yo<i/></p>
    </div>
  </structure>
</root>
'
assert @pp == expected, "It looked like this: #{@pp}"
  end

end<commit_msg>condensed tests and moved examples into holde module<commit_after># encoding: UTF-8
require 'nokogiri'
require 'minitest/autorun'
require_relative '../lib/prettyprint'

class PrettyPrintTests < Minitest::Test

  OP1 = {
    :block => %w(root block structure div),
    :compact => %w(p),
    :inline => %w(i),
    :preserve_whitespace => true,
    :tab => '  '
  }

  OP2 = {
    :block => %w(root),
    :compact => %w(p),
    :inline => %w(i),
    :preserve_whitespace => false,
    :tab => '  '
  }

  OP3 = {
    :block => %w(root),
    :compact => %w(p),
    :inline => %w(i),
    :tab => '  '
  }

  def setup_and_exercise options
    @parsed = Nokogiri.XML @input
    @pp = PrettyPrint.new(options).pp(@parsed)
  end

  def test_strips_inline_and_compact_space_when_ws_is_false
    @input = "<root>  <p> </p><p>stuff<i> </i></p>  </root>"
    setup_and_exercise OP2
    assert @pp =~ /<p\/>/
    assert @pp =~ /<i\/>/
    assert @pp !~ /<root>  <p>/
  end

  def test_ws_preserve_defaults_to_true_if_not_given
    @input = "<root>  <p> </p><p>stuff<i> </i></p>  </root>"
    setup_and_exercise OP3
    assert @pp =~ /<p> <\/p>/
    assert @pp =~ /<i> <\/i>/
    assert @pp !~ /<root>  <p>/
  end

  def test_more_complex_example
    @input = Examples.example1[:in]
    setup_and_exercise OP1
    expected = Examples.example1[:out]
    assert @pp == expected, "It looked like this: #{@pp}"
  end

  module Examples

    def self.example1
      {:in => '<root>  <block>
 <p> </p>
    
    </block><p>stuff<i> </i></p>  
    
    <structure>
                       <div>
                       <p>yo yo<i/></p>
</div>
</structure> <structure>
                       <div>
                       <p>yo yo<i/></p>
</div>
</structure></root>',
      :out => '<?xml version="1.0"?>
<root>
  <block>
    <p> </p>
  </block>
  <p>stuff<i> </i></p>
  <structure>
    <div>
      <p>yo yo<i/></p>
    </div>
  </structure>
  <structure>
    <div>
      <p>yo yo<i/></p>
    </div>
  </structure>
</root>
'
      }
    end
  end

end
{'fraction_non_alphanumeric': 0.2013888888888889, 'fraction_numerical': 0.005376344086021506, 'mean_word_length': 2.242556281771968, 'pattern_counts': {'":': 0, '<': 187, '<?xml version=': 2, '>': 215, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '482835', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1794, 'n_words': 372}