Sha256: 677b811121923eb97601d6525c55dadd2a550b84e47365fec5d3f8fc304d7ab4

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

class Entry
  include Motion::Encodable
  properties :title, :body
  def initialize(params={})
    @title = params[:title]
    @body = params[:body]
  end
end

describe Motion::Encodable do
  before do
    @params = {
        title: 'bar',
        body: 'baz'
    }
  end

  describe 'NSCoder protocol' do
    before do
      @entry = Entry.new(@params)
      dir_path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0]
      @file_path = dir_path + '/entry.dat'
    end

    after do
      # remove saved file
      file_manager = NSFileManager.defaultManager
      if file_manager.fileExistsAtPath(@file_path)
        error = Pointer.new(:object)
        file_manager.removeItemAtPath(@file_path, error: error)
        raise error[0] if error[0]
      end
    end

    it 'should responds to required methods' do
      @entry.respond_to?(:'initWithCoder:').should == true
      @entry.respond_to?(:'encodeWithCoder:').should == true
    end

    it 'should be able to load as data' do
      entry_as_data = @entry.to_data
      loaded_entry = Entry.load(entry_as_data)
      loaded_entry.instance_variable_get(:'@title').should == @params[:title]
      loaded_entry.instance_variable_get(:'@body').should == @params[:body]
    end

    it 'should be able to save into file' do
      @entry.save_to_file(@file_path)
      entry_as_data = NSData.dataWithContentsOfFile(@file_path)
      loaded_entry = Entry.load(entry_as_data)
      loaded_entry.instance_variable_get('@title').should == @params[:title]
      loaded_entry.instance_variable_get('@body').should == @params[:body]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
motion-encodable-0.0.3 spec/motion/encodable_spec.rb