codeless_code 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +1 -1
  3. data/.reek.yml +14 -0
  4. data/.rubocop.yml +18 -0
  5. data/.ruby-version +1 -0
  6. data/{test/test_codeless_code.rb → .travis.yml} +12 -10
  7. data/Gemfile +5 -0
  8. data/Gemfile.lock +21 -0
  9. data/Guardfile +13 -1
  10. data/README.md +6 -0
  11. data/Rakefile +26 -11
  12. data/VERSION +1 -1
  13. data/bin/codeless_code +3 -1
  14. data/codeless_code.gemspec +33 -12
  15. data/lib/codeless_code/catalog.rb +3 -1
  16. data/lib/codeless_code/cli.rb +56 -24
  17. data/lib/codeless_code/commands/filter_fables.rb +15 -10
  18. data/lib/codeless_code/commands/list_translations.rb +3 -0
  19. data/lib/codeless_code/fable.rb +42 -53
  20. data/lib/codeless_code/fable_set.rb +8 -6
  21. data/lib/codeless_code/filters/builders.rb +4 -2
  22. data/lib/codeless_code/filters/composite.rb +3 -1
  23. data/lib/codeless_code/filters/date.rb +40 -30
  24. data/lib/codeless_code/filters/from_options.rb +45 -28
  25. data/lib/codeless_code/filters/headers/base.rb +61 -0
  26. data/lib/codeless_code/filters/{header_integer.rb → headers/integer.rb} +16 -19
  27. data/lib/codeless_code/filters/{header_string.rb → headers/string.rb} +12 -22
  28. data/lib/codeless_code/filters/lang.rb +3 -0
  29. data/lib/codeless_code/filters/translator.rb +7 -2
  30. data/lib/codeless_code/filters.rb +16 -8
  31. data/lib/codeless_code/formats/base.rb +20 -7
  32. data/lib/codeless_code/formats/parsers/base.rb +106 -0
  33. data/lib/codeless_code/formats/parsers/plain.rb +49 -0
  34. data/lib/codeless_code/formats/parsers/term.rb +79 -0
  35. data/lib/codeless_code/formats/plain.rb +21 -12
  36. data/lib/codeless_code/formats/raw.rb +2 -0
  37. data/lib/codeless_code/formats/term.rb +21 -15
  38. data/lib/codeless_code/language_set.rb +7 -4
  39. data/lib/codeless_code/options.rb +5 -3
  40. data/lib/codeless_code/renderers/fable.rb +13 -8
  41. data/lib/codeless_code/renderers/term_page.rb +46 -31
  42. data/lib/codeless_code.rb +119 -106
  43. data/test/codeless_code/commands/test_filter_fables.rb +89 -0
  44. data/test/codeless_code/filters/headers/test_integer.rb +86 -0
  45. data/test/codeless_code/filters/headers/test_string.rb +86 -0
  46. data/test/codeless_code/filters/test_builders.rb +4 -2
  47. data/test/codeless_code/filters/test_composite.rb +70 -0
  48. data/test/codeless_code/filters/test_date.rb +99 -0
  49. data/test/codeless_code/filters/test_langs.rb +50 -0
  50. data/test/codeless_code/filters/test_translator.rb +51 -0
  51. data/test/codeless_code/renderers/test_fable.rb +98 -0
  52. data/test/codeless_code/renderers/test_term_page.rb +87 -0
  53. data/test/codeless_code/test_catalog.rb +12 -5
  54. data/test/codeless_code/test_cli.rb +85 -0
  55. data/test/codeless_code/test_fable.rb +19 -10
  56. data/test/codeless_code/test_fable_set.rb +17 -5
  57. data/test/codeless_code/test_language_set.rb +16 -3
  58. data/test/codeless_code/test_options.rb +3 -11
  59. data/test/helper.rb +36 -10
  60. data/test/support/fs.rb +103 -0
  61. metadata +65 -11
  62. data/.document +0 -5
  63. data/data/README.md +0 -34
  64. data/lib/codeless_code/formats/plain_generator.rb +0 -157
  65. data/lib/codeless_code/formats/term_generator.rb +0 -167
  66. data/test/codeless_code/filters/test_header_integer.rb +0 -82
  67. data/test/codeless_code/filters/test_header_string.rb +0 -82
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ # codeless_code filters and prints fables from http://thecodelesscode.com
4
+ # Copyright (C) 2018 Jon Sangster
5
+ #
6
+ # This program is free software: you can redistribute it and/or modify it under
7
+ # the terms of the GNU General Public License as published by the Free Software
8
+ # Foundation, either version 3 of the License, or (at your option) any later
9
+ # version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful, but WITHOUT
12
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
+ # details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License along with
17
+ # this program. If not, see <https://www.gnu.org/licenses/>.
18
+ require 'helper'
19
+
20
+ module Filters
21
+ class TestDate < UnitTest
22
+ def test_enabled?
23
+ refute_predicate filter, :enabled?
24
+ refute_predicate filter(exclude: false), :enabled?
25
+
26
+ assert_predicate filter(exact: '2018-12-23'), :enabled?
27
+ assert_predicate filter(min: '2018-12-23'), :enabled?
28
+ assert_predicate filter(max: '2018-12-23'), :enabled?
29
+ assert_predicate filter(exclude: true), :enabled?
30
+ end
31
+
32
+ def test_call_exact
33
+ assert_filter(exact: '2018-12-23')
34
+ assert_filter(exact: '2018-12')
35
+ assert_filter(exact: '2018')
36
+
37
+ refute_filter(exact: '1999-12-23')
38
+ end
39
+
40
+ def test_call_min
41
+ assert_filter(min: '2018-12-22')
42
+ assert_filter(min: '2018-12-23')
43
+ assert_filter(min: '2018-12')
44
+ assert_filter(min: '2018')
45
+
46
+ refute_filter(min: '2018-12-24')
47
+ end
48
+
49
+ def test_call_max
50
+ assert_filter(max: '2018-12-23')
51
+ assert_filter(max: '2018-12-24')
52
+ assert_filter(max: '2018-12')
53
+ assert_filter(max: '2018')
54
+
55
+ refute_filter(max: '2018-12-22')
56
+ end
57
+
58
+ def test_call_exclude
59
+ assert_filter(create_dateless_fable, exclude: true)
60
+
61
+ refute_filter(exclude: true)
62
+ end
63
+
64
+ private
65
+
66
+ def filter(**opts)
67
+ Filters::Date.new(**opts)
68
+ end
69
+
70
+ def assert_filter(fab = nil, **opts)
71
+ fab ||= create_fable
72
+ filt = filter(**opts)
73
+ assert filt.call(fab), format('Expected %p to match %p', opts, fab)
74
+ end
75
+
76
+ def refute_filter(fab = nil, **opts)
77
+ fab ||= create_fable
78
+ filt = filter(**opts)
79
+ refute filt.call(fab), format('Expected %p not to match %p', opts, fab)
80
+ end
81
+
82
+ def create_fable(date = '2018-12-23')
83
+ mock_fable(<<-BODY.strip)
84
+ Number: 123
85
+ Date: #{date}
86
+
87
+ body
88
+ BODY
89
+ end
90
+
91
+ def create_dateless_fable
92
+ mock_fable(<<-BODY.strip)
93
+ Number: 123
94
+
95
+ body
96
+ BODY
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ # codeless_code filters and prints fables from http://thecodelesscode.com
4
+ # Copyright (C) 2018 Jon Sangster
5
+ #
6
+ # This program is free software: you can redistribute it and/or modify it under
7
+ # the terms of the GNU General Public License as published by the Free Software
8
+ # Foundation, either version 3 of the License, or (at your option) any later
9
+ # version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful, but WITHOUT
12
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
+ # details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License along with
17
+ # this program. If not, see <https://www.gnu.org/licenses/>.
18
+ require 'helper'
19
+
20
+ module Filters
21
+ class TestLang < UnitTest
22
+ def test_enabled
23
+ refute_predicate Filters::Lang.new, :enabled?
24
+
25
+ assert_predicate filter(exact: :en), :enabled?
26
+ end
27
+
28
+ def test_call
29
+ fable = create_fable(dir: 'en-test')
30
+ assert filter(exact: :en).call(fable),
31
+ 'expected lang to match directory name'
32
+ refute filter(exact: :fr).call(fable),
33
+ 'expected lang to not match other language'
34
+ end
35
+
36
+ private
37
+
38
+ def filter(exact:)
39
+ Filters::Lang.new(exact: exact)
40
+ end
41
+
42
+ def create_fable(body = 'body', **args)
43
+ mock_fable(<<-FABLE.strip, **args)
44
+ Number: 123
45
+
46
+ #{body}
47
+ FABLE
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ # codeless_code filters and prints fables from http://thecodelesscode.com
4
+ # Copyright (C) 2018 Jon Sangster
5
+ #
6
+ # This program is free software: you can redistribute it and/or modify it under
7
+ # the terms of the GNU General Public License as published by the Free Software
8
+ # Foundation, either version 3 of the License, or (at your option) any later
9
+ # version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful, but WITHOUT
12
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
+ # details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License along with
17
+ # this program. If not, see <https://www.gnu.org/licenses/>.
18
+ require 'helper'
19
+
20
+ module Filters
21
+ class TestTranslator < UnitTest
22
+ def test_enabled
23
+ refute_predicate Filters::Translator.new, :enabled?
24
+
25
+ assert_predicate filter(exact: 'test', casecmp: nil), :enabled?
26
+ assert_predicate filter(exact: nil, casecmp: 'test'), :enabled?
27
+ end
28
+
29
+ def test_call_exact
30
+ fable = create_fable(dir: 'en-Test')
31
+ assert filter(exact: 'Test', casecmp: nil).call(fable),
32
+ 'expected translator to match directory name'
33
+ refute filter(exact: 'test', casecmp: nil).call(fable),
34
+ 'expected lang to not match other language'
35
+ end
36
+
37
+ private
38
+
39
+ def filter(exact:, casecmp:)
40
+ Filters::Translator.new(exact: exact, casecmp: casecmp)
41
+ end
42
+
43
+ def create_fable(body = 'body', **args)
44
+ mock_fable(<<-FABLE.strip, **args)
45
+ Number: 123
46
+
47
+ #{body}
48
+ FABLE
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ # codeless_code filters and prints fables from http://thecodelesscode.com
4
+ # Copyright (C) 2018 Jon Sangster
5
+ #
6
+ # This program is free software: you can redistribute it and/or modify it under
7
+ # the terms of the GNU General Public License as published by the Free Software
8
+ # Foundation, either version 3 of the License, or (at your option) any later
9
+ # version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful, but WITHOUT
12
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
+ # details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License along with
17
+ # this program. If not, see <https://www.gnu.org/licenses/>.
18
+ require 'helper'
19
+
20
+ module Renderers
21
+ class TestFable < UnitTest
22
+ def test_delegation
23
+ fable = create_fable(dir: 'en-test')
24
+ renderer = Renderers::Fable.new(fable)
25
+
26
+ keys = %i[body headers lang translator title geekiness names topics]
27
+ keys.each do |key|
28
+ assert_equal fable.send(key), renderer.send(key)
29
+ end
30
+ end
31
+
32
+ def test_for_pager
33
+ renderer = Renderers::Fable.new(create_fable('body'))
34
+
35
+ page = renderer.for_pager(Formats::Raw)
36
+ assert_kind_of Renderers::TermPage, page
37
+
38
+ assert_equal renderer.title, page.title
39
+ assert_equal 'body', page.body
40
+ end
41
+
42
+ def test_for_pager_headers
43
+ renderer = Renderers::Fable.new(create_fable('body'))
44
+ page = renderer.for_pager(Formats::Raw)
45
+
46
+ assert_equal %w[Number Date Geekiness Names Topics Test],
47
+ page.headers.keys
48
+ refute_includes page.headers.keys, 'Title'
49
+ end
50
+
51
+ def test_for_pager_fallback_format
52
+ renderer = Renderers::Fable.new(create_fable('<p>body</p>'))
53
+
54
+ assert_output(nil, /Error parsing.*format error!/) do
55
+ page = renderer.for_pager(failure_format, fallback: Formats::Raw)
56
+ assert_equal '<p>body</p>', page.body
57
+ end
58
+ end
59
+
60
+ def test_for_list
61
+ renderer = Renderers::Fable.new(create_fable)
62
+ assert_equal '00123 Test Title', renderer.for_list
63
+ end
64
+
65
+ def test_for_list_with_headers
66
+ renderer = Renderers::Fable.new(create_fable)
67
+ assert_equal '00123 Test Title Number: "123", Date: "2018-12-23"',
68
+ renderer.for_list(%w[Number Date])
69
+ end
70
+
71
+ def test_for_list__title_width
72
+ renderer = Renderers::Fable.new(create_fable)
73
+ assert_equal '00123 Test Title', renderer.for_list(title_width: 15)
74
+ end
75
+
76
+ private
77
+
78
+ def create_fable(body = 'body', **args)
79
+ mock_fable(<<-FABLE.strip, **args)
80
+ Number: 123
81
+ Title: Test Title
82
+ Date: 2018-12-23
83
+ Geekiness: 1
84
+ Names: person, other
85
+ Topics: foo, bar, baz
86
+ Test: some text
87
+
88
+ #{body}
89
+ FABLE
90
+ end
91
+
92
+ def failure_format
93
+ proc { raise 'format error!' }.tap do |format|
94
+ format.define_singleton_method(:new) { |_| self }
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ # codeless_code filters and prints fables from http://thecodelesscode.com
4
+ # Copyright (C) 2018 Jon Sangster
5
+ #
6
+ # This program is free software: you can redistribute it and/or modify it under
7
+ # the terms of the GNU General Public License as published by the Free Software
8
+ # Foundation, either version 3 of the License, or (at your option) any later
9
+ # version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful, but WITHOUT
12
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
+ # details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License along with
17
+ # this program. If not, see <https://www.gnu.org/licenses/>.
18
+ require 'helper'
19
+
20
+ module Renderers
21
+ class TestTermPage < UnitTest
22
+ def test_title
23
+ page = Renderers::TermPage.new
24
+ assert_nil page.title
25
+
26
+ page.title = 'test'
27
+ assert_equal 'test', page.title
28
+ end
29
+
30
+ def test_body
31
+ page = Renderers::TermPage.new
32
+ assert_nil page.body
33
+
34
+ page.body = 'test'
35
+ assert_equal 'test', page.body
36
+ end
37
+
38
+ def test_headers
39
+ page = Renderers::TermPage.new
40
+ assert_empty page.headers
41
+
42
+ page.add_header('Key', 'Value')
43
+ assert_equal({ 'Key' => 'Value' }, page.headers)
44
+ end
45
+
46
+ def test_to_s
47
+ page = term_page('Some Title', 'Some Body')
48
+ assert_match(/Some Title/, page.to_s)
49
+ assert_match(/Some Body/, page.to_s)
50
+ end
51
+
52
+ def test_to_s_headers
53
+ page = term_page('Title', 'Body')
54
+ page.add_header('My Header', 'Test 123')
55
+ assert_match(/^My Header: Test 123$/, page.to_s)
56
+ end
57
+
58
+ def test_to_s_seperators
59
+ page = term_page('Title', 'Body')
60
+ assert_match(/^-+$/, page.to_s)
61
+ assert_match(/^=+$/, page.to_s)
62
+ end
63
+
64
+ private
65
+
66
+ def term_page(title, body, **args)
67
+ Renderers::TermPage.new(**args).tap do |page|
68
+ page.title = title
69
+ page.body = body
70
+ end
71
+ end
72
+
73
+ def create_fable(body = 'body', **args)
74
+ mock_fable(<<-FABLE.strip, **args)
75
+ Number: 123
76
+ Title: Test Title
77
+ Date: 2018-12-23
78
+ Geekiness: 1
79
+ Names: person, other
80
+ Topics: foo, bar, baz
81
+ Test: some text
82
+
83
+ #{body}
84
+ FABLE
85
+ end
86
+ end
87
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # codeless_code filters and prints fables from http://thecodelesscode.com
2
4
  # Copyright (C) 2018 Jon Sangster
