require File.expand_path('../spec_helper.rb', __FILE__)

describe SettingsPlistGenerator = CocoaPodsAcknowledgements::SettingsPlistGenerator do

  before do
    @spec1 = SpecHelper.spec1
    @spec2 = SpecHelper.spec2
    SettingsPlistGenerator.stubs(:file_accessor).returns(nil)
    @sandbox = temporary_sandbox
    @target_description = stub('target_description',
                               :specs => [@spec1, @spec2],
                               :platform_name => 'ios')
  end

  it 'generates metadata' do
    result = SettingsPlistGenerator.generate(@target_description, @sandbox, [])
    result.should == {
      'Title' => 'Acknowledgements',
      'StringsTable' => 'Acknowledgements',
      'PreferenceSpecifiers' => [
        {
          'Title' => 'Acknowledgements',
          'Type' => 'PSGroupSpecifier',
          'FooterText' => 'This application makes use of the following third party libraries:'
        },
        {
          'Title' => 'monkeylib',
          'Type' => 'PSGroupSpecifier',
          'FooterText' => 'Permission is hereby granted ...'
        },
        {
          'Title' => 'BananaLib',
          'Type' => 'PSGroupSpecifier',
          'FooterText' => 'Permission is hereby granted ...'
        },
        {
          'Title' => nil,
          'Type' => 'PSGroupSpecifier',
          'FooterText' => 'Generated by CocoaPods - https://cocoapods.org'
        },
      ]
    }
  end

  it 'generates nil if specs is empty' do
    target_description = stub('target_description',
                              :specs => [],
                              :platform_name => 'ios')
    result = SettingsPlistGenerator.generate(target_description, @sandbox, [])
    result.should.be.nil?
  end

  it 'does not include metadata for excluded specs' do
    target_description = stub('target_description',
                              :specs => [@spec1, @spec2],
                              :platform_name => 'ios')
    result = SettingsPlistGenerator.generate(target_description, @sandbox, [@spec1.name])
    result.should == {
      'Title' => 'Acknowledgements',
      'StringsTable' => 'Acknowledgements',
      'PreferenceSpecifiers' => [
        {
          'Title' => 'Acknowledgements',
          'Type' => 'PSGroupSpecifier',
          'FooterText' => 'This application makes use of the following third party libraries:'
        },
        {
          'Title' => 'BananaLib',
          'Type' => 'PSGroupSpecifier',
          'FooterText' => 'Permission is hereby granted ...'
        },
        {
          'Title' => nil,
          'Type' => 'PSGroupSpecifier',
          'FooterText' => 'Generated by CocoaPods - https://cocoapods.org'
        },
      ]
    }
  end
end
