require 'spec_helper'

describe Compare::CLI do
  describe "CLI" do
    it 'will parse specified options with short flags' do
      args = ["-o", "bing", "foo", "bar"]
      results = Compare::CLI.parse args

      expect(results).not_to be nil
      expect(results.output).to eq("bing")
      expect(results.left).to eq("foo")
      expect(results.right).to eq("bar")
    end

    it 'will parse specified options with long enabled flags' do
      args = ["--output=bing", "foo", "bar"]
      results = Compare::CLI.parse args

      expect(results).not_to be nil
      expect(results.output).to eq("bing")
      expect(results.left).to eq("foo")
      expect(results.right).to eq("bar")
    end

    it 'will parse specified options with no flags' do
      args = ["foo", "bar"]
      results = Compare::CLI.parse args

      expect(results).not_to be nil
      expect(results.output).to eq("")
      expect(results.left).to eq("foo")
      expect(results.right).to eq("bar")
    end

    it 'will print usage and exit if left and right directories are not specified' do
      help = %Q(Usage: compare [-o=FILE] left_directory right_directory)
      expect do
        output = capture_stdout { Compare::CLI.parse([]) }
        expect(output).to eq(help)
      end.to raise_error(SystemExit)
    end

    it 'will print usage and exit if only left directory is specified' do
      help = %Q(Usage: compare [-o=FILE] left_directory right_directory)
      expect do
        output = capture_stdout { Compare::CLI.parse(["foo"]) }
        expect(output).to eq(help)
      end.to raise_error(SystemExit)
    end

    it 'will print usage and exit if only empty strings for the directories are specified' do
      help = %Q(Usage: compare [-o=FILE] left_directory right_directory)
      expect do
        output = capture_stdout { Compare::CLI.parse(["", ""]) }
        expect(output).to eq(help)
      end.to raise_error(SystemExit)
    end

    it 'should return a usage message with "options" if arg is false' do
      help = %Q(Usage: compare [options] left_directory right_directory)
      expect(Compare::CLI.usage_message(false)).to eq(help)
    end

    it 'should return a usage message with "options" if arg is not provided' do
      help = %Q(Usage: compare [options] left_directory right_directory)
      expect(Compare::CLI.usage_message).to eq(help)
    end

    it 'should return a usage message with the options if arg is true' do
      help = %Q(Usage: compare [-o=FILE] left_directory right_directory)
      expect(Compare::CLI.usage_message(true)).to eq(help)
    end
  end
end
