rewrite 0.0.1
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.
- data/test/test_andand.rb +120 -0
- data/test/test_call_by_name.rb +37 -0
- data/test/test_call_by_thunk.rb +106 -0
- data/test/test_helper.rb +2 -0
- data/test/test_rewriter_helpers.rb +59 -0
- data/test/test_syntax_let.rb +46 -0
- data/test/test_with.rb +23 -0
- metadata +68 -0
data/test/test_andand.rb
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
include Rewrite::With
|
4
|
+
include Rewrite::Prelude
|
5
|
+
|
6
|
+
class Symbol
|
7
|
+
def to_proc
|
8
|
+
Proc.new { |*args| args.shift.__send__(self, *args) }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class TestAndand < Test::Unit::TestCase
|
13
|
+
|
14
|
+
def test_simple_andand
|
15
|
+
assert_equal(
|
16
|
+
'Hello' + ' World',
|
17
|
+
with(andand) do
|
18
|
+
'Hello'.andand + ' World'
|
19
|
+
end
|
20
|
+
)
|
21
|
+
assert_nil(
|
22
|
+
with(andand) do
|
23
|
+
nil.andand + ' World'
|
24
|
+
end
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_with_parameters
|
29
|
+
=begin
|
30
|
+
[:proc,
|
31
|
+
nil,
|
32
|
+
[:call,
|
33
|
+
[:call, [:lit, 1], :andand],
|
34
|
+
:+,
|
35
|
+
[:array, [:lit, 2]]
|
36
|
+
]
|
37
|
+
]
|
38
|
+
=end
|
39
|
+
assert_equal(
|
40
|
+
1.+(2),
|
41
|
+
with(andand) do
|
42
|
+
1.andand.+(2)
|
43
|
+
end
|
44
|
+
)
|
45
|
+
assert_nil(
|
46
|
+
with(andand) do
|
47
|
+
nil.andand.+(2)
|
48
|
+
end
|
49
|
+
)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_with_a_block
|
53
|
+
=begin
|
54
|
+
[:proc,
|
55
|
+
nil,
|
56
|
+
[:iter,
|
57
|
+
[:call, [:call, [:lit, 1..10], :andand], :inject],
|
58
|
+
[:masgn, [:array, [:dasgn_curr, :x], [:dasgn_curr, :y]]],
|
59
|
+
[:call, [:dvar, :x], :+, [:array, [:dvar, :y]]]
|
60
|
+
]
|
61
|
+
]
|
62
|
+
=end
|
63
|
+
assert_equal(
|
64
|
+
(1..10).inject { |x, y| x + y },
|
65
|
+
with(andand) do
|
66
|
+
(1..10).andand.inject { |x, y| x + y }
|
67
|
+
end
|
68
|
+
)
|
69
|
+
assert_nil(
|
70
|
+
with(andand) do
|
71
|
+
nil.andand.inject { |x, y| x + y }
|
72
|
+
end
|
73
|
+
)
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_with_a_parameter_and_a_block
|
77
|
+
assert_equal(
|
78
|
+
(1..10).inject(42) { |x, y| x + y },
|
79
|
+
with(andand) do
|
80
|
+
(1..10).andand.inject(42) { |x, y| x + y }
|
81
|
+
end
|
82
|
+
)
|
83
|
+
assert_nil(
|
84
|
+
with(andand) do
|
85
|
+
nil.andand.inject(42) { |x, y| x + y }
|
86
|
+
end
|
87
|
+
)
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_with_a_block_pass
|
91
|
+
assert_equal(
|
92
|
+
(1..10).inject(42, &:+),
|
93
|
+
with(andand) do
|
94
|
+
(1..10).andand.inject(42, &:+)
|
95
|
+
end
|
96
|
+
)
|
97
|
+
assert_nil(
|
98
|
+
with(andand) do
|
99
|
+
nil.andand.inject(42, &:+)
|
100
|
+
end
|
101
|
+
)
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_nest
|
105
|
+
assert_equal(
|
106
|
+
(1..10).inject((1..5).inject(-5) { |x, y| x * y }) { |x, y| x + y },
|
107
|
+
with(andand) do
|
108
|
+
(1..10).andand.inject(
|
109
|
+
(1..5).andand.inject(-5) { |x, y| x * y }
|
110
|
+
) { |x, y| x + y }
|
111
|
+
end
|
112
|
+
)
|
113
|
+
assert_nil(
|
114
|
+
with(andand) do
|
115
|
+
nil.andand.inject((1..5).andand.inject(-5) { |x, y| x * y }) { |x, y| x + y }
|
116
|
+
end
|
117
|
+
)
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestCalledByName < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include Rewrite::Prelude
|
6
|
+
|
7
|
+
def test_nullo_case
|
8
|
+
assert_equal(
|
9
|
+
Rewrite.arr_for do
|
10
|
+
lambda { |foo|
|
11
|
+
foo.call(lambda { :foo })
|
12
|
+
}.call(
|
13
|
+
lambda { |x| nil }
|
14
|
+
)
|
15
|
+
end,
|
16
|
+
called_by_name(:foo) { |x| nil }.process(
|
17
|
+
Rewrite.sexp_for do
|
18
|
+
foo(:foo)
|
19
|
+
end
|
20
|
+
).to_a
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_thunk_semantics
|
25
|
+
Rewrite.with(
|
26
|
+
called_by_name(:foo) { |change_it, get_it|
|
27
|
+
assert_equal(0, get_it)
|
28
|
+
change_it
|
29
|
+
assert_equal(100, get_it)
|
30
|
+
}
|
31
|
+
) do
|
32
|
+
x = 0
|
33
|
+
foo(x = 100, x)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
module Rewrite
|
4
|
+
|
5
|
+
class TestCallByThunk < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_nothings
|
8
|
+
assert_equal(
|
9
|
+
Rewrite.arr_for do
|
10
|
+
:baz
|
11
|
+
end,
|
12
|
+
CallByThunk.new().process(
|
13
|
+
Rewrite.sexp_for do
|
14
|
+
:baz
|
15
|
+
end
|
16
|
+
).to_a
|
17
|
+
)
|
18
|
+
assert_equal(
|
19
|
+
Rewrite.arr_for do
|
20
|
+
:baz
|
21
|
+
end,
|
22
|
+
CallByThunk.new(:foo).process(
|
23
|
+
Rewrite.sexp_for do
|
24
|
+
:baz
|
25
|
+
end
|
26
|
+
).to_a
|
27
|
+
)
|
28
|
+
assert_equal(
|
29
|
+
Rewrite.arr_for do
|
30
|
+
bar(:baz)
|
31
|
+
end,
|
32
|
+
CallByThunk.new(:foo).process(
|
33
|
+
Rewrite.sexp_for do
|
34
|
+
bar(:baz)
|
35
|
+
end
|
36
|
+
).to_a
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_convert_one_empty
|
41
|
+
assert_equal(
|
42
|
+
lambda { |foo|
|
43
|
+
Rewrite.arr_for do
|
44
|
+
foo.call()
|
45
|
+
end
|
46
|
+
}.call(nil),
|
47
|
+
CallByThunk.new(:foo).process(
|
48
|
+
Rewrite.sexp_for do
|
49
|
+
foo()
|
50
|
+
end
|
51
|
+
).to_a
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_convert_one
|
56
|
+
assert_equal(
|
57
|
+
lambda { |foo| # make foo a dvar
|
58
|
+
Rewrite.arr_for do
|
59
|
+
foo.call(lambda { :baz })
|
60
|
+
end
|
61
|
+
}.call(nil),
|
62
|
+
CallByThunk.new(:foo).process(
|
63
|
+
Rewrite.sexp_for do
|
64
|
+
foo(:baz)
|
65
|
+
end
|
66
|
+
).to_a
|
67
|
+
)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_nested_invocation
|
71
|
+
assert_equal(
|
72
|
+
lambda { |fib|
|
73
|
+
Rewrite.arr_for do
|
74
|
+
fib.call(lambda { fib.call(lambda { n-1 }) })
|
75
|
+
end
|
76
|
+
}.call(nil),
|
77
|
+
CallByThunk.new(:fib).process(
|
78
|
+
Rewrite.sexp_for do
|
79
|
+
fib(fib(n-1))
|
80
|
+
end
|
81
|
+
).to_a
|
82
|
+
)
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_nested_but_not_us
|
86
|
+
assert_equal(
|
87
|
+
lambda { |fib|
|
88
|
+
Rewrite.arr_for do
|
89
|
+
foo(
|
90
|
+
fib.call(lambda { n-1 })
|
91
|
+
)
|
92
|
+
end
|
93
|
+
}.call(nil),
|
94
|
+
CallByThunk.new(:fib).process(
|
95
|
+
Rewrite.sexp_for do
|
96
|
+
foo(
|
97
|
+
fib(n-1)
|
98
|
+
)
|
99
|
+
end
|
100
|
+
).to_a
|
101
|
+
)
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestRewriteHelpers < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_simple_rewrite
|
6
|
+
|
7
|
+
assert_equal(
|
8
|
+
lambda { |a| a.call }.to_sexp.to_a,
|
9
|
+
Rewrite::RewriteVariablesAsThunkCalls.new(:a).process(
|
10
|
+
lambda { |a| a }.to_sexp
|
11
|
+
).to_a
|
12
|
+
)
|
13
|
+
|
14
|
+
assert_equal(
|
15
|
+
lambda { |a, b| a.call + b }.to_sexp.to_a,
|
16
|
+
Rewrite::RewriteVariablesAsThunkCalls.new(:a).process(
|
17
|
+
lambda { |a,b| a + b }.to_sexp
|
18
|
+
).to_a
|
19
|
+
)
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_nested_rewrite
|
24
|
+
|
25
|
+
assert_equal(
|
26
|
+
lambda { |a| lambda { |b| b } }.to_sexp.to_a,
|
27
|
+
Rewrite::VariableRewriter.new(:a, s(:dvar, :a)).process(
|
28
|
+
lambda { |a| lambda { |b| b } }.to_sexp
|
29
|
+
).to_a
|
30
|
+
)
|
31
|
+
|
32
|
+
assert_equal(
|
33
|
+
lambda { |a| lambda { |b| a.call } }.to_sexp.to_a,
|
34
|
+
Rewrite::RewriteVariablesAsThunkCalls.new(:a).process(
|
35
|
+
lambda { |a| lambda { |b| a } }.to_sexp
|
36
|
+
).to_a
|
37
|
+
)
|
38
|
+
|
39
|
+
assert_equal(
|
40
|
+
lambda { |a| lambda { |a| a } }.to_sexp.to_a,
|
41
|
+
Rewrite::RewriteVariablesAsThunkCalls.new(:a).process(
|
42
|
+
lambda { |a| lambda { |a| a } }.to_sexp
|
43
|
+
).to_a
|
44
|
+
)
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_multiple_rewites
|
49
|
+
|
50
|
+
assert_equal(
|
51
|
+
lambda { |a,b| a.call || b.call }.to_sexp.to_a,
|
52
|
+
Rewrite::RewriteVariablesAsThunkCalls.new(:a,:b).process(
|
53
|
+
lambda { |a,b| a || b }.to_sexp
|
54
|
+
).to_a
|
55
|
+
)
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
module Rewrite
|
4
|
+
|
5
|
+
class TestDefVar < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def arr_for &proc
|
8
|
+
Rewrite.sexp_for(&proc).to_a
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_and_one
|
12
|
+
assert_equal(
|
13
|
+
arr_for {
|
14
|
+
lambda { |foo| :baz }.call(:foo)
|
15
|
+
},
|
16
|
+
DefVar.new(:foo, Rewrite.sexp_for { :foo }).process(
|
17
|
+
Rewrite.sexp_for { :baz }
|
18
|
+
).to_a
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_one_and_one
|
23
|
+
assert_equal(
|
24
|
+
arr_for {
|
25
|
+
lambda { |foo, bar| :baz }.call(:foo, :bar)
|
26
|
+
},
|
27
|
+
DefVar.new(:bar, Rewrite.sexp_for { :bar }).process(
|
28
|
+
Rewrite.sexp_for { lambda { |foo| :baz }.call(:foo) }
|
29
|
+
).to_a
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_two_and_one
|
34
|
+
assert_equal(
|
35
|
+
arr_for {
|
36
|
+
lambda { |foo, bar, blitz| :baz }.call(:foo, :bar, :blitz)
|
37
|
+
},
|
38
|
+
DefVar.new(:blitz, Rewrite.sexp_for { :blitz }).process(
|
39
|
+
Rewrite.sexp_for { lambda { |foo, bar| :baz }.call(:foo, :bar) }
|
40
|
+
).to_a
|
41
|
+
)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
data/test/test_with.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'parse_tree'
|
4
|
+
require 'sexp_processor'
|
5
|
+
|
6
|
+
class TestWith < Test::Unit::TestCase
|
7
|
+
|
8
|
+
include Rewrite::With
|
9
|
+
|
10
|
+
class NoopProcessor < SexpProcessor
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_null_processor
|
15
|
+
assert_equal(
|
16
|
+
:foo,
|
17
|
+
with(NoopProcessor.new) do
|
18
|
+
:foo
|
19
|
+
end
|
20
|
+
)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rewrite
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Reg Braithwaite
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-17 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Syntactic metaprogramming for Ruby
|
17
|
+
email:
|
18
|
+
- raganwald+rewrite@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files: []
|
26
|
+
|
27
|
+
has_rdoc: true
|
28
|
+
homepage: http://rewrite.rubyforge.org
|
29
|
+
post_install_message: |+
|
30
|
+
|
31
|
+
For more information on rewrite, see http://rewrite.rubyforge.org
|
32
|
+
|
33
|
+
NOTE: Change this information in PostInstall.txt
|
34
|
+
You can also delete it if you don't want it.
|
35
|
+
|
36
|
+
|
37
|
+
rdoc_options:
|
38
|
+
- --main
|
39
|
+
- README.txt
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project: rewrite
|
57
|
+
rubygems_version: 1.1.1
|
58
|
+
signing_key:
|
59
|
+
specification_version: 2
|
60
|
+
summary: Syntactic metaprogramming for Ruby
|
61
|
+
test_files:
|
62
|
+
- test/test_andand.rb
|
63
|
+
- test/test_call_by_name.rb
|
64
|
+
- test/test_call_by_thunk.rb
|
65
|
+
- test/test_helper.rb
|
66
|
+
- test/test_rewriter_helpers.rb
|
67
|
+
- test/test_syntax_let.rb
|
68
|
+
- test/test_with.rb
|