3
5
  #
@@ -47,11 +49,15 @@ class TestCatalog < UnitTest
47
49
  end
48
50
 
49
51
  def test_select
50
- all_filter = ->(fable) { true }
51
- none_filter = ->(fable) { false }
52
+ all_filter = ->(_fable) { true }
52
53
 
53
54
  assert_kind_of Enumerable, catalog.select(all_filter)
54
55
  catalog.select(all_filter).each { |fable| assert_kind_of Fable, fable }
56
+ end
57
+
58
+ def test_select_expected_size
59
+ all_filter = ->(_fable) { true }
60
+ none_filter = ->(_fable) { false }
55
61
 
56
62
  assert_equal catalog.fables.size, catalog.select(all_filter).size
57
63
  assert_equal 0, catalog.select(none_filter).size
@@ -59,8 +65,9 @@ class TestCatalog < UnitTest
59
65
 
60
66
  private
61
67
 
62
- def catalog(dir = DEFAULT_DATA)
63
- (@catalog ||= {})[dir] ||= Catalog.new(dir)
68
+ def fake_fs
69
+ FakeDir.new('/').tap do |fs|
70
+ fs.create_path('en-test/case-123.txt')
71
+ end
64
72
  end
65
73
  end
66
-
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ # codeless_code filters and prints fables from http://thecodelesscode.com
4
+ # Copyright (C) 2018 Jon Sangster
5
+ #
6
+ # This program is free software: you can redistribute it and/or modify it under
7
+ # the terms of the GNU General Public License as published by the Free Software
8
+ # Foundation, either version 3 of the License, or (at your option) any later
9
+ # version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful, but WITHOUT
12
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
+ # details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License along with
17
+ # this program. If not, see <https://www.gnu.org/licenses/>.
18
+ require 'helper'
19
+ require 'minitest/mock'
20
+
21
+ class TestCli < UnitTest
22
+ def setup
23
+ @pager = ENV.delete('PAGER')
24
+ end
25
+
26
+ def teardown
27
+ ENV['PAGER'] = @pager
28
+ end
29
+
30
+ def test_call_default
31
+ expected = "00123 Test Case\n00234 Test Case 2\n"
32
+ assert_output(expected) { cli.call }
33
+ end
34
+
35
+ def test_call_single_by_number
36
+ [
37
+ " Test Case\n" \
38
+ "==================\n" \
39
+ "Number: 123\n" \
40
+ " Date: 2018-12-23\n" \
41
+ "------------------\n" \
42
+ "\n" \
43
+ "body\n"
44
+ ].each { |expected| assert_output(expected) { cli('123').call } }
45
+ end
46
+
47
+ def test_help
48
+ assert_output(/Usage: test_app/) { cli('-h').call }
49
+ end
50
+
51
+ def test_version
52
+ assert_output(/^test_app #{VERSION}/) { cli('--version').call }
53
+ end
54
+
55
+ def test_list_translations
56
+ assert_output("en test\n") { cli('--list-translations').call }
57
+ end
58
+
59
+ private
60
+
61
+ def cli(*args)
62
+ Cli.new('test_app', args).tap do |cli|
63
+ cli.send(:options).instance_variable_set(:@data_dir, fake_fs)
64
+ end
65
+ end
66
+
67
+ def fake_fs # rubocop:disable Metrics/MethodLength
68
+ FakeDir.new('/').tap do |fs|
69
+ fs.create_path('en-test/case-123.txt', <<-FABLE)
70
+ Title: Test Case
71
+ Number: 123
72
+ Date: 2018-12-23
73
+
74
+ body
75
+ FABLE
76
+ fs.create_path('en-test/case-234.txt', <<-FABLE)
77
+ Title: Test Case 2
78
+ Number: 234
79
+ Date: 2018-12-30
80
+
81
+ body 2
82
+ FABLE
83
+ end
84
+ end
85
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # codeless_code filters and prints fables from http://thecodelesscode.com
2
4
  # Copyright (C) 2018 Jon Sangster
3
5
  #
@@ -28,7 +30,7 @@ class TestFable < UnitTest
28
30
  assert_kind_of Hash, fable.headers
29
31
  refute_empty fable.headers.keys
30
32
 
31
- %w[Date Title Number Geekiness Topics].each do |key|
33
+ %w[Date Title Number].each do |key|
32
34
  assert_includes fable.headers.keys, key
33
35
  end
34
36
  end
@@ -40,16 +42,12 @@ class TestFable < UnitTest
40
42
  assert_predicate new_fable, :read_headers?
41
43
  end
42
44
 
43
- def test_date
44
- assert_kind_of Date, fable.date
45
- end
46
-
47
45
  def test_lang
48
- assert_equal :en, fable('en-qi').lang
46
+ assert_equal :en, fable('en-test').lang
49
47
  end
50
48
 
51
49
  def test_translator
52
- assert_equal 'qi', fable('en-qi').translator
50
+ assert_equal 'test', fable('en-test').translator
53
51
  end
54
52
 
55
53
  def test_names
@@ -62,9 +60,20 @@ class TestFable < UnitTest
62
60
 
63
61
  private
64
62
 
65
- def fable(dir = 'en-qi', fable = 'case-1.txt')
63
+ def fable(dir = 'en-test', fable = 'case-123.txt', root: fake_fs)
66
64
  (@fable ||= {})["#{dir}/#{fable}"] ||=
67
- Fable.new(DEFAULT_DATA.glob(dir).first.glob(fable).first)
65
+ Fable.new(root.glob(dir).first.glob(fable).first)
68
66
  end
69
- end
70
67
 
68
+ def fake_fs
69
+ FakeDir.new('/').tap do |fs|
70
+ fs.create_path('en-test/case-123.txt', <<-FABLE)
71
+ Title: Test Case
72
+ Number: 123
73
+ Date: 2018-12-23
74
+
75
+ body
76
+ FABLE
77
+ end
78
+ end
79
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # codeless_code filters and prints fables from http://thecodelesscode.com
2
4
  # Copyright (C) 2018 Jon Sangster
3
5
  #
@@ -37,11 +39,15 @@ class TestFableSet < UnitTest
37
39
  end
38
40
 
39
41
  def test_filter
40
- all_filter = ->(fable) { true }
41
- none_filter = ->(fable) { false }
42
+ all_filter = ->(_fable) { true }
42
43
 
43
44
  assert_kind_of Enumerable, set.filter(all_filter)
44
45
  set.filter(all_filter).each { |fable| assert_kind_of Fable, fable }
46
+ end
47
+
48
+ def test_filter_expected_side
49
+ all_filter = ->(_fable) { true }
50
+ none_filter = ->(_fable) { false }
45
51
 
46
52
  assert_equal set.fables.size, set.filter(all_filter).size
47
53
  assert_equal 0, set.filter(none_filter).size
@@ -49,8 +55,14 @@ class TestFableSet < UnitTest
49
55
 
50
56
  private
51
57
 
52
- def set(dirname = 'en-qi')
53
- (@set ||= {})[dirname] ||= FableSet.new(DEFAULT_DATA.glob(dirname).first)
58
+ def set(dirname = 'en-qi', root: fake_fs)
59
+ (@set ||= {})[dirname] ||= FableSet.new(root.glob(dirname).first)
54
60
  end
55
- end
56
61
 
62
+ def fake_fs
63
+ FakeDir.new('/').tap do |fs|
64
+ fs.create_path('en-qi/case-123.txt')
65
+ fs.create_path('zh-hanzik/case-123.txt')
66
+ end
67
+ end
68
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # codeless_code filters and prints fables from http://thecodelesscode.com
2
4
  # Copyright (C) 2018 Jon Sangster
3
5
  #
@@ -28,10 +30,21 @@ class TestLanguageSet < UnitTest
28
30
  set.fable_sets.each { |set| assert_kind_of FableSet, set }
29
31
  end
30
32
 
33
+ def test_fable_sets_filtered
34
+ assert_equal %w[qi another], set.map(&:translator)
35
+ end
36
+
31
37
  private
32
38
 
33
- def set(lang = :en)
34
- (@set ||= {})[lang] ||= LanguageSet.new(lang, root_dir: DEFAULT_DATA)
39
+ def set(lang = :en, root: fake_fs)
40
+ (@set ||= {})[lang] ||= LanguageSet.new(lang, root_dir: root)
35
41
  end
36
- end
37
42
 
43
+ def fake_fs
44
+ FakeDir.new('/').tap do |fs|
45
+ fs.create_path('en-qi/case-123.txt')
46
+ fs.create_path('en-another/case-234.txt')
47
+ fs.create_path('zh-hanzik/case-123.txt')
48
+ end
49
+ end
50
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # codeless_code filters and prints fables from http://thecodelesscode.com
2
4
  # Copyright (C) 2018 Jon Sangster
3
5
  #
@@ -14,6 +16,7 @@
14
16
  # You should have received a copy of the GNU General Public License along with
15
17
  # this program. If not, see <https://www.gnu.org/licenses/>.
16
18
  require 'helper'
19
+ require 'slop'
17
20
 
18
21
  class TestOptions < UnitTest
19
22
  def test_opts
@@ -51,15 +54,4 @@ class TestOptions < UnitTest
51
54
  def test_data
52
55
  assert_equal Pathname.new('/foo'), options('-p', '/foo').data_dir
53
56
  end
54
-
55
- def test_data_dir_default
56
- assert_equal DEFAULT_DATA, options.data_dir
57
- end
58
-
59
- private
60
-
61
- def options(*args)
62
- (@options ||= {})[args] ||= Options.new('codeless_code', args)
63
- end
64
57
  end
65
-