<commit_before>require File.expand_path('../spec_helper', __FILE__)

describe Xcodeproj::Config do
  describe 'In general' do
    extend SpecHelper::TemporaryDirectory

    before do
      @hash = { 'OTHER_LDFLAGS' => '-framework "Foundation"' }
      @config = Xcodeproj::Config.new(@hash)
      @config_fixture = fixture_path('oneline-key-value.xcconfig')
      @config_with_include_fixture = fixture_path('config-with-include.xcconfig')
    end

    it 'can be created with hash' do
      config = Xcodeproj::Config.new(@hash)
      config.to_hash.should.be.equal @hash
    end

    it 'can be created with file path' do
      config = Xcodeproj::Config.new(@config_fixture)
      config.to_hash.should.be.equal @hash
    end

    it 'can be created with File instance' do
      xcconfig_file = File.new(@config_fixture)
      xcconfig = Xcodeproj::Config.new(xcconfig_file)
      xcconfig.to_hash.should.be.equal @hash
    end

    it 'does not equal if attributes change' do
      other_config = @config.dup
      other_config.should.be.equal @config
      other_config.attributes['SOME_KEY'] = 'SOME_VALUE'
      other_config.should.not == @config
    end

    it 'does not equal if other linker flags change change' do
      other_config = @config.dup
      other_config.should.be.equal @config
      other_config.other_linker_flags[:simple].add '--some-option'
      other_config.should.not == @config
    end

    it 'does not equal if includes change' do
      other_config = @config.dup
      other_config.should.be.equal @config
      other_config.includes.push 'Somefile'
      other_config.should.not == @config
    end

    it 'does not modifies the hash used for initialization' do
      original = @hash.dup
      Xcodeproj::Config.new(@hash)
      @hash.should.be.equal original
    end

    it 'parses the frameworks and the libraries' do
      hash = { 'OTHER_LDFLAGS' => '-framework Foundation -weak_framework Twitter -lxml2.2.7.3' }
      config = Xcodeproj::Config.new(hash)
      config.frameworks.to_a.should.be.equal %w(Foundation)
      config.weak_frameworks.to_a.should.be.equal %w(Twitter)
      config.libraries.to_a.should.be.equal %w(xml2.2.7.3)
    end

    it 'can be serialized with #to_s' do
      @config.to_s.should.be.equal "OTHER_LDFLAGS = -framework \"Foundation\"\n"
    end

    it 'strips trailing whitespace when serializing with #to_s' do
      @config.attributes['FOO'] = 'BAR   '
      @config.to_s.should == "FOO = BAR\nOTHER_LDFLAGS = -framework \"Foundation\"\n"
    end

    it 'sorts the internal data by setting name when serializing with #to_s' do
      config = Xcodeproj::Config.new('Y' => '2', 'Z' => '3', 'X' => '1')
      config.to_s.should == "X = 1\nY = 2\nZ = 3\n"
    end

    it 'can serialize an empty config' do
      config = Xcodeproj::Config.new
      config.to_s.should == "\n"
    end

    it 'can be serialized with #to_hash' do
      @config.to_hash.should.be.equal @hash
    end

    it 'ignores settings that are only inherited in #to_hash' do
      config = Xcodeproj::Config.new('Y' => '$(inherited)', 'Z' => '${inherited}', 'X' => '$(inherited) 1')
      config.to_hash.should == { 'X' => '$(inherited) 1' }
    end

    it 'handles non-string settings in #to_s' do
      config = Xcodeproj::Config.new('X' => true, 'Y' => 123)
      config.to_s.should == <<-EOS
