ruby_traverser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,92 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class TraversalTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ define_method :"test select block with multi element array argument" do
7
+ src = %q{
8
+ gem 'ripper', :src => 'github'
9
+ }
10
+ code = Ripper::RubyBuilder.build(src)
11
+ call_node = code.find_call('gem', :args => ['ripper', {:src => 'github'}])
12
+ assert_equal Ruby::Call, call_node.class
13
+ end
14
+
15
+ define_method :"test find gem statement inside group" do
16
+ src = %q{
17
+ group :test do
18
+ gem 'ripper', :src => 'github'
19
+ end
20
+ }
21
+ code = Ripper::RubyBuilder.build(src)
22
+ block_node = code.find_call('group', :args => [:test], :block => true)
23
+ assert_equal Ruby::Call, block_node.class
24
+ call_node = block_node.find_call('gem', :args => ['ripper', {:src => 'github'}])
25
+ assert_equal Ruby::Call, call_node.class
26
+ # puts call_node.to_ruby
27
+ end
28
+
29
+ define_method :"test find gem statement inside group using better DSL" do
30
+ src = %q{
31
+ gem 'ripper', :src => 'github', :blip => 'blap'
32
+ group :test do
33
+ gem 'ripper', :src => 'github'
34
+ end
35
+
36
+ }
37
+ code = Ripper::RubyBuilder.build(src)
38
+ code.inside_block('group', :args => [:test]) do |b|
39
+ call_node = b.find_call('gem', :args => ['ripper', {:src => 'github'}])
40
+ assert_equal Ruby::Call, call_node.class
41
+ puts call_node.to_ruby
42
+ end
43
+ end
44
+
45
+
46
+ define_method :"test find variabel in method definition" do
47
+ src = %q{
48
+ def hello_world(a)
49
+ my_var
50
+ end
51
+ }
52
+
53
+ code = Ripper::RubyBuilder.build(src)
54
+ code.inside_def('hello_world', :params => ['a']) do |b|
55
+ call_node = b.find_variable('my_var')
56
+ assert_equal Ruby::Variable, call_node.class
57
+ puts call_node.to_ruby
58
+ end
59
+ end
60
+
61
+ define_method :"test find assignment in method definition" do
62
+ src = %q{
63
+ def hello_world(a)
64
+ my_var = 2
65
+ end
66
+ }
67
+
68
+ code = Ripper::RubyBuilder.build(src)
69
+ code.inside_def('hello_world', :params => ['a']) do |b|
70
+ call_node = b.find_assignment('my_var')
71
+ assert_equal Ruby::Assignment, call_node.class
72
+ puts call_node.to_ruby
73
+ end
74
+ end
75
+
76
+
77
+ define_method :"test find method definition" do
78
+ src = %q{
79
+ def hello_world(a)
80
+ gem 'ripper', :src => 'github'
81
+ end
82
+ }
83
+ code = Ripper::RubyBuilder.build(src)
84
+ code.inside_def('hello_world', :params => ['a']) do |b|
85
+ call_node = b.find_call('gem', :args => ['ripper', {:src => 'github'}], :verbose => true)
86
+ assert_equal Ruby::Call, call_node.class
87
+ puts call_node.to_ruby
88
+ end
89
+ end
90
+
91
+
92
+ end
@@ -0,0 +1,128 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class TraversalTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ def setup
7
+ end
8
+
9
+ define_method :"test select Module with complex namespace" do
10
+ src = %q{
11
+ module Xyz::Xxx::Blip
12
+ 2
13
+ end
14
+ }
15
+
16
+ code = Ripper::RubyBuilder.build(src)
17
+ module_node = code.find_module('Xyz::Xxx::Blip')
18
+ assert_equal Ruby::Module, module_node.class
19
+ # puts "module: #{module_node}"
20
+ end
21
+
22
+ define_method :"test select Class with complex namespace" do
23
+ src = %q{
24
+ class Abc::Bef::Monty
25
+ end
26
+ }
27
+ code = Ripper::RubyBuilder.build(src)
28
+ clazz_node = code.find_class('Abc::Bef::Monty')
29
+ assert_equal Ruby::Class, clazz_node.class
30
+ # puts "class: #{clazz_node}"
31
+ end
32
+
33
+
34
+ define_method :"test select Class that inherits from other Class" do
35
+ src = %q{
36
+ class Monty < Abc::Blip
37
+ end
38
+ }
39
+ code = Ripper::RubyBuilder.build(src)
40
+ clazz_node = code.find_class('Monty', :superclass => 'Abc::Blip')
41
+ assert_equal Ruby::Class, clazz_node.class
42
+ end
43
+
44
+ define_method :"test select block" do
45
+ src = %q{
46
+ my_block = 7
47
+ my_block do
48
+ 1
49
+ end
50
+ }
51
+ code = Ripper::RubyBuilder.build(src)
52
+ block_node = code.find_call('my_block')
53
+ assert_equal Ruby::Call, block_node.class
54
+ end
55
+
56
+ define_method :"test select block with args" do
57
+ src = %q{
58
+
59
+ my_block :hello => 7 do
60
+ 1
61
+ end
62
+ }
63
+ code = Ripper::RubyBuilder.build(src)
64
+ block_node = code.find_call('my_block', :args => [{:hello => 7}])
65
+ assert_equal Ruby::Call, block_node.class
66
+ end
67
+
68
+ define_method :"test select block with params" do
69
+ src = %q{
70
+
71
+ my_block do |v|
72
+ 1
73
+ end
74
+ }
75
+ code = Ripper::RubyBuilder.build(src)
76
+ block_node = code.find_call('my_block', :block_params => ['v'])
77
+ assert_equal Ruby::Call, block_node.class
78
+ end
79
+
80
+ define_method :"test select block with multi element hash argument" do
81
+ src = %q{
82
+
83
+ my_block :a => 7, b => 3 do |v|
84
+ 1
85
+ end
86
+ }
87
+ code = Ripper::RubyBuilder.build(src)
88
+ block_node = code.find_call('my_block', :args => [{:a => 7, 'b' => 3}])
89
+ assert_equal Ruby::Call, block_node.class
90
+ end
91
+
92
+ define_method :"test select block with multi element array argument" do
93
+ src = %q{
94
+
95
+ my_block ['a', 'b'] do |v|
96
+ 1
97
+ end
98
+ }
99
+ code = Ripper::RubyBuilder.build(src)
100
+ block_node = code.find_call('my_block', :args => [{:array =>['a', 'b']}])
101
+ assert_equal Ruby::Call, block_node.class
102
+ end
103
+
104
+ define_method :"test select block with multi element array argument" do
105
+ src = %q{
106
+ gem 'ripper', :src => 'github'
107
+ }
108
+ code = Ripper::RubyBuilder.build(src)
109
+ call_node = code.find_call('gem', :args => ['ripper', {:src => 'github'}])
110
+ assert_equal Ruby::Call, call_node.class
111
+ end
112
+
113
+ define_method :"test select block with multi element array argument" do
114
+ src = %q{
115
+ group :test do
116
+ gem 'ripper', :src => 'github'
117
+ end
118
+ }
119
+ code = Ripper::RubyBuilder.build(src)
120
+ block_node = code.find_call('group', :args => [:test], :block => true)
121
+ assert_equal Ruby::Call, block_node.class
122
+ # call_node = code.find_call('gem', :args => ['ripper', {:src => 'github'}])
123
+ # assert_equal Ruby::Call, call_node.class
124
+ end
125
+
126
+
127
+
128
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_traverser
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Kristian Mandrup
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-27 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 0
31
+ version: 2.0.0
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: ripper2ruby
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">"
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 0
44
+ - 2
45
+ version: 0.0.2
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ description: traverse a ruby code model and optionally mutate it along the way using a nice rubyish DSL
49
+ email: kmandrup@gmail.com
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files:
55
+ - LICENSE
56
+ - README.markdown
57
+ files:
58
+ - .gitignore
59
+ - LICENSE
60
+ - README.markdown
61
+ - Rakefile
62
+ - VERSION
63
+ - lib/mutate/api.rb
64
+ - lib/mutate/replacer.rb
65
+ - lib/rails/api_wrapper.rb
66
+ - lib/ruby_traverser.rb
67
+ - lib/traversal/api/finders.rb
68
+ - lib/traversal/api/inside.rb
69
+ - lib/traversal/api/traversal.rb
70
+ - lib/traversal/misc.rb
71
+ - lib/traversal/mixin.rb
72
+ - lib/traversal/module.rb
73
+ - lib/traversal/patch.rb
74
+ - ruby_traverser.gemspec
75
+ - spec/ruby_traverser_spec.rb
76
+ - spec/spec.opts
77
+ - spec/spec_helper.rb
78
+ - test/mutate/mutate_class_test.rb
79
+ - test/mutate/mutate_test.rb
80
+ - test/rails_api/gemfile_api.rb
81
+ - test/test_helper.rb
82
+ - test/traversal/gemfile_test.rb
83
+ - test/traversal/ruby_api_traversal_test.rb
84
+ has_rdoc: true
85
+ homepage: http://github.com/kristianmandrup/ruby_trav
86
+ licenses: []
87
+
88
+ post_install_message:
89
+ rdoc_options:
90
+ - --charset=UTF-8
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ requirements: []
108
+
109
+ rubyforge_project:
110
+ rubygems_version: 1.3.6
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: traverse and mutate ruby code using a nice DSL
114
+ test_files:
115
+ - spec/ruby_traverser_spec.rb
116
+ - spec/spec_helper.rb
117
+ - test/mutate/mutate_class_test.rb
118
+ - test/mutate/mutate_test.rb
119
+ - test/rails_api/gemfile_api.rb
120
+ - test/test_helper.rb
121
+ - test/traversal/gemfile_test.rb
122
+ - test/traversal/ruby_api_traversal_test.rb