
require 'awestruct/cli/options'

describe Awestruct::CLI::Options do

  it 'should have reasonable defaults' do
    options = Awestruct::CLI::Options.new
    options.generate.should == nil
    options.server.should   == false
    options.deploy.should   == false

    options.port.should      == 4242
    options.bind_addr.should == 'localhost'

    options.auto.should  == false
    options.force.should == false
    options.init.should  == false

    options.framework.should == 'compass'
    options.scaffold.should == true

    options.base_url.should == 'http://localhost:4242'
    options.profile.should  == nil
    options.script.should   == nil
    options.verbose.should  == false

    options.output_dir == File.join( __FILE__, '..', '_site' )
    options.source_dir == File.join( __FILE__, '..' )
  end

  context 'parsing' do
    def parse!(*args)
      Awestruct::CLI::Options.parse! args
    end

    it 'should parse server-related args' do
      parse!( '-s' ).server.should == true
      parse!( '--server' ).server.should == true

      parse!( '-p', '8180' ).port.should == 8180
      parse!( '--port', '8181' ).port.should == 8181

      parse!( '-b', '1.2.3.4' ).bind_addr.should == '1.2.3.4'
      parse!( '--bind', '5.6.7.8' ).bind_addr.should == '5.6.7.8'

      parse!( '-u', 'http://mysite.com/' ).base_url.should == 'http://mysite.com/' 
      parse!( '--url', 'http://mysite.com/' ).base_url.should == 'http://mysite.com/'
    end

    it 'should parse profile-related args' do
      parse!( '-P', 'numberwang' ).profile.should == 'numberwang'
      parse!( '--profile', 'superhans' ).profile.should == 'superhans'
    end

    it 'should parse generation-related args' do
      parse!( '-g' ).generate.should == true
      parse!( '--generate' ).generate.should == true
      parse!( '--no-generate' ).generate.should == false

      parse!( '--force' ).force.should == true

      parse!( '-a' ).auto.should == true
      parse!( '--auto' ).auto.should == true
    end

    # I'm not sure if we'll ever do this
    # it 'should parse script-related args' do
    #   pending 'Not yet implemented. See issue #248.'
    #   #parse!( '--run', 'puts "hi"' ).script.should == 'puts "hi"'
    # end

    it 'should turn off generate when doing a --deploy' do
      result = parse!( '--deploy' )
      result.deploy.should be_truthy
      result.generate.should be_falsey
    end

    it 'should turn off generate when doing a --deploy unless explicitly turned back on' do
      result = parse!( '--deploy', '--generate' )
      result.deploy.should be_truthy
      result.generate.should be_truthy

      result = parse!( '--generate', '--deploy' )
      result.deploy.should be_truthy
      result.generate.should be_truthy
    end

    it 'should turn on verbose when -w or --verbose is explicitly turned back on' do
      result = parse!( '-w' )
      result.verbose.should be_truthy

      result = parse!( '--verbose' )
      result.verbose.should be_truthy
    end

    it 'should generate by default' do
      parse!().generate.should be_truthy
    end

    it 'should parse directory options' do
      result = Awestruct::CLI::Options.parse!(%w(--source-dir /tmp --output-dir /tmp/new-site))
      result.source_dir.should eql '/tmp'
      result.output_dir.should eql '/tmp/new-site'
    end

    it 'by default, output_dir should be "_site" under source_dir' do
      result = Awestruct::CLI::Options.parse!(%w(--source-dir /tmp))
      result.source_dir.should eql '/tmp'
      result.output_dir.should eql '/tmp/_site' 
    end

    it 'by default, perf logging should be disabled' do
      result = Awestruct::CLI::Options.parse!(%w(--source-dir /tmp))
      result.perf_log.should eql false
      end

    it '--perf should enable perf logging' do
      result = Awestruct::CLI::Options.parse!(%w(--source-dir /tmp --perf))
      result.perf_log.should eql true
    end

    context 'source dir should not override output dir' do
      context 'source dir first' do
        result = Awestruct::CLI::Options.parse!(%w(--source-dir /tmp --output-dir /tmp/output))
        it { expect(result.output_dir).to eq '/tmp/output' }
      end
      context 'output dir first' do
        result = Awestruct::CLI::Options.parse!(%w(--output-dir /tmp/output --source-dir /tmp))
        it { expect(result.output_dir).to eq '/tmp/output' }
      end
    end
  end

end