X = true
Y = 123
      EOS
    end

    it 'can prefix values during serialization' do
      @prefix_hash = { 'PODS_PREFIX_OTHER_LDFLAGS' => @hash['OTHER_LDFLAGS'] }
      @config.to_hash('PODS_PREFIX_').should.be.equal @prefix_hash
    end

    it 'does not serialize with #to_s when inspecting the object' do
      @config.inspect.should == @config.to_hash.inspect
    end

    it 'can be compared with other instances' do
      config_dupe = Xcodeproj::Config.new(@hash)
      config_dupe.should.be.equal @config
    end

    it 'merges another config hash in place' do
      @config.merge!('HEADER_SEARCH_PATHS' => '/some/path')
      @config.to_hash.should == {
        'OTHER_LDFLAGS' => '-framework "Foundation"',
        'HEADER_SEARCH_PATHS' => '/some/path',
      }
    end

    it 'merges another config hash in place with the `<<` shortcut' do
      @config << { 'HEADER_SEARCH_PATHS' => '/some/path' }
      @config.to_hash.should == {
        'OTHER_LDFLAGS' => '-framework "Foundation"',
        'HEADER_SEARCH_PATHS' => '/some/path',
      }
    end

    it 'merges another hash in a new one' do
      new = @config.merge('HEADER_SEARCH_PATHS' => '/some/path')
      new.object_id.should.not == @config.object_id
      new.to_hash.should == {
        'OTHER_LDFLAGS' => '-framework "Foundation"',
        'HEADER_SEARCH_PATHS' => '/some/path',
      }
      @config.to_hash.should == { 'OTHER_LDFLAGS' => '-framework "Foundation"' }
    end

    it 'appends a value for the same key when merging' do
      @config.merge!('OTHER_LDFLAGS' => '-l xml2.2.7.3')
      @config.to_hash.should == {
        'OTHER_LDFLAGS' => '-l"xml2.2.7.3" -framework "Foundation"',
      }<commit_msg>Add more tests for config merge behavior<commit_after>require File.expand_path('../spec_helper', __FILE__)

describe Xcodeproj::Config do
  describe 'In general' do
    extend SpecHelper::TemporaryDirectory

    before do
      @hash = { 'OTHER_LDFLAGS' => '-framework "Foundation"' }
      @config = Xcodeproj::Config.new(@hash)
      @config_fixture = fixture_path('oneline-key-value.xcconfig')
      @config_with_include_fixture = fixture_path('config-with-include.xcconfig')
    end

    it 'can be created with hash' do
      config = Xcodeproj::Config.new(@hash)
      config.to_hash.should.be.equal @hash
    end

    it 'can be created with file path' do
      config = Xcodeproj::Config.new(@config_fixture)
      config.to_hash.should.be.equal @hash
    end

    it 'can be created with File instance' do
      xcconfig_file = File.new(@config_fixture)
      xcconfig = Xcodeproj::Config.new(xcconfig_file)
      xcconfig.to_hash.should.be.equal @hash
    end

    it 'does not equal if attributes change' do
      other_config = @config.dup
      other_config.should.be.equal @config
      other_config.attributes['SOME_KEY'] = 'SOME_VALUE'
      other_config.should.not == @config
    end

    it 'does not equal if other linker flags change change' do
      other_config = @config.dup
      other_config.should.be.equal @config
      other_config.other_linker_flags[:simple].add '--some-option'
      other_config.should.not == @config
    end

    it 'does not equal if includes change' do
      other_config = @config.dup
      other_config.should.be.equal @config
      other_config.includes.push 'Somefile'
      other_config.should.not == @config
    end

    it 'does not modifies the hash used for initialization' do
      original = @hash.dup
      Xcodeproj::Config.new(@hash)
      @hash.should.be.equal original
    end

    it 'parses the frameworks and the libraries' do
      hash = { 'OTHER_LDFLAGS' => '-framework Foundation -weak_framework Twitter -lxml2.2.7.3' }
      config = Xcodeproj::Config.new(hash)
      config.frameworks.to_a.should.be.equal %w(Foundation)
      config.weak_frameworks.to_a.should.be.equal %w(Twitter)
      config.libraries.to_a.should.be.equal %w(xml2.2.7.3)
    end

    it 'can be serialized with #to_s' do
      @config.to_s.should.be.equal "OTHER_LDFLAGS = -framework \"Foundation\"\n"
    end

    it 'strips trailing whitespace when serializing with #to_s' do
      @config.attributes['FOO'] = 'BAR   '
      @config.to_s.should == "FOO = BAR\nOTHER_LDFLAGS = -framework \"Foundation\"\n"
    end

    it 'sorts the internal data by setting name when serializing with #to_s' do
      config = Xcodeproj::Config.new('Y' => '2', 'Z' => '3', 'X' => '1')
      config.to_s.should == "X = 1\nY = 2\nZ = 3\n"
    end

    it 'can serialize an empty config' do
      config = Xcodeproj::Config.new
      config.to_s.should == "\n"
    end

    it 'can be serialized with #to_hash' do
      @config.to_hash.should.be.equal @hash
    end

    it 'ignores settings that are only inherited in #to_hash' do
      config = Xcodeproj::Config.new('Y' => '$(inherited)', 'Z' => '${inherited}', 'X' => '$(inherited) 1')
      config.to_hash.should == { 'X' => '$(inherited) 1' }
    end

    it 'handles non-string settings in #to_s' do
      config = Xcodeproj::Config.new('X' => true, 'Y' => 123)
      config.to_s.should == <<-EOS
