sexp_path 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/sexp_path'
3
+
4
+ class SexpReplacementTest < Test::Unit::TestCase
5
+ def test_replacing_exact_matches
6
+ sexp = s(:a, s(:b), :c)
7
+ actual = sexp.replace_sexp(s(:b)){ :b }
8
+
9
+ assert_equal( s(:a, :b, :c), actual)
10
+ end
11
+
12
+ def test_replacing_root
13
+ sexp = s(:a, s(:b), :c)
14
+ actual = sexp.replace_sexp(Q?{t(:a)}){ s(:new) }
15
+
16
+ assert_equal( s(:new), actual)
17
+ end
18
+
19
+
20
+ end
@@ -0,0 +1,127 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/sexp_path'
3
+ require 'parse_tree'
4
+ require 'set'
5
+
6
+ # Here's a crazy idea, these tests actually use sexp_path on some "real"
7
+ # code to see if it can satisfy my requirements.
8
+ #
9
+ # These tests are two fold:
10
+ # 1. Make sure it works
11
+ # 2. Make sure it's not painful to use
12
+ class UseCaseTest < Test::Unit::TestCase
13
+ def setup
14
+ path = File.dirname(__FILE__) + '/sample.rb'
15
+ sample = File.read(path)
16
+ @sexp = Sexp.from_array(ParseTree.new.parse_tree_for_string(sample, path))
17
+ end
18
+
19
+ def test_finding_methods
20
+ methods = @sexp / Q?{ t(:defn) }
21
+ assert_equal 5, methods.length
22
+ end
23
+
24
+ def test_finding_classes_and_methods
25
+ res = @sexp / Q?{ s(:class, atom % 'name', _, _) }
26
+ assert_equal 1, res.length
27
+ assert_equal :ExampleTest, res.first['name']
28
+
29
+ methods = res / Q?{ t(:defn) }
30
+ assert_equal 5, methods.length
31
+ end
32
+
33
+ def test_finding_empty_test_methods
34
+ empty_body = Q?{ s(:scope, s(:block, t(:args), s(:nil))) }
35
+ res = @sexp / Q?{ s(:defn, m(/^test_.+/) % 'name', empty_body ) }
36
+ assert_equal 1, res.length
37
+ assert_equal :test_b, res.first['name']
38
+ end
39
+
40
+ def test_finding_duplicate_test_names
41
+ res = @sexp / Q?{ s(:defn, m(/^test_.+/) % 'name', _ ) }
42
+ seen = Set.new()
43
+ repeated = 0
44
+ res.each do |m|
45
+ name = m['name']
46
+ repeated += 1 if seen.include? name
47
+ seen << name
48
+ end
49
+
50
+ assert_equal 1, repeated, "Should have caught test_a being repeated"
51
+ end
52
+
53
+ def test_rewriting_colon2s_oh_man_i_hate_those_in_most_cases_but_i_understand_why_they_are_there
54
+ colon2 = Q?{ s(:colon2, s(:const, atom % 'const'), atom % 'scope') }
55
+
56
+ # Hacky, could be done better
57
+ while (results = (@sexp / colon2)) && !results.empty?
58
+ results.each do |result|
59
+ result.sexp.replace(s(:const, result.values_at('const','scope').join('::') ))
60
+ end
61
+ end
62
+
63
+ expected_sexp = s(:const, "Test::Unit::TestCase")
64
+ assert_equal 1, (@sexp / expected_sexp).length, @sexp.inspect
65
+ end
66
+ end
67
+
68
+ # Contents of sample.rb sexp below:
69
+ __END__
70
+ s(:block,
71
+ s(:call, nil, :require, s(:arglist, s(:str, "test/unit"))),
72
+ s(:call, nil, :require, s(:arglist, s(:str, "test/unit/testcase"))),
73
+ s(:class,
74
+ :ExampleTest,
75
+ s(:colon2, s(:colon2, s(:const, :Test), :Unit), :TestCase),
76
+ s(:scope,
77
+ s(:block,
78
+ s(:defn,
79
+ :setup,
80
+ s(:args),
81
+ s(:scope, s(:block, s(:call, s(:lit, 1), :+, s(:arglist, s(:lit, 2)))))),
82
+ s(:defn,
83
+ :test_a,
84
+ s(:args),
85
+ s(:scope,
86
+ s(:block,
87
+ s(:call,
88
+ nil,
89
+ :assert_equal,
90
+ s(:arglist,
91
+ s(:call, s(:lit, 1), :+, s(:arglist, s(:lit, 2))),
92
+ s(:lit, 4)))))),
93
+ s(:defn, :test_b, s(:args), s(:scope, s(:block, s(:nil)))),
94
+ s(:defn,
95
+ :test_a,
96
+ s(:args),
97
+ s(:scope,
98
+ s(:block,
99
+ s(:call,
100
+ nil,
101
+ :assert_equal,
102
+ s(:arglist,
103
+ s(:call, s(:lit, 1), :+, s(:arglist, s(:lit, 2))),
104
+ s(:lit, 3)))))),
105
+ s(:call, nil, :private, s(:arglist)),
106
+ s(:defn,
107
+ :helper_method,
108
+ s(:args,
109
+ :apples,
110
+ :oranges,
111
+ :cakes,
112
+ s(:block, s(:lasgn, :cakes, s(:nil)))),
113
+ s(:scope,
114
+ s(:block,
115
+ s(:iter,
116
+ s(:call,
117
+ s(:call,
118
+ s(:array, s(:lvar, :apples), s(:lvar, :oranges), s(:lvar, :cakes)),
119
+ :compact,
120
+ s(:arglist)),
121
+ :map,
122
+ s(:arglist)),
123
+ s(:lasgn, :food),
124
+ s(:call,
125
+ s(:call, s(:lvar, :food), :to_s, s(:arglist)),
126
+ :upcase,
127
+ s(:arglist))))))))))
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sexp_path
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Sanderson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-12 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sexp_processor
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: "3.0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: ParseTree
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: "2.1"
34
+ version:
35
+ description: " Allows you to do example based pattern matching and queries against S Expressions (sexp).\n"
36
+ email: netghost@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ - TODO
44
+ files:
45
+ - README.rdoc
46
+ - Rakefile
47
+ - TODO
48
+ - VERSION.yml
49
+ - examples/print_methods.rb
50
+ - examples/sexp_grep.rb
51
+ - lib/sexp_path.rb
52
+ - lib/sexp_path/line_numbering_processor.rb
53
+ - lib/sexp_path/matcher/all.rb
54
+ - lib/sexp_path/matcher/any.rb
55
+ - lib/sexp_path/matcher/atom.rb
56
+ - lib/sexp_path/matcher/base.rb
57
+ - lib/sexp_path/matcher/block.rb
58
+ - lib/sexp_path/matcher/child.rb
59
+ - lib/sexp_path/matcher/include.rb
60
+ - lib/sexp_path/matcher/not.rb
61
+ - lib/sexp_path/matcher/pattern.rb
62
+ - lib/sexp_path/matcher/sibling.rb
63
+ - lib/sexp_path/matcher/type.rb
64
+ - lib/sexp_path/matcher/wild.rb
65
+ - lib/sexp_path/sexp_collection.rb
66
+ - lib/sexp_path/sexp_query_builder.rb
67
+ - lib/sexp_path/sexp_result.rb
68
+ - lib/sexp_path/traverse.rb
69
+ - test/line_numbering_processor_test.rb
70
+ - test/sample.rb
71
+ - test/sexp_path_capture_test.rb
72
+ - test/sexp_path_matching_test.rb
73
+ - test/sexp_replacement_test.rb
74
+ - test/use_case_test.rb
75
+ has_rdoc: true
76
+ homepage: http://github.com/adamsanderson/sexp_path
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --charset=UTF-8
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 1.3.5
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Pattern matching for S-Expressions (sexp).
103
+ test_files:
104
+ - test/line_numbering_processor_test.rb
105
+ - test/sexp_path_capture_test.rb
106
+ - test/sexp_path_matching_test.rb
107
+ - test/sexp_replacement_test.rb
108
+ - test/use_case_test.rb