ru2 2.1.4

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.
@@ -0,0 +1,136 @@
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
@@ -0,0 +1,5 @@
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"
@@ -0,0 +1 @@
1
+ bar
@@ -0,0 +1,3 @@
1
+ foo
2
+ foo
3
+ foo
@@ -0,0 +1,38 @@
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
@@ -0,0 +1,72 @@
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
@@ -0,0 +1,126 @@
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
@@ -0,0 +1,41 @@
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
@@ -0,0 +1,20 @@
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
@@ -0,0 +1,6 @@
1
+ module FixturesHelper
2
+ def fixture_path(*args)
3
+ directory = Pathname.new(File.dirname(File.absolute_path(__FILE__)))
4
+ directory.join('..', 'fixtures', *args).to_s
5
+ end
6
+ end
@@ -0,0 +1,30 @@
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
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ru2
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Tom Benner
8
+ - Alexander Pavlenko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-12-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 3.2.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 3.2.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: appraisal
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.1'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.1'
56
+ description: Ruby in your shell!
57
+ email:
58
+ - tombenner@gmail.com
59
+ - alerticus@gmail.com
60
+ executables:
61
+ - ru
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".travis.yml"
67
+ - Appraisals
68
+ - Gemfile
69
+ - MIT-LICENSE
70
+ - README.md
71
+ - Rakefile
72
+ - bin/ru
73
+ - doc/help.erb
74
+ - doc/logo.png
75
+ - gemfiles/activesupport_3.gemfile
76
+ - gemfiles/activesupport_3.gemfile.lock
77
+ - gemfiles/activesupport_4.gemfile
78
+ - gemfiles/activesupport_4.gemfile.lock
79
+ - gemfiles/activesupport_5.gemfile
80
+ - gemfiles/activesupport_5.gemfile.lock
81
+ - lib/ru.rb
82
+ - lib/ru/array.rb
83
+ - lib/ru/file.rb
84
+ - lib/ru/iterator.rb
85
+ - lib/ru/option_printer.rb
86
+ - lib/ru/process.rb
87
+ - lib/ru/stream.rb
88
+ - lib/ru/version.rb
89
+ - 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
+ homepage: https://github.com/AlexanderPavlenko/ru
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.5.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Ruby in your shell!
127
+ test_files: []