simple_console 0.0.1 → 0.0.2

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: 56350eea932e0e3c637b90edc4159c14df177b82
4
- data.tar.gz: 09e4971c3c20e63aa661b792afc265a6197ce5e5
3
+ metadata.gz: 5c732548976f9a9abed280b2feb3e4794852a850
4
+ data.tar.gz: 6ca5f7cb9937abc5f952f6ad1f3f756a5a076e63
5
5
  SHA512:
6
- metadata.gz: 12dcba42f32c6284b3fab823595bdf5bae940be2ddcb9b8810fa2e0ca33ff5930da0451027e5e60b0db40b6d694f37698ff61ecfe5a44c8f3504c0200e175d8a
7
- data.tar.gz: d4717557c086c2d01bec5338e9ecf1a8fa99cb9bcf16495b5bc63bd3eb7df2ff406b20bda146265d7568996ceea32db2fac9036af98e1a067905fffa0699dca5
6
+ metadata.gz: 9ff0209a9c1218d621c87fa4be33972ba468a4a948a0c566cfd06e00fd36cea6c1a368ee535c41da5279a9c505c8d9f2704ea9f384f1fe28c53dcaf83e95dbeb
7
+ data.tar.gz: c32129efd3f8e6a9429575e0f32cd96d4f0ef60a4c1728865a6ae8fe107a86cf015030efb3a3dca2a955f1c91fd8077624bb905643f47b3c84d64f329599ef1b
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require gem_helper
data/console.gemspec CHANGED
@@ -20,4 +20,6 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "pry"
23
25
  end
data/examples/demo.rb CHANGED
@@ -1,4 +1,5 @@
1
-
1
+ #! /usr/bin/env ruby
2
+ #
2
3
  require 'bundler/setup'
3
4
  require 'console'
4
5
 
@@ -12,6 +13,11 @@ class Demo
12
13
  define_cmd(:rand, "output rand number") do |max = 100|
13
14
  puts rand(max.to_i)
14
15
  end
16
+
17
+ define_cmd(:incr, "increment number") do |i = 1|
18
+ @incr_number ||= 0
19
+ puts @incr_number += i.to_i
20
+ end
15
21
  end
16
22
 
17
23
  Demo.new.start("demo > ")
@@ -0,0 +1,19 @@
1
+ module Console; module DefaultCommands
2
+ def self.included(cls)
3
+ cls.instance_eval do
4
+
5
+ define_cmd(:help, "show commands") do
6
+ self.class.commands.each do |key, val|
7
+ puts " #{key}: #{val[:desc]}"
8
+ end
9
+ end
10
+
11
+ define_cmd(:exit, "quit client") do
12
+ puts 'good bye!'
13
+ exit
14
+ end
15
+
16
+ end
17
+ end
18
+ end; end
19
+
@@ -1,3 +1,3 @@
1
1
  module Console
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/console.rb CHANGED
@@ -2,46 +2,51 @@ require "console/version"
2
2
 
3
3
  require 'readline'
4
4
 
5
+ require 'console/default_commands'
6
+
7
+
5
8
  module Console
6
9
  def self.included(cls)
7
10
  cls.extend(ClassMethods)
8
-
9
- ### default commands ###
10
- cls.instance_eval do
11
- define_cmd(:help, "show commands") do
12
- @commands.each do |key, val|
13
- puts " #{key}: #{val[:desc]}"
14
- end
15
- end
16
-
17
- define_cmd(:exit, "quit client") do
18
- puts 'good bye!'
19
- exit
20
- end
21
- end
11
+ cls.include(Console::DefaultCommands)
22
12
  end
23
13
 
24
14
 
25
15
  module ClassMethods
26
16
  def define_cmd(name, desc, &block)
27
- @commands ||= {}
28
- @commands[name.to_s] = {desc: desc, block: block}
17
+ commands[name.to_s] = {desc: desc, block: block}
18
+
19
+ define_method(command_method_name(name), &block)
29
20
  end
30
21
 
