<commit_before>#!/usr/bin/ruby
# encoding: UTF-8

require 'English'

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')

require 'daniel'

module Daniel
  class MainProgram
    attr_accessor :passphrase, :passwords, :lines, :output, :warnings
    attr_accessor :params, :clipboard

    def read_passphrase
      if @passphrase.is_a? Array
        @passphrase.shift
      else
        @passphrase
      end
    end

    def output_password(pass, _clipboard = false)
      @passwords ||= []
      @passwords << pass
    end

    def warn(*args)
      @warnings ||= []
      @warnings << args
    end

    def read_line
      fail EOFError if @lines.empty?
      @lines.shift
    end

    def puts(*args)
      @output ||= []
      @output << args
    end

    def print(*)
    end
  end
end

describe Daniel::MainProgram do
  it 'parses args correctly' do
    prog = Daniel::MainProgram.new
    prog.parse_args(%w(-l8 -v1 -f15))
    expect(prog.params.length).to eq 8
    expect(prog.params.version).to eq 1
    expect(prog.params.flags).to eq 15
  end

  it 'parses -m correctly' do
    prog = Daniel::MainProgram.new
    prog.parse_args(%w(-m))
    expect(prog.params.flags).to eq Daniel::Flags::REPLICATE_EXISTING
  end

  it 'refuses to accept -f and -m together' do
    prog = Daniel::MainProgram.new
    expect { prog.parse_args(%w(-m -f15)) }
      .to raise_error(RuntimeError, /can't.*both.*-m.*-f/i)
  end

  it 'generates reasonable output' do
    prog = Daniel::MainProgram.new
    prog.lines = ['example.tld']
    prog.passphrase = 'foobar'
    prog.main([])
    expect(prog.passwords).to eq ['<PASSWORD>']
    expect(prog.output.flatten).to eq [
      '# ok, checksum is 72eb36',
      'Reminder is: 72eb360a1000example.tld'
    ]
  end

  it 'processes !! properly' do
    prog = Daniel::MainProgram.new
    prog.lines = ['example.tld', '!!']
    prog.passphrase = '<PASSWORD>'
    prog.main([])
    expect(prog.passwords).to eq [
      '<PASSWORD>',
      '<PASSWORD>'
    ]
    expect(prog.output.flatten).to eq [
      '# ok, checksum is 72eb36',
      'Reminder is: 72eb360a1000example.tld',
      'Reminder is: 72eb360a1000example.tld'
    ]
  end

  it 'processes commands properly' do
    prog = Daniel::MainProgram.new
    prog.lines = [
      '!length=8',
      '!version=1',
      '!flags=15',
      'example.tld'
    ]
    prog.passphrase = '<PASSWORD>'
    prog.main([])
    expect(prog.passwords).to eq ['<PASSWORD>']
    expect(prog.output.flatten).to eq [
      '# ok, checksum is 72eb36',
      'Reminder is: 72eb360f0801example.tld'
    ]
  end

  it 'handles reloading the passphrase properly' do
    prog = Daniel::MainProgram.new
    prog.lines = [
      'example.tld',
      '!pass',
      'bar'
    ]
    prog.passphrase = %w(foobar foo)
    prog.main([])
    expect(prog.passwords).to eq [
      '<PASSWORD>',
      '<PASSWORD>'
    ]
    expect(prog.output.flatten).to eq [
      '# ok, checksum is 72eb36',
      'Reminder is: 72eb360a1000example.tld',
      '# ok, checksum is 8244c5',
      'Reminder is: 8244c50a1000bar'
    ]
  end

  it 'processes command-line arguments properly' do
    prog = Daniel::MainProgram.new
    prog.lines = ['example.tld']
    prog.passphrase = '<PASSWORD>'
    prog.main(%w(-l8 -v1 -f15))
    expect(prog.passwords).to eq ['<PASSWORD>']
    expect(prog.output.flatten).to eq [
      '# ok, checksum is 72eb36',
      'Reminder is: 72eb360f0801example.tld'
    ]
  end

  it 'processes reminders properly' do
    prog = Daniel::MainProgram.new
    prog.lines = ['example.tld']
    prog.passphrase = '<PASSWORD>'
    prog.main(['72eb360f0801example.tld', '<PASSWORD>example.tld'])
    expect(prog.passwords).to eq ['<PASSWORD>', '<PASSWORD>']
    expect(prog.output.flatten).to eq ['# ok, checksum is 72eb36']
  end

  it 'handles mismatched reminders properly' do
    prog = Daniel::MainProgram.new
    prog.lines = ['example.tld']
    prog.passphrase = '<PASSWORD>'
    expect { prog.main(['ffffff0f0801example.tld']) }
      .to raise_error(RuntimeError, /checksum mismatch/i)
  end
<commit_msg>Fix broken tests on Ruby 1.8.7.<commit_after>#!/usr/bin/ruby
# encoding: UTF-8

require 'English'

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')

require 'daniel'

module Daniel
  class MainProgram
    attr_accessor :passphrase, :passwords, :lines, :output, :warnings
    attr_accessor :params, :clipboard

    def read_passphrase
      if @passphrase.is_a? Array
        @passphrase.shift
      else
        @passphrase
      end
    end

    def output_password(pass, _clipboard = false)
      @passwords ||= []
      @passwords << pass
    end

    def warn(*args)
      @warnings ||= []
      @warnings << args
    end

    def read_line
      fail EOFError if @lines.empty?
      @lines.shift
    end

    def puts(*args)
      @output ||= []
      @output << args
    end

    def print(*)
    end
  end
end

describe Daniel::MainProgram do
  it 'parses args correctly' do
    prog = Daniel::MainProgram.new
    prog.parse_args(%w(-l8 -v1 -f15))
    expect(prog.params.length).to eq 8
    expect(prog.params.version).to eq 1
    expect(prog.params.flags).to eq 15
  end

  it 'parses -m correctly' do
    prog = Daniel::MainProgram.new
    prog.parse_args(%w(-m))
    expect(prog.params.flags).to eq Daniel::Flags::REPLICATE_EXISTING
  end

  it 'refuses to accept -f and -m together' do
    prog = Daniel::MainProgram.new
    expect { prog.parse_args(%w(-m -f15)) } \
      .to raise_error(RuntimeError, /can't.*both.*-m.*-f/i)
  end

  it 'generates reasonable output' do
    prog = Daniel::MainProgram.new
    prog.lines = ['example.tld']
    prog.passphrase = '<PASSWORD>'
    prog.main([])
    expect(prog.passwords).to eq ['<PASSWORD>']
    expect(prog.output.flatten).to eq [
      '# ok, checksum is 72eb36',
      'Reminder is: 72eb360a1000example.tld'
    ]
  end

  it 'processes !! properly' do
    prog = Daniel::MainProgram.new
    prog.lines = ['example.tld', '!!']
    prog.passphrase = '<PASSWORD>'
    prog.main([])
    expect(prog.passwords).to eq [
      '<PASSWORD>',
      '<PASSWORD>'
    ]
    expect(prog.output.flatten).to eq [
      '# ok, checksum is 72eb36',
      'Reminder is: 72eb360a1000example.tld',
      'Reminder is: 72eb360a1000example.tld'
    ]
  end

  it 'processes commands properly' do
    prog = Daniel::MainProgram.new
    prog.lines = [
      '!length=8',
      '!version=1',
      '!flags=15',
      'example.tld'
    ]
    prog.passphrase = '<PASSWORD>'
    prog.main([])
    expect(prog.passwords).to eq ['<PASSWORD>']
    expect(prog.output.flatten).to eq [
      '# ok, checksum is 72eb36',
      'Reminder is: 72eb360f0801example.tld'
    ]
  end

  it 'handles reloading the passphrase properly' do
    prog = Daniel::MainProgram.new
    prog.lines = [
      'example.tld',
      '!pass',
      'bar'
    ]
    prog.passphrase = %w(<PASSWORD>)
    prog.main([])
    expect(prog.passwords).to eq [
      '<PASSWORD>',
      '<PASSWORD>'
    ]
    expect(prog.output.flatten).to eq [
      '# ok, checksum is 72eb36',
      'Reminder is: 72eb360a1000example.tld',
      '# ok, checksum is 8244c5',
      'Reminder is: 8244c50a1000bar'
    ]
  end

  it 'processes command-line arguments properly' do
    prog = Daniel::MainProgram.new
    prog.lines = ['example.tld']
    prog.passphrase = 'foobar'
    prog.main(%w(-l8 -v1 -f15))
    expect(prog.passwords).to eq ['<PASSWORD>']
    expect(prog.output.flatten).to eq [
      '# ok, checksum is 72eb36',
      'Reminder is: 72eb360f0801example.tld'
    ]
  end

  it 'processes reminders properly' do
    prog = Daniel::MainProgram.new
    prog.lines = ['example.tld']
    prog.passphrase = '<PASSWORD>'
    prog.main(['72eb360f0801example.tld', '72eb360a1000example.tld'])
    expect(prog.passwords).to eq ['mJRUHjid', 'nj&xzO@hz&QvuoGY']
    expect(prog.output.flatten).to eq ['# ok, checksum is 72eb36']
  end

  it 'handles mismatched reminders properly' do
    prog = Daniel::MainProgram.new
    prog.lines = ['example.tld']
    prog.passphrase = '<PASSWORD>'
    expect { prog.main(['ffffff0f0801example.tld']) } \
      .to raise_error(RuntimeError, /checksum mismatch/i)
  end

{'fraction_non_alphanumeric': 0.14226451927769643, 'fraction_numerical': 0.032210834553440704, 'mean_word_length': 2.7090497737556563, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 31, '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': '290329', 'n_tokens_mistral': 3170, 'n_tokens_neox': 2869, 'n_words': 622}