switchout 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6fa9b66327528cf7587b799f9e69d60516f86f04
4
- data.tar.gz: 99829d9bfdf6af7d2347e3d959666338e5bf17aa
3
+ metadata.gz: 3c4142d46db35e0070fa8eb7e6f12c070c7c3791
4
+ data.tar.gz: 5062b47b10138e839967281d2126dc62a0d48f19
5
5
  SHA512:
6
- metadata.gz: 0e8dccf7cf70c4ab8791ebd8a727561236537da1985932abc67ac0483dd1b19d200ef5c9c0e7242718fc2caa2f0589bb861d7a8810b22bd00f0af3c455912c58
7
- data.tar.gz: 3fdb1374e3992125c8d729d53d632b103ce087c01be1a63fd41b88add29535c9cb7587fad61e886f50e9055a567fddc6318fd5f07ffa3f745c124663b1de04aa
6
+ metadata.gz: dba0ce005271299eeac08f5479512281e699ebd2b218aab5b2b77b28cc349efd24da5ff257f002e770ee87ab11d4b11c591b1a572713023003a26293e7eedc4f
7
+ data.tar.gz: 9b8f68df096da7bd1c3d58d3228995fbe426ea55318b93d4798fb309e254ecdf39ffa6818375573ac6374d3bbad14bdf6b60717c5053fae4dbb62a313b83483c
@@ -0,0 +1,59 @@
1
+ class Switch
2
+ VERSION = "0.0.2"
3
+
4
+ class << self
5
+ def in(input)
6
+ stdin = $stdin
7
+ $stdin = StringIO.new(input).tap {|io| io.rewind}
8
+ yield
9
+ $stdin.read
10
+ ensure
11
+ $stdin = stdin
12
+ end
13
+
14
+ def out(return_io=false)
15
+ pipe = StringIO.new
16
+ stdout = $stdout
17
+ $stdout = pipe
18
+ yield
19
+ return_io ? pipe : pipe.string
20
+ ensure
21
+ $stdout = stdout
22
+ end
23
+
24
+ def up(options)
25
+ defaults = {
26
+ stdout: false,
27
+ stdin: nil
28
+ }
29
+ options = defaults.merge options
30
+
31
+ stdin = $stdin
32
+ stdout = $stdout
33
+
34
+ $stdin = StringIO.new(options[:stdin]).tap {|io| io.rewind}
35
+ pipe = StringIO.new
36
+ $stdout = pipe
37
+
38
+ yield
39
+
40
+ return_values = {}
41
+ return_values[:out] = options[:stdout] ? pipe : pipe.string
42
+ return_values[:in] = $stdin.read
43
+ return_values
44
+ ensure
45
+ $stdin = stdin
46
+ $stdout = stdout
47
+ end
48
+ end
49
+
50
+ def in(input)
51
+ self.class.in(input)
52
+ end
53
+ def out(return_io=false)
54
+ self.class.out(return_io)
55
+ end
56
+ def up(options)
57
+ self.class.up(options)
58
+ end
59
+ end
@@ -0,0 +1,91 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+ =begin
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Limits the available syntax to the non-monkey patched syntax that is
54
+ # recommended. For more details, see:
55
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
56
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
57
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
58
+ config.disable_monkey_patching!
59
+
60
+ # This setting enables warnings. It's recommended, but in some cases may
61
+ # be too noisy due to issues in dependencies.
62
+ config.warnings = true
63
+
64
+ # Many RSpec users commonly either run the entire suite or an individual
65
+ # file, and it's useful to allow more verbose output when running an
66
+ # individual spec file.
67
+ if config.files_to_run.one?
68
+ # Use the documentation formatter for detailed output,
69
+ # unless a formatter has already been configured
70
+ # (e.g. via a command-line flag).
71
+ config.default_formatter = 'doc'
72
+ end
73
+
74
+ # Print the 10 slowest examples and example groups at the
75
+ # end of the spec run, to help surface which specs are running
76
+ # particularly slow.
77
+ config.profile_examples = 10
78
+
79
+ # Run specs in random order to surface order dependencies. If you find an
80
+ # order dependency and want to debug it, you can fix the order by providing
81
+ # the seed, which is printed after each run.
82
+ # --seed 1234
83
+ config.order = :random
84
+
85
+ # Seed global randomization in this process using the `--seed` CLI option.
86
+ # Setting this allows you to use `--seed` to deterministically reproduce
87
+ # test failures related to randomization by passing the same `--seed` value
88
+ # as the one that triggered the failure.
89
+ Kernel.srand config.seed
90
+ =end
91
+ end
@@ -0,0 +1,51 @@
1
+ require_relative '../lib/switch'
2
+
3
+ describe Switch do
4
+ describe '.out' do
5
+ it 'returns the total value sent to that output by default' do
6
+ text = 'this is what you get'
7
+ expect(Switch.out { print text }).to eq text
8
+ end
9
+
10
+ it 'can switch the standard output with an argument set as a StringIO' do
11
+ text = 'this is what you get'
12
+ switched_output = Switch.out(true) { print text }
13
+ expect(switched_output.class).to eq StringIO
14
+ switched_output.rewind
15
+ expect(switched_output.read).to eq text
16
+ end
17
+ end
18
+
19
+ describe '.in' do
20
+ it 'switches out standard input with an argument' do
21
+ switched_in = 'this is what you get'
22
+ closure = nil
23
+ Switch.in(switched_in) {closure = gets}
24
+ expect(closure).to eq switched_in
25
+ end
26
+
27
+ it 'returns the rest of the original input object as a string after whatever was read from it' do
28
+ after_switched_in = "this is what you don't"
29
+ switched_in = "this is what you get\n#{after_switched_in}"
30
+ closure = nil
31
+ expect(Switch.in(switched_in) {closure = gets}).to eq after_switched_in
32
+ end
33
+ end
34
+
35
+ describe '.up' do
36
+ it 'combines both .in and .out' do
37
+ text = 'this is what you get'
38
+ after_switched_in = "this is what you don't"
39
+ switched_in = "this is what you get\n#{after_switched_in}"
40
+ closure = nil
41
+ value = Switch.up(stdin: switched_in) do
42
+ print text
43
+ closure = gets
44
+ end
45
+
46
+ expect(value[:out]).to eq text
47
+ expect(closure.chomp).to eq text
48
+ expect(value[:in]).to eq after_switched_in
49
+ end
50
+ end
51
+ end
@@ -1,11 +1,11 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'switchout'
4
+ require 'switch'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "switchout"
8
- spec.version = Switchout::VERSION
8
+ spec.version = Switch::VERSION
9
9
  spec.authors = ["Philip Hughes"]
10
10
  spec.email = ["philip@h4w5.com"]
11
11
  spec.summary = %q{Switch standard in or standard out for a moent in order to capture it.}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switchout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Hughes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-19 00:00:00.000000000 Z
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -50,7 +50,9 @@ files:
50
50
  - LICENSE.txt
51
51
  - README.md
52
52
  - Rakefile
53
- - lib/switchout.rb
53
+ - lib/switch.rb
54
+ - spec/spec_helper.rb
55
+ - spec/switchout_spec.rb
54
56
  - switchout.gemspec
55
57
  homepage: ''
56
58
  licenses:
@@ -1,5 +0,0 @@
1
- class Switchout
2
- VERSION = "0.0.1"
3
-
4
- # ...
5
- end