ru2 2.3.1 → 2.4.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2bf8b7e55213af2a3730866b3d572d3268eab750
4
- data.tar.gz: c858b907bfceb9f97a705eee4c572a764bd7ee55
3
+ metadata.gz: 753e06244839e9059fbbbc0183fd0484acd1a0f4
4
+ data.tar.gz: 3c5e4bfb9fe1812c37abdcb110f4995c180f8041
5
5
  SHA512:
6
- metadata.gz: dd8136cbc35266aeee002efb7728e9ff4b62a00e987c08550540f1677f3500c1b3ae10aa27ce6d5128836cc1b9bc1fbadf7d2f13c894f7e8bdf24c3f39f6d595
7
- data.tar.gz: 7776259585c4a6a54b484711649d92327dd9761dcfd663a2930a44924a11583cb58344ee7640071f81bbbdb9d14555e768229dd1e4489089d12c6c8b02bd9da7
6
+ metadata.gz: 6beaeb2bab3765797140accb7543354271e864265a99ba8a40acd40b3deb6810be4888d35cf7a764507bf073b2c05e8ef60f4d6e49c2e95a00e0cc95442e93a4
7
+ data.tar.gz: 5639a3fcacb9bbf362375f376d041567ba78d6be2d7b0fbe0bf14d30e3d793107083461a6ec571bf901b7aa55299e39aa507233664ca7e2b6d76b5d8ccd7a8dd
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  .DS_Store
2
2
  Gemfile.lock
