mangdown 0.14.0 → 0.14.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,117 @@
1
+ require "test_helper"
2
+
3
+ module Mangdown
4
+ class AdapterTest < Minitest::Test
5
+ def bogus_adapter(&block)
6
+ Class.new(Mangdown::Adapter::Base, &block)
7
+ end
8
+
9
+ def test_adapter
10
+ TestAdapter.new(:uri, :doc, :name)
11
+ end
12
+
13
+ def test_public_api
14
+ class_methods = %w{ for? site }
15
+ instance_methods = %w{ uri name doc site is_manga_list? is_manga? is_chapter? is_page? manga_list manga chapter_list chapter page_list page }
16
+
17
+ adapter = bogus_adapter
18
+ class_methods.each do |method|
19
+ assert adapter.respond_to?(method, false)
20
+ end
21
+
22
+ instance = adapter.new(:uri, :doc, :name)
23
+ instance_methods.each do |method|
24
+ assert instance.respond_to?(method, false)
25
+ end
26
+ end
27
+
28
+ def test_set_site
29
+ adapter = bogus_adapter do
30
+ site "test site"
31
+ end
32
+
33
+ assert_equal "test site", adapter.site
34
+ end
35
+
36
+ # Test the test adapter?
37
+ def test_for_accepts_anything
38
+ assert TestAdapter.for?(:anything)
39
+ end
40
+
41
+ def test_methods_that_always_respond_true
42
+ adapter = test_adapter
43
+
44
+ assert adapter.is_manga_list?
45
+ assert adapter.is_manga?
46
+ assert adapter.is_chapter?
47
+ assert adapter.is_page?
48
+
49
+ assert adapter.is_manga_list?(:anything)
50
+ assert adapter.is_manga?(:anything)
51
+ assert adapter.is_chapter?(:anything)
52
+ assert adapter.is_page?(:anything)
53
+ end
54
+
55
+ def test_manga_list_returns_array_of_hashes
56
+ adapter = test_adapter
57
+
58
+ list = adapter.manga_list
59
+
60
+ assert_respond_to list, :to_ary
61
+ assert_respond_to list.first, :to_hash
62
+ assert_equal [:uri, :name, :site].sort, list.first.keys.sort
63
+ end
64
+
65
+ def test_manga_returns_a_hash
66
+ adapter = test_adapter
67
+
68
+ manga = adapter.manga
69
+
70
+ assert_respond_to manga, :to_hash
71
+ assert_equal [:uri, :name, :site].sort, manga.keys.sort
72
+ end
73
+
74
+ def test_chapter_list_returns_array_of_hashes
75
+ adapter = test_adapter
76
+
77
+ list = adapter.chapter_list
78
+
79
+ assert_respond_to list, :to_ary
80
+ assert_respond_to list.first, :to_hash
81
+ assert_equal [:uri, :name, :site].sort, list.first.keys.sort
82
+ end
83
+
84
+ def test_chapter_returns_a_hash
85
+ adapter = test_adapter
86
+
87
+ chapter = adapter.chapter
88
+ keys = [:uri, :name, :site, :manga, :chapter].sort
89
+
90
+ assert_respond_to chapter, :to_hash
91
+ assert_equal keys, chapter.keys.sort
92
+ end
93
+
94
+ def test_page_list_returns_array_of_hashes
95
+ adapter = test_adapter
96
+
97
+ list = adapter.page_list
98
+
99
+ assert_respond_to list, :to_ary
100
+ assert_respond_to list.first, :to_hash
101
+ assert_equal [:uri, :name, :site].sort, list.first.keys.sort
102
+ end
103
+
104
+ def test_page_returns_a_hash
105
+ adapter = test_adapter
106
+
107
+ page = adapter.page
108
+
109
+ assert_respond_to page, :to_hash
110
+ assert_equal [:uri, :name, :site].sort, page.keys.sort
111
+ end
112
+
113
+ def test_doc
114
+ assert_equal :doc, test_adapter.doc
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,115 @@
1
+ require 'stringio'
2
+ require 'test_helper'
3
+
4
+ module Mangdown
5
+ class ChapterTest < Minitest::Test
6
+ def test_adapter
7
+ TestAdapter.new(:uri, :doc, :name)
8
+ end
9
+
10
+ def setup
11
+ @chapter = Chapter.new('uri', 'name', 'manga', 1)
12
+ @chapter.adapter = test_adapter
13
+ end
14
+
15
+ def test_new
16
+ @chapter.adapter = 'adapter'
17
+
18
+ assert_equal 'uri', @chapter.uri
19
+ assert_equal 'name', @chapter.name
20
+ assert_equal [], @chapter.pages
21
+ assert_equal 1, @chapter.chapter
22
+ assert_equal 'adapter', @chapter.adapter
23
+ end
24
+
25
+ def test_inspect_differs_from_object_inspect
26
+ unbound_inspect = Object.instance_method(:inspect)
27
+
28
+ refute_equal unbound_inspect.bind(@chapter).call(), @chapter.inspect
29
+ end
30
+
31
+ def test_to_s_differs_from_object_to_s
32
+ unbound_to_s = Object.instance_method(:to_s)
33
+
34
+ refute_equal unbound_to_s.bind(@chapter).call(), @chapter.to_s
35
+ end
36
+
37
+ def test_cbz
38
+ CBZ.class_eval do
39
+ class << self
40
+ alias old_one one
41
+ def one(path)
42
+ path
43
+ end
44
+ end
45
+ end
46
+
47
+ @chapter.instance_variable_set(:@path, 'path')
48
+
49
+ assert_equal 'path', @chapter.cbz
50
+
51
+ CBZ.class_eval do
52
+ class << self
53
+ alias one old_one
54
+ end
55
+ end
56
+ end
57
+
58
+ def test_to_chapter
59
+ assert_equal @chapter, @chapter.to_chapter
60
+ end
61
+
62
+ def test_to_path
63
+ assert_equal(
64
+ "#{Mangdown::DOWNLOAD_DIR}/manga/name", @chapter.to_path.to_s
65
+ )
66
+ end
67
+
68
+ def test_set_path
69
+ @chapter.set_path(Dir.pwd)
70
+ assert_equal "#{Dir.pwd}/name", @chapter.to_path.to_s
71
+ end
72
+
73
+ def test_load_pages
74
+ Tools.class_eval do
75
+ class << self
76
+ alias old_hydra_streaming hydra_streaming
77
+
78
+ def hydra_streaming(objects)
79
+ objects.map do |obj|
80
+ next unless yield(:before, obj)
81
+ yield(:succeeded, obj)
82
+ yield(:body, obj, 'data')
83
+ yield(:complete, obj)
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ @chapter.load_pages
90
+
91
+ refute_empty @chapter.pages
92
+
93
+ Tools.class_eval do
94
+ class << self
95
+ alias hydra_streaming old_hydra_streaming
96
+ end
97
+ end
98
+ end
99
+
100
+ def test_each
101
+ @chapter.pages.concat(Array.new(10, {}))
102
+
103
+ assert_equal @chapter.pages.length, @chapter.each.count
104
+
105
+ @chapter.each.with_index do |page, i|
106
+ assert_equal @chapter.pages[i].object_id, page.object_id
107
+ end
108
+ end
109
+
110
+ def test_download_to
111
+ skip
112
+ end
113
+ end
114
+ end
115
+
@@ -0,0 +1,50 @@
1
+ require "test_helper"
2
+
3
+ module Mangdown
4
+ class MangaListTest < Minitest::Test
5
+ def setup
6
+ hashes = Array.new(5, { uri: '' })
7
+ @list = MangaList.new(hashes)
8
+ end
9
+
10
+ def test_manga_list_manga
11
+ assert_equal 5, @list.manga.length
12
+ assert @list.manga.all? { |manga| manga.is_a?(MDHash) }
13
+ end
14
+
15
+ def test_each
16
+ count = 0
17
+ @list.manga.each do |manga|
18
+ count += 1
19
+ assert manga.is_a?(MDHash)
20
+ end
21
+ end
22
+
23
+ def test_to_yaml
24
+ yaml = @list.to_yaml
25
+ hashes = YAML.load(yaml)
26
+
27
+ assert_equal 5, hashes.length
28
+ assert hashes.all? { |hash| hash.is_a?(Hash) }
29
+ end
30
+
31
+ def test_load_manga
32
+ count = @list.manga.length
33
+ @list.load_manga('uri')
34
+
35
+ assert @list.manga.length > count
36
+ assert @list.manga.all? { |manga| manga.is_a?(MDHash) }
37
+ end
38
+
39
+ def test_array_conversions
40
+ assert_equal @list.to_a, @list.to_ary
41
+ assert_equal @list.manga, @list.to_a
42
+ end
43
+
44
+ def test_merge
45
+ assert_equal @list.to_a.length * 2, @list.merge(@list).to_a.length
46
+ assert_instance_of MangaList, @list.merge(MangaList.new)
47
+ assert @list.manga.all? { |manga| manga.is_a?(MDHash) }
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,161 @@
1
+ require 'stringio'
2
+ require 'test_helper'
3
+
4
+ module Mangdown
5
+ class MangaTest < Minitest::Test
6
+ def test_adapter
7
+ TestAdapter.new(:uri, :doc, :name)
8
+ end
9
+
10
+ def test_new
11
+ manga = Manga.new('uri', 'name')
12
+ manga.adapter = 'adapter'
13
+
14
+ assert_equal 'uri', manga.uri
15
+ assert_equal 'name', manga.name
16
+ assert_equal [], manga.chapters
17
+ assert_equal 'adapter', manga.adapter
18
+ end
19
+
20
+ def test_inspect_differs_from_object_inspect
21
+ unbound_inspect = Object.instance_method(:inspect)
22
+ manga = Manga.new('uri', 'name')
23
+
24
+ refute_equal unbound_inspect.bind(manga).call(), manga.inspect
25
+ end
26
+
27
+ def test_to_s_differs_from_object_to_s
28
+ unbound_to_s = Object.instance_method(:to_s)
29
+ manga = Manga.new('uri', 'name')
30
+
31
+ refute_equal unbound_to_s.bind(manga).call(), manga.to_s
32
+ end
33
+
34
+ def test_cbz
35
+ CBZ.class_eval do
36
+ class << self
37
+ alias old_all all
38
+ def all(path)
39
+ path
40
+ end
41
+ end
42
+ end
43
+
44
+ manga = Manga.new('uri', 'name')
45
+ manga.instance_variable_set(:@path, 'path')
46
+
47
+ assert_equal 'path', manga.cbz
48
+
49
+ CBZ.class_eval do
50
+ class << self
51
+ alias all old_all
52
+ end
53
+ end
54
+ end
55
+
56
+ def test_to_manga
57
+ manga = Manga.new('uri', 'name')
58
+
59
+ assert_equal manga, manga.to_manga
60
+ end
61
+
62
+ def test_to_path
63
+ manga = Manga.new('uri', 'name')
64
+
65
+ assert_equal "#{Mangdown::DOWNLOAD_DIR}/name", manga.to_path.to_s
66
+ end
67
+
68
+ def test_set_path
69
+ manga = Manga.new('uri', 'name')
70
+
71
+ manga.set_path(Dir.pwd)
72
+ assert_equal "#{Dir.pwd}/name", manga.to_path.to_s
73
+ end
74
+
75
+ def test_load_chapters
76
+ manga = Manga.new('uri', 'name')
77
+ manga.adapter = test_adapter
78
+ manga.load_chapters
79
+
80
+ refute_empty manga.chapters
81
+ end
82
+
83
+ def test_each
84
+ manga = Manga.new('uri', 'name')
85
+ manga.chapters.concat(Array.new(10, {}))
86
+
87
+ assert_equal manga.chapters.length, manga.each.count
88
+
89
+ manga.each.with_index do |chapter, i|
90
+ assert_equal manga.chapters[i].object_id, chapter.object_id
91
+ end
92
+ end
93
+
94
+ def test_download_passes_args_to_download_to_with_nil_dir
95
+ Manga.class_eval do
96
+ alias old_download_to download_to
97
+
98
+ def download_to(*args)
99
+ args
100
+ end
101
+ end
102
+
103
+ manga = Manga.new('uri', 'name')
104
+ assert_equal [nil, 1, 2, 3], manga.download(1, 2, 3)
105
+
106
+ Manga.class_eval do
107
+ alias download_to old_download_to
108
+ end
109
+ end
110
+
111
+ def test_download_to
112
+ Chapter.class_eval do
113
+ @@counter = 0
114
+
115
+ alias old_download_to download_to
116
+
117
+ def download_to(*args)
118
+ @@counter += 1
119
+
120
+ if @@counter % 3 == 1
121
+ { failed: [], succeeded: [:some], skipped: [] }
122
+ elsif @@counter % 3 == 2
123
+ { failed: [], succeeded: [], skipped: [:some] }
124
+ else
125
+ { failed: [:some], succeeded: [], skipped: [] }
126
+ end
127
+ end
128
+
129
+ alias old_fetch_each_page fetch_each_page
130
+
131
+ def fetch_each_page(*)
132
+ end
133
+ end
134
+
135
+ io = StringIO.new
136
+ Mangdown.configure_logger(file: io)
137
+
138
+ manga = Manga.new('uri', 'name')
139
+ manga.adapter = test_adapter
140
+ manga.load_chapters
141
+
142
+ result = manga.download_to(Dir.pwd)
143
+
144
+ assert_equal 5, manga.count
145
+ assert_equal 5, result.values.flatten.length
146
+ assert_equal 2, result[:succeeded].length
147
+ assert_equal 2, result[:skipped].length
148
+ assert_equal 1, result[:failed].length
149
+
150
+ assert_equal 1, io.string.scan(/ERROR/).length
151
+
152
+ Mangdown.configure_logger(file: STDOUT)
153
+
154
+ Chapter.class_eval do
155
+ alias download_to old_download_to
156
+ alias fetch_each_page old_fetch_each_page
157
+ end
158
+ end
159
+ end
160
+ end
161
+
@@ -0,0 +1,70 @@
1
+ require 'test_helper'
2
+ require 'fileutils'
3
+
4
+ module Mangdown
5
+ class CBZTest < Minitest::Test
6
+ def setup
7
+ @temp = Pathname.new(__dir__).join('tmp')
8
+ @root_dir = @temp.join('root 1')
9
+ @sub_dir_1 = @root_dir.join('sub_dir 1')
10
+ @sub_dir_2 = @root_dir.join('sub_dir 2')
11
+
12
+ Dir.mkdir(@temp)
13
+ Dir.mkdir(@root_dir)
14
+ Dir.mkdir(@sub_dir_1)
15
+ Dir.mkdir(@sub_dir_2)
16
+
17
+ (1..9).each do |i|
18
+ filename = "file #{i}.ext"
19
+ FileUtils.touch(@sub_dir_1.join(filename))
20
+ FileUtils.touch(@sub_dir_2.join(filename))
21
+ end
22
+ end
23
+
24
+ def teardown
25
+ FileUtils.rm_r(@temp)
26
+ end
27
+
28
+ def test_all
29
+ CBZ.all(@root_dir)
30
+
31
+ refute File.exist?(@root_dir)
32
+
33
+ new_root_dir = @temp.join('root 00001')
34
+
35
+ list = Dir.new(new_root_dir).entries
36
+ assert_includes list, 'sub_dir 00001'
37
+ assert_includes list, 'sub_dir 00002'
38
+ assert_includes list, 'sub_dir 00001.cbz'
39
+ assert_includes list, 'sub_dir 00002.cbz'
40
+
41
+ count = 0
42
+ Zip::File.open(new_root_dir.join('sub_dir 00001.cbz')) do |zip|
43
+ count += zip.size
44
+ end
45
+
46
+ assert_equal 9, count
47
+ end
48
+
49
+ def test_one
50
+ CBZ.one(@sub_dir_1)
51
+
52
+ list = Dir.new(@root_dir).entries
53
+ refute File.exist?(@sub_dir_1), list.inspect
54
+ assert File.exist?(@sub_dir_2), 'Should only validate the correct dir'
55
+
56
+ assert_includes list, 'sub_dir 00001'
57
+ assert_includes list, 'sub_dir 00001.cbz'
58
+ refute_includes(
59
+ list, 'sub_dir 00002.cbz', 'Should only zip the correct dir'
60
+ )
61
+
62
+ count = 0
63
+ Zip::File.open(@root_dir.join('sub_dir 00001.cbz')) do |zip|
64
+ count += zip.size
65
+ end
66
+
67
+ assert_equal 9, count
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ module Mangdown
4
+ class EqualityTest < Minitest::Test
5
+ def test_spaceship_operator
6
+ name_struct = Struct.new(:name) do
7
+ include Equality
8
+ end
9
+
10
+ assert_equal name_struct.new('Name'), name_struct.new('Name')
11
+ refute_equal name_struct.new('Name'), name_struct.new('Other Name')
12
+ assert name_struct.new('Name') < name_struct.new('Other Name')
13
+ assert name_struct.new('Other Name') > name_struct.new('Name')
14
+
15
+ assert_equal 0, name_struct.new('Name') <=> name_struct.new('Name')
16
+
17
+ names = %w(a b c d e f).map { |name| name_struct.new(name) }
18
+
19
+ assert_equal names, names.shuffle.sort
20
+ assert_equal names.reverse, names.shuffle.sort.reverse
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,48 @@
1
+ require 'stringio'
2
+ require 'test_helper'
3
+
4
+ module Mangdown
5
+ class LoggingTest < Minitest::Test
6
+
7
+ class LoggingEnabled
8
+ include Logging
9
+
10
+ def log(message, level)
11
+ logger.__send__(level, message)
12
+ end
13
+
14
+ def public_logger
15
+ logger
16
+ end
17
+
18
+ def device
19
+ logger.instance_variable_get(:@logdev).dev
20
+ end
21
+ end
22
+
23
+ def setup
24
+ @io = StringIO.new
25
+ Mangdown.configure_logger(file: @io)
26
+
27
+ @instance = LoggingEnabled.new
28
+ end
29
+
30
+ def teardown
31
+ Mangdown.configure_logger(file: STDOUT)
32
+ end
33
+
34
+ def test_logger
35
+ @instance.log('hello world', :debug)
36
+ @instance.log('hello world', :fatal)
37
+
38
+ assert_kind_of Logger, @instance.public_logger
39
+ assert_equal 1, @io.string.scan(/DEBUG/).length
40
+ assert_equal 1, @io.string.scan(/FATAL/).length
41
+ assert_equal 2, @io.string.scan(/hello world/).length
42
+ end
43
+
44
+ def test_configure_logger
45
+ assert_equal @io, @instance.device
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,59 @@
1
+ require 'test_helper'
2
+
3
+ module Mangdown
4
+ class PropertiesTest < Minitest::Test
5
+ class SomeObject
6
+ include Properties
7
+
8
+ properties :property_a, :property_b
9
+ end
10
+
11
+ def setup
12
+ @object = SomeObject.new
13
+ end
14
+
15
+ def test_properties
16
+ assert_equal({ property_a: nil, property_b: nil }, @object.properties)
17
+ end
18
+
19
+ def test_getters_and_setters
20
+ @object.property_a = 'Property A'
21
+
22
+ assert_equal 'Property A', @object.property_a
23
+
24
+ assert_equal 'Property A', @object.properties[:property_a]
25
+ end
26
+
27
+ def test_delegates_methods_to_properties
28
+ assert_equal @object.properties.inspect, @object.inspect
29
+ assert_equal @object.properties.to_s, @object.to_s
30
+ end
31
+
32
+ def test_hash_access
33
+ @object.property_a = 'Property A'
34
+
35
+ assert_equal 'Property A', @object[:property_a]
36
+ end
37
+
38
+ def test_to_hash_conversion
39
+ assert_equal @object.properties, @object.to_hash
40
+ end
41
+
42
+ def test_assign_property_value_as_instance_variable
43
+ @object.instance_variable_set(:@property_b, 'Property B')
44
+
45
+ assert_equal 'Property B', @object[:property_b]
46
+ end
47
+
48
+ def test_fill_properties
49
+ @object.property_a = 'Property A'
50
+
51
+ @object.fill_properties(
52
+ property_a: 'Other Value', property_b: 'Property B'
53
+ )
54
+
55
+ assert_equal 'Property A', @object.property_a
56
+ assert_equal 'Property B', @object.property_b
57
+ end
58
+ end
59
+ end