require 'rspec/autorun'
require File.expand_path('../spec_helper', __FILE__)
require File.expand_path('../../lib/mr_poole', __FILE__)

def write_config_file
  f = File.open('_config.yml', 'w')
  f.puts 'poole:'
  f.puts '  default_layout: _layouts/poole.md'
  f.puts '  default_extension: md'
  f.close
end

def write_config_file_no_poole
  f = File.open('_config.yml', 'w')
  f.puts 'name: Douglas N. Adams'
  f.puts 'description: Life, the universe, and everything'
  f.close
end

def write_config_file_bad_yaml
  c = File.open('_config.yml', 'w')
  c.puts 'poole:'
  c.puts '  word_separator: -'
  c.close
end

module MrPoole
  describe Config do
    before(:each) { @olddir, @tmpdir = make_jekyll_dir }
    after(:each) { clean_tmp_files(@tmpdir, @olddir) }

    describe '::initialize' do
      it 'is empty with no config file' do
        config = Config.new
        expect(config).to be_empty
      end

      it "is empty with no 'poole' key in config file" do
        write_config_file_no_poole
        config = Config.new
        expect(config).to be_empty
      end

      it "is not empty with 'poole' key in config file" do
        write_config_file
        config = Config.new
        expect(config).not_to be_empty
      end

      it "exits with on bad YAML" do
        write_config_file_bad_yaml
        expect {
          capture_stdout { cli = CLI.new([]) }
        }.to raise_error(SystemExit)
      end

      it "prints a reasonable error message on bad YAML" do
        write_config_file_bad_yaml
        msg = aborted_poole_output(['post', 'title']).call
        expect(msg).to match(/error.*yaml/i)
      end
    end

    describe '#inspect' do
      it 'looks like OpenStruct.inspect' do
        config = Config.new
        expect(config.inspect).to match(/#<OpenStruct/)
      end

    end

    describe '#method_missing' do
      before(:each) { write_config_file }
      let(:config) { Config.new }

      it 'allows access to defined members' do
        expect(config.default_extension).to eq('md')
      end

      it 'returns nil for unknown key' do
        expect(config.bogus_key).to be_nil
      end
    end

  end
end
