-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_choppa.rb
148 lines (121 loc) · 4.62 KB
/
test_choppa.rb
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
# coding: utf-8
require "test/unit"
require "minitest/spec"
require "./choppa"
describe ChoppaProcessor do
it 'does nothing with an empty OPML' do
processor = ChoppaProcessor.new(build_opml)
processor.process!
assert_equal(0, processor.doc.xpath('/opml/body/*').count)
end
it 'sorts two daily feeds into groups for every day of the week' do
processor = ChoppaProcessor.new(
build_opml('[daily]' => %w(Tesugen BLDGBLOG)))
processor.process!
doc = processor.doc
assert_equal(1, doc.xpath('/opml/body/outline[@text="[daily]"]').count)
days = (1..7).zip(%w(Monday Tuesday Wednesday Thursday Friday Saturday
Sunday)).map {|x| x * ' '}
days.each do |day|
assert_equal(1, doc.xpath("/opml/body/outline[@text='#{day}']").count)
assert_equal(1, doc.xpath(
"//*[@text='#{day}']/outline[@text='Tesugen']").count)
assert_equal(1, doc.xpath(
"//*[@text='#{day}']/outline[@text='BLDGBLOG']").count)
end
end
it 'adds a twice weekly feed to groups for Monday and Thursday' do
processor = ChoppaProcessor.new(
build_opml('[twice weekly]' => %w(Lifehacker)))
processor.process!
doc = processor.doc
assert_equal(1, doc.xpath(
'/opml/body/outline[@text="[twice weekly]"]').count)
['1 Monday', '4 Thursday'].each do |day|
assert_equal(1, doc.xpath("/opml/body/outline[@text='#{day}']").count)
assert_equal(1, doc.xpath(
"//*[@text='#{day}']/outline[@text='Lifehacker']").count)
end
end
it 'adds a bunch of twice weekly feeds to groups for different days' do
processor = ChoppaProcessor.new(
build_opml('[twice weekly]' => %w(Selby ISO50 Waxy Dezeen TABlog)))
processor.process!
doc = processor.doc
['1 Monday', '4 Thursday'].each do |day|
assert_equal(1, doc.xpath(
"//*[@text='#{day}']/outline[@text='Selby']").count)
end
['2 Tuesday', '5 Friday'].each do |day|
assert_equal(1, doc.xpath(
"//*[@text='#{day}']/outline[@text='ISO50']").count)
end
['5 Friday', '1 Monday'].each do |day|
assert_equal(1, doc.xpath(
"//*[@text='#{day}']/outline[@text='TABlog']").count)
end
end
it 'adds every-other-day feeds to groups for different days' do
processor = ChoppaProcessor.new(
build_opml('[every other day]' => %w(Mavenist Thoughtful)))
processor.process!
doc = processor.doc
['1 Monday', '3 Wednesday', '5 Friday', '7 Sunday'].each do |day|
assert_equal(1, doc.xpath(
"//*[@text='#{day}']/outline[@text='Mavenist']").count)
end
['2 Tuesday', '4 Thursday', '6 Saturday', '1 Monday'].each do |day|
assert_equal(1, doc.xpath(
"//*[@text='#{day}']/outline[@text='Thoughtful']").count)
end
end
it 'discards day groups from previous runs' do
processor = ChoppaProcessor.new(
build_opml('[twice weekly]' => %w(Mymarkup), '2 Tuesday' => %w(Mymarkup),
'[every other day]' => %w(POKE), '5 Friday' => %w(POKE)))
processor.process!
doc = processor.doc
assert_equal(0, doc.xpath("//*[@text='2 Tuesday']/outline[@text='Mymarkup']").count)
assert_equal(0, doc.xpath("//*[@text='5 Friday']/outline[@text='POKE']").count)
['1 Monday', '4 Thursday'].each do |day|
assert_equal(1, doc.xpath(
"//*[@text='#{day}']/outline[@text='Mymarkup']").count)
end
['2 Tuesday', '4 Thursday', '6 Saturday', '1 Monday'].each do |day|
assert_equal(1, doc.xpath(
"//*[@text='#{day}']/outline[@text='POKE']").count)
end
end
it 'preserves day groups for feeds not in twice daily or every other day' do
processor = ChoppaProcessor.new(
build_opml('2 Tuesday' => %w(Mymarkup), '5 Friday' => %w(Mymarkup)))
processor.process!
doc = processor.doc
['2 Tuesday', '5 Friday'].each do |day|
assert_equal(1, doc.xpath("/opml/body/outline[@text='#{day}']").count)
assert_equal(1, doc.xpath(
"//*[@text='#{day}']/outline[@text='Mymarkup']").count)
end
end
private
def build_opml(feeds = {})
Nokogiri::XML::Builder.new do |xml|
xml.opml {
xml.head {
xml.title "Choppa them Google Reader feeds"
}
xml.body {
feeds.each do |group, feed_names|
xml.outline(:text => group, :title => group) {
feed_names.each do |feed|
xml.outline :text => feed, :title => feed, :type => 'rss',
:'htmlUrl' => "http://#{feed.downcase}.com/",
:'xmlUrl' => "http://#{feed.downcase}.com/feed"
end
}
end
}
}
end.doc
end
end