catch_and_release 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/catch_and_release.rb +4 -0
- data/lib/catch_and_release/catch.rb +61 -0
- data/lib/catch_and_release/release.rb +34 -0
- data/lib/catch_and_release/version.rb +3 -0
- data/spec/lib/catch_and_release/catch_spec.rb +81 -0
- data/spec/lib/catch_and_release/release_spec.rb +54 -0
- data/spec/lib/catch_and_release_spec.rb +7 -0
- data/spec/spec_helper.rb +2 -0
- metadata +55 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 95a40f343f7a05c372d164592e586f7e11e22d65
|
4
|
+
data.tar.gz: ce43684c7ea5068116ad79687780dd093a6a444a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 777d2738723980eae67121c9fc387a50d8724aca8a9abfce747970503d71ff3d8134a749aae5d4dbfff39255b354a30567f32d85bc6c4f73397bd17fe6d969de
|
7
|
+
data.tar.gz: b50e5e40b4041a721d1797e3e6915063ca18940e1e5eb84be2c6f99ceba269e693746b0dd77fdd330c8097240255453867d05a4401c10c6f7a391e58d41c32e6
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module CatchAndRelease
|
2
|
+
module Catch
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def stdout &block
|
6
|
+
Stdout.new.capture(&block).read
|
7
|
+
end
|
8
|
+
|
9
|
+
def stderr &block
|
10
|
+
Stderr.new.capture(&block).read
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Stdout
|
15
|
+
attr_reader :orig_stdout
|
16
|
+
attr_reader :new_stdout
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
@orig_stdout = $stdout
|
20
|
+
@new_stdout = StringIO.new
|
21
|
+
end
|
22
|
+
|
23
|
+
def capture &block
|
24
|
+
$stdout = new_stdout
|
25
|
+
yield
|
26
|
+
return self
|
27
|
+
ensure
|
28
|
+
$stdout = orig_stdout
|
29
|
+
end
|
30
|
+
|
31
|
+
def read
|
32
|
+
new_stdout.rewind
|
33
|
+
new_stdout.read
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Stderr
|
38
|
+
attr_reader :orig_stderr
|
39
|
+
attr_reader :new_stderr
|
40
|
+
|
41
|
+
def initialize
|
42
|
+
@orig_stderr = $stderr
|
43
|
+
@new_stderr = StringIO.new
|
44
|
+
end
|
45
|
+
|
46
|
+
def capture &block
|
47
|
+
$stderr = new_stderr
|
48
|
+
yield
|
49
|
+
return self
|
50
|
+
ensure
|
51
|
+
$stderr = orig_stderr
|
52
|
+
end
|
53
|
+
|
54
|
+
def read
|
55
|
+
new_stderr.rewind
|
56
|
+
new_stderr.read
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module CatchAndRelease
|
2
|
+
module Release
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def stdin *args, &block
|
7
|
+
Stdin.new.release(*args, &block)
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
class Stdin
|
13
|
+
attr_reader :orig_stdin
|
14
|
+
attr_reader :new_stdin
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
@orig_stdin = $stdin
|
18
|
+
@new_stdin = StringIO.new
|
19
|
+
end
|
20
|
+
|
21
|
+
def release *args, &block
|
22
|
+
$stdin = new_stdin
|
23
|
+
$stdin.puts(args.shift) until args.empty?
|
24
|
+
$stdin.rewind
|
25
|
+
|
26
|
+
yield
|
27
|
+
ensure
|
28
|
+
$stdin = orig_stdin
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CatchAndRelease::Catch do
|
4
|
+
|
5
|
+
describe "::stdout" do
|
6
|
+
it "returns $stdout output from the given block" do
|
7
|
+
out = described_class.stdout do
|
8
|
+
$stdout.print "hello world"
|
9
|
+
end
|
10
|
+
|
11
|
+
expect( out ).to eq "hello world"
|
12
|
+
end
|
13
|
+
|
14
|
+
context "with a non-default stdout" do
|
15
|
+
before :each do
|
16
|
+
@set_stdout = StringIO.new
|
17
|
+
$stdout = @set_stdout
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns $stdout to it's existing state after the call" do
|
21
|
+
described_class.stdout do
|
22
|
+
$stdout.print "hello world"
|
23
|
+
end
|
24
|
+
|
25
|
+
expect( $stdout ).to eq @set_stdout
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when an exception is raised" do
|
29
|
+
it "returns $stdout to it's existing state after the call " do
|
30
|
+
begin
|
31
|
+
described_class.stdout do
|
32
|
+
raise "hello world"
|
33
|
+
end
|
34
|
+
rescue RuntimeError
|
35
|
+
end
|
36
|
+
|
37
|
+
expect( $stdout ).to eq @set_stdout
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "::stderr" do
|
44
|
+
it "returns $stderr output from the given block" do
|
45
|
+
out = described_class.stderr do
|
46
|
+
$stderr.print "hello world"
|
47
|
+
end
|
48
|
+
|
49
|
+
expect( out ).to eq "hello world"
|
50
|
+
end
|
51
|
+
|
52
|
+
context "with a non-default stderr" do
|
53
|
+
before :each do
|
54
|
+
@set_stderr = StringIO.new
|
55
|
+
$stderr = @set_stderr
|
56
|
+
end
|
57
|
+
|
58
|
+
it "returns $stderr to it's existing state after the call" do
|
59
|
+
described_class.stderr do
|
60
|
+
$stderr.print "hello world"
|
61
|
+
end
|
62
|
+
|
63
|
+
expect( $stderr ).to eq @set_stderr
|
64
|
+
end
|
65
|
+
|
66
|
+
context "when an exception is raised" do
|
67
|
+
it "returns $stderr to it's existing state after the call " do
|
68
|
+
begin
|
69
|
+
described_class.stderr do
|
70
|
+
raise "hello world"
|
71
|
+
end
|
72
|
+
rescue RuntimeError
|
73
|
+
end
|
74
|
+
|
75
|
+
expect( $stderr ).to eq @set_stderr
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CatchAndRelease::Release do
|
4
|
+
|
5
|
+
describe "::stdin" do
|
6
|
+
|
7
|
+
it "loads args into stdin" do
|
8
|
+
described_class.stdin 'hello world' do
|
9
|
+
expect( get_input ).to eq 'hello world'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "loads args in order to stdin" do
|
14
|
+
described_class.stdin 'hello world', 'second arg' do
|
15
|
+
expect( get_input ).to eq 'hello world'
|
16
|
+
expect( get_input ).to eq 'second arg'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "with a non-default stdin" do
|
21
|
+
before :each do
|
22
|
+
@set_stdin = StringIO.new
|
23
|
+
$stdin = @set_stdin
|
24
|
+
end
|
25
|
+
|
26
|
+
it "returns $stdin to it's existing state after the call" do
|
27
|
+
described_class.stdin 'hello world' do
|
28
|
+
'nothing'
|
29
|
+
end
|
30
|
+
|
31
|
+
expect( $stdin ).to eq @set_stdin
|
32
|
+
end
|
33
|
+
|
34
|
+
context "when an exception is raised" do
|
35
|
+
it "returns $stdin to it's existing state after the call " do
|
36
|
+
begin
|
37
|
+
described_class.stdin 'hello world' do
|
38
|
+
raise "hello world"
|
39
|
+
end
|
40
|
+
rescue RuntimeError
|
41
|
+
end
|
42
|
+
|
43
|
+
expect( $stdin ).to eq @set_stdin
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
def get_input
|
51
|
+
$stdin.gets.chomp
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: catch_and_release
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steven Sloan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-02 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: ' Tools for testing standard inputs & outputs '
|
14
|
+
email: stevenosloan@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/catch_and_release/catch.rb
|
20
|
+
- lib/catch_and_release/release.rb
|
21
|
+
- lib/catch_and_release/version.rb
|
22
|
+
- lib/catch_and_release.rb
|
23
|
+
- spec/lib/catch_and_release/catch_spec.rb
|
24
|
+
- spec/lib/catch_and_release/release_spec.rb
|
25
|
+
- spec/lib/catch_and_release_spec.rb
|
26
|
+
- spec/spec_helper.rb
|
27
|
+
homepage: http://github.com/stevenosloan/catch_and_release
|
28
|
+
licenses:
|
29
|
+
- MIT
|
30
|
+
metadata: {}
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 1.9.3
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 2.0.5
|
48
|
+
signing_key:
|
49
|
+
specification_version: 4
|
50
|
+
summary: Tools for testing standard inputs & outputs
|
51
|
+
test_files:
|
52
|
+
- spec/lib/catch_and_release/catch_spec.rb
|
53
|
+
- spec/lib/catch_and_release/release_spec.rb
|
54
|
+
- spec/lib/catch_and_release_spec.rb
|
55
|
+
- spec/spec_helper.rb
|