3
+ gemfiles/*.gemfile.lock
3
4
  *.gem
4
5
  log/*.log
5
6
  .bundle/
data/.travis.yml CHANGED
@@ -1,7 +1,12 @@
1
+ dist: trusty
1
2
  language: ruby
2
- script: bundle exec rspec
3
+ install: bundle install; bundle exec appraisal
4
+ script: bundle exec appraisal rspec
5
+ sudo: required
3
6
  rvm:
4
- - 1.9.3
5
- - 2.0.0
6
- - 2.1
7
- - 2.2
7
+ - '2.0'
8
+ - '2.1'
9
+ - '2.2'
10
+ - '2.3'
11
+ - '2.4'
12
+ #- 'rubinius-3.81'
data/Appraisals CHANGED
@@ -6,6 +6,8 @@ appraise "activesupport-4" do
6
6
  gem "activesupport", "~>4.0"
7
7
  end
8
8
 
9
- appraise "activesupport-5" do
10
- gem "activesupport", "~>5.0"
9
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.2.2')
10
+ appraise "activesupport-5" do
11
+ gem "activesupport", "~>5.0"
12
+ end
11
13
  end
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gemspec
3
+ gem 'appraisal', '~> 1.0'
4
+ gem 'rspec', '~> 3.1'
data/README.md CHANGED
@@ -4,7 +4,7 @@ Ruby in your shell!
4
4
 
5
5
  <img src="https://raw.github.com/tombenner/ru/master/doc/logo.png" />
6
6
 
7
- [<img src="https://secure.travis-ci.org/tombenner/ru.png" />](http://travis-ci.org/tombenner/ru)
7
+ [<img src="https://secure.travis-ci.org/AlexanderPavlenko/ru.png" />](http://travis-ci.org/AlexanderPavlenko/ru)
8
8
 
9
9
  Overview
10
10
  --------
data/doc/help.erb CHANGED
@@ -1,4 +1,5 @@
1
- Ru (version <%= version %>)
1
+ Ru v<%= version %>
2
+
2
3
  Ruby in your shell!
3
4
 
4
5
  Ru brings Ruby's expressiveness, cleanliness, and readability to the command line. It lets you avoid looking up pesky options in man pages and Googling how to write a transformation in bash that would take you approximately 1s to write in Ruby.
@@ -21,13 +22,12 @@ $ ru 'map(:to_i).sum' myfile
21
22
  $ ru 'map(:to_i).sum' myfile myfile
22
23
  10
23
24
 
24
- You can also run Ruby code without any input by prepending a `! `:
25
- $ ru '! 2 + 3'
25
+ You can also run Ruby code without any input by prepending a `=`:
26
+ $ ru '= 2 + 3'
26
27
  5
27
28
 
28
29
  The code argument is run as if it has `$stdin.each_line.map(&:chomp).` prepended to it. The result is converted to a string and printed. So, if you run `ru 'map(:to_i).sum'`, you can think of it as `puts $stdin.each_line.map(&:chomp).map(:to_i).sum`.
29
30
 
30
31
  In addition to the methods provided by Ruby Core and Active Support, Ru provides other methods for performing transformations, like `each_line`, `files`, and `grep`.
31
32
 
32
- To read more, see the README:
33
- https://github.com/tombenner/ru
33
+ To read more, see the README: https://github.com/AlexanderPavlenko/ru
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
+ gem "appraisal", "~> 1.0"
6
+ gem "rspec", "~> 3.1"
5
7
  gem "activesupport", "~>3.0"
6
-
7
- gemspec :path => "../"
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
+ gem "appraisal", "~> 1.0"
6
+ gem "rspec", "~> 3.1"
5
7
  gem "activesupport", "~>4.0"
6
-
7
- gemspec :path => "../"
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
+ gem "appraisal", "~> 1.0"
6
+ gem "rspec", "~> 3.1"
5
7
  gem "activesupport", "~>5.0"
6
-
7
- gemspec :path => "../"
@@ -3,8 +3,15 @@ require 'ostruct'
3
3
 
4
4
  module Ru
5
5
  class OptionPrinter
6
+
7
+ attr_accessor :options_parser
8
+
9
+ def initialize(options_parser: nil)
10
+ @options_parser = options_parser
11
+ end
12
+
6
13
  def exists?(option_key)
7
- ! (options[option_key].nil? || options[option_key].empty?)
14
+ !options[option_key].to_s.empty?
8
15
  end
9
16
 
10
17
  def run(option_key, option_value=nil)
@@ -15,16 +22,24 @@ module Ru
15
22
 
16
23
  def options
17
24
  {
18
- help: :get_help,
25
+ h: :get_usage,
26
+ help: :get_help,
19
27
  version: :get_version
20
28
  }
21
29
  end
22
30
 
31
+ def get_usage
32
+ options_parser.to_s
33
+ end
34
+
23
35
  def get_help
24
- namespace = OpenStruct.new(version: Ru::VERSION)
36
+ namespace = OpenStruct.new(version: Ru::VERSION)
25
37
  template_path = ::File.expand_path("../../../doc/help.erb", __FILE__)
26
- template = ::File.open(template_path).read
27
- ERB.new(template).result(namespace.instance_eval { binding })
38
+ template = ::File.open(template_path).read
39
+ result = get_usage
40
+ result.concat "\n"
41
+ result.concat ERB.new(template).result(namespace.instance_eval { binding })
42
+ result
28
43
  end
29
44
 
30
45
  def get_version
data/lib/ru/process.rb CHANGED
@@ -4,87 +4,143 @@ module Ru
4
4
  class Process
5
5
 
6
6
  def initialize
7
- @option_printer = OptionPrinter.new
7
+ @options = {}
8
+ @options_parser = OptionParser.new do |opts|
9
+ opts.banner = <<-EOF
10
+ Usage: ru [OPTION]... CODE FILE...
11
+ cat FILE | ru [OPTION]... CODE
12
+ echo CODE | ru [OPTION]...
13
+ echo CODE | ru [OPTION]... '' [FILE...]
14
+ EOF
15
+
16
+ opts.on("-h", "Print usage help") do
17
+ @options[:h] = true
18
+ end
19
+
20
+ opts.on("--help", "Print examples") do
21
+ @options[:help] = true
22
+ end
23
+
24
+ opts.on("-v", "--version", "Print version") do
25
+ @options[:version] = true
26
+ end
27
+
28
+ opts.on('-s', '--stream', 'Stream mode') do
29
+ @options[:stream] = true
30
+ end
31
+
32
+ opts.on('-b', '--binary', 'Binary mode') do
33
+ @options[:binary] = true
34
+ end
35
+
36
+ opts.on('--debug', 'Print debug messages') do
37
+ @options[:debug] = true
38
+ require 'logger'
39
+ $debug = Logger.new($stderr)
40
+ $debug.formatter = proc do |severity, _datetime, _progname, msg|
41
+ "#{severity}: #{msg}\n"
42
+ end
43
+ end
44
+ end
45
+ @option_printer = OptionPrinter.new(options_parser: @options_parser)
8
46
  end
9
47
 
10
48
  def run
11
49
  output = process_options
12
50
  return output if output
13
51
 
14
- args = ARGV.dup
15
- @code = args.shift
52
+ args = ARGV.dup
53
+ code = args.shift.to_s
54
+ parsed = prepare_code(code) { get_stdin(args, @options[:stream]) }
55
+ $debug.debug parsed.inspect if $debug
56
+ stdin = parsed[:stdin]
16
57
 
17
- if @code.empty?
18
- $stderr.puts @option_printer.run(:help)
19
- return
58
+ if parsed[:code].empty?
59
+ $debug.error "CODE is empty" if $debug
60
+ $stderr.puts @option_printer.run(:h)
61
+ exit false
20
62
  end
21
63
 
22
- @parsed = prepare_code(@code)
23
- @stdin = get_stdin(args, @options[:stream]) if @parsed[:get_stdin]
24
-
25
64
  context =
26
- if @stdin.nil?
65
+ if stdin.kind_of?(String) && stdin.empty?
66
+ $debug.info "STDIN is empty" if $debug
27
67
  Ru::Array.new([])
28
68
  else
29
69
  if @options[:stream]
30
70
  if @options[:binary]
31
- Ru::Stream.new(@stdin.each_byte.lazy)
71
+ $debug.info "binary stream mode" if $debug
72
+ Ru::Stream.new(stdin.each_byte.lazy)
32
73
  else
33
- Ru::Stream.new(@stdin.each_line.lazy.map { |line| line.chomp("\n".freeze) })
74
+ $debug.info "text stream mode" if $debug
75
+ Ru::Stream.new(stdin.each_line.lazy.map { |line| line.chomp("\n".freeze) })
34
76
  end
35
77
  else
36
78
  if @options[:binary]
37
- Ru::Array.new(@stdin.bytes)
79
+ $debug.info "binary mode" if $debug
80
+ Ru::Array.new(stdin.bytes)
38
81
  else
82
+ $debug.info "text mode" if $debug
39
83
  # Prevent 'invalid byte sequence in UTF-8'
40
- @stdin.encode!('UTF-8', 'UTF-8', invalid: :replace)
41
- Ru::Array.new(@stdin.split("\n"))
84
+ stdin.encode!('UTF-8', 'UTF-8', invalid: :replace)
85
+ Ru::Array.new(stdin.split("\n"))
42
86
  end
43
87
  end
44
88
  end
45
89
 
46
90
  begin
47
- output = context.instance_eval(@parsed[:code])
91
+ output = context.instance_eval(parsed[:code])
48
92
  rescue NoMethodError
93
+ $debug.info "loading active_support/all" if $debug
49
94
  require 'active_support/all'
50
95
 
51
- output = context.instance_eval(@parsed[:code])
96
+ output = context.instance_eval(parsed[:code])
52
97
  end
53
- output = @stdin if output == nil
98
+ output = stdin if output == nil
54
99
 
55
100
  prepare_output(output)
56
101
  end
57
102
 
58
103
  private
59
104
 
60
- def prepare_code(code)
105
+ def prepare_code(code, &block)
61
106
  return code unless code.kind_of?(String)
62
- if code.start_with?('[')
63
- { code: "to_self#{code}", get_stdin: true }
107
+ if code.empty?
108
+ $debug.info "reading CODE from STDIN" if $debug
109
+ stdin_code = $stdin.read
110
+ if stdin_code.empty?
111
+ { code: '', stdin: yield }
112
+ else
113
+ prepare_code(stdin_code, &block)
114
+ end
115
+ elsif code.start_with?('[')
116
+ { code: "to_self#{code}", stdin: yield }
64
117
  elsif code.start_with?('=')
65
- { code: code[1..-1], get_stdin: false }
118
+ { code: code[1..-1], stdin: '' }
66
119
  elsif code.start_with?('!')
67
120
  require 'active_support/deprecation'
68
121
  ActiveSupport::Deprecation.warn %('!1+2' syntax is going to be replaced with '=1+2')
69
- { code: code[1..-1], get_stdin: false }
122
+ { code: code[1..-1], stdin: '' }
70
123
  else
71
- { code: code, get_stdin: true }
124
+ { code: code, stdin: yield }
72
125
  end
73
126
  end
74
127
 
75
128
  def prepare_output(output)
76
129
  if output.respond_to?(:to_stdout)
130
+ $debug.info "serializing with output.to_stdout" if $debug
77
131
  output = output.to_stdout
78
132
  end
79
133
  if output.kind_of?(::Array)
134
+ $debug.info "serializing with output.join" if $debug
80
135
  output = output.join("\n")
81
136
  end
82
137
  output.to_s
83
138
  end
84
139
 
85
140
  def process_options
86
- @options = get_options
87
- @options.each do |option, value|
141
+ @options_parser.parse!
142
+ $debug.info "options: #{@options.inspect}" if $debug
143
+ @options.each do |option, _value|
88
144
  if @option_printer.exists?(option)
89
145
  return @option_printer.run(option)
90
146
  end
@@ -92,39 +148,21 @@ module Ru
92
148
  nil
93
149
  end
94
150
 
95
- def get_options
96
- options = {}
97
- OptionParser.new do |opts|
98
- opts.on("-h", "--help", "Print help") do
99
- options[:help] = true
100
- end
101
-
102
- opts.on("-v", "--version", "Print version") do
103
- options[:version] = true
104
- end
105
-
106
- opts.on('-s', '--stream', 'Stream mode') do
107
- options[:stream] = true
108
- end
109
-
110
- opts.on('-b', '--binary', 'Binary mode') do
111
- options[:binary] = true
112
- end
113
- end.parse!
114
- options
115
- end
116
-
117
151
  def get_stdin(paths, stream)
118
- if ! paths.empty?
152
+ if !paths.empty?
119
153
  if stream
154
+ $debug.info "opening file #{paths[0].inspect}" if $debug
120
155
  ::File.open(paths[0])
121
156
  else
157
+ $debug.info "reading files: #{paths.map(&:inspect).join(",\n")}" if $debug
122
158
  paths.map { |path| ::File.read(path) }.join("\n")
123
159
  end
124
160
  else
125
161
  if stream
162
+ $debug.info "opening STDIN" if $debug
126
163
  $stdin
127
164
  else
165
+ $debug.info "reading STDIN" if $debug
128
166
  $stdin.read
129
167
  end
130
168
  end
data/lib/ru/stream.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'set'
2
+
1
3
  class Enumerator::Lazy
2
4
  def [](number_or_range, length=1)
3
5
  if number_or_range.kind_of? Range
data/lib/ru/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ru
2
- VERSION = '2.3.1'
2
+ VERSION = '2.4.0'
3
3
  end
data/ru.gemspec CHANGED
@@ -1,12 +1,14 @@
1
1
  require File.expand_path('../lib/ru/version', __FILE__)
2
2
 
3
3
  Gem::Specification.new do |s|
4
- s.authors = ['Tom Benner', 'Alexander Pavlenko']
5
- s.email = ['tombenner@gmail.com', 'alerticus@gmail.com']
4
+ s.authors = ['Tom Benner', 'Alexander Pavlenko']
5
+ s.email = ['tombenner@gmail.com', 'alerticus@gmail.com']
6
6
  s.description = s.summary = %q{Ruby in your shell!}
7
- s.homepage = 'https://github.com/AlexanderPavlenko/ru'
7
+ s.homepage = 'https://github.com/AlexanderPavlenko/ru'
8
8
 
9
- s.files = `git ls-files`.split($\)
9
+ s.files = `git ls-files -z`.split("\x0").reject do |f|
10
+ f.match(%r{^(test|spec|features)/})
11
+ end
10
12
  s.name = 'ru2'
11
13
  s.executables = ['ru']
12
14
  s.require_paths = ['lib']
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ru2
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.1
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Benner
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-02-03 00:00:00.000000000 Z
12
+ date: 2017-06-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -73,11 +73,8 @@ files:
73
73
  - doc/help.erb
74
74
  - doc/logo.png
75
75
  - gemfiles/activesupport_3.gemfile
76
- - gemfiles/activesupport_3.gemfile.lock
77
76
  - gemfiles/activesupport_4.gemfile
78
- - gemfiles/activesupport_4.gemfile.lock
79
77
  - gemfiles/activesupport_5.gemfile
80
- - gemfiles/activesupport_5.gemfile.lock
81
78
  - lib/ru.rb
82
79
  - lib/ru/array.rb
83
80
  - lib/ru/file.rb
@@ -87,19 +84,6 @@ files:
87
84
  - lib/ru/stream.rb
88
85
  - lib/ru/version.rb
89
86
  - ru.gemspec
90
- - spec/examples/misc_examples_spec.rb
91
- - spec/examples/sed_examples_spec.rb
92
- - spec/examples/stream_examples_spec.rb
93
- - spec/fixtures/files/access.log
94
- - spec/fixtures/files/bar.txt
95
- - spec/fixtures/files/foo.txt
96
- - spec/lib/array_spec.rb
97
- - spec/lib/iterator_spec.rb
98
- - spec/lib/process_spec.rb
99
- - spec/lib/stream_spec.rb
100
- - spec/spec_helper.rb
101
- - spec/support/fixtures_helper.rb
102
- - spec/support/process_helper.rb
103
87
  homepage: https://github.com/AlexanderPavlenko/ru
104
88
  licenses:
105
89
  - MIT
@@ -120,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
104
  version: '0'
121
105
  requirements: []
122
106
  rubyforge_project:
123
- rubygems_version: 2.6.8
107
+ rubygems_version: 2.6.11
124
108
  signing_key:
125
109
  specification_version: 4
126
110
  summary: Ruby in your shell!
@@ -1,46 +0,0 @@
1
- PATH
2
- remote: ..
3
- specs:
4
- ru2 (2.3.1)
5
- activesupport (>= 3.2.0)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- activesupport (3.2.22.5)
11
- i18n (~> 0.6, >= 0.6.4)
12
- multi_json (~> 1.0)
13
- appraisal (1.0.3)
14
- bundler
15
- rake
16
- thor (>= 0.14.0)
17
- diff-lcs (1.3)
18
- i18n (0.8.0)
19
- multi_json (1.12.1)
20
- rake (12.0.0)
21
- rspec (3.5.0)
22
- rspec-core (~> 3.5.0)
23
- rspec-expectations (~> 3.5.0)
24
- rspec-mocks (~> 3.5.0)
25
- rspec-core (3.5.4)
26
- rspec-support (~> 3.5.0)
27
- rspec-expectations (3.5.0)
28
- diff-lcs (>= 1.2.0, < 2.0)
29
- rspec-support (~> 3.5.0)
30
- rspec-mocks (3.5.0)
31
- diff-lcs (>= 1.2.0, < 2.0)
32
- rspec-support (~> 3.5.0)
33
- rspec-support (3.5.0)
34
- thor (0.19.4)
35
-
36
- PLATFORMS
37
- ruby
38
-
39
- DEPENDENCIES
40
- activesupport (~> 3.0)
41
- appraisal (~> 1.0)
42
- rspec (~> 3.1)
43
- ru2!
44
-
45
- BUNDLED WITH
46
- 1.14.3
@@ -1,53 +0,0 @@
1
- PATH
2
- remote: ..
3
- specs:
4
- ru2 (2.3.1)
5
- activesupport (>= 3.2.0)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- activesupport (4.2.7.1)
11
- i18n (~> 0.7)
12
- json (~> 1.7, >= 1.7.7)
13
- minitest (~> 5.1)
14
- thread_safe (~> 0.3, >= 0.3.4)
15
- tzinfo (~> 1.1)
16
- appraisal (1.0.3)
17
- bundler
18
- rake
19
- thor (>= 0.14.0)
20
- diff-lcs (1.3)
21
- i18n (0.8.0)
22
- json (1.8.6)
23
- minitest (5.10.1)
24
- rake (12.0.0)
25
- rspec (3.5.0)
26
- rspec-core (~> 3.5.0)
27
- rspec-expectations (~> 3.5.0)
28
- rspec-mocks (~> 3.5.0)
29
- rspec-core (3.5.4)
30
- rspec-support (~> 3.5.0)
31
- rspec-expectations (3.5.0)
32
- diff-lcs (>= 1.2.0, < 2.0)
33
- rspec-support (~> 3.5.0)
34
- rspec-mocks (3.5.0)
35
- diff-lcs (>= 1.2.0, < 2.0)
36
- rspec-support (~> 3.5.0)
37
- rspec-support (3.5.0)
38
- thor (0.19.4)
39
- thread_safe (0.3.5)
40
- tzinfo (1.2.2)
41
- thread_safe (~> 0.1)
42
-
43
- PLATFORMS
44
- ruby
45
-
46
- DEPENDENCIES
47
- activesupport (~> 4.0)
48
- appraisal (~> 1.0)
49
- rspec (~> 3.1)
50
- ru2!
51
-
52
- BUNDLED WITH
53
- 1.14.3
@@ -1,52 +0,0 @@
1
- PATH
2
- remote: ..
3
- specs:
4
- ru2 (2.3.1)
5
- activesupport (>= 3.2.0)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- activesupport (5.0.1)
11
- concurrent-ruby (~> 1.0, >= 1.0.2)
12
- i18n (~> 0.7)
13
- minitest (~> 5.1)
14
- tzinfo (~> 1.1)
15
- appraisal (1.0.3)
16
- bundler
17
- rake
18
- thor (>= 0.14.0)
19
- concurrent-ruby (1.0.4)
20
- diff-lcs (1.3)
21
- i18n (0.8.0)
22
- minitest (5.10.1)
23
- rake (12.0.0)
24
- rspec (3.5.0)
25
- rspec-core (~> 3.5.0)
26
- rspec-expectations (~> 3.5.0)
27
- rspec-mocks (~> 3.5.0)
28
- rspec-core (3.5.4)
29
- rspec-support (~> 3.5.0)
30
- rspec-expectations (3.5.0)
31
- diff-lcs (>= 1.2.0, < 2.0)
32
- rspec-support (~> 3.5.0)
33
- rspec-mocks (3.5.0)
34
- diff-lcs (>= 1.2.0, < 2.0)
35
- rspec-support (~> 3.5.0)
36
- rspec-support (3.5.0)
37
- thor (0.19.4)
38
- thread_safe (0.3.5)
39
- tzinfo (1.2.2)
40
- thread_safe (~> 0.1)
41
-
42
- PLATFORMS
43
- ruby
44
-
45
- DEPENDENCIES
46
- activesupport (~> 5.0)
47
- appraisal (~> 1.0)
48
- rspec (~> 3.1)
49
- ru2!
50
-
51
- BUNDLED WITH
52
- 1.14.3
@@ -1,40 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'misc examples' do
4
- include FixturesHelper
5
- include ProcessHelper
6
-
7
- # http://stackoverflow.com/questions/450799/shell-command-to-sum-integers-one-per-line
8
- context "summing integers" do
9
- it "sums" do
10
- lines = (1..10).to_a.map(&:to_s)
11
- out = run('map(:to_i).sum', lines)
12
- expect(out).to eq('55')
13
- end
14
- end
15
-
16
- # http://stackoverflow.com/questions/6022384/bash-tool-to-get-nth-line-from-a-file
17
- context "printing the nth line" do
18
- it "prints" do
19
- lines = (1..10).to_a.map(&:to_s)
20
- out = run('[4]', lines)
21
- expect(out).to eq('5')
22
- end
23
- end
24
-
25
- # https://coderwall.com/p/ueazhw
26
- context "sorting an Apache access log by response time" do
27
- it "sorts" do
28
- file = fixture_path('files', 'access.log')
29
- out = run(['map { |line| [line[/(\d+)( ".+"){2}$/, 1].to_i, line] }.sort.reverse.map(:join, " ")', file])
30
- expect(out).to eq(<<-EOF.strip
31
- 584912 66.249.64.14 - - [18/Sep/2004:11:07:48 +1000] "GET /file.txt HTTP/1.0" 200 584912 "-" "Googlebot/2.1"
32
- 6433 66.249.64.14 - - [18/Sep/2004:11:07:48 +1000] "GET / HTTP/1.0" 200 6433 "-" "Googlebot/2.1"
33
- 6433 66.249.64.13 - - [18/Sep/2004:11:07:48 +1000] "GET / HTTP/1.0" 200 6433 "-" "Googlebot/2.1"
34
- 468 66.249.64.14 - - [18/Sep/2004:11:07:48 +1000] "GET /robots.txt HTTP/1.0" 200 468 "-" "Googlebot/2.1"
35
- 468 66.249.64.13 - - [18/Sep/2004:11:07:48 +1000] "GET /robots.txt HTTP/1.0" 200 468 "-" "Googlebot/2.1"
36
- EOF
37
- )
38
- end
39
- end
40
- end
@@ -1,95 +0,0 @@
1
- require 'spec_helper'
2
-
3
- # https://www.gnu.org/software/sed/manual/sed.html#Examples
4
- describe 'sed examples' do
5
- include FixturesHelper
6
- include ProcessHelper
7
-
8
- context "centering lines" do
9
- it "centers" do
10
- lines = %w{john paul george} + [' ringo ']
11
- out = run('each_line.strip.center(10)', lines)
12
- expect(out).to eq(" john \n paul \n george \n ringo ")
13
- end
14
- end
15
-
16
- context "increment a number" do
17
- it "increments" do
18
- lines = ('5'..'10').to_a
19
- out = run('(each_line.to_i+1)', lines)
20
- expect(out).to eq(('6'..'11').to_a.join("\n"))
21
- end
22
- end
23
-
24
- context "reverse characters of lines" do
25
- it "reverses" do
26
- lines = %w{john paul george ringo}
27
- out = run('each_line.reverse', lines)
28
- expect(out).to eq("nhoj\nluap\negroeg\nognir")
29
- end
30
- end
31
-
32
- context "numbering lines" do
33
- it "numbers" do
34
- lines = %w{john paul george ringo}
35
- out = run('map.with_index { |line, index| "#{(index+1).to_s.rjust(6)} #{line}" }', lines)
36
- expect(out).to eq(" 1 john\n 2 paul\n 3 george\n 4 ringo")
37
- end
38
- end
39
-
40
- context "counting lines" do
41
- it "counts" do
42
- lines = %w{john paul george ringo}
43
- out = run('length', lines)
44
- expect(out).to eq("4")
45
- end
46
- end
47
-
48
- context "printing the first lines" do
49
- it "prints" do
50
- lines = %w{john paul george ringo}
51
- out = run('[0,2]', lines)
52
- expect(out).to eq("john\npaul")
53
- end
54
- end
55
-
56
- context "printing the last lines" do
57
- it "prints" do
58
- lines = %w{john paul george ringo}
59
- out = run('[2..-1]', lines)
60
- expect(out).to eq("george\nringo")
61
- end
62
- end
63
-
64
- context "make duplicate lines unique" do
65
- it "dedupes" do
66
- lines = %w{john john paul george george george ringo}
67
- out = run('uniq', lines)
68
- expect(out).to eq("john\npaul\ngeorge\nringo")
69
- end
70
- end
71
-
72
- context "print duplicated lines of input" do
73
- it "prints" do
74
- lines = %w{john john paul george george george ringo}
75
- out = run('select { |line| self.count(line) > 1 }', lines)
76
- expect(out).to eq("john\njohn\ngeorge\ngeorge\ngeorge")
77
- end
78
- end
79
-
80
- context "remove all duplicated lines" do
81
- it "removes" do
82
- lines = %w{john john paul george george george ringo}
83
- out = run('select { |line| self.count(line) == 1 }', lines)
84
- expect(out).to eq("paul\nringo")
85
- end
86
- end
87
-
88
- context "squeezing blank lines" do
89
- it "squeezes" do
90
- lines = "john\n\npaul\ngeorge\n\n\nringo"
91
- out = run('to_s.squeeze("\n")', lines)
92
- expect(out).to eq("john\npaul\ngeorge\nringo")
93
- end
94
- end
95
- end
@@ -1,136 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'stream examples' do
4
- include FixturesHelper
5
- include ProcessHelper
6
-
7
- context 'misc stream examples' do
8
- # http://stackoverflow.com/questions/450799/shell-command-to-sum-integers-one-per-line
9
- context "summing integers" do
10
- it "sums" do
11
- lines = (1..10).to_a.map(&:to_s)
12
- out = run_stream('map(:to_i).sum', lines)
13
- expect(out).to eq('55')
14
- end
15
- end
16
-
17
- # http://stackoverflow.com/questions/6022384/bash-tool-to-get-nth-line-from-a-file
18
- context "printing the nth line" do
19
- it "prints" do
20
- lines = (1..10).to_a.map(&:to_s)
21
- out = run_stream('[4]', lines)
22
- expect(out).to eq('5')
23
- end
24
- end
25
-
26
- # https://coderwall.com/p/ueazhw
27
- context "sorting an Apache access log by response time" do
28
- it "sorts" do
29
- file = fixture_path('files', 'access.log')
30
- out = run_stream(['map { |line| [line[/(\d+)( ".+"){2}$/, 1].to_i, line] }.sort.reverse.map(:join, " ")', file])
31
- expect(out).to eq(<<-EOF.strip
32
- 584912 66.249.64.14 - - [18/Sep/2004:11:07:48 +1000] "GET /file.txt HTTP/1.0" 200 584912 "-" "Googlebot/2.1"
33
- 6433 66.249.64.14 - - [18/Sep/2004:11:07:48 +1000] "GET / HTTP/1.0" 200 6433 "-" "Googlebot/2.1"
34
- 6433 66.249.64.13 - - [18/Sep/2004:11:07:48 +1000] "GET / HTTP/1.0" 200 6433 "-" "Googlebot/2.1"
35
- 468 66.249.64.14 - - [18/Sep/2004:11:07:48 +1000] "GET /robots.txt HTTP/1.0" 200 468 "-" "Googlebot/2.1"
36
- 468 66.249.64.13 - - [18/Sep/2004:11:07:48 +1000] "GET /robots.txt HTTP/1.0" 200 468 "-" "Googlebot/2.1"
37
- EOF
38
- )
39
- end
40
- end
41
- end
42
-
43
- # https://www.gnu.org/software/sed/manual/sed.html#Examples
44
- context 'sed stream examples' do
45
- include FixturesHelper
46
- include ProcessHelper
47
-
48
- context "centering lines" do
49
- it "centers" do
50
- lines = %w{john paul george} + [' ringo ']
51
- out = run_stream('each_line.strip.center(10)', lines)
52
- expect(out).to eq(" john \n paul \n george \n ringo ")
53
- end
54
- end
55
-
56
- context "increment a number" do
57
- it "increments" do
58
- lines = ('5'..'10').to_a
59
- out = run_stream('(each_line.to_i+1)', lines)
60
- expect(out).to eq(('6'..'11').to_a.join("\n"))
61
- end
62
- end
63
-
64
- context "reverse characters of lines" do
65
- it "reverses" do
66
- lines = %w{john paul george ringo}
67
- out = run_stream('each_line.reverse', lines)
68
- expect(out).to eq("nhoj\nluap\negroeg\nognir")
69
- end
70
- end
71
-
72
- context "numbering lines" do
73
- it "numbers" do
74
- lines = %w{john paul george ringo}
75
- out = run_stream('each_with_index.map { |line, index| "#{(index+1).to_s.rjust(6)} #{line}" }', lines)
76
- expect(out).to eq(" 1 john\n 2 paul\n 3 george\n 4 ringo")
77
- end
78
- end
79
-
80
- context "counting lines" do
81
- it "counts" do
82
- lines = %w{john paul george ringo}
83
- out = run_stream('length', lines)
84
- expect(out).to eq("4")
85
- end
86
- end
87
-
88
- context "printing the first lines" do
89
- it "prints" do
90
- lines = %w{john paul george ringo}
91
- out = run_stream('[0,2]', lines)
92
- expect(out).to eq("john\npaul")
93
- end
94
- end
95
-
96
- context "printing the last lines" do
97
- it "prints" do
98
- lines = %w{john paul george ringo}
99
- out = run_stream('[2..-1]', lines)
100
- expect(out).to eq("george\nringo")
101
- end
102
- end
103
-
104
- context "make duplicate lines unique" do
105
- it "dedupes" do
106
- lines = %w{john john paul george george george ringo}
107
- out = run_stream('uniq', lines)
108
- expect(out).to eq("john\npaul\ngeorge\nringo")
109
- end
110
- end
111
-
112
- context "print duplicated lines of input" do
113
- it "prints" do
114
- lines = %w{john john paul george george george ringo}
115
- out = run_stream('select { |line| self.count(line) > 1 }', lines)
116
- expect(out).to eq("john\njohn\ngeorge\ngeorge\ngeorge")
117
- end
118
- end
119
-
120
- context "remove all duplicated lines" do
121
- it "removes" do
122
- lines = %w{john john paul george george george ringo}
123
- out = run_stream('select { |line| self.count(line) == 1 }', lines)
124
- expect(out).to eq("paul\nringo")
125
- end
126
- end
127
-
128
- context "squeezing blank lines" do
129
- it "squeezes" do
130
- lines = "john\n\npaul\ngeorge\n\n\nringo"
131
- out = run_stream('to_s.squeeze("\n")', lines)
132
- expect(out).to eq("john\npaul\ngeorge\nringo")
133
- end
134
- end
135
- end
136
- end
@@ -1,5 +0,0 @@
1
- 66.249.64.13 - - [18/Sep/2004:11:07:48 +1000] "GET /robots.txt HTTP/1.0" 200 468 "-" "Googlebot/2.1"
2
- 66.249.64.13 - - [18/Sep/2004:11:07:48 +1000] "GET / HTTP/1.0" 200 6433 "-" "Googlebot/2.1"
3
- 66.249.64.14 - - [18/Sep/2004:11:07:48 +1000] "GET /robots.txt HTTP/1.0" 200 468 "-" "Googlebot/2.1"
4
- 66.249.64.14 - - [18/Sep/2004:11:07:48 +1000] "GET / HTTP/1.0" 200 6433 "-" "Googlebot/2.1"
5
- 66.249.64.14 - - [18/Sep/2004:11:07:48 +1000] "GET /file.txt HTTP/1.0" 200 584912 "-" "Googlebot/2.1"
@@ -1 +0,0 @@
1
- bar
@@ -1,3 +0,0 @@
1
- foo
2
- foo
3
- foo
@@ -1,38 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Ru::Array do
4
- describe "Array method" do
5
- it "returns a Ru::Array" do
6
- array = described_class.new(%w{john paul george ringo})
7
- expect(array.sort).to be_a(Ru::Array)
8
- end
9
- end
10
-
11
- describe "#each_line" do
12
- it "calls to_s" do
13
- array = described_class.new((1..3).to_a)
14
- expect(array.each_line.to_s.to_a).to eq(described_class.new(('1'..'3')))
15
- end
16
-
17
- it "calls methods with arguments" do
18
- array = described_class.new((1..3).to_a)
19
- expect(array.each_line.modulo(2).to_a).to eq([1, 0, 1])
20
- end
21
- end
22
-
23
- describe "#map" do
24
- it "takes one argument" do
25
- array = described_class.new(%w{john paul george ringo})
26
- expect(array.map(:reverse)).to eq(%w{nhoj luap egroeg ognir})
27
- end
28
- it "takes two arguments" do
29
- array = described_class.new(%w{john paul george ringo})
30
- expect(array.map(:[], 0)).to eq(%w{j p g r})
31
- end
32
-
33
- it "returns a Ru::Array" do
34
- array = described_class.new(%w{john paul george ringo})
35
- expect(array.map(:[], 0)).to be_a(Ru::Array)
36
- end
37
- end
38
- end
@@ -1,72 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Ru::Iterator do
4
- context "Array" do
5
- describe "#to_a" do
6
- it "returns the array" do
7
- iterator = described_class.new(%w{john paul george ringo})
8
- expect(iterator.to_a).to eq(%w{john paul george ringo})
9
- end
10
- end
11
-
12
- describe "#to_stdout" do
13
- it "returns the string" do
14
- iterator = described_class.new(%w{john paul george ringo})
15
- expect(iterator.to_stdout).to eq("john\npaul\ngeorge\nringo")
16
- end
17
-
18
- context "with a method called on it" do
19
- it "returns the string" do
20
- iterator = described_class.new(%w{john paul george ringo})
21
- expect(iterator.to_s.to_stdout).to eq("john\npaul\ngeorge\nringo")
22
- end
23
- end
24
- end
25
- end
26
-
27
- context "Ru::Array" do
28
- describe "#to_a" do
29
- it "returns the array" do
30
- iterator = described_class.new(Ru::Array.new(%w{john paul george ringo}))
31
- expect(iterator.to_a).to eq(%w{john paul george ringo})
32
- end
33
- end
34
-
35
- describe "#to_stdout" do
36
- it "returns the string" do
37
- iterator = described_class.new(Ru::Array.new(%w{john paul george ringo}))
38
- expect(iterator.to_stdout).to eq("john\npaul\ngeorge\nringo")
39
- end
40
-
41
- context "with a method called on it" do
42
- it "returns the string" do
43
- iterator = described_class.new(Ru::Array.new(%w{john paul george ringo}))
44
- expect(iterator.to_s.to_stdout).to eq("john\npaul\ngeorge\nringo")
45
- end
46
- end
47
- end
48
- end
49
-
50
- context "Ru::Stream" do
51
- describe "#to_a" do
52
- it "returns the array" do
53
- iterator = described_class.new(Ru::Stream.new(%w{john paul george ringo}.each.lazy))
54
- expect(iterator.to_a).to eq(%w{john paul george ringo})
55
- end
56
- end
57
-
58
- describe "#to_stdout" do
59
- it "returns the string" do
60
- iterator = described_class.new(Ru::Stream.new(%w{john paul george ringo}.each.lazy))
61
- expect(iterator.to_stdout).to eq("john\npaul\ngeorge\nringo")
62
- end
63
-
64
- context "with a method called on it" do
65
- it "returns the string" do
66
- iterator = described_class.new(Ru::Stream.new(%w{john paul george ringo}.each.lazy))
67
- expect(iterator.to_s.to_stdout).to eq("john\npaul\ngeorge\nringo")
68
- end
69
- end
70
- end
71
- end
72
- end
@@ -1,126 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Ru::Process do
4
- include FixturesHelper
5
- include ProcessHelper
6
-
7
- describe "#run" do
8
- it "runs []" do
9
- lines = %w{john paul george ringo}
10
- out = run('[1,2]', lines)
11
- expect(out).to eq("paul\ngeorge")
12
- end
13
-
14
- it "runs files" do
15
- paths = [
16
- fixture_path('files', 'bar.txt'),
17
- fixture_path('files', 'foo.txt')
18
- ]
19
- out = run('files', paths)
20
- expect(out).to eq("bar.txt\nfoo.txt")
21
- end
22
-
23
- it "runs format" do
24
- paths = [
25
- fixture_path('files', 'bar.txt'),
26
- fixture_path('files', 'foo.txt')
27
- ]
28
- out = run("files.format('l')", paths)
29
- lines = out.split("\n")
30
- expect(lines.length).to eq(2)
31
- lines.each do |line|
32
- # 644 tom staff 11 2014-11-04 08:29 foo.txt
33
- expect(line).to match(/^\d{3}\t\w+\t\w+\t\d+\t\d{4}\-\d{2}-\d{2}\t\d{2}:\d{2}\t[\w\.]+$/)
34
- end
35
- end
36
-
37
- it "runs grep" do
38
- lines = %w{john paul george ringo}
39
- out = run("grep(/o[h|r]/)", lines)
40
- expect(out).to eq("john\ngeorge")
41
- end
42
-
43
- it "runs map with two arguments" do
44
- lines = %w{john paul george ringo}
45
- out = run('map(:[], 0)', lines)
46
- expect(out).to eq(%w{j p g r}.join("\n"))
47
- end
48
-
49
- it "runs sort" do
50
- lines = %w{john paul george ringo}
51
- out = run('sort', lines)
52
- expect(out).to eq(lines.sort.join("\n"))
53
- end
54
-
55
- it "takes files as arguments" do
56
- out = run(['to_s', fixture_path('files', 'foo.txt')])
57
- expect(out).to eq("foo\nfoo\nfoo")
58
- end
59
-
60
- it "runs code prepended by '='" do
61
- out = run('=2 + 3')
62
- expect(out).to eq('5')
63
- end
64
-
65
- context "no arguments" do
66
- it "prints help" do
67
- allow(STDERR).to receive(:puts) do |out|
68
- expect(out).to include('Ruby in your shell!')
69
- end
70
- allow_any_instance_of(Ru::Process).to receive(:exit).with(1)
71
- run('')
72
- end
73
- end
74
-
75
- context "an undefined method" do
76
- it "raises a NoMethodError" do
77
- lines = %w{john paul george ringo}
78
- expect { out = run('foo', lines) }.to raise_error(NoMethodError)
79
- end
80
- end
81
-
82
- describe "options" do
83
- context "-h" do
84
- it "shows help" do
85
- out = run('--help')
86
- expect(out).to include('Ruby in your shell!')
87
- end
88
- end
89
-
90
- context "--help" do
91
- it "shows help" do
92
- out = run('-h')
93
- expect(out).to include('Ruby in your shell!')
94
- end
95
- end
96
-
97
- context "help with a second argument" do
98
- it "shows help" do
99
- out = run(['--help', 'foo'])
100
- expect(out).to include('Ruby in your shell!')
101
- end
102
- end
103
-
104
- context "-v" do
105
- it "shows the version" do
106
- out = run('-v')
107
- expect(out).to eq(Ru::VERSION)
108
- end
109
- end
110
-
111
- context "--version" do
112
- it "shows the version" do
113
- out = run('--version')
114
- expect(out).to eq(Ru::VERSION)
115
- end
116
- end
117
-
118
- context "version with a second argument" do
119
- it "shows the version" do
120
- out = run(['--version', 'foo'])
121
- expect(out).to eq(Ru::VERSION)
122
- end
123
- end
124
- end
125
- end
126
- end
@@ -1,41 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'spec_helper'
4
-
5
- describe Ru::Stream do
6
- describe "Array method" do
7
- it "returns a Ru::Array" do
8
- array = described_class.new(%w{john paul george ringo}.each.lazy)
9
- expect(array.to_a.sort).to be_a(Ru::Array)
10
- end
11
- end
12
-
13
- describe "#each_line" do
14
- it "calls to_s" do
15
- array = described_class.new((1..3).each.lazy)
16
- expect(array.each_line.to_s).to be_a(described_class)
17
- expect(array.each_line.to_s.to_a).to eq(Ru::Array.new('1'..'3'))
18
- end
19
-
20
- it "calls methods with arguments" do
21
- array = described_class.new((1..3).each.lazy)
22
- expect(array.each_line.modulo(2).to_a).to eq([1, 0, 1])
23
- end
24
- end
25
-
26
- describe "#map" do
27
- it "takes one argument" do
28
- array = described_class.new(%w{john paul george ringo}.each.lazy)
29
- expect(array.map(:reverse).to_a).to eq(%w{nhoj luap egroeg ognir})
30
- end
31
- it "takes two arguments" do
32
- array = described_class.new(%w{john paul george ringo}.each.lazy)
33
- expect(array.map(:[], 0).to_a).to eq(%w{j p g r})
34
- end
35
-
36
- it "returns a Ru::Stream" do
37
- array = described_class.new(%w{john paul george ringo}.each.lazy)
38
- expect(array.map(:[], 0)).to be_a(Ru::Stream)
39
- end
40
- end
41
- end
data/spec/spec_helper.rb DELETED
@@ -1,20 +0,0 @@
1
- require 'ru'
2
-
3
- require 'support/fixtures_helper'
4
- require 'support/process_helper'
5
-
6
- RSpec.configure do |config|
7
- # ## Mock Framework
8
- #
9
- # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
10
- #
11
- # config.mock_with :mocha
12
- # config.mock_with :flexmock
13
- # config.mock_with :rr
14
-
15
- # Run specs in random order to surface order dependencies. If you find an
16
- # order dependency and want to debug it, you can fix the order by providing
17
- # the seed, which is printed after each run.
18
- # --seed 1234
19
- config.order = "random"
20
- end
@@ -1,8 +0,0 @@
1
- require 'pathname'
2
-
3
- module FixturesHelper
4
- def fixture_path(*args)
5
- directory = Pathname.new(File.dirname(File.absolute_path(__FILE__)))
6
- directory.join('..', 'fixtures', *args).to_s
7
- end
8
- end
@@ -1,30 +0,0 @@
1
- module ProcessHelper
2
- def run(args, stdin=nil)
3
- if args.kind_of?(String)
4
- args = [args]
5
- end
6
- stub_const('ARGV', args)
7
-
8
- if stdin.kind_of?(Array)
9
- stdin = stdin.join("\n")
10
- end
11
- if stdin
12
- stdin_double = double
13
- allow(stdin_double).to receive(:read).and_return(stdin)
14
- allow(stdin_double).to receive(:each_line).and_return(stdin.each_line)
15
- allow(stdin_double).to receive(:each_byte).and_return(stdin.each_byte)
16
- $stdin = stdin_double
17
- end
18
-
19
- process = Ru::Process.new
20
- process.run
21
- end
22
-
23
- def run_stream(args, stdin=nil)
24
- if args.kind_of?(String)
25
- args = [args]
26
- end
27
- args.unshift ['-s', '--stream'].sample
28
- run args, stdin
29
- end
30
- end