# Encoding: utf-8
# IBM WebSphere Application Server Liberty Buildpack
# Copyright IBM Corp. 2017
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'spec_helper'
require 'memory_limit_helper'
require_relative  '../../../resources/memory/memory_limit'
require_relative  '../../../resources/memory/memory_size'

describe MemoryLimit do
  include_context 'memory_limit_helper'

  it 'should accept memory with an uppercase G',
     memory_limit: '1G' do

    expect(described_class.memory_limit).to eq(MemorySize.new('1048576K'))
  end

  it 'should accept memory with an lowercase G',
     memory_limit: '1g' do

    expect(described_class.memory_limit).to eq(MemorySize.new('1048576K'))
  end

  it 'should accept memory with an uppercase M',
     memory_limit: '1M' do

    expect(described_class.memory_limit).to eq(MemorySize.new('1024K'))
  end

  it 'should accept memory with an lowercase M',
     memory_limit: '1m' do

    expect(described_class.memory_limit).to eq(MemorySize.new('1024K'))
  end

  it 'should return nil if a memory limit is not specified',
     memory_limit: nil do

    expect(described_class.memory_limit).to be_nil
  end

  it 'should fail if a memory limit does not have a unit',
     memory_limit: '-1' do

    expect { described_class.memory_limit }.to raise_error(/Invalid/)
  end

  it 'should fail if a memory limit is not an number',
     memory_limit: 'xm' do

    expect { described_class.memory_limit }.to raise_error(/Invalid/)
  end

  it 'should fail if a memory limit is not an integer',
     memory_limit: '-1.1m' do

    expect { described_class.memory_limit }.to raise_error(/Invalid/)
  end

  it 'should fail if a memory limit is negative',
     memory_limit: '-1m' do

    expect { described_class.memory_limit }.to raise_error(/Invalid/)
  end

end
