LittleWeasel 3.0.3 → 5.0.0

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 (151) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +3 -0
  3. data/.reek.yml +17 -0
  4. data/.rspec +4 -2
  5. data/.rubocop.yml +187 -0
  6. data/.ruby-version +1 -1
  7. data/.yardopts +2 -0
  8. data/CHANGELOG.md +22 -1
  9. data/Gemfile +3 -1
  10. data/Jenkinsfile +20 -0
  11. data/LittleWeasel.gemspec +31 -18
  12. data/README.md +408 -42
  13. data/Rakefile +296 -3
  14. data/lib/LittleWeasel/block_results.rb +81 -0
  15. data/lib/LittleWeasel/configure.rb +98 -0
  16. data/lib/LittleWeasel/dictionary.rb +125 -0
  17. data/lib/LittleWeasel/dictionary_key.rb +48 -0
  18. data/lib/LittleWeasel/dictionary_manager.rb +91 -0
  19. data/lib/LittleWeasel/errors/dictionary_file_already_loaded_error.rb +9 -0
  20. data/lib/LittleWeasel/errors/dictionary_file_empty_error.rb +8 -0
  21. data/lib/LittleWeasel/errors/dictionary_file_not_found_error.rb +8 -0
  22. data/lib/LittleWeasel/errors/dictionary_file_too_large_error.rb +16 -0
  23. data/lib/LittleWeasel/errors/language_required_error.rb +8 -0
  24. data/lib/LittleWeasel/errors/must_override_error.rb +8 -0
  25. data/lib/LittleWeasel/filters/en_us/currency_filter.rb +19 -0
  26. data/lib/LittleWeasel/filters/en_us/numeric_filter.rb +19 -0
  27. data/lib/LittleWeasel/filters/en_us/single_character_word_filter.rb +21 -0
  28. data/lib/LittleWeasel/filters/word_filter.rb +59 -0
  29. data/lib/LittleWeasel/filters/word_filter_managable.rb +80 -0
  30. data/lib/LittleWeasel/filters/word_filter_validatable.rb +31 -0
  31. data/lib/LittleWeasel/filters/word_filterable.rb +19 -0
  32. data/lib/LittleWeasel/filters/word_filters_validatable.rb +29 -0
  33. data/lib/LittleWeasel/metadata/dictionary_metadata.rb +145 -0
  34. data/lib/LittleWeasel/metadata/invalid_words_metadata.rb +134 -0
  35. data/lib/LittleWeasel/metadata/invalid_words_service_results.rb +45 -0
  36. data/lib/LittleWeasel/metadata/metadata_observable_validatable.rb +22 -0
  37. data/lib/LittleWeasel/metadata/metadata_observerable.rb +90 -0
  38. data/lib/LittleWeasel/metadata/metadatable.rb +134 -0
  39. data/lib/LittleWeasel/modules/class_name_to_symbol.rb +26 -0
  40. data/lib/LittleWeasel/modules/configurable.rb +26 -0
  41. data/lib/LittleWeasel/modules/deep_dup.rb +11 -0
  42. data/lib/LittleWeasel/modules/dictionary_cache_keys.rb +34 -0
  43. data/lib/LittleWeasel/modules/dictionary_cache_servicable.rb +26 -0
  44. data/lib/LittleWeasel/modules/dictionary_cache_validatable.rb +18 -0
  45. data/lib/LittleWeasel/modules/dictionary_creator_servicable.rb +27 -0
  46. data/lib/LittleWeasel/modules/dictionary_file_loader.rb +67 -0
  47. data/lib/LittleWeasel/modules/dictionary_key_validatable.rb +17 -0
  48. data/lib/LittleWeasel/modules/dictionary_keyable.rb +24 -0
  49. data/lib/LittleWeasel/modules/dictionary_metadata_servicable.rb +29 -0
  50. data/lib/LittleWeasel/modules/dictionary_metadata_validatable.rb +15 -0
  51. data/lib/LittleWeasel/modules/dictionary_source_validatable.rb +15 -0
  52. data/lib/LittleWeasel/modules/dictionary_sourceable.rb +86 -0
  53. data/lib/LittleWeasel/modules/dictionary_validatable.rb +18 -0
  54. data/lib/LittleWeasel/modules/language.rb +24 -0
  55. data/lib/LittleWeasel/modules/language_validatable.rb +14 -0
  56. data/lib/LittleWeasel/modules/locale.rb +23 -0
  57. data/lib/LittleWeasel/modules/order_validatable.rb +16 -0
  58. data/lib/LittleWeasel/modules/orderable.rb +17 -0
  59. data/lib/LittleWeasel/modules/region.rb +24 -0
  60. data/lib/LittleWeasel/modules/region_validatable.rb +14 -0
  61. data/lib/LittleWeasel/modules/tag_validatable.rb +14 -0
  62. data/lib/LittleWeasel/modules/taggable.rb +31 -0
  63. data/lib/LittleWeasel/modules/word_results_validatable.rb +28 -0
  64. data/lib/LittleWeasel/preprocessors/en_us/capitalize_preprocessor.rb +22 -0
  65. data/lib/LittleWeasel/preprocessors/preprocessed_word.rb +29 -0
  66. data/lib/LittleWeasel/preprocessors/preprocessed_word_validatable.rb +56 -0
  67. data/lib/LittleWeasel/preprocessors/preprocessed_words.rb +59 -0
  68. data/lib/LittleWeasel/preprocessors/preprocessed_words_validatable.rb +28 -0
  69. data/lib/LittleWeasel/preprocessors/word_preprocessable.rb +19 -0
  70. data/lib/LittleWeasel/preprocessors/word_preprocessor.rb +123 -0
  71. data/lib/LittleWeasel/preprocessors/word_preprocessor_managable.rb +114 -0
  72. data/lib/LittleWeasel/preprocessors/word_preprocessor_validatable.rb +40 -0
  73. data/lib/LittleWeasel/preprocessors/word_preprocessors_validatable.rb +24 -0
  74. data/lib/LittleWeasel/services/dictionary_cache_service.rb +211 -0
  75. data/lib/LittleWeasel/services/dictionary_creator_service.rb +94 -0
  76. data/lib/LittleWeasel/services/dictionary_file_loader_service.rb +37 -0
  77. data/lib/LittleWeasel/services/dictionary_killer_service.rb +35 -0
  78. data/lib/LittleWeasel/services/dictionary_metadata_service.rb +116 -0
  79. data/lib/LittleWeasel/services/invalid_words_service.rb +59 -0
  80. data/lib/LittleWeasel/version.rb +3 -1
  81. data/lib/LittleWeasel/word_results.rb +146 -0
  82. data/lib/LittleWeasel.rb +5 -184
  83. data/spec/factories/dictionary.rb +43 -0
  84. data/spec/factories/dictionary_cache_service.rb +95 -0
  85. data/spec/factories/dictionary_creator_service.rb +16 -0
  86. data/spec/factories/dictionary_file_loader_service.rb +13 -0
  87. data/spec/factories/dictionary_hash.rb +39 -0
  88. data/spec/factories/dictionary_key.rb +14 -0
  89. data/spec/factories/dictionary_killer_service.rb +14 -0
  90. data/spec/factories/dictionary_manager.rb +10 -0
  91. data/spec/factories/dictionary_metadata.rb +16 -0
  92. data/spec/factories/dictionary_metadata_service.rb +16 -0
  93. data/spec/factories/numeric_filter.rb +12 -0
  94. data/spec/factories/preprocessed_word.rb +16 -0
  95. data/spec/factories/preprocessed_words.rb +41 -0
  96. data/spec/factories/single_character_word_filter.rb +12 -0
  97. data/spec/factories/word_results.rb +16 -0
  98. data/spec/lib/LittleWeasel/block_results_spec.rb +248 -0
  99. data/spec/lib/LittleWeasel/configure_spec.rb +74 -0
  100. data/spec/lib/LittleWeasel/dictionary_key_spec.rb +118 -0
  101. data/spec/lib/LittleWeasel/dictionary_manager_spec.rb +166 -0
  102. data/spec/lib/LittleWeasel/dictionary_spec.rb +289 -0
  103. data/spec/lib/LittleWeasel/filters/en_us/currency_filter_spec.rb +80 -0
  104. data/spec/lib/LittleWeasel/filters/en_us/numeric_filter_spec.rb +66 -0
  105. data/spec/lib/LittleWeasel/filters/en_us/single_character_word_filter_spec.rb +58 -0
  106. data/spec/lib/LittleWeasel/filters/word_filter_managable_spec.rb +180 -0
  107. data/spec/lib/LittleWeasel/filters/word_filter_spec.rb +151 -0
  108. data/spec/lib/LittleWeasel/filters/word_filter_validatable_spec.rb +94 -0
  109. data/spec/lib/LittleWeasel/filters/word_filters_validatable_spec.rb +48 -0
  110. data/spec/lib/LittleWeasel/integraton_tests/dictionary_integration_spec.rb +201 -0
  111. data/spec/lib/LittleWeasel/metadata/dictionary_creator_servicable_spec.rb +54 -0
  112. data/spec/lib/LittleWeasel/metadata/dictionary_metadata_spec.rb +209 -0
  113. data/spec/lib/LittleWeasel/metadata/invalid_words_metadata_spec.rb +155 -0
  114. data/spec/lib/LittleWeasel/metadata/metadata_observerable_spec.rb +31 -0
  115. data/spec/lib/LittleWeasel/metadata/metadatable_spec.rb +35 -0
  116. data/spec/lib/LittleWeasel/modules/class_name_to_symbol_spec.rb +21 -0
  117. data/spec/lib/LittleWeasel/modules/dictionary_file_loader_spec.rb +125 -0
  118. data/spec/lib/LittleWeasel/modules/dictionary_sourceable_spec.rb +81 -0
  119. data/spec/lib/LittleWeasel/modules/language_spec.rb +112 -0
  120. data/spec/lib/LittleWeasel/modules/locale_spec.rb +95 -0
  121. data/spec/lib/LittleWeasel/modules/region_spec.rb +112 -0
  122. data/spec/lib/LittleWeasel/preprocessors/en_us/capitalize_preprocessor_spec.rb +34 -0
  123. data/spec/lib/LittleWeasel/preprocessors/preprocessed_word_spec.rb +105 -0
  124. data/spec/lib/LittleWeasel/preprocessors/preprocessed_word_validatable_spec.rb +143 -0
  125. data/spec/lib/LittleWeasel/preprocessors/preprocessed_words_spec.rb +77 -0
  126. data/spec/lib/LittleWeasel/preprocessors/preprocessed_words_validatable_spec.rb +58 -0
  127. data/spec/lib/LittleWeasel/preprocessors/word_preprocessor_managable_spec.rb +242 -0
  128. data/spec/lib/LittleWeasel/preprocessors/word_preprocessor_spec.rb +218 -0
  129. data/spec/lib/LittleWeasel/preprocessors/word_preprocessor_validatable_spec.rb +109 -0
  130. data/spec/lib/LittleWeasel/preprocessors/word_preprocessors_validatable_spec.rb +49 -0
  131. data/spec/lib/LittleWeasel/services/dictionary_cache_service_spec.rb +444 -0
  132. data/spec/lib/LittleWeasel/services/dictionary_creator_service_spec.rb +119 -0
  133. data/spec/lib/LittleWeasel/services/dictionary_file_loader_service_spec.rb +71 -0
  134. data/spec/lib/LittleWeasel/services/dictionary_metadata_service_spec.rb +279 -0
  135. data/spec/lib/LittleWeasel/word_results_spec.rb +275 -0
  136. data/spec/lib/LittleWeasel/workflow/workflow_spec.rb +20 -0
  137. data/spec/spec_helper.rb +117 -6
  138. data/spec/support/factory_bot.rb +15 -0
  139. data/spec/support/file_helpers.rb +46 -0
  140. data/spec/support/files/empty-dictionary.txt +0 -0
  141. data/{lib/dictionary → spec/support/files/en-US-big.txt} +262156 -31488
  142. data/spec/support/files/en-US-tagged.txt +26 -0
  143. data/spec/support/files/en-US.txt +26 -0
  144. data/spec/support/files/en.txt +26 -0
  145. data/spec/support/files/es-ES.txt +27 -0
  146. data/spec/support/files/es.txt +27 -0
  147. data/spec/support/general_helpers.rb +68 -0
  148. data/spec/support/shared_contexts.rb +107 -0
  149. data/spec/support/shared_examples.rb +105 -0
  150. metadata +378 -38
  151. data/spec/checker/checker_spec.rb +0 -286
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :dictionary_hash, class: Hash do
5
+ dictionary_words do
6
+ %w(apple
7
+ better
8
+ cat
9
+ dog
10
+ everyone
11
+ fat
12
+ game
13
+ help
14
+ italic
15
+ jasmine
16
+ kelp
17
+ love
18
+ man
19
+ nope
20
+ octopus
21
+ popeye
22
+ queue
23
+ ruby
24
+ stop
25
+ top
26
+ ultimate
27
+ very
28
+ was
29
+ xylophone
30
+ yes
31
+ zebra)
32
+ end
33
+
34
+ skip_create
35
+ initialize_with do
36
+ LittleWeasel::Dictionary.to_hash(dictionary_words: dictionary_words)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :dictionary_key, class: LittleWeasel::DictionaryKey do
5
+ language { :en }
6
+ region { :us }
7
+ tag {}
8
+
9
+ skip_create
10
+ initialize_with do
11
+ new language: language, region: region, tag: tag
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :dictionary_killer_service, class: LittleWeasel::Services::DictionaryKillerService do
5
+ dictionary_key { create(:dictionary_key) }
6
+ dictionary_cache { {} }
7
+ dictionary_metadata { {} }
8
+
9
+ skip_create
10
+ initialize_with do
11
+ new(dictionary_key: dictionary_key, dictionary_cache: dictionary_cache, dictionary_metadata: dictionary_metadata)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :dictionary_manager, class: LittleWeasel::DictionaryManager do
5
+ skip_create
6
+ initialize_with do
7
+ new
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :dictionary_metadata, class: LittleWeasel::Metadata::DictionaryMetadata do
5
+ dictionary_words {}
6
+ dictionary_key { create(:dictionary_key) }
7
+ dictionary_cache { {} }
8
+ dictionary_metadata { {} }
9
+
10
+ skip_create
11
+ initialize_with do
12
+ dictionary_hash = dictionary_words || create(:dictionary_hash)
13
+ new dictionary_words: dictionary_hash, dictionary_key: dictionary_key, dictionary_cache: dictionary_cache, dictionary_metadata: dictionary_metadata
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: false
2
+
3
+ require 'pry'
4
+
5
+ FactoryBot.define do
6
+ factory :dictionary_metadata_service, class: LittleWeasel::Services::DictionaryMetadataService do
7
+ dictionary_key { create(:dictionary_key) }
8
+ dictionary_cache { {} }
9
+ dictionary_metadata { {} }
10
+
11
+ skip_create
12
+ initialize_with do
13
+ new(dictionary_key: dictionary_key, dictionary_cache: dictionary_cache, dictionary_metadata: dictionary_metadata)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :numeric_filter, class: LittleWeasel::Filters::EnUs::NumericFilter do
5
+ filter_on { true }
6
+
7
+ skip_create
8
+ initialize_with do
9
+ new filter_on: filter_on
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :preprocessed_word, class: LittleWeasel::Preprocessors::PreprocessedWord do
5
+ original_word {}
6
+ preprocessed {}
7
+ preprocessed_word {}
8
+ preprocessor {}
9
+ preprocessor_order {}
10
+
11
+ skip_create
12
+ initialize_with do
13
+ new original_word: original_word, preprocessed: preprocessed, preprocessed_word: preprocessed_word, preprocessor: preprocessor, preprocessor_order: preprocessor_order
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: false
2
+
3
+ require 'pry'
4
+
5
+ FactoryBot.define do
6
+ factory :preprocessed_words, class: LittleWeasel::Preprocessors::PreprocessedWords do
7
+ original_word { 'word' }
8
+ preprocessed_words {}
9
+
10
+ transient do
11
+ with_word_processors { 0 }
12
+ end
13
+
14
+ skip_create
15
+ initialize_with do
16
+ new original_word: original_word, preprocessed_words: preprocessed_words
17
+ end
18
+
19
+ after :create do |preprocessed_words, evaluator|
20
+ with_word_processors = evaluator.with_word_processors
21
+
22
+ if with_word_processors.positive?
23
+ preprocessed_words.preprocessed_words = []
24
+ preprocessed_word = evaluator.original_word
25
+
26
+ with_word_processors.times do |index|
27
+ preprocessed_word_object = create(:preprocessed_word,
28
+ original_word: preprocessed_word,
29
+ preprocessed: true,
30
+ preprocessed_word: "#{preprocessed_word}-#{index}").tap do |preprocessed_word_object|
31
+ preprocessed_word_object.preprocessed_word = "#{preprocessed_word}-#{index}"
32
+ preprocessed_word_object.preprocessor = "preprocesor#{index}"
33
+ preprocessed_word_object.preprocessor_order = index
34
+ end
35
+ preprocessed_words.preprocessed_words << preprocessed_word_object
36
+ preprocessed_word = preprocessed_word_object.preprocessed_word
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :single_character_word_filter, class: LittleWeasel::Filters::EnUs::SingleCharacterWordFilter do
5
+ filter_on { true }
6
+
7
+ skip_create
8
+ initialize_with do
9
+ new filter_on: filter_on
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :word_results, class: LittleWeasel::WordResults do
5
+ original_word {}
6
+ filters_matched { [] }
7
+ preprocessed_words {}
8
+ word_cached { false }
9
+ word_valid { false }
10
+
11
+ skip_create
12
+ initialize_with do
13
+ new filters_matched: filters_matched, original_word: original_word, preprocessed_words: preprocessed_words, word_cached: word_cached, word_valid: word_valid
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,248 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe LittleWeasel::BlockResults do
6
+ subject { described_class.new original_word_block: original_word_block }
7
+
8
+ def ceate_word_results(word:, word_valid: false, word_cached: false, filters_matched: [], preprocessed_words: nil)
9
+ create(:word_results, original_word: word, word_valid: word_valid, word_cached: word_cached, filters_matched: filters_matched, preprocessed_words: preprocessed_words)
10
+ end
11
+
12
+ let(:original_word_block) { 'Original word block' }
13
+
14
+ #.new
15
+ describe '.new' do
16
+ it 'instantiates an object' do
17
+ expect { subject }.to_not raise_error
18
+ end
19
+
20
+ it 'initializes #word_results to an empty Array' do
21
+ expect(subject.word_results).to eq []
22
+ end
23
+
24
+ it 'initializes #original_word_block to the original_word_block argument passed' do
25
+ expect(subject.original_word_block).to eq original_word_block
26
+ end
27
+ end
28
+
29
+ #<<
30
+ describe '<<' do
31
+ context 'with an invalid argument' do
32
+ it 'raises an error' do
33
+ expect { subject << :not_a_word_results_object }.to raise_error(/Argument word_result is not a WordResults object/)
34
+ end
35
+ end
36
+
37
+ context 'with a valid argument' do
38
+ it 'adds the WordResults object to the #word_results Array' do
39
+ expect { subject << ceate_word_results(word: 'word01') }.to \
40
+ change { subject.word_results.count }.from(0).to(1)
41
+ end
42
+ end
43
+ end
44
+
45
+ #success?
46
+ describe '#success?' do
47
+ before do
48
+ subject << ceate_word_results(word: 'word01', word_valid: true)
49
+ subject << ceate_word_results(word: 'word02', word_valid: false, filters_matched: [:matched_filter])
50
+ end
51
+
52
+ context 'when all WordResults#successful? return true' do
53
+ before do
54
+ subject << ceate_word_results(word: 'word03', word_valid: false, filters_matched: [:matched_filter])
55
+ subject << ceate_word_results(word: 'word04', word_valid: false, filters_matched: [:matched_filter])
56
+ end
57
+
58
+ it 'returns true' do
59
+ expect(subject.success?).to be true
60
+ end
61
+ end
62
+
63
+ context 'when any WordResults#word_valid? objects return false' do
64
+ before do
65
+ subject << ceate_word_results(word: 'word03', word_valid: false)
66
+ end
67
+
68
+ it 'returns false' do
69
+ expect(subject.success?).to be false
70
+ end
71
+ end
72
+
73
+ context 'when any WordResults#filter_match? objects return false' do
74
+ before do
75
+ subject << ceate_word_results(word: 'word03')
76
+ end
77
+
78
+ it 'returns false' do
79
+ expect(subject.success?).to be false
80
+ end
81
+ end
82
+ end
83
+
84
+ #words_valid?
85
+ describe '#words_valid?' do
86
+ before do
87
+ subject << ceate_word_results(word: 'word01', word_valid: true)
88
+ subject << ceate_word_results(word: 'word02', word_valid: true)
89
+ end
90
+
91
+ context 'when all WordResults#word_valid? objects return true' do
92
+ it 'returns true' do
93
+ expect(subject.words_valid?).to be true
94
+ end
95
+ end
96
+
97
+ context 'when any WordResults#word_valid? objects return false' do
98
+ before do
99
+ subject << ceate_word_results(word: 'word03', word_valid: false)
100
+ end
101
+
102
+ it 'returns false' do
103
+ expect(subject.words_valid?).to be false
104
+ end
105
+ end
106
+ end
107
+
108
+ #filters_match?
109
+ describe '#filters_match?' do
110
+ before do
111
+ subject << ceate_word_results(word: 'word01', filters_matched: [:matched_filter])
112
+ subject << ceate_word_results(word: 'word02', filters_matched: [:matched_filter])
113
+ end
114
+
115
+ context 'when all WordResults#filters_match? objects return true' do
116
+ it 'returns true' do
117
+ expect(subject.filters_match?).to be true
118
+ end
119
+ end
120
+
121
+ context 'when any WordResults#filters_match? objects return false' do
122
+ before do
123
+ subject << ceate_word_results(word: 'word03', filters_matched: [])
124
+ end
125
+
126
+ it 'returns false' do
127
+ expect(subject.filters_match?).to be false
128
+ end
129
+ end
130
+ end
131
+
132
+ #preprocessed_words?
133
+ describe '#preprocessed_words?' do
134
+ before do
135
+ subject << ceate_word_results(word: word, preprocessed_words: preprocessed_words)
136
+ end
137
+
138
+ let(:word) { 'word' }
139
+ let(:preprocessed_words) { create(:preprocessed_words, original_word: word, with_word_processors: 1) }
140
+
141
+ context 'when all WordResults#preprocessed_word? objects return true' do
142
+ it 'returns true' do
143
+ expect(subject.preprocessed_words?).to be true
144
+ end
145
+ end
146
+ end
147
+
148
+ #preprocessed_words_or_original_words
149
+ describe '#preprocessed_words_or_original_words' do
150
+ context 'with all preprocessed words' do
151
+ before do
152
+ subject << ceate_word_results(word: words[0], preprocessed_words: preprocessed_words[0])
153
+ subject << ceate_word_results(word: words[1], preprocessed_words: preprocessed_words[1])
154
+ end
155
+
156
+ let(:words) { %w(word1 word2) }
157
+ let(:preprocessed_words) do
158
+ [
159
+ create(:preprocessed_words, original_word: words[0], with_word_processors: 1),
160
+ create(:preprocessed_words, original_word: words[1], with_word_processors: 2)
161
+ ]
162
+ end
163
+
164
+ it 'returns all the preprocessed words' do
165
+ expect(subject.preprocessed_words_or_original_words).to eq %w(word1-0 word2-0-1)
166
+ end
167
+ end
168
+
169
+ context 'with all original words' do
170
+ before do
171
+ subject << ceate_word_results(word: words[0])
172
+ subject << ceate_word_results(word: words[1])
173
+ end
174
+
175
+ let(:words) { %w(word1 word2) }
176
+
177
+ it 'returns all the original words' do
178
+ expect(subject.preprocessed_words_or_original_words).to eq %w(word1 word2)
179
+ end
180
+ end
181
+
182
+ context 'with mixed original words and preprocessed words' do
183
+ before do
184
+ subject << ceate_word_results(word: words[0], word_valid: true)
185
+ subject << ceate_word_results(word: words[1], word_valid: true, preprocessed_words: preprocessed_words[0])
186
+ subject << ceate_word_results(word: words[2], word_valid: true)
187
+ subject << ceate_word_results(word: words[3], word_valid: true, preprocessed_words: preprocessed_words[1])
188
+ end
189
+
190
+ let(:words) { %w(word0 word1 word2 word3) }
191
+ let(:preprocessed_words) do
192
+ [
193
+ create(:preprocessed_words, original_word: words[1], with_word_processors: 1),
194
+ create(:preprocessed_words, original_word: words[3], with_word_processors: 2)
195
+ ]
196
+ end
197
+
198
+ it 'returns all the original and preprocessed words' do
199
+ expect(subject.preprocessed_words_or_original_words).to eq %w(word0 word1-0 word2 word3-0-1)
200
+ end
201
+ end
202
+ end
203
+
204
+ #words_cached?
205
+ describe '#words_cached?' do
206
+ before do
207
+ subject << ceate_word_results(word: words[0])
208
+ subject << ceate_word_results(word: words[1])
209
+ subject << ceate_word_results(word: words[2])
210
+ subject << ceate_word_results(word: words[3])
211
+ end
212
+
213
+ let(:words) { %w(word1 word2 word3 word4) }
214
+
215
+ context 'when all the words are cached' do
216
+ before do
217
+ allow_any_instance_of(LittleWeasel::WordResults).to receive(:word_cached?).and_return(true)
218
+ end
219
+
220
+ it 'returns true' do
221
+ expect(subject.words_cached?).to eq true
222
+ end
223
+ end
224
+
225
+ context 'when some of the words are cached' do
226
+ before do
227
+ allow(subject.word_results[0]).to receive(:word_cached?).and_return(false)
228
+ allow(subject.word_results[1]).to receive(:word_cached?).and_return(true)
229
+ allow(subject.word_results[2]).to receive(:word_cached?).and_return(false)
230
+ allow(subject.word_results[3]).to receive(:word_cached?).and_return(true)
231
+ end
232
+
233
+ it 'returns false' do
234
+ expect(subject.words_cached?).to eq false
235
+ end
236
+ end
237
+
238
+ context 'when NONE of the words are cached' do
239
+ before do
240
+ allow_any_instance_of(LittleWeasel::WordResults).to receive(:word_cached?).and_return(false)
241
+ end
242
+
243
+ it 'returns false' do
244
+ expect(subject.words_cached?).to eq false
245
+ end
246
+ end
247
+ end
248
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe LittleWeasel do
6
+ subject do
7
+ described_class.configure { |config| }
8
+ described_class.configuration
9
+ end
10
+
11
+ context 'default configurable settings' do
12
+ describe '#max_dictionary_file_megabytes' do
13
+ it 'set to 5 by default' do
14
+ expect(subject.max_dictionary_file_megabytes).to eq 5
15
+ end
16
+ end
17
+
18
+ describe '#max_invalid_words_bytesize' do
19
+ it 'set to 25_000 by default' do
20
+ expect(subject.max_invalid_words_bytesize).to eq 25_000
21
+ end
22
+ end
23
+
24
+ describe '#metadata_observers' do
25
+ it 'set to an Array with InvalidWordsMetadata by default' do
26
+ expect(subject.metadata_observers).to eq [
27
+ LittleWeasel::Metadata::InvalidWordsMetadata
28
+ ]
29
+ end
30
+ end
31
+
32
+ describe '#word_block_regex' do
33
+ it 'set to the default regex to split work blocks by default' do
34
+ expect(subject.word_block_regex).to eq /[[[:word:]]'-]+/
35
+ end
36
+ end
37
+ end
38
+
39
+ #.configuration
40
+ describe '.configuration' do
41
+ context 'when passing a block' do
42
+ subject do
43
+ described_class.configure do |config|
44
+ config.max_dictionary_file_megabytes = max_dictionary_file_megabytes
45
+ config.metadata_observers = metadata_observers
46
+ config.word_block_regex = word_block_regex
47
+ end
48
+ described_class.configuration
49
+ end
50
+
51
+ let(:max_dictionary_file_megabytes) { 1_222_333 }
52
+ let(:metadata_observers) { %i(observer0 observer1) }
53
+ let(:word_block_regex) { :word_block_regex }
54
+
55
+ describe '#max_dictionary_file_megabytes=' do
56
+ it 'sets the value' do
57
+ expect(subject.max_dictionary_file_megabytes).to eq max_dictionary_file_megabytes
58
+ end
59
+ end
60
+
61
+ describe '#metadata_observers=' do
62
+ it 'sets the value' do
63
+ expect(subject.metadata_observers).to eq metadata_observers
64
+ end
65
+ end
66
+
67
+ describe '#word_block_regex=' do
68
+ it 'sets the value' do
69
+ expect(subject.word_block_regex).to eq word_block_regex
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe LittleWeasel::DictionaryKey, type: :class do
6
+ subject { described_class.new(language: language, region: region, tag: tag) }
7
+
8
+ let(:language) {}
9
+ let(:region) {}
10
+ let(:tag) {}
11
+
12
+ #.new
13
+ describe '.new' do
14
+ context 'when passing valid arguments' do
15
+ let(:language) { :EN }
16
+ let(:region) { :us }
17
+ let(:tag) { :TAGGED}
18
+
19
+ it 'instantiates the object' do
20
+ expect { subject }.to_not raise_error
21
+ end
22
+
23
+ it 'normalizes language and converts it to lowercase' do
24
+ expect(subject.language).to eq :en
25
+ end
26
+
27
+ it 'normalizes region and converts it to uppercase' do
28
+ expect(subject.region).to eq :US
29
+ end
30
+
31
+ it 'leaves tag case unchanged' do
32
+ expect(subject.tag).to eq :TAGGED
33
+ end
34
+ end
35
+
36
+ context 'when passing invalid arguments' do
37
+ context 'when passing an invalid language' do
38
+ it 'raises an error' do
39
+ expect { subject }.to raise_error "Argument language '#{language}' is not a Symbol."
40
+ end
41
+ end
42
+
43
+ context 'when passing an invalid region' do
44
+ let(:language) { :en }
45
+ let(:region) { 1 }
46
+
47
+ it 'raises an error' do
48
+ expect { subject }.to raise_error "Argument region '#{region}' is not a Symbol."
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ #.key
55
+ describe '.key' do
56
+ let(:language) { :xx }
57
+ let(:region) { :yy }
58
+ let(:tag) { :zz }
59
+
60
+ it 'returns the locale' do
61
+ expect(described_class.key language: language, region: region, tag: tag).to eq 'xx-YY-zz'
62
+ end
63
+ end
64
+
65
+ #key
66
+ describe '#key' do
67
+ context 'with no tag' do
68
+ context 'with language' do
69
+ let(:language) { :EN }
70
+
71
+ it 'returns the key in the form of a locale String that includes language only (e.g. "en")' do
72
+ expect(subject.key).to eq 'en'
73
+ end
74
+ end
75
+
76
+ context 'with language and region' do
77
+ let(:language) { :EN }
78
+ let(:region) { :us }
79
+
80
+ it 'returns the key in the form of a locale String that includes language and region (e.g. "en-US")' do
81
+ expect(subject.key).to eq 'en-US'
82
+ end
83
+ end
84
+ end
85
+
86
+ context 'with a tag' do
87
+ let(:tag) { :tagged }
88
+
89
+ context 'with language and a tag' do
90
+ let(:language) { :EN }
91
+
92
+ it 'returns the key in the form of a locale String that includes the language and appended tag (e.g. "en-tag")' do
93
+ expect(subject.key).to eq 'en-tagged'
94
+ end
95
+ end
96
+
97
+ context 'with language and region' do
98
+ let(:language) { :EN }
99
+ let(:region) { :us }
100
+
101
+ it 'returns the key in the form of a locale String that includes language, region and the appended tag (e.g. "en-US-tag")' do
102
+ expect(subject.key).to eq 'en-US-tagged'
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ #to_s
109
+ describe '#to_s' do
110
+ let(:language) { :en }
111
+ let(:region) { :us }
112
+ let(:tag) { :tagged }
113
+
114
+ it '#to_s is an alias for #key' do
115
+ expect(subject.to_s).to eq 'en-US-tagged'
116
+ end
117
+ end
118
+ end