casty 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,195 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ROOT = File.expand_path('..', File.dirname(__FILE__))
4
+ $:.unshift "#{ROOT}/lib"
5
+
6
+ require 'test/unit'
7
+ require 'stringio'
8
+ require 'fileutils'
9
+ require 'casty'
10
+
11
+ # a dir to cd into for creating files and such
12
+ TEST_DIR = "#{File.dirname(__FILE__)}/var"
13
+
14
+ # --------------------------------------------------------------------
15
+ # Helpers for testing
16
+ # --------------------------------------------------------------------
17
+
18
+ class Array
19
+ def same_list?(other)
20
+ self.length == other.length or
21
+ return false
22
+ self.zip(other).all? do |mine, yours|
23
+ mine.equal? yours or
24
+ return false
25
+ end
26
+ end
27
+ end
28
+
29
+ class Integer
30
+ #
31
+ # Return a `self'-element array containing the result of the given
32
+ # block.
33
+ #
34
+ def of(&blk)
35
+ Array.new(self, &blk)
36
+ end
37
+ end
38
+
39
+ module Test::Unit::Assertions
40
+ INDENT = ' '
41
+ #
42
+ # Assert that the given string is parsed as expected. The given
43
+ # string is of the format:
44
+ #
45
+ # <program>
46
+ # ----
47
+ # <expected inspect string>
48
+ #
49
+ # The <program> part is yielded to obtain the AST.
50
+ #
51
+ def check_ast(test_data)
52
+ inp, exp = test_data.split(/^----+\n/)
53
+ ast = yield(inp)
54
+ assert_tree(ast)
55
+ assert(ast.is_a?(C::Node))
56
+ assert_equal_inspect_strs(exp, ast.inspect)
57
+ end
58
+ #
59
+ # Assert that the given Node#inspect strings are equal.
60
+ #
61
+ def assert_equal_inspect_strs(exp, out)
62
+ # remove EOL space
63
+ out = out.gsub(/ *$/, '')
64
+ exp = exp.gsub(/ *$/, '')
65
+
66
+ # normalize BOL space
67
+ exp.gsub!(%r'^#{INDENT}*') do |s|
68
+ levels = s.length / INDENT.length
69
+ C::Node::INSPECT_TAB*levels
70
+ end
71
+
72
+ # compare
73
+ msg = "Debug strings unequal:\n#{juxtapose('Expected', exp, 'Output', out)}"
74
+ assert_block(msg){out == exp}
75
+ end
76
+ #
77
+ # Return a string of `s1' and `s2' side by side with a dividing line
78
+ # in between indicating differences. `h1' and `h2' are the column
79
+ # headings.
80
+ #
81
+ def juxtapose(h1, s1, h2, s2)
82
+ s1 = s1.split("\n")
83
+ s2 = s2.split("\n")
84
+ rows = [s1.length, s2.length].max
85
+ wl = s1.map{|line| line.length}.max
86
+ wr = s2.map{|line| line.length}.max
87
+ ret = ''
88
+ ret << "#{('-'*wl)}----#{'-'*wr}\n"
89
+ ret << "#{h1.ljust(wl)} || #{h2}\n"
90
+ ret << "#{('-'*wl)}-++-#{'-'*wr}\n"
91
+ (0...rows).each do |i|
92
+ if i >= s1.length
93
+ ret << "#{' '*wl} > #{s2[i]}\n"
94
+ elsif i >= s2.length
95
+ ret << "#{s1[i].ljust(wl)} <\n"
96
+ elsif s1[i] == s2[i]
97
+ ret << "#{s1[i].ljust(wl)} || #{s2[i]}\n"
98
+ else
99
+ ret << "#{s1[i].ljust(wl)} <> #{s2[i]}\n"
100
+ end
101
+ end
102
+ ret << "#{('-'*wl)}----#{'-'*wr}\n"
103
+ return ret
104
+ end
105
+ #
106
+ # Assert that an exception of the given class is raised, and that
107
+ # the message matches the given regex.
108
+ #
109
+ def assert_error(klass, re)
110
+ ex = nil
111
+ assert_raise(klass) do
112
+ begin
113
+ yield
114
+ rescue Exception => ex
115
+ raise
116
+ end
117
+ end
118
+ assert_match(re, ex.message)
119
+ end
120
+ #
121
+ # Assert that the given ast's nodes' parents are correct, and there
122
+ # aren't non-Nodes where there shouldn't be.
123
+ #
124
+ def assert_tree(ast)
125
+ meth = 'unknown method'
126
+ caller.each do |line|
127
+ if line =~ /in `(test_.*?)'/ #`
128
+ meth = $1
129
+ break
130
+ end
131
+ end
132
+ filename = "#{self.class}_#{meth}.out"
133
+ begin
134
+ assert_tree1(ast, nil)
135
+ assert(true)
136
+ rescue BadTreeError => e
137
+ require 'pp'
138
+ open("#{filename}", 'w'){|f| PP.pp(ast, f)}
139
+ flunk("#{e.message}. Output dumped to `#{filename}'.")
140
+ end
141
+ end
142
+ #
143
+ def assert_tree1(x, parent)
144
+ if x.is_a? C::Node
145
+ parent.equal? x.parent or
146
+ raise BadTreeError, "#{x.class}:0x#{(x.id << 1).to_s(16)} has #{x.parent ? 'wrong' : 'no'} parent"
147
+ x.fields.each do |field|
148
+ next if !field.child?
149
+ val = x.send(field.reader)
150
+ next if val.nil?
151
+ val.is_a? C::Node or
152
+ raise BadTreeError, "#{x.class}:0x#{(x.id << 1).to_s(16)} is a non-Node child"
153
+ assert_tree1(val, x)
154
+ end
155
+ end
156
+ end
157
+ class BadTreeError < StandardError; end
158
+ #
159
+ # Assert that `arg' is a C::NodeList.
160
+ #
161
+ def assert_list(arg)
162
+ assert_kind_of(C::NodeList, arg)
163
+ end
164
+ #
165
+ # Assert that `arg' is an empty C::NodeList.
166
+ #
167
+ def assert_empty_list(arg)
168
+ assert_list arg
169
+ assert(arg.empty?)
170
+ end
171
+ #
172
+ # Assert that the elements of exp are the same as those of out, and
173
+ # are in the same order.
174
+ #
175
+ def assert_same_list(exp, out)
176
+ assert_equal(exp.length, out.length, "Checking length")
177
+ (0...exp.length).each do |i|
178
+ assert_same(exp[i], out[i], "At index #{i} (of 0...#{exp.length})")
179
+ end
180
+ end
181
+ #
182
+ # Assert that out is ==, but not the same as exp (i.e., it is a
183
+ # copy).
184
+ #
185
+ def assert_copy(exp, out)
186
+ assert_not_same exp, out
187
+ assert_equal exp, out
188
+ end
189
+ #
190
+ # Assert the invariants of `node'.
191
+ #
192
+ def assert_invariants(node)
193
+ node.assert_invariants(self)
194
+ end
195
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: casty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - George Ogata
9
+ - Vasily Fedoseyev
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-12-19 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake-compiler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: racc
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: 1.4.8
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 1.4.8
47
+ description: Fork of CAST, C parser and AST constructor.
48
+ email:
49
+ - george.ogata@gmail.com
50
+ - vasilyfedoseyev@gmail.com
51
+ executables: []
52
+ extensions:
53
+ - ext/casty/extconf.rb
54
+ extra_rdoc_files: []
55
+ files:
56
+ - .gitignore
57
+ - CHANGELOG
58
+ - Gemfile
59
+ - LICENSE
60
+ - README.markdown
61
+ - Rakefile
62
+ - casty.gemspec
63
+ - ext/casty/cast.c
64
+ - ext/casty/cast.h
65
+ - ext/casty/extconf.rb
66
+ - ext/casty/parser.c
67
+ - ext/casty/yylex.re
68
+ - lib/casty.rb
69
+ - lib/casty/c.y
70
+ - lib/casty/c_nodes.rb
71
+ - lib/casty/inspect.rb
72
+ - lib/casty/node.rb
73
+ - lib/casty/node_list.rb
74
+ - lib/casty/parse.rb
75
+ - lib/casty/preprocessor.rb
76
+ - lib/casty/tempfile.rb
77
+ - lib/casty/to_s.rb
78
+ - lib/casty/version.rb
79
+ - test/all.rb
80
+ - test/c_nodes_test.rb
81
+ - test/lexer_test.rb
82
+ - test/node_list_test.rb
83
+ - test/node_test.rb
84
+ - test/parse_test.rb
85
+ - test/parser_test.rb
86
+ - test/preprocessor_test.rb
87
+ - test/render_test.rb
88
+ - test/test_helper.rb
89
+ - ext/casty/yylex.c
90
+ - lib/casty/c.tab.rb
91
+ homepage: http://github.com/Vasfed/cast
92
+ licenses:
93
+ - MIT
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.24
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: C parser and AST constructor.
116
+ test_files:
117
+ - test/all.rb
118
+ - test/c_nodes_test.rb
119
+ - test/lexer_test.rb
120
+ - test/node_list_test.rb
121
+ - test/node_test.rb
122
+ - test/parse_test.rb
123
+ - test/parser_test.rb
124
+ - test/preprocessor_test.rb
125
+ - test/render_test.rb
126
+ - test/test_helper.rb
127
+ has_rdoc: