sorcerer 0.3.9 → 0.3.10

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -11,7 +11,7 @@ Sorcerer is targetted mainly at small snippets of Ruby code,
11
11
  expressable in a single line. Longer examples may be re-sourced, but
12
12
  they will be rendered in a single line format.
13
13
 
14
- **Version: 0.3.7**
14
+ **Version: 0.3.10**
15
15
 
16
16
  ## Limitations
17
17
 
@@ -11,6 +11,6 @@ module Sorcerer
11
11
  end
12
12
 
13
13
  require 'sorcerer/resource'
14
+ require 'sorcerer/signature'
14
15
  require 'sorcerer/subexpression'
15
16
  require 'sorcerer/version'
16
-
@@ -116,7 +116,8 @@ module Sorcerer
116
116
  def params_are_empty?(sexp)
117
117
  params = sexp
118
118
  params = sexp[1] if sexp.first == :paren || sexp.first == :arg_paren
119
- params[1].nil? || params[1].empty?
119
+ sig = Signature.new(params)
120
+ sig.empty?
120
121
  end
121
122
 
122
123
  def opt_parens(sexp)
@@ -154,42 +155,42 @@ module Sorcerer
154
155
  false
155
156
  end
156
157
 
157
- def params(normal_args, default_args, rest_arg, unknown, keyw_args, opts_arg, block_arg)
158
+ def params(sig)
158
159
  first = true
159
- if normal_args
160
- normal_args.each do |sx|
160
+ if sig.normal_args
161
+ sig.normal_args.each do |sx|
161
162
  first = emit_separator(", ", first)
162
163
  resource(sx)
163
164
  end
164
165
  end
165
- if default_args
166
- default_args.each do |sx|
166
+ if sig.default_args
167
+ sig.default_args.each do |sx|
167
168
  first = emit_separator(", ", first)
168
169
  resource(sx[0])
169
170
  emit("=")
170
171
  resource(sx[1])
171
172
  end
172
173
  end
173
- if rest_arg
174
+ if sig.rest_arg
174
175
  first = emit_separator(", ", first)
175
- resource(rest_arg)
176
+ resource(sig.rest_arg)
176
177
  end
177
- if keyw_args
178
- keyw_args.each do |sx|
178
+ if sig.keyword_args
179
+ sig.keyword_args.each do |sx|
179
180
  first = emit_separator(", ", first)
180
181
  resource(sx[0])
181
182
  emit(" ")
182
183
  resource(sx[1])
183
184
  end
184
185
  end
185
- if opts_arg
186
+ if sig.opts_arg
186
187
  first = emit_separator(", ", first)
187
188
  emit("**")
188
- emit(opts_arg[1])
189
+ emit(sig.opts_arg[1])
189
190
  end
190
- if block_arg
191
+ if sig.block_arg
191
192
  first = emit_separator(", ", first)
192
- resource(block_arg)
193
+ resource(sig.block_arg)
193
194
  end
194
195
  end
195
196
 
@@ -707,11 +708,7 @@ module Sorcerer
707
708
  },
708
709
  :param_error => NYI,
709
710
  :params => lambda { |sexp|
710
- if sexp.size == 8
711
- params(sexp[1], sexp[2], sexp[3], sexp[4], sexp[5], sexp[6], sexp[7])
712
- else
713
- params(sexp[1], sexp[2], sexp[3], sexp[4],nil, nil, sexp[5])
714
- end
711
+ params(Signature.new(sexp))
715
712
  },
