]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - test/youtube_test.rb
233f31bafadd7e9d71918a94ac70f0547c457589
[dead/whatever-dl.git] / test / youtube_test.rb
1 #
2 # Copyright Michael Orlitzky
3 #
4 # http://michael.orlitzky.com/
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # http://www.fsf.org/licensing/licenses/gpl.html
17 #
18
19 require 'test/unit'
20 require 'src/websites/youtube'
21
22 class YoutubeTest < Test::Unit::TestCase
23
24 def test_owns_youtube_urls
25 assert(Youtube.owns_url?('http://www.youtube.com/watch?v=SudixyugiX4'))
26 assert(Youtube.owns_url?('http://www.youtube.com/watch?v=SudixyugiX4&hl=en'))
27 assert(Youtube.owns_url?('http://youtube.com/watch?v=SudixyugiX4&hl=en'))
28 assert(Youtube.owns_url?('http://www.youtube.com/v/SudixyugiX4'))
29 assert(Youtube.owns_url?('http://www.youtube.com/v/SudixyugiX4&hl=en'))
30 assert(Youtube.owns_url?('http://youtube.com/v/SudixyugiX4&hl=en'))
31 assert(Youtube.owns_url?('http://www.youtube.com/watch?v=K9iDMcmm0tE'))
32 assert(Youtube.owns_url?('http://www.youtube.com/watch?v=K9iDMcmm0tE#'))
33 assert(Youtube.owns_url?('http://www.youtube.com/v/K9iDMcmm0tE'))
34 assert(Youtube.owns_url?('http://www.youtube.com/watch?v=83-hlYMH1XE'))
35 assert(Youtube.owns_url?('http://www.youtube.com/watch?v=83-hlYMH1XE&feature=dir'))
36 assert(Youtube.owns_url?('http://in.youtube.com/watch?v=VcydqSpYN00&feature=channel_page'))
37 assert(Youtube.owns_url?('http://uk.youtube.com/watch?v=LN4Ov6ZLcrI'))
38 assert(Youtube.owns_url?('http://www.youtube.com/meetlocalbiz#p/u/0/rJVWV4aA6Jk'))
39 end
40
41
42 def test_doesnt_own_redtube_urls
43 assert(!Youtube.owns_url?('http://www.redtube.com/6807'))
44 assert(!Youtube.owns_url?('www.redtube.com/6807'))
45 assert(!Youtube.owns_url?('http://redtube.com/6807'))
46 assert(!Youtube.owns_url?('redtube.com/6807'))
47 end
48
49
50 def test_parse_video_id
51 yt = Youtube.new('http://www.youtube.com/watch?v=SudixyugiX4')
52 expected_result = 'SudixyugiX4'
53 actual_result = yt.send('parse_video_id')
54 assert_equal(expected_result, actual_result)
55 end
56
57
58 def test_parse_video_id_again
59 yt = Youtube.new('http://www.youtube.com/watch?v=K9iDMcmm0tE')
60 expected_result = 'K9iDMcmm0tE'
61 actual_result = yt.send('parse_video_id')
62 assert_equal(expected_result, actual_result)
63 end
64
65
66 def test_get_available_formats
67 # Make sure that we can parse the available formats from a page.
68 yt = Youtube.new(nil)
69
70 page_data = nil
71
72 File.open('test/fixtures/youtube/BUer8Dv2HW8.html') do |f|
73 page_data = f.read
74 end
75
76 actual_formats = yt.send('get_available_formats', page_data)
77 expected_formats = [ 37, 22, 35, 18, 34, 5 ]
78
79 # Make sure that all of the elements of the "expected" set are in
80 # the "actual" set and vice-versa.
81 assert_equal([], expected_formats - actual_formats)
82 assert_equal([], actual_formats - expected_formats)
83 end
84
85
86 def test_get_available_formats_from_url
87 # Tests a different regex than the other available formats test.
88 yt = Youtube.new(nil)
89
90 page_data = nil
91
92 File.open('test/fixtures/youtube/cpU27Xp8zdM.html') do |f|
93 page_data = f.read
94 end
95
96 actual_formats = yt.send('get_available_formats', page_data)
97 expected_formats = [ 34, 5 ]
98
99 # Make sure that all of the elements of the "expected" set are in
100 # the "actual" set and vice-versa.
101 assert_equal([], expected_formats - actual_formats)
102 assert_equal([], actual_formats - expected_formats)
103 end
104
105
106 def test_get_desired_format
107 # The get_desired_format() method should choose the best of the
108 # available formats.
109 yt = Youtube.new(nil)
110
111 page_data = nil
112
113 File.open('test/fixtures/youtube/BUer8Dv2HW8.html') do |f|
114 page_data = f.read
115 end
116
117 available_formats = yt.send('get_available_formats', page_data)
118 actual_result = yt.send('get_desired_format', available_formats)
119 expected_result = 37
120
121 assert_equal(expected_result, actual_result)
122 end
123
124
125 def test_get_video_filename
126 yt = Youtube.new('http://www.youtube.com/watch?v=SudixyugiX4')
127 expected_result = 'SudixyugiX4.flv'
128 actual_result = yt.get_video_filename()
129 assert_equal(expected_result, actual_result)
130 end
131
132
133 def test_get_video_filename_again
134 yt = Youtube.new('http://www.youtube.com/watch?v=K9iDMcmm0tE')
135 expected_result = 'K9iDMcmm0tE.flv'
136 actual_result = yt.get_video_filename()
137 assert_equal(expected_result, actual_result)
138 end
139
140
141 def test_get_troublesome_video_filename_first_form
142 # This non-alphanumeric video id was causing
143 # get_video_filename to barf.
144 yt = Youtube.new('http://www.youtube.com/watch?v=83-hlYMH1XE&feature=dir')
145 expected_result = '83-hlYMH1XE.flv'
146 actual_result = yt.get_video_filename()
147 assert_equal(expected_result, actual_result)
148 end
149
150
151 def test_get_troublesome_video_filename_second_form
152 yt = Youtube.new('http://www.youtube.com/v/83-hlYMH1XE&feature=dir')
153 expected_result = '83-hlYMH1XE.flv'
154 actual_result = yt.get_video_filename()
155 assert_equal(expected_result, actual_result)
156 end
157
158 end