forked from rcodetools/rcodetools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.TDC
158 lines (110 loc) · 4.35 KB
/
README.TDC
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
= Overview
Ruby is very dynamic language, therefore it is impossible to do
accurate completion without executing script. While executing script
from start to cursor point is often dangerous, executing unit test
script covering current point is SAFE. I call this methodology
`Test-Driven Completion' (TDC).
As I have already stated in README, browsing documentation of method
(rct-doc) is almost identical operation to completion. This
discussion is applicable to rct-doc.
= Why TDD Is Needed
In the following code snippet:
File.unlink a_file
File. <-
If you complete after `File.', rct-complete actually deletes a_file.
Normally it is unpleasant.
In real-life development, side-effect is inevitable.
In the foo method which are not called:
def foo
1. <-
end
If the code does not call foo, rct-complete cannot do any completions.
Before TDC, if you want to do completion in methods, you have to write
method call and remove it after completion. Too useless!!
= Messianic Unit Test Script
Recently Test-Driven Development (TDD) is widespread. Many developers
write unit tests. Fortunately Ruby's unit tester, Test::Unit, is
sophisticated enough to test one test method. Unit tests are
self-enclosed: they must tear down resources, so executing unit tests
are SAFE. TDC uses unit test to do completion.
= TDC Methodology
(1) Switch to unit test script.
(2) Write a test for target method.
(3) Switch to implementation script.
(4) You can write target method WITH COMPLETION!
(5) Back to (1)
TDC methodology is almost identical to TDD. TDC is very easy for TDDers.
= TDC With Example
For simplicity, suppose that you are unfamiliar with Time class and
you want to write a method to format date string.
The directory structure and file contents is following:
/tmp/mylib0/
/tmp/mylib0/lib/
mylib0.rb
/tmp/mylib0/test/
test_mylib0.rb
List: mylib0.rb
# contrived example of long-runtime method
def mysleep(x)
sleep x
end
def mytime(tm)
end
List: test_mylib0.rb
require 'test/unit'
require 'mylib0'
class TestMylib0 < Test::Unit::TestCase
def test_0_mysleep
s = Time.now
mysleep 3.0
e = Time.now
assert_in_delta 3.0, e-s, 0.01
end
def test_1_mytime
end
end
These sample files are in demo/ directory.
== Switch to unit test script.
TDC starts with writing unit test as TDD does.
Open test_mylib0.rb.
== Write a test for target method.
Suppose that you want to write mytime method and test_1_mytime test
method, and that you want to experiment Time class first (before
forming an assertion).
In TDC, you do not have to write an assertion first: just write only a
method call. If you are familiar with Time class, you are free to
write an assertion, of course.
def test_1_mytime
mytime(Time.now)
end
At this time, the cursor position is in test_1_mytime test method.
== Switch to implementation script.
Open mylib0.rb with the `ruby-toggle-file' script. For example, in Emacs use
the `ruby-toggle-buffer' command, and in vim the <localleader>t (by default
\t) binding. Since in TDD/TDC you often switch between the test and the
implementation, it is much handier than typing the filename manually.
The rct-complete uses latest-selected test script as TDC test script
and test method at cursor position as TDC test method. In this case,
test_mylib0.rb is TDC test script and test_1_mytime is TDC test
method. If the cursor position of test_mylib0.rb is at the top,
rct-complete executes whole test methods in test_mylib0.rb. Therefore
latency of completion is longer.
== You can write target method WITH COMPLETION!
Fill mytime method.
def mytime(tm)
tm.
end
Do completion after `tm.'. Here! Your editor is listing methods `tm'
accepts!! If your editor has help-on-candidate mechanism (eg. Emacs +
Icicles), you would see documentation of each listed method.
Then you find `Time#strftime' method. Type `str' and do completion.
def mytime(tm)
tm.strftime
end
Usage is... use `rct-doc' (in Emacs, `rct-ri') after `strftime'.
After you are familiar with Time class, switch to test script and write assertions.
= When Modifying Another Method
If you want to modify already-written method, setting cursor position
of corresponding test script to corresponding test method is better.
It tells rct-complete new test script and test method, so you can do
completion in the new method.