716
713
  :paren => lambda { |sexp|
717
714
  emit("(")
@@ -0,0 +1,38 @@
1
+ module Sorcerer
2
+ class Signature
3
+
4
+ attr_reader :normal_args, :default_args, :rest_arg, :keyword_args, :opts_arg, :block_arg
5
+
6
+ def initialize(sexp)
7
+ @normal_args = sexp[1]
8
+ @default_args = sexp[2]
9
+ @rest_arg = sexp[3]
10
+ if ruby2_style_param_list?(sexp)
11
+ @keyword_args = sexp[5]
12
+ @opts_arg = sexp[6]
13
+ @block_arg = sexp[7]
14
+ else
15
+ @block_arg = sexp[5]
16
+ end
17
+ end
18
+
19
+ def empty?
20
+ missing?(normal_args) &&
21
+ missing?(default_args) &&
22
+ missing?(rest_arg) &&
23
+ missing?(keyword_args) &&
24
+ missing?(opts_arg) &&
25
+ missing?(block_arg)
26
+ end
27
+
28
+ private
29
+
30
+ def missing?(args)
31
+ args.nil? || args.empty?
32
+ end
33
+
34
+ def ruby2_style_param_list?(sexp)
35
+ sexp.size == 8
36
+ end
37
+ end
38
+ end
@@ -2,7 +2,7 @@ module Sorcerer
2
2
  VERSION_NUMBERS = [
3
3
  VERSION_MAJOR = 0,
4
4
  VERSION_MINOR = 3,
5
- VERSION_BUILD = 9,
5
+ VERSION_BUILD = 10,
6
6
  ]
7
7
 
8
8
  VERSION = VERSION_NUMBERS.join('.')
@@ -1,44 +1,26 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- begin
4
- require 'redcloth'
5
- directory "html"
3
+ directory "html"
6
4
 
7
- desc "Display the README file"
8
- task :readme => "html/README.html" do
9
- sh "open html/README.html"
10
- end
11
-
12
- desc "format the README file"
13
- task "html/README.html" => ['html', 'README.textile', "readme:update"] do
14
- open("README.textile") do |source|
15
- open('html/README.html', 'w') do |out|
16
- out.write(RedCloth.new(source.read).to_html)
17
- end
18
- end
19
- end
5
+ desc "Display the README file"
6
+ task :readme => "README.md" do
7
+ sh "ghpreview README.md"
8
+ end
20
9
 
21
- namespace "readme" do
22
- desc "Update the version in the readme"
23
- task :update do
24
- open("README.textile") do |ins|
25
- open("new_readme.txt", "w") do |outs|
26
- while line = ins.gets
27
- if line =~ /^\*Version: .*\*$/
28
- line = "*Version: #{PKG_VERSION}*"
29
- end
30
- outs.puts line
10
+ namespace "readme" do
11
+ desc "Update the version in the readme"
12
+ file "README.md" => ["lib/sorcerer/version.rb"] do
13
+ open("README.md") do |ins|
14
+ open("new_readme.txt", "w") do |outs|
15
+ while line = ins.gets
16
+ if line =~ /^\*\*Version: .*\*\*$/
17
+ line = "**Version: #{PKG_VERSION}**"
31
18
  end
19
+ outs.puts line
32
20
  end
33
21
  end
34
- mv "README.textile", "README.bak"
35
- mv "new_readme.txt", "README.textile"
36
22
  end
37
- end
38
-
39
- rescue LoadError => ex
40
- task :readme do
41
- fail "Install RedCloth to generate the README"
23
+ mv "README.md", "README.bak"
24
+ mv "new_readme.txt", "README.md"
42
25
  end
43
26
  end
44
-
@@ -567,9 +567,6 @@ class ResourceTest < Test::Unit::TestCase
567
567
  end
568
568
 
569
569
  def test_can_source_def
570
- assert_resource_lines "def f; end"
571
- assert_resource_lines "def f; #x; end"
572
- assert_resource_lines "def f a; end"
573
570
  assert_resource_lines "def f(); end"
574
571
  assert_resource_lines "def f(a); end"
575
572
  assert_resource_lines "def f(a, b); end"
@@ -580,10 +577,29 @@ class ResourceTest < Test::Unit::TestCase
580
577
  assert_resource_lines "def f(a); #x; #y; end"
581
578
  end
582
579
 
580
+ def test_can_source_def_without_parens
581
+ assert_resource_lines "def f; end"
582
+ assert_resource_lines "def f; #x; end"
583
+ assert_resource_lines "def f a; end"
584
+ assert_resource_lines "def f b=1; end"
585
+ assert_resource_lines "def f *args; end"
586
+ assert_resource_lines "def f &block; end"
587
+ end
588
+
583
589
  if RUBY_VERSION >= "2.0.0"
584
590
  def test_can_source_ruby2_defs
585
591
  assert_resource_lines "def f(a, b=1, *args, c: 2, **opts, &block); end"
586
592
  assert_resource_lines "def f(a, aa, b=1, bb=2, *args, c: 3, cc: 4, **opts, &block); end"
593
+ assert_resource_lines "def f(c: 3); end"
594
+ assert_resource_lines "def f(**opts); end"
595
+ end
596
+ end
597
+
598
+ if RUBY_VERSION >= "2.0.0"
599
+ def test_can_source_ruby2_defs_without_parens
600
+ assert_resource_lines "def f a, b=1, *args, c: 2, **opts, &block; end"
601
+ assert_resource_lines "def f a, aa, b=1, bb=2, *args, c: 3, cc: 4, **opts, &block; end"
602
+ assert_resource_lines "def f **opts; end"
587
603
  end
588
604
  end
589
605
 
@@ -0,0 +1,102 @@
1
+ require 'test/unit'
2
+
3
+ require 'sorcerer'
4
+ require 'ripper'
5
+
6
+ class SignatureTest < Test::Unit::TestCase
7
+
8
+ def test_populate_fields_with_nil
9
+ sig = signature_for("def foo; end")
10
+
11
+ assert_nil sig.normal_args
12
+ assert_nil sig.default_args
13
+ assert_nil sig.rest_arg
14
+ assert_nil sig.keyword_args
15
+ assert_nil sig.opts_arg
16
+ assert_nil sig.block_arg
17
+ end
18
+
19
+ def test_populate_fields_with_real_args
20
+ sig = signature_for("def foo(a, b=1, *args, &block); end")
21
+
22
+ assert_equal [[:@ident, "a", [1, 8]]], sig.normal_args
23
+ assert_equal [[[:@ident, "b", [1, 11]], [:@int, "1", [1, 13]]]], sig.default_args
24
+ assert_equal [:rest_param, [:@ident, "args", [1, 17]]], sig.rest_arg
25
+ assert_nil sig.keyword_args
26
+ assert_nil sig.opts_arg
27
+ assert_equal [:blockarg, [:@ident, "block", [1, 24]]], sig.block_arg
28
+ end
29
+
30
+ if RUBY_VERSION >= "2.0"
31
+ def test_populate_fields_with_real_args_in_ruby2
32
+ sig = signature_for("def foo(a, b=1, *args, c: 2, **opts, &block); end")
33
+
34
+ assert_equal [[:@ident, "a", [1, 8]]], sig.normal_args
35
+ assert_equal [[[:@ident, "b", [1, 11]], [:@int, "1", [1, 13]]]], sig.default_args
36
+ assert_equal [:rest_param, [:@ident, "args", [1, 17]]], sig.rest_arg
37
+ assert_equal [[[:@label, "c:", [1, 23]], [:@int, "2", [1, 26]]]], sig.keyword_args
38
+ assert_equal [:@ident, "opts", [1, 31]], sig.opts_arg
39
+ assert_equal [:blockarg, [:@ident, "block", [1, 38]]], sig.block_arg
40
+ end
41
+ end
42
+
43
+ def test_empty_when_no_args
44
+ sig = signature_for("def foo(); end")
45
+ assert sig.empty?, "should be empty"
46
+ end
47
+
48
+ def test_not_empty_when_normal_args
49
+ sig = signature_for("def foo(a); end")
50
+ assert ! sig.empty?, "should not be empty"
51
+ end
52
+
53
+ def test_not_empty_when_default_args
54
+ sig = signature_for("def foo(b=1); end")
55
+ assert ! sig.empty?, "should not be empty"
56
+ end
57
+
58
+ def test_not_empty_when_rest_arg
59
+ sig = signature_for("def foo(*rest); end")
60
+ assert ! sig.empty?, "should not be empty"
61
+ end
62
+
63
+ if RUBY_VERSION >= "2.0"
64
+ def test_not_empty_when_keyword_args
65
+ sig = signature_for("def foo(c: 1); end")
66
+ assert ! sig.empty?, "should not be empty"
67
+ end
68
+
69
+ def test_not_empty_when_options_args
70
+ sig = signature_for("def foo(**opts); end")
71
+ assert ! sig.empty?, "should not be empty"
72
+ end
73
+ end
74
+
75
+ def test_not_empty_when_block_arg
76
+ sig = signature_for("def foo(&block); end")
77
+ assert ! sig.empty?, "should not be empty"
78
+ end
79
+
80
+ private
81
+
82
+ def signature_for(string)
83
+ sexp = Ripper::SexpBuilder.new(string).parse
84
+ params = find_params(sexp)
85
+ Sorcerer::Signature.new(params)
86
+ end
87
+
88
+ def find_params(sexp)
89
+ if ! sexp.is_a?(Array)
90
+ nil
91
+ elsif sexp.first == :params
92
+ sexp
93
+ else
94
+ sexp.each { |subexp|
95
+ found = find_params(subexp)
96
+ return found if found
97
+ }
98
+ nil
99
+ end
100
+ end
101
+
102
+ end
metadata CHANGED
@@ -1,80 +1,64 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sorcerer
3
- version: !ruby/object:Gem::Version
4
- hash: 1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.10
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 3
9
- - 9
10
- version: 0.3.9
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Jim Weirich
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2013-02-04 00:00:00 Z
12
+ date: 2013-02-06 00:00:00.000000000 Z
19
13
  dependencies: []
20
-
21
- description: Generate the original Ruby source from a Ripper-style abstract syntax tree.
14
+ description: Generate the original Ruby source from a Ripper-style abstract syntax
15
+ tree.
22
16
  email: jim.weirich@gmail.com
23
17
  executables: []
24
-
25
18
  extensions: []
26
-
27
19
  extra_rdoc_files: []
28
-
29
- files:
20
+ files:
30
21
  - README.md
31
22
  - Rakefile
32
23
  - doc/jamis.rb
33
24
  - rakelib/git.rake
34
25
  - rakelib/readme.rake
35
26
  - rakelib/testing.rake
27
+ - lib/sorcerer.rb
36
28
  - lib/sorcerer/resource.rb
29
+ - lib/sorcerer/signature.rb
37
30
  - lib/sorcerer/subexpression.rb
38
31
  - lib/sorcerer/version.rb
39
- - lib/sorcerer.rb
40
32
  - test/assert_resource_helper.rb
41
33
  - test/sorcerer/resource_test.rb
34
+ - test/sorcerer/signature_test.rb
42
35
  - test/sorcerer/subexpression_test.rb
43
36
  homepage: http://github.com/jimweirich/sorcerer
44
37
  licenses: []
45
-
46
38
  post_install_message:
47
- rdoc_options:
39
+ rdoc_options:
48
40
  - --line-numbers
49
41
  - --inline-source
50
42
  - --title
51
43
  - Rake -- Ruby Make
52
- require_paths:
44
+ require_paths:
53
45
  - lib
54
- required_ruby_version: !ruby/object:Gem::Requirement
46
+ required_ruby_version: !ruby/object:Gem::Requirement
55
47
  none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- hash: 3
60
- segments:
61
- - 0
62
- version: "0"
63
- required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
53
  none: false
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- hash: 3
69
- segments:
70
- - 0
71
- version: "0"
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
72
58
  requirements: []
73
-
74
59
  rubyforge_project: sorcerer
75
60
  rubygems_version: 1.8.24
76
61
  signing_key:
77
62
  specification_version: 3
78
63
  summary: Generate Source from Ripper ASTs
79
64
  test_files: []
80
-