31
- def run_cmd(cmd_name, args)
32
- if @commands[cmd_name]
33
- @commands[cmd_name][:block].call(*args)
22
+ def run_cmd(instance, cmd_name, args = [])
23
+ cmd_name = cmd_name.to_s
24
+
25
+ if commands[cmd_name]
26
+ instance.send(command_method_name(cmd_name), *args)
34
27
  else
35
28
  puts "Invalid command '#{cmd_name}'"
36
29
  end
37
30
  rescue => e
38
31
  puts e.message
39
32
  end
33
+
34
+ def commands
35
+ @commands ||= {}
36
+ end
37
+
38
+
39
+ private
40
+
41
+ def command_method_name(cmd_name)
42
+ "_cmd_#{cmd_name}"
43
+ end
44
+
40
45
  end
41
46
 
42
47
 
43
- def start(prompt = '> ', hello = nil)
44
- puts (hello || "use 'help' command show all commands")
48
+ def start(prompt = '> ', options = {})
49
+ puts (options[:hello] || options['hello'] || "use 'help' command show all commands")
45
50
 
46
51
  loop do
47
52
  cmd_name, *cmd_args = Readline.readline(prompt, true).split
@@ -50,9 +55,9 @@ module Console
50
55
  puts
51
56
  next
52
57
  else
53
- self.class.run_cmd(cmd_name, cmd_args)
58
+ self.class.run_cmd(self, cmd_name, cmd_args)
54
59
  end
55
60
  end
56
61
  end
57
-
58
62
  end
