texttube 5.1.1 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,280 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
- require_relative '../lib/texttube/base.rb'
5
- require_relative '../lib/texttube/filterable.rb'
6
-
7
- describe "TextTube" do
8
- let(:content) { <<MARKDOWN
9
- ## The Rainfall Problem ##
10
-
11
- I read of a test given to students to see if they understand the concepts in the first part of a computer science course. It was in an interesting paper called *What Makes Code Hard to Understand?*[[http://arxiv.org/abs/1304.5257|What Makes Code Hard to Understand? Michael Hansen, Robert L. Goldstone, Andrew Lumsdaine]].
12
-
13
- > To solve it, students must write a program that averages a list of numbers (rainfall amounts), where the list is terminated with a specific value – e.g., a negative number or 999999.
14
-
15
- Of course, reading about the problem I immediately wanted to try it, but even though the idea of posing the problem was to test the understanding of loops, the problem lends itself naturally to a recursive solution, and that's much more interesting than a boring old loop! I realised it wouldn't run well in Ruby but decided to write it anyway:
16
-
17
- ::::ruby
18
- def rainfall(droplets,total=0,measures=1,limit=99_999)
19
- return total.to_f.div measures if droplets == limit
20
- unless droplets < 0
21
- total += droplets
22
- measures += 1
23
- end
24
- rainfall rand(limit + 1), total, measures
25
- end
26
-
27
- Normally I'd start this off with a random number, but just to make it clear that it works I started with the termination number. Then, I reran it until it worked. It gives you an idea of how poor Ruby is at recursion.
28
-
29
- ::::shell
30
- rainfall 99_999
31
- # => 0
32
- rainfall 99_998
33
- SystemStackError: stack level too deep
34
- from /Users/iainuser/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/irb/workspace.rb:80
35
- Maybe IRB bug!
36
- rainfall 99_998
37
- SystemStackError: stack level too deep
38
- from /Users/iainuser/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/irb/workspace.rb:80
39
- Maybe IRB bug!
40
- rainfall 99_998
41
- SystemStackError: stack level too deep
42
- from /Users/iainuser/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/irb/workspace.rb:80
43
- Maybe IRB bug!
44
- rainfall 99_998
45
- SystemStackError: stack level too deep
46
- from /Users/iainuser/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/irb/workspace.rb:80
47
- Maybe IRB bug!
48
- rainfall 99_998
49
- # => 49693
50
-
51
- Just to be clear, I ran this with Ruby 2.0.0-rc2 as well with the same results. It's a pity that Ruby fails at this kind of thing, because it is such an elegant and useful language that seems to have borrowed the best of many other niche languages. Recursion is often the most elegant *and* performant[[http://dictionary.cambridge.org/dictionary/british/pedant|If you're one of these then you won't like that I made a word up. Unfortunately for you, I'm British, which means that I understand that English is a living language, it is my tool not my master, and it has a long history of being changed to suit the speaker or writer. This is one such case.]] solution.
52
- MARKDOWN
53
- }
54
- context "with simple examples" do
55
- before :all do
56
- module AFilter
57
- extend TextTube::Filterable
58
-
59
- filter_with :double do |text|
60
- text * 2
61
- end
62
-
63
- filter_with :triple do |text|
64
- text * 3
65
- end
66
- end
67
-
68
- module BFil
69
- extend TextTube::Filterable
70
-
71
- filter_with :spacial do |current,options|
72
- current.split(//).join " "
73
- end
74
- end
75
-
76
- class NeuS < TextTube::Base
77
- register BFil
78
- register AFilter
79
- register do
80
- filter_with :dashes do |text|
81
- "---#{text}---"
82
- end
83
- end
84
- end
85
- end
86
- let(:n) { NeuS.new "abc" }
87
- subject { n }
88
- it { should == "abc" }
89
- its(:filters) { should =~ [:spacial, :dashes, :double, :triple] }
90
- its(:filter) { should == "---a b ca b ca b ca b ca b ca b c---" }
91
- context "ordering" do
92
- context "given one filter" do
93
- subject { n.filter :spacial }
94
- it { should == "a b c" }
95
- end
96
- context "given several filters but not all" do
97
- subject { n.filter :spacial, :dashes }
98
- it { should == "---a b c---" }
99
- end
100
- context "given several filters in a different order" do
101
- subject { n.filter :dashes, :double, :spacial, :triple }
102
- it { should == "- - - a b c - - - - - - a b c - - -- - - a b c - - - - - - a b c - - -- - - a b c - - - - - - a b c - - -" }
103
- end
104
- end
105
- end
106
- context "Real examples" do
107
- require_relative "../lib/texttube.rb"
108
- require 'rdiscount'
109
-
110
- context "An article that needs all the filters" do
111
- before :all do
112
- TextTube.load_all_filters
113
- class MyFilter < TextTube::Base
114
- register TextTube::Coderay
115
- register TextTube::LinkReffing
116
- register TextTube::EmbeddingAudio
117
- register TextTube::EmbeddingVideo
118
- register TextTube::InsideBlock
119
- register do
120
- filter_with :rdiscount do |text|
121
- RDiscount.new(text).to_html
122
- end
123
- end
124
- end
125
- end
126
- let(:expected) { <<HTML
127
- <h2>The Rainfall Problem</h2>
128
-
129
- <p>I read of a test given to students to see if they understand the concepts in the first part of a computer science course. It was in an interesting paper called <em>What Makes Code Hard to Understand?</em><a href="#0" title="Jump to reference">⁰</a>.</p>
130
-
131
- <blockquote><p>To solve it, students must write a program that averages a list of numbers (rainfall amounts), where the list is terminated with a specific value – e.g., a negative number or 999999.</p></blockquote>
132
-
133
- <p>Of course, reading about the problem I immediately wanted to try it, but even though the idea of posing the problem was to test the understanding of loops, the problem lends itself naturally to a recursive solution, and that's much more interesting than a boring old loop! I realised it wouldn't run well in Ruby but decided to write it anyway:</p>
134
-
135
- <pre><code class="CodeRay"><span class="keyword">def</span> <span class="function">rainfall</span>(droplets,total=<span class="integer">0</span>,measures=<span class="integer">1</span>,limit=<span class="integer">99_999</span>)
136
- <span class="keyword">return</span> total.to_f.div measures <span class="keyword">if</span> droplets == limit
137
- <span class="keyword">unless</span> droplets &lt; <span class="integer">0</span>
138
- total += droplets
139
- measures += <span class="integer">1</span>
140
- <span class="keyword">end</span>
141
- rainfall rand(limit + <span class="integer">1</span>), total, measures
142
- <span class="keyword">end</span></code></pre>
143
-
144
- <p>Normally I'd start this off with a random number, but just to make it clear that it works I started with the termination number. Then, I reran it until it worked. It gives you an idea of how poor Ruby is at recursion.</p>
145
-
146
- <pre><code class="CodeRay">rainfall 99_999
147
- # =&gt; 0
148
- rainfall 99_998
149
- SystemStackError: stack level too deep
150
- from /Users/iainuser/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/irb/workspace.rb:80
151
- Maybe IRB bug!
152
- rainfall 99_998
153
- SystemStackError: stack level too deep
154
- from /Users/iainuser/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/irb/workspace.rb:80
155
- Maybe IRB bug!
156
- rainfall 99_998
157
- SystemStackError: stack level too deep
158
- from /Users/iainuser/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/irb/workspace.rb:80
159
- Maybe IRB bug!
160
- rainfall 99_998
161
- SystemStackError: stack level too deep
162
- from /Users/iainuser/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/irb/workspace.rb:80
163
- Maybe IRB bug!
164
- rainfall 99_998
165
- # =&gt; 49693</code></pre>
166
-
167
- <p>Just to be clear, I ran this with Ruby 2.0.0-rc2 as well with the same results. It's a pity that Ruby fails at this kind of thing, because it is such an elegant and useful language that seems to have borrowed the best of many other niche languages. Recursion is often the most elegant <em>and</em> performant<a href="#1" title="Jump to reference">¹</a> solution.</p>
168
-
169
- <div id="reflinks">
170
- <p><a name="0"></a>[0] <a href="http://arxiv.org/abs/1304.5257" title="http://arxiv.org/abs/1304.5257">http://arxiv.org/abs/1304.5257</a> What Makes Code Hard to Understand? Michael Hansen, Robert L. Goldstone, Andrew Lumsdaine</p>
171
-
172
- <p><a name="1"></a>[1] <a href="http://dictionary.cambridge.org/dictionary/british/pedant" title="http://dictionary.cambridge.org/dictionary/british/pedant">http://dictionary.cambridge.org/dictionary/br...</a> If you're one of these then you won't like that I made a word up. Unfortunately for you, I'm British, which means that I understand that English is a living language, it is my tool not my master, and it has a long history of being changed to suit the speaker or writer. This is one such case.</p>
173
- </div>
174
-
175
- HTML
176
- }
177
- let(:my_f) { MyFilter.new content }
178
- subject { my_f.filter :embeddingvideo, :embeddingaudio, :linkreffing, :rdiscount, :coderay, :insideblock }
179
- it { should == expected }
180
-
181
- describe "With options" do
182
- context "Passed to the class" do
183
- before :all do
184
- MyFilter.options.merge! :linkreffing => {kind: :none}
185
- end
186
- subject { MyFilter.new(content).filter :embeddingvideo, :embeddingaudio, :linkreffing, :rdiscount, :coderay, :insideblock }
187
- it { should_not == expected }
188
-
189
- context "and passed to filter" do
190
- subject { MyFilter.new(content).filter :embeddingvideo, :embeddingaudio, :linkreffing, :rdiscount, :coderay, :insideblock, :linkreffing => {kind: :reference} }
191
- it { should == expected }
192
- end
193
- end
194
- end
195
- end
196
-
197
- context "An atom feed that only needs some of the filters" do
198
- let(:expected) { <<EXPECTED
199
- <h2>The Rainfall Problem</h2>
200
-
201
- <p>I read of a test given to students to see if they understand the concepts in the first part of a computer science course. It was in an interesting paper called <em>What Makes Code Hard to Understand?</em>.</p>
202
-
203
- <blockquote><p>To solve it, students must write a program that averages a list of numbers (rainfall amounts), where the list is terminated with a specific value – e.g., a negative number or 999999.</p></blockquote>
204
-
205
- <p>Of course, reading about the problem I immediately wanted to try it, but even though the idea of posing the problem was to test the understanding of loops, the problem lends itself naturally to a recursive solution, and that's much more interesting than a boring old loop! I realised it wouldn't run well in Ruby but decided to write it anyway:</p>
206
-
207
- <pre><code>::::ruby
208
- def rainfall(droplets,total=0,measures=1,limit=99_999)
209
- return total.to_f.div measures if droplets == limit
210
- unless droplets &lt; 0
211
- total += droplets
212
- measures += 1
213
- end
214
- rainfall rand(limit + 1), total, measures
215
- end
216
- </code></pre>
217
-
218
- <p>Normally I'd start this off with a random number, but just to make it clear that it works I started with the termination number. Then, I reran it until it worked. It gives you an idea of how poor Ruby is at recursion.</p>
219
-
220
- <pre><code>::::shell
221
- rainfall 99_999
222
- # =&gt; 0
223
- rainfall 99_998
224
- SystemStackError: stack level too deep
225
- from /Users/iainuser/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/irb/workspace.rb:80
226
- Maybe IRB bug!
227
- rainfall 99_998
228
- SystemStackError: stack level too deep
229
- from /Users/iainuser/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/irb/workspace.rb:80
230
- Maybe IRB bug!
231
- rainfall 99_998
232
- SystemStackError: stack level too deep
233
- from /Users/iainuser/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/irb/workspace.rb:80
234
- Maybe IRB bug!
235
- rainfall 99_998
236
- SystemStackError: stack level too deep
237
- from /Users/iainuser/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/irb/workspace.rb:80
238
- Maybe IRB bug!
239
- rainfall 99_998
240
- # =&gt; 49693
241
- </code></pre>
242
-
243
- <p>Just to be clear, I ran this with Ruby 2.0.0-rc2 as well with the same results. It's a pity that Ruby fails at this kind of thing, because it is such an elegant and useful language that seems to have borrowed the best of many other niche languages. Recursion is often the most elegant <em>and</em> performant solution.</p>
244
- EXPECTED
245
- }
246
- before :all do
247
- TextTube.load_all_filters
248
- class MyFilter < TextTube::Base
249
- register TextTube::LinkReffing
250
- register TextTube::EmbeddingAudio
251
- register TextTube::EmbeddingVideo
252
- register do
253
- filter_with :rdiscount do |text|
254
- RDiscount.new(text).to_html
255
- end
256
- end
257
- end
258
- end
259
- describe "With options" do
260
- context "passed to filter" do
261
- subject { MyFilter.new(content).filter :embeddingvideo, :embeddingaudio, :linkreffing, :rdiscount, :linkreffing => {kind: :none} }
262
- it { should == expected }
263
- end
264
- context "set on the instance" do
265
- subject { MyFilter.new(content, :linkreffing => {kind: :none}).filter :embeddingvideo, :embeddingaudio, :linkreffing, :rdiscount }
266
- it { should == expected }
267
- end
268
- context "set on the class" do
269
- before :all do
270
- MyFilter.options.merge! :linkreffing => {kind: :none}
271
- end
272
-
273
- subject {
274
- MyFilter.new(content).filter :embeddingvideo, :embeddingaudio, :linkreffing, :rdiscount }
275
- it { should == expected }
276
- end
277
- end
278
- end
279
- end
280
- end
@@ -1,45 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
- require_relative '../lib/texttube/base.rb'
5
- require 'texttube/filters/spiffing'
6
-
7
- describe "TextTube" do
8
- let(:content) { <<CSS
9
- body {
10
- background-colour: darkgrey;
11
- background-photograph: url(logo.gif);
12
- transparency: .7;
13
-
14
- font: 72px "Comic Sans", cursive !please;
15
- font-weight: plump;
16
- p { text-align: centre }
17
- fieldset input {
18
- text-transform: capitalise;
19
- }
20
- }
21
- CSS
22
- }
23
-
24
- let(:expected) { <<EXPECTED
25
- body {
26
- background-color: darkgray;
27
- background-image: url(logo.gif);
28
- opacity: .7;
29
-
30
- font: 72px "Comic Sans", cursive !important;
31
- font-weight: bold;
32
- p { text-align: center }
33
- fieldset input {
34
- text-transform: capitalize;
35
- }
36
- }
37
- EXPECTED
38
- }
39
- class CssString < TextTube::Base
40
- register TextTube::Spiffing
41
- end
42
-
43
- subject { CssString.new(content).filter }
44
- it { should == expected }
45
- end