Sha256: 495c938773abf16e905b2a51443ad63ca524f574cd7dde006aaba5bdfaf23848

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

require 'spec_helper'

Thing = Struct.new(:foo)

class MyPlugin
  include Cinch::Plugin

  def initialize(*args)
    super
    @storage = Cinch::Storage.new(config[:filename] || '/dev/null', [])
  end


  match /store (.*)/,       method: :store_text
  match /storething (.*)/,  method: :store_object

  def store_text(m, text)
    @storage.data << text
    @storage.synced_save(@bot)
  end

  def store_object(m, text)
    @storage.data << Thing.new(text)
    @storage.synced_save(@bot)
  end
end


describe Cinch::Storage do
  include Cinch::Test

  before(:each) do
    @filename = '/tmp/storage_test.tmp'
    File.delete(@filename) if File.exist?(@filename)
    @bot = make_bot(MyPlugin, { filename: @filename })
  end

  it 'should allow users to store information between bot runs' do
    storage = Cinch::Storage.new(@filename, [])
    storage.data << 'foo'
    storage.save

    expect(Cinch::Storage.new(@filename).data)
      .to include('foo')
  end

  it 'should handle multiple bot threads attempting to write to the same file' do
    10.times { |x| get_replies(make_message(@bot, "!store #{x}")) }
    expect(Cinch::Storage.new(@filename).data.length).to eq(10)
  end

  it 'should write object data to the disk' do
    get_replies(make_message(@bot, "!storething foobar"))
    expect(Cinch::Storage.new(@filename).data.first).to eq(Thing.new('foobar'))
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cinch-storage-1.2.1 spec/cinch-storage_spec.rb
cinch-storage-1.2.0 spec/cinch-storage_spec.rb