X = true
Y = 123
      EOS
    end

    it 'can prefix values during serialization' do
      @prefix_hash = { 'PODS_PREFIX_OTHER_LDFLAGS' => @hash['OTHER_LDFLAGS'] }
      @config.to_hash('PODS_PREFIX_').should.be.equal @prefix_hash
    end

    it 'does not serialize with #to_s when inspecting the object' do
      @config.inspect.should == @config.to_hash.inspect
    end

    it 'can be compared with other instances' do
      config_dupe = Xcodeproj::Config.new(@hash)
      config_dupe.should.be.equal @config
    end

    it 'merges another config hash in place' do
      @config.merge!('HEADER_SEARCH_PATHS' => '/some/path')
      @config.to_hash.should == {
        'OTHER_LDFLAGS' => '-framework "Foundation"',
        'HEADER_SEARCH_PATHS' => '/some/path',
      }
    end

    it 'merges another config hash in place with the `<<` shortcut' do
      @config << { 'HEADER_SEARCH_PATHS' => '/some/path' }
      @config.to_hash.should == {
        'OTHER_LDFLAGS' => '-framework "Foundation"',
        'HEADER_SEARCH_PATHS' => '/some/path',
      }

      @config << { 'HEADER_SEARCH_PATHS' => '/some/path/subdir', 'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) ' }
      @config.to_hash['HEADER_SEARCH_PATHS'].should == '/some/path /some/path/subdir'

      @config << { 'GCC_PREPROCESSOR_DEFINITIONS' => 'COCOAPODS=1' }
      @config.to_hash['GCC_PREPROCESSOR_DEFINITIONS'].should == '$(inherited) COCOAPODS=1'
    end

    it 'merges another hash in a new one' do
      new = @config.merge('HEADER_SEARCH_PATHS' => '/some/path')
      new.object_id.should.not == @config.object_id
      new.to_hash.should == {
        'OTHER_LDFLAGS' => '-framework "Foundation"',
        'HEADER_SEARCH_PATHS' => '/some/path',
      }
      @config.to_hash.should == { 'OTHER_LDFLAGS' => '-framework "Foundation"' }
    end

    it 'appends a value for the same key when merging' do
      @config.merge!('OTHER_LDFLAGS' => '-l xml2.2.7.3')
      @config.to_hash.should == {
        'OTHER_LDFLAGS' => '-l"xml2.2.7.3" -framework "Foundation"',
      }
{'fraction_non_alphanumeric': 0.1323079862437906, 'fraction_numerical': 0.0059228123805884604, 'mean_word_length': 2.875971862273232, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 8, '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': '4077301', 'n_tokens_mistral': 3598, 'n_tokens_neox': 3522, 'n_words': 822}