require_relative '_test_helper'

describe Sashite::CGN::Integer do
  subject { Sashite::CGN::Integer }

  describe '.load' do
    describe 'negative integer' do
      let(:cgn_document) { '-42' }
      let(:ruby_object) { -42 }

      it 'parses a CGN string' do
        subject.load(cgn_document).must_equal ruby_object
      end

      describe 'errors' do
        it 'raises with the opposite of zero' do
          -> { subject.load '-0' }.must_raise ArgumentError
        end
      end
    end

    describe 'unsigned integer' do
      let(:cgn_document) { '42' }
      let(:ruby_object) { 42 }

      it 'parses a CGN string' do
        subject.load(cgn_document).must_equal ruby_object
      end

      describe 'errors' do
        it 'raises with an integer beginning by zero' do
          -> { subject.load '04' }.must_raise ArgumentError
        end
      end
    end
  end

  describe '.dump' do
    describe 'negative integer' do
      let(:cgn_document) { '-42' }
      let(:ruby_object) { -42 }

      it 'emits some CGN' do
        subject.dump(ruby_object).must_equal cgn_document
      end

      describe 'errors' do
        it 'raises with the opposite of zero' do
          -> { subject.dump '-0' }.must_raise ArgumentError
        end
      end
    end

    describe 'unsigned integer' do
      let(:cgn_document) { '42' }
      let(:ruby_object) { 42 }

      it 'emits some CGN' do
        subject.dump(ruby_object).must_equal cgn_document
      end

      describe 'errors' do
        it 'raises with an integer beginning by zero' do
          -> { subject.dump '04' }.must_raise ArgumentError
        end
      end
    end
  end
end