63
+
@@ -0,0 +1,11 @@
1
+
2
+ require 'bundler/setup'
3
+ require 'pry'
4
+
5
+ require 'spec_helper'
6
+ require 'console'
7
+
8
+
9
+ RSpec.configure do |config|
10
+ end
11
+
@@ -0,0 +1,91 @@
1
+ RSpec.describe Console do
2
+ let(:cls) { Class.new { include Console } }
3
+
4
+
5
+ describe 'include console' do
6
+ it 'should create start method' do
7
+ expect(cls.new).to be_respond_to(:start)
8
+ end
9
+ end
10
+
11
+
12
+ describe '#start' do
13
+ it 'should run cmd' do
14
+ a = cls.new
15
+ allow(Readline).to receive(:readline).and_return('help')
16
+ # 'and_raise': stop loop
17
+ expect(cls).to receive(:run_cmd).with(a, 'help', []).and_raise
18
+
19
+ thread = Thread.new { a.start }
20
+ sleep 0.1
21
+ thread.kill
22
+ end
23
+
24
+ it 'should print hello' do
25
+ expect {
26
+ thread = Thread.new { cls.new.start('>', hello: 'hehe') }
27
+ sleep 0.1
28
+ thread.kill
29
+ }.to output("hehe\n").to_stdout
30
+ end
31
+
32
+ it 'empty cmd, should not exec' do
33
+ allow(Readline).to receive(:readline).and_return(nil, '', ' ')
34
+ expect(cls).to_not receive(:run_cmd)
35
+
36
+ thread = Thread.new { cls.new.start(nil, hello: '') }
37
+ sleep 0.1
38
+ thread.kill
39
+ end
40
+
41
+ it 'should call readline with prompt' do
42
+ expect(Readline).to receive(:readline).with(' ===> ', true)
43
+ thread = Thread.new { cls.new.start(' ===> ') }
44
+ sleep 0.1
45
+ thread.kill
46
+ end
47
+ end
48
+
49
+
50
+ describe '.define_cmd' do
51
+ it 'should create instance method' do
52
+ expect {
53
+ cls.define_cmd(:add, "desc") { |a, b| puts a + b }
54
+ }.to change { cls.instance_methods.include?(:_cmd_add) }.to(true)
55
+ end
56
+ end
57
+
58
+
59
+ describe '.run_cmd' do
60
+ it 'should call command' do
61
+ cls.define_cmd(:incr, 'incr') { @a ||=0; @a += 1 }
62
+ a, b = cls.new, cls.new
63
+ expect(cls.run_cmd(a, :incr)).to eq(1)
64
+ expect(cls.run_cmd(a, :incr)).to eq(2)
65
+ expect(cls.run_cmd(b, :incr)).to eq(1)
66
+ end
67
+
68
+ it 'should exec proc via console instance' do
69
+ a = cls.new
70
+ cls.define_cmd(:self, 'self') { self }
71
+ expect(cls.run_cmd(a, :self)).to eq(a)
72
+ end
73
+
74
+ it 'invalid command, should print error' do
75
+ a = cls.new
76
+ expect {
77
+ expect(cls.run_cmd(a, :aaa)).to eq(nil)
78
+ }.to output("Invalid command 'aaa'\n").to_stdout
79
+ end
80
+
81
+ it 'command raise error, should print error' do
82
+ a = cls.new
83
+ cls.define_cmd(:error, 'error') { raise 'raise error' }
84
+
85
+ expect {
86
+ expect(cls.run_cmd(a, :error)).to eq(nil)
87
+ }.to output("raise error\n").to_stdout
88
+ end
89
+ end
90
+ end
91
+
@@ -0,0 +1,87 @@
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
+ # These two settings work together to allow you to limit a spec run
44
+ # to individual examples or groups you care about by tagging them with
45
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
46
+ # get run.
47
+ config.filter_run :focus
48
+ config.run_all_when_everything_filtered = true
49
+
50
+ # Limits the available syntax to the non-monkey patched syntax that is
51
+ # recommended. For more details, see:
52
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
53
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
54
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
55
+ config.disable_monkey_patching!
56
+
57
+ # This setting enables warnings. It's recommended, but in some cases may
58
+ # be too noisy due to issues in dependencies.
59
+ config.warnings = true
60
+
61
+ # Many RSpec users commonly either run the entire suite or an individual
62
+ # file, and it's useful to allow more verbose output when running an
63
+ # individual spec file.
64
+ if config.files_to_run.one?
65
+ # Use the documentation formatter for detailed output,
66
+ # unless a formatter has already been configured
67
+ # (e.g. via a command-line flag).
68
+ config.default_formatter = 'doc'
69
+ end
70
+
71
+ # Print the 10 slowest examples and example groups at the
72
+ # end of the spec run, to help surface which specs are running
73
+ # particularly slow.
74
+ config.profile_examples = 10
75
+
76
+ # Run specs in random order to surface order dependencies. If you find an
77
+ # order dependency and want to debug it, you can fix the order by providing
78
+ # the seed, which is printed after each run.
79
+ # --seed 1234
80
+ config.order = :random
81
+
82
+ # Seed global randomization in this process using the `--seed` CLI option.
83
+ # Setting this allows you to use `--seed` to deterministically reproduce
84
+ # test failures related to randomization by passing the same `--seed` value
85
+ # as the one that triggered the failure.
86
+ Kernel.srand config.seed
87
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_console
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
  - jiangzhi.xie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-24 00:00:00.000000000 Z
11
+ date: 2015-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  description: create belong to your console
42
70
  email:
43
71
  - xiejiangzhi@gmail.com
@@ -46,6 +74,7 @@ extensions: []
46
74
  extra_rdoc_files: []
47
75
  files:
48
76
  - ".gitignore"
77
+ - ".rspec"
49
78
  - Gemfile
50
79
  - LICENSE.txt
51
80
  - README.md
@@ -53,7 +82,11 @@ files:
53
82
  - console.gemspec
54
83
  - examples/demo.rb
55
84
  - lib/console.rb
85
+ - lib/console/default_commands.rb
56
86
  - lib/console/version.rb
87
+ - spec/gem_helper.rb
88
+ - spec/lib/console_spec.rb
89
+ - spec/spec_helper.rb
57
90
  homepage: ''
58
91
  licenses:
59
92
  - MIT
@@ -78,4 +111,7 @@ rubygems_version: 2.2.2
78
111
  signing_key:
79
112
  specification_version: 4
80
113
  summary: Simple console
81
- test_files: []
114
+ test_files:
115
+ - spec/gem_helper.rb
116
+ - spec/lib/console_spec.rb
117
+ - spec/spec_helper.rb