<commit_before># frozen_string_literal: true

RSpec.describe RuboCop::Cop::Layout::BlockEndNewline do
  subject(:cop) { described_class.new }

  it 'does not register an offense for a one-liner' do
    expect_no_offenses('test do foo end')
  end

  it 'does not register an offense for multiline blocks with newlines before '\
     'the end' do
    inspect_source(<<-RUBY.strip_indent)
      test do
        foo
      end
    RUBY
    expect(cop.messages.empty?).to be(true)
  end

  it 'registers an offense when multiline block end is not on its own line' do
    expect_offense(<<-RUBY.strip_indent)
      test do
        foo end
            ^^^ Expression at 2, 7 should be on its own line.
    RUBY
  end

  it 'registers an offense when multiline block } is not on its own line' do
    expect_offense(<<-RUBY.strip_indent)
      test {
        foo }
            ^ Expression at 2, 7 should be on its own line.
    RUBY
  end

  it 'autocorrects a do/end block where the end is not on its own line' do
    src = <<-RUBY.strip_indent
      test do
        foo  end
    RUBY

    new_source = autocorrect_source(src)

    expect(new_source).to eq(['test do',
                              '  foo',
                              'end',
                              ''].join("\n"))
  end

  it 'autocorrects a {} block where the } is not on its own line' do
    src = <<-RUBY.strip_indent
      test {
        foo  }
    RUBY

    new_source = autocorrect_source(src)

    expect(new_source).to eq(['test {',
                              '  foo',
                              '}',
                              ''].join("\n"))
  end

  it 'autocorrects a {} block where the } is top level code ' \
    'outside of a class' do
    new_source = autocorrect_source(<<-RUBY.strip_indent)
      # frozen_string_literal: true<commit_msg>Cleanup Layout/BlockEndNewline tests<commit_after># frozen_string_literal: true

RSpec.describe RuboCop::Cop::Layout::BlockEndNewline do
  subject(:cop) { described_class.new }

  it 'accepts a one-liner' do
    expect_no_offenses('test do foo end')
  end

  it 'accepts multiline blocks with newlines before the end' do
    expect_no_offenses(<<-RUBY.strip_indent)
      test do
        foo
      end
    RUBY
  end

  it 'registers an offense when multiline block end is not on its own line' do
    expect_offense(<<-RUBY.strip_indent)
      test do
        foo end
            ^^^ Expression at 2, 7 should be on its own line.
    RUBY
  end

  it 'registers an offense when multiline block } is not on its own line' do
    expect_offense(<<-RUBY.strip_indent)
      test {
        foo }
            ^ Expression at 2, 7 should be on its own line.
    RUBY
  end

  it 'autocorrects a do/end block where the end is not on its own line' do
    new_source = autocorrect_source(<<-RUBY.strip_indent)
      test do
        foo  end
    RUBY

    expect(new_source).to eq(<<-RUBY.strip_indent)
      test do
        foo
      end
    RUBY
  end

  it 'autocorrects a {} block where the } is not on its own line' do
    new_source = autocorrect_source(<<-RUBY.strip_indent)
      test {
        foo  }
    RUBY

    expect(new_source).to eq(<<-RUBY.strip_indent)
      test {
        foo
      }
    RUBY
  end

  it 'autocorrects a {} block where the } is top level code ' \
    'outside of a class' do
    new_source = autocorrect_source(<<-RUBY.strip_indent)
      # frozen_string_literal: true
{'fraction_non_alphanumeric': 0.08343057176196032, 'fraction_numerical': 0.002333722287047841, 'mean_word_length': 2.0725806451612905, 'pattern_counts': {'":': 0, '<': 31, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4817061', 'n_tokens_mistral': 1149, 'n_tokens_neox': 1076, 'n_words': 353}