mocksocket 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1,51 @@
1
+ mocksocket
2
+ ----------
3
+
4
+ tiny class providing an IO/TCPSocket mock, full
5
+ duplex and everything. mainly useful for testing
6
+ TCPSocket applications.
7
+
8
+ >> require 'mocksocket'
9
+ >> client, server = MockSocket.pipe #=> [M, M]
10
+ >> client.puts "hello, I'm a client!"
11
+ >> server.gets #=> "hello, I'm a client!\n"
12
+ >> server.puts "welcome, client."
13
+ >> client.gets #=> "welcome, client.\n"
14
+
15
+ it uses Timeout, so instead of stalled tests, you
16
+ get some timeout exception, if you're trying to
17
+ read from an empty buffer.
18
+
19
+ methods implemented: #puts, #print, #gets, #eof?
20
+ and the ever-handy #empty?
21
+
22
+ as well there's tiny test extensions for test/unit:
23
+
24
+ require 'mocksocket/test'
25
+
26
+ module Test::Unit::Assertions
27
+ include MockSocket::Assertions
28
+ end
29
+
30
+ def setup
31
+ @c, @s = MockSocket.pipe
32
+ end
33
+
34
+ def test_empty_buffer
35
+ assert_empty_buffer @c
36
+ end
37
+
38
+ .. and bacon:
39
+
40
+ require 'mocksocket/bacon'
41
+
42
+ describe "awesome" do
43
+ before { @c, @s = MockSocket.pipe }
44
+ end
45
+
46
+ should "be empty" do
47
+ @c.should.be empty_buffer
48
+ end
49
+
50
+ that's about it. (c) 2009 harry vangberg and
51
+ released under the MIT license. enjoy.
@@ -0,0 +1,9 @@
1
+ require 'bacon'
2
+
3
+ class Bacon::Context
4
+ def empty_buffer
5
+ lambda {|io|
6
+ should.raise(Errno::EAGAIN) {io.read_nonblock 1}
7
+ }
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module MockSocket::Assertions
2
+ def assert_empty_buffer(io)
3
+ assert_raise(Errno::EAGAIN) { io.read_nonblock 1 }
4
+ end
5
+ end
data/lib/mocksocket.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'timeout'
2
+
3
+ class MockSocket
4
+ def self.pipe
5
+ socket1, socket2 = new, new
6
+ socket1.in, socket2.out = IO.pipe
7
+ socket2.in, socket1.out = IO.pipe
8
+ [socket1, socket2]
9
+ end
10
+
11
+ attr_accessor :in, :out
12
+ def puts(m) @out.puts(m) end
13
+ def print(m) @out.print(m) end
14
+ def gets()
15
+ Timeout.timeout(1) {@in.gets}
16
+ end
17
+ def eof?() @in.eof? end
18
+ def empty?
19
+ begin
20
+ @in.read_nonblock(1)
21
+ false
22
+ rescue Errno::EAGAIN
23
+ true
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "mocksocket"
3
+ s.version = "0.1"
4
+ s.date = "2009-12-10"
5
+ s.summary = "io/tcpsocket mock for testing"
6
+ s.email = "harry@vangberg.name"
7
+ s.homepage = "http://github.com/ichverstehe/mocksocket"
8
+ s.description = "io/tcpsocket mock for testing."
9
+ s.has_rdoc = false
10
+ s.authors = ["Harry Vangberg"]
11
+ s.files = [
12
+ "README",
13
+ "mocksocket.gemspec",
14
+ "lib/mocksocket.rb",
15
+ "lib/mocksocket/test.rb",
16
+ "lib/mocksocket/bacon.rb"
17
+ ]
18
+ end
19
+
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mocksocket
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Harry Vangberg
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-10 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: io/tcpsocket mock for testing.
17
+ email: harry@vangberg.name
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ - mocksocket.gemspec
27
+ - lib/mocksocket.rb
28
+ - lib/mocksocket/test.rb
29
+ - lib/mocksocket/bacon.rb
30
+ has_rdoc: true
31
+ homepage: http://github.com/ichverstehe/mocksocket
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: io/tcpsocket mock for testing
58
+ test_files: []
59
+