Sha256: f5541912375a3d14827e5860cc85f344555bbf17e3d8102c1d278f09b9dba9dc

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

# -*- coding: utf-8 -*-
require 'test_helper'
require_relative '../lib/takosan'

class TakosanTest < Test::Unit::TestCase
  def setup
    Takosan.url = "http://irc.example.com:4649"
    Takosan.channel = "#example"
  end

  def test_privmsg
    http = mock('http')
    http.stub_everything
    http.expects(:request)
    Net::HTTP.expects(:new).with('irc.example.com', 4649).returns(http)

    req = mock('req')
    req.stub_everything
    req.expects(:form_data=).with do |params|
      (params['channel'] == '#example') && (params['message'] == 'foo bar buzz')
    end
    Net::HTTP::Post.expects(:new).with('/privmsg').returns(req)

    Takosan.privmsg('foo bar buzz')
  end

  def test_privmsg_with_message_attachments
    http = mock('http')
    http.stub_everything
    http.expects(:request)
    Net::HTTP.expects(:new).with('irc.example.com', 4649).returns(http)

    req = mock('req')
    req.stub_everything
    req.expects(:form_data=).with do |params|
      (params['channel'] == '#example') && (params['message'] == 'foo bar buzz')
    end
    Net::HTTP::Post.expects(:new).with('/privmsg').returns(req)

    Takosan.privmsg('foo bar buzz', title: 'black', color: '#000')
  end

  def test_rescue_timeout_error
    http = stub('http')
    http.stub_everything
    http.stubs(:request).raises(Timeout::Error, 'timeout error!')
    Net::HTTP.expects(:new).with('irc.example.com', 4649).returns(http)

    req = stub('req')
    req.stub_everything
    Net::HTTP::Post.expects(:new).with('/privmsg').returns(req)

    assert_nothing_raised do
      Takosan.privmsg('foo bar buzz')
    end
  end

  def test_rescue_socket_error
    http = stub('http')
    http.stub_everything
    http.stubs(:request).raises(SocketError, 'connection error!')
    Net::HTTP.expects(:new).with('irc.example.com', 4649).returns(http)

    req = stub('req')
    req.stub_everything
    Net::HTTP::Post.expects(:new).with('/privmsg').returns(req)

    assert_nothing_raised do
      Takosan.privmsg('foo bar buzz')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
takosan-1.3.0 test/takosan_test.rb