wrapio 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 45094b97507f0d3de37b6722e7c2bb71954b9afb
4
+ data.tar.gz: 2557f7625b9331e6b7f8086fbed025099c8eaf02
5
+ SHA512:
6
+ metadata.gz: 758593985bcd6ddb259423f368e5e0a59fe1005d502db4331b63b8444cfa15240b4a2871f73f54f6479df0df30710f156c7634833dc37ddea6531f5320e01225
7
+ data.tar.gz: eaf37351a2d1d954bd6b81c1d32e5d8060c3d8c917aadac0c541b3a4afd6a5c068fa880eca5b3b835a5444a0a3cfc9e84adf7aca1184849f238f997ad72c3837
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ *todo*
12
+ *TODO*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wrapio.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Chris Scavello
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # WrapIO
2
+
3
+ Allows the faking of input to STDIN and capturing of output from STDOUT with semantic method names. Great for testing!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'wrapio'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install wrapio
20
+
21
+ ## Usage
22
+
23
+ ### WrapIO
24
+ #### Captures output from STDOUT
25
+
26
+ ```ruby
27
+ # wrapio_test.rb
28
+
29
+ output = WrapIO.of do
30
+ puts "Hello World!"
31
+ end
32
+
33
+ puts output
34
+ ```
35
+
36
+ ```
37
+ $ ruby wrapio_test.rb
38
+
39
+ "Hello World!"
40
+ ```
41
+
42
+ #### Enable debug to see the faked STDIN
43
+
44
+ ```ruby
45
+ # wrapio_test.rb
46
+
47
+ WrapIO.debug = true
48
+ output = WrapIO.of('input') do
49
+ gets
50
+ puts 'output'
51
+ end
52
+ ```
53
+
54
+ ```
55
+ $ ruby wrapio_test.rb
56
+
57
+ -- WrapIO Capture#output --
58
+ -- WrapIO Fake#gets 1 --
59
+ input
60
+ ________________________
61
+ output
62
+
63
+ ____________________________
64
+ ```
65
+
66
+ ### Or use the internal module classes separately
67
+
68
+ #### WrapIO::Fake
69
+
70
+ ```ruby
71
+ # fake_test.rb
72
+
73
+ WrapIO::Fake.input('Hello!') do
74
+ input = gets
75
+ end
76
+ puts input
77
+ ```
78
+
79
+ ```
80
+ $ ruby fake_test.rb
81
+
82
+ "Hello!"
83
+ ```
84
+
85
+ #### WrapIO::Capture
86
+
87
+ ```ruby
88
+ # capture_test.rb
89
+
90
+ output = WrapIO::Capture.output do
91
+ print "Hi!"
92
+ end
93
+ puts output
94
+ ```
95
+
96
+ ```
97
+ $ ruby capture_test.rb
98
+
99
+ "Hi!"
100
+ ```
101
+
102
+ ## Development
103
+
104
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
105
+
106
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
107
+
108
+ ## Contributing
109
+
110
+ Bug reports and pull requests are welcome on GitHub at https://github.com/BideoWego/wrapio. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
111
+
112
+
113
+ ## License
114
+
115
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
116
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require_relative "../lib/wrapio.rb"
5
+
6
+ WrapIO.debug = true
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require "irb"
16
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/wrapio.rb ADDED
@@ -0,0 +1,108 @@
1
+ require_relative 'wrapio/version.rb'
2
+ require_relative 'wrapio/capture.rb'
3
+ require_relative 'wrapio/fake.rb'
4
+
5
+ ##
6
+ # A small module that allows performs both
7
+ # capturing output from STDOUT
8
+ # and faking input to STDIN.
9
+
10
+ module WrapIO
11
+
12
+ ##
13
+ # @!attribute debug
14
+ # @return [Boolean] +true+ if debugging is enabled
15
+
16
+ @@debug = false
17
+
18
+ ##
19
+ # Internal markers for debug logging
20
+
21
+ @@delimiters = {
22
+ :input => 'Fake#gets',
23
+ :output => 'Capture#output'
24
+ }
25
+
26
+ ##
27
+ # @!attribute default_input
28
+ # @return [String, Array] the default input if not specified in +WrapIO#of+
29
+
30
+ @@default_input = ''
31
+
32
+ ##
33
+ # @return [Boolean] +true+ if debugging is enabled
34
+
35
+ def self.debug
36
+ @@debug
37
+ end
38
+
39
+ ##
40
+ # Enable or disables debugging
41
+ # @param value [Boolean] +true+ if debugging is enabled
42
+
43
+ def self.debug=(value)
44
+ @@debug = value
45
+ end
46
+
47
+ ##
48
+ # @return [String, Array] the default input if not specified in +WrapIO#of+
49
+
50
+ def self.default_input
51
+ @@default_input
52
+ end
53
+
54
+ ##
55
+ # Set the default input if not specified in +WrapIO#of+
56
+ # @param value [String, Array] the default input if not specified in +WrapIO#of+
57
+
58
+ def self.default_input=(value)
59
+ @@default_input = value
60
+ end
61
+
62
+ ##
63
+ # The main action of the +WrapIO+ module
64
+ # As the semantic reading of the module name and function implies.
65
+ # It wraps the input and output of the given block, making the input
66
+ # passable as a parameter and the output accessible as a return value.
67
+ #
68
+ # @param input [String, Array<String>] the input or array of inputs
69
+ # @yield the block of code from which to wrap I/O
70
+ # @return [String] the captured output from STDOUT
71
+
72
+ def self.of(input=nil)
73
+ input = WrapIO.default_input.map{|i| i.to_s.dup} unless input
74
+ Capture.output do
75
+ Fake.input(input) do
76
+ yield
77
+ end
78
+ end
79
+ end
80
+
81
+ ##
82
+ # Used within module classes to output debug data
83
+ #
84
+ # @param data [String] the string to output
85
+ # @param type [Symbol] the type of data e.g. +:input+ or +:output+
86
+ # @param index [Integer] optionally provide an index of the data
87
+
88
+ def self.log(data, type, index=nil)
89
+ puts delimit(data, type, index.to_s)
90
+ end
91
+
92
+ private
93
+
94
+ ##
95
+ # Used internally to mark beginnings and endings of debug logging sections
96
+ #
97
+ # @param data [String] the string to delimit
98
+ # @param type [Symbol] the type of data
99
+ # @param index [Index] a the index of the data
100
+
101
+ def self.delimit(data, type, index)
102
+ delimitation = "-- WrapIO %T% %I% --"
103
+ b = delimitation.gsub(/%T%/, @@delimiters[type]).gsub(/%I%/, index)
104
+ c = data
105
+ e = '_' * b.length
106
+ [b, c, e].join("\n")
107
+ end
108
+ end
@@ -0,0 +1,25 @@
1
+ module WrapIO
2
+
3
+ ##
4
+ # A small class built to capture the output from STDOUT.
5
+
6
+ class Capture
7
+
8
+ ##
9
+ # Captures the output of +$stdout+.
10
+ # Temporarily swaps +$stdout+ with an instance of the +StringIO+ class.
11
+ #
12
+ # @yield the block of code from which to capture output
13
+
14
+ def self.output
15
+ begin
16
+ $stdout = captor = StringIO.new
17
+ yield
18
+ ensure
19
+ $stdout = STDOUT
20
+ end
21
+ WrapIO.log(captor.string, :output) if WrapIO.debug
22
+ captor.string
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,60 @@
1
+ module WrapIO
2
+
3
+ ##
4
+ # Enables faking of input to STDIN
5
+
6
+ class Fake
7
+
8
+ ##
9
+ # Used interally to store the given input data
10
+
11
+ @data = nil
12
+
13
+ ##
14
+ # @note Do not create instances of the +WrapIO::Fake+ class. Instead call +WrapIO::Fake#input+ to fake input to STDIN
15
+ # @param input [String, Array<String>] the input
16
+
17
+ def initialize(input)
18
+ @data = input
19
+ @length = input.length
20
+ end
21
+
22
+ ##
23
+ # Provides a proxy for +STDIN#gets+.
24
+ # When the instance of +$stdin+ is swapped in +WrapIO::Fake#input+
25
+ # this method with be called instead of the usual +STDIN#gets+.
26
+ # Allowing injection of faked input.
27
+ #
28
+ # This function destructively iterates through the +@data+
29
+ # array injecting the next value into +$stdin+. Once all indexes
30
+ # in the given data are sent to +$stdin+, all subsequent calls to +gets+
31
+ # will receive an empty string.
32
+
33
+ def gets
34
+ next_input = @data.shift
35
+ index = @length - @data.length
36
+ WrapIO.log(next_input, :input, index) if WrapIO.debug
37
+ next_input.to_s
38
+ end
39
+
40
+ ##
41
+ # This is the main action of the +WrapIO::Fake+ class.
42
+ # It passes the given input to +$stdin+.
43
+ # Pass a single string or an array of strings to it and
44
+ # each time +gets+ is called, a string from that array with be passed
45
+ # to +$stdin+ starting from index 0 and moving to the end.
46
+ #
47
+ # @param value [String, Array<String>] the value of the faked input(s)
48
+ # @yield the block of code to which you want to pass the given input(s)
49
+
50
+ def self.input(value=nil)
51
+ value = [value] unless value.is_a?(Array)
52
+ begin
53
+ $stdin = new(value)
54
+ yield
55
+ ensure
56
+ $stdin = STDIN
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,7 @@
1
+ module WrapIO
2
+
3
+ ##
4
+ # The curent version
5
+
6
+ VERSION = "0.0.1"
7
+ end
data/wrapio.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wrapio/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wrapio"
8
+ spec.version = WrapIO::VERSION
9
+ spec.authors = ["Chris Scavello"]
10
+ spec.email = ["bideowego@gmail.com"]
11
+
12
+ spec.summary = %q{Allows the faking of input to STDIN and capturing of output from STDOUT with semantic method names. Great for testing!}
13
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/BideoWego/wrapio"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec"
33
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wrapio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Scavello
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - bideowego@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/wrapio.rb
73
+ - lib/wrapio/capture.rb
74
+ - lib/wrapio/fake.rb
75
+ - lib/wrapio/version.rb
76
+ - wrapio.gemspec
77
+ homepage: https://github.com/BideoWego/wrapio
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ allowed_push_host: https://rubygems.org
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.6
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Allows the faking of input to STDIN and capturing of output from STDOUT with
102
+ semantic method names. Great for testing!
103
+ test_files: []
104
+ has_rdoc: