LittleWeasel 3.0.4 → 5.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/codeql-analysis.yml +72 -0
- data/.gitignore +19 -17
- data/.reek.yml +17 -0
- data/.rspec +4 -2
- data/.rubocop.yml +187 -0
- data/.ruby-version +1 -1
- data/.yardopts +2 -0
- data/CHANGELOG.md +21 -1
- data/Gemfile +3 -1
- data/Gemfile.lock +114 -0
- data/Jenkinsfile +20 -0
- data/LittleWeasel.gemspec +31 -18
- data/README.md +408 -42
- data/Rakefile +296 -3
- data/lib/LittleWeasel/block_results.rb +81 -0
- data/lib/LittleWeasel/configure.rb +98 -0
- data/lib/LittleWeasel/dictionary.rb +125 -0
- data/lib/LittleWeasel/dictionary_key.rb +48 -0
- data/lib/LittleWeasel/dictionary_manager.rb +91 -0
- data/lib/LittleWeasel/errors/dictionary_file_already_loaded_error.rb +9 -0
- data/lib/LittleWeasel/errors/dictionary_file_empty_error.rb +8 -0
- data/lib/LittleWeasel/errors/dictionary_file_not_found_error.rb +8 -0
- data/lib/LittleWeasel/errors/dictionary_file_too_large_error.rb +16 -0
- data/lib/LittleWeasel/errors/language_required_error.rb +8 -0
- data/lib/LittleWeasel/errors/must_override_error.rb +8 -0
- data/lib/LittleWeasel/filters/en_us/currency_filter.rb +19 -0
- data/lib/LittleWeasel/filters/en_us/numeric_filter.rb +19 -0
- data/lib/LittleWeasel/filters/en_us/single_character_word_filter.rb +21 -0
- data/lib/LittleWeasel/filters/word_filter.rb +59 -0
- data/lib/LittleWeasel/filters/word_filter_managable.rb +80 -0
- data/lib/LittleWeasel/filters/word_filter_validatable.rb +31 -0
- data/lib/LittleWeasel/filters/word_filterable.rb +19 -0
- data/lib/LittleWeasel/filters/word_filters_validatable.rb +29 -0
- data/lib/LittleWeasel/metadata/dictionary_metadata.rb +145 -0
- data/lib/LittleWeasel/metadata/invalid_words_metadata.rb +134 -0
- data/lib/LittleWeasel/metadata/invalid_words_service_results.rb +45 -0
- data/lib/LittleWeasel/metadata/metadata_observable_validatable.rb +22 -0
- data/lib/LittleWeasel/metadata/metadata_observerable.rb +90 -0
- data/lib/LittleWeasel/metadata/metadatable.rb +134 -0
- data/lib/LittleWeasel/modules/class_name_to_symbol.rb +26 -0
- data/lib/LittleWeasel/modules/configurable.rb +26 -0
- data/lib/LittleWeasel/modules/deep_dup.rb +11 -0
- data/lib/LittleWeasel/modules/dictionary_cache_keys.rb +34 -0
- data/lib/LittleWeasel/modules/dictionary_cache_servicable.rb +26 -0
- data/lib/LittleWeasel/modules/dictionary_cache_validatable.rb +18 -0
- data/lib/LittleWeasel/modules/dictionary_creator_servicable.rb +27 -0
- data/lib/LittleWeasel/modules/dictionary_file_loader.rb +67 -0
- data/lib/LittleWeasel/modules/dictionary_key_validatable.rb +17 -0
- data/lib/LittleWeasel/modules/dictionary_keyable.rb +24 -0
- data/lib/LittleWeasel/modules/dictionary_metadata_servicable.rb +29 -0
- data/lib/LittleWeasel/modules/dictionary_metadata_validatable.rb +15 -0
- data/lib/LittleWeasel/modules/dictionary_source_validatable.rb +15 -0
- data/lib/LittleWeasel/modules/dictionary_sourceable.rb +86 -0
- data/lib/LittleWeasel/modules/dictionary_validatable.rb +18 -0
- data/lib/LittleWeasel/modules/language.rb +24 -0
- data/lib/LittleWeasel/modules/language_validatable.rb +14 -0
- data/lib/LittleWeasel/modules/locale.rb +23 -0
- data/lib/LittleWeasel/modules/order_validatable.rb +16 -0
- data/lib/LittleWeasel/modules/orderable.rb +17 -0
- data/lib/LittleWeasel/modules/region.rb +24 -0
- data/lib/LittleWeasel/modules/region_validatable.rb +14 -0
- data/lib/LittleWeasel/modules/tag_validatable.rb +14 -0
- data/lib/LittleWeasel/modules/taggable.rb +31 -0
- data/lib/LittleWeasel/modules/word_results_validatable.rb +28 -0
- data/lib/LittleWeasel/preprocessors/en_us/capitalize_preprocessor.rb +22 -0
- data/lib/LittleWeasel/preprocessors/preprocessed_word.rb +29 -0
- data/lib/LittleWeasel/preprocessors/preprocessed_word_validatable.rb +56 -0
- data/lib/LittleWeasel/preprocessors/preprocessed_words.rb +59 -0
- data/lib/LittleWeasel/preprocessors/preprocessed_words_validatable.rb +28 -0
- data/lib/LittleWeasel/preprocessors/word_preprocessable.rb +19 -0
- data/lib/LittleWeasel/preprocessors/word_preprocessor.rb +123 -0
- data/lib/LittleWeasel/preprocessors/word_preprocessor_managable.rb +114 -0
- data/lib/LittleWeasel/preprocessors/word_preprocessor_validatable.rb +40 -0
- data/lib/LittleWeasel/preprocessors/word_preprocessors_validatable.rb +24 -0
- data/lib/LittleWeasel/services/dictionary_cache_service.rb +211 -0
- data/lib/LittleWeasel/services/dictionary_creator_service.rb +94 -0
- data/lib/LittleWeasel/services/dictionary_file_loader_service.rb +37 -0
- data/lib/LittleWeasel/services/dictionary_killer_service.rb +35 -0
- data/lib/LittleWeasel/services/dictionary_metadata_service.rb +116 -0
- data/lib/LittleWeasel/services/invalid_words_service.rb +59 -0
- data/lib/LittleWeasel/version.rb +3 -1
- data/lib/LittleWeasel/word_results.rb +146 -0
- data/lib/LittleWeasel.rb +72 -186
- data/spec/factories/dictionary.rb +43 -0
- data/spec/factories/dictionary_cache_service.rb +95 -0
- data/spec/factories/dictionary_creator_service.rb +16 -0
- data/spec/factories/dictionary_file_loader_service.rb +13 -0
- data/spec/factories/dictionary_hash.rb +39 -0
- data/spec/factories/dictionary_key.rb +14 -0
- data/spec/factories/dictionary_killer_service.rb +14 -0
- data/spec/factories/dictionary_manager.rb +10 -0
- data/spec/factories/dictionary_metadata.rb +16 -0
- data/spec/factories/dictionary_metadata_service.rb +16 -0
- data/spec/factories/numeric_filter.rb +12 -0
- data/spec/factories/preprocessed_word.rb +16 -0
- data/spec/factories/preprocessed_words.rb +41 -0
- data/spec/factories/single_character_word_filter.rb +12 -0
- data/spec/factories/word_results.rb +16 -0
- data/spec/lib/LittleWeasel/block_results_spec.rb +248 -0
- data/spec/lib/LittleWeasel/configure_spec.rb +74 -0
- data/spec/lib/LittleWeasel/dictionary_key_spec.rb +118 -0
- data/spec/lib/LittleWeasel/dictionary_manager_spec.rb +166 -0
- data/spec/lib/LittleWeasel/dictionary_spec.rb +289 -0
- data/spec/lib/LittleWeasel/filters/en_us/currency_filter_spec.rb +80 -0
- data/spec/lib/LittleWeasel/filters/en_us/numeric_filter_spec.rb +66 -0
- data/spec/lib/LittleWeasel/filters/en_us/single_character_word_filter_spec.rb +58 -0
- data/spec/lib/LittleWeasel/filters/word_filter_managable_spec.rb +180 -0
- data/spec/lib/LittleWeasel/filters/word_filter_spec.rb +151 -0
- data/spec/lib/LittleWeasel/filters/word_filter_validatable_spec.rb +94 -0
- data/spec/lib/LittleWeasel/filters/word_filters_validatable_spec.rb +48 -0
- data/spec/lib/LittleWeasel/integraton_tests/dictionary_integration_spec.rb +201 -0
- data/spec/lib/LittleWeasel/metadata/dictionary_creator_servicable_spec.rb +54 -0
- data/spec/lib/LittleWeasel/metadata/dictionary_metadata_spec.rb +209 -0
- data/spec/lib/LittleWeasel/metadata/invalid_words_metadata_spec.rb +155 -0
- data/spec/lib/LittleWeasel/metadata/metadata_observerable_spec.rb +31 -0
- data/spec/lib/LittleWeasel/metadata/metadatable_spec.rb +35 -0
- data/spec/lib/LittleWeasel/modules/class_name_to_symbol_spec.rb +21 -0
- data/spec/lib/LittleWeasel/modules/dictionary_file_loader_spec.rb +125 -0
- data/spec/lib/LittleWeasel/modules/dictionary_sourceable_spec.rb +81 -0
- data/spec/lib/LittleWeasel/modules/language_spec.rb +112 -0
- data/spec/lib/LittleWeasel/modules/locale_spec.rb +95 -0
- data/spec/lib/LittleWeasel/modules/region_spec.rb +112 -0
- data/spec/lib/LittleWeasel/preprocessors/en_us/capitalize_preprocessor_spec.rb +34 -0
- data/spec/lib/LittleWeasel/preprocessors/preprocessed_word_spec.rb +105 -0
- data/spec/lib/LittleWeasel/preprocessors/preprocessed_word_validatable_spec.rb +143 -0
- data/spec/lib/LittleWeasel/preprocessors/preprocessed_words_spec.rb +77 -0
- data/spec/lib/LittleWeasel/preprocessors/preprocessed_words_validatable_spec.rb +58 -0
- data/spec/lib/LittleWeasel/preprocessors/word_preprocessor_managable_spec.rb +242 -0
- data/spec/lib/LittleWeasel/preprocessors/word_preprocessor_spec.rb +218 -0
- data/spec/lib/LittleWeasel/preprocessors/word_preprocessor_validatable_spec.rb +109 -0
- data/spec/lib/LittleWeasel/preprocessors/word_preprocessors_validatable_spec.rb +49 -0
- data/spec/lib/LittleWeasel/services/dictionary_cache_service_spec.rb +444 -0
- data/spec/lib/LittleWeasel/services/dictionary_creator_service_spec.rb +119 -0
- data/spec/lib/LittleWeasel/services/dictionary_file_loader_service_spec.rb +71 -0
- data/spec/lib/LittleWeasel/services/dictionary_metadata_service_spec.rb +279 -0
- data/spec/lib/LittleWeasel/word_results_spec.rb +275 -0
- data/spec/lib/LittleWeasel/workflow/workflow_spec.rb +20 -0
- data/spec/spec_helper.rb +117 -6
- data/spec/support/factory_bot.rb +15 -0
- data/spec/support/file_helpers.rb +46 -0
- data/spec/support/files/empty-dictionary.txt +0 -0
- data/{lib/dictionary → spec/support/files/en-US-big.txt} +262156 -31488
- data/spec/support/files/en-US-tagged.txt +26 -0
- data/spec/support/files/en-US.txt +26 -0
- data/spec/support/files/en.txt +26 -0
- data/spec/support/files/es-ES.txt +27 -0
- data/spec/support/files/es.txt +27 -0
- data/spec/support/general_helpers.rb +68 -0
- data/spec/support/shared_contexts.rb +107 -0
- data/spec/support/shared_examples.rb +105 -0
- metadata +418 -70
- data/spec/checker/checker_spec.rb +0 -286
@@ -0,0 +1,444 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe LittleWeasel::Services::DictionaryCacheService do
|
6
|
+
include_context 'dictionary cache'
|
7
|
+
include_context 'dictionary keys'
|
8
|
+
include_context 'dictionary sourceable'
|
9
|
+
|
10
|
+
subject { create(:dictionary_cache_service, dictionary_cache: dictionary_cache) }
|
11
|
+
|
12
|
+
let(:en_us_dictionary_key) { dictionary_key_for(language: :en, region: :us) }
|
13
|
+
let(:en_gb_dictionary_key) { dictionary_key_for(language: :en, region: :gb) }
|
14
|
+
let(:es_es_dictionary_key) { dictionary_key_for(language: :es, region: :es) }
|
15
|
+
|
16
|
+
let(:dictionary_key) { en_us_dictionary_key }
|
17
|
+
let(:key) { dictionary_key.key }
|
18
|
+
let(:file) { dictionary_path_for(file_name: key) }
|
19
|
+
let(:file_minus_ext) { key }
|
20
|
+
let(:dictionary_cache) { {} }
|
21
|
+
let!(:initialized_dictionary_cache) { LittleWeasel::Modules::DictionaryCacheKeys.initialize_dictionary_cache(dictionary_cache: {}) }
|
22
|
+
|
23
|
+
shared_examples 'the dictionary_cache object reference has not changed' do
|
24
|
+
it 'the dictionary_cache object has not changed' do
|
25
|
+
expect(actual_dictionary_cache).to eq expected_dictionary_cache
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe 'class methods' do
|
30
|
+
#.init
|
31
|
+
describe '.init' do
|
32
|
+
subject! { create(:dictionary_cache_service, dictionary_file_source: true, dictionary_cache: dictionary_cache) }
|
33
|
+
|
34
|
+
before do
|
35
|
+
create(:dictionary_cache_service, dictionary_file_source: true, dictionary_cache: dictionary_cache, dictionary_key: en_gb_dictionary_key)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'resets the cache to an initiaized state for all keys in the cache' do
|
39
|
+
expect { described_class.init(dictionary_cache: dictionary_cache) }.to change { dictionary_cache }.to(initialized_dictionary_cache)
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'maintains dictionary_cache object integrity' do
|
43
|
+
it_behaves_like 'the dictionary_cache object reference has not changed' do
|
44
|
+
before { subject }
|
45
|
+
let(:actual_dictionary_cache) { described_class.init(dictionary_cache: dictionary_cache) }
|
46
|
+
let(:expected_dictionary_cache) { dictionary_cache }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
#.init?
|
52
|
+
describe '.init?' do
|
53
|
+
context 'when passing an initialized dictionary cache Hash' do
|
54
|
+
it 'returns true' do
|
55
|
+
expect(described_class.init? dictionary_cache: initialized_dictionary_cache).to eq true
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'when passing a NON-initialized dictionary cache Hash' do
|
60
|
+
before do
|
61
|
+
subject.add_dictionary_source dictionary_source: file
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'returns false' do
|
65
|
+
expect(described_class.init? dictionary_cache: dictionary_cache).to eq false
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
#.count
|
71
|
+
describe '.count' do
|
72
|
+
context 'when there are no dictionary references' do
|
73
|
+
subject { create(:dictionary_cache_service) }
|
74
|
+
|
75
|
+
it 'returns 0' do
|
76
|
+
expect(described_class.count(dictionary_cache: subject.dictionary_cache)).to eq 0
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'when there are are dictionary references' do
|
81
|
+
subject { create(:dictionary_cache_service, dictionary_file_source: true) }
|
82
|
+
|
83
|
+
it 'returns the dictionary reference count' do
|
84
|
+
expect(described_class.count(dictionary_cache: subject.dictionary_cache)).to eq 1
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
#init
|
91
|
+
describe '#init' do
|
92
|
+
subject! { create(:dictionary_cache_service, dictionary_file_source: true, dictionary_cache: dictionary_cache) }
|
93
|
+
|
94
|
+
let!(:subject2) { create(:dictionary_cache_service, dictionary_file_source: true, dictionary_cache: dictionary_cache, dictionary_key: en_gb_dictionary_key) }
|
95
|
+
let!(:original_dictionary_cache) { dictionary_cache }
|
96
|
+
|
97
|
+
it 'resets the cache to an initiaized state for the GIVEN KEY ONLY' do
|
98
|
+
expect { subject.init }.to change { dictionary_cache }.from(original_dictionary_cache).to(subject2.dictionary_cache)
|
99
|
+
expect { subject2.init }.to change { dictionary_cache }.from(original_dictionary_cache).to(subject.dictionary_cache)
|
100
|
+
end
|
101
|
+
|
102
|
+
describe 'maintains dictionary_cache object integrity' do
|
103
|
+
it_behaves_like 'the dictionary_cache object reference has not changed' do
|
104
|
+
let(:actual_dictionary_cache) { subject.init.dictionary_cache }
|
105
|
+
let(:expected_dictionary_cache) { dictionary_cache }
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
#dictionary_reference?
|
111
|
+
describe '#dictionary_reference?' do
|
112
|
+
subject { create(:dictionary_cache_service, dictionary_key: dictionary_key, dictionary_file_source: true, dictionary_cache: dictionary_cache) }
|
113
|
+
|
114
|
+
context 'when the dictionary reference exists' do
|
115
|
+
it 'returns true' do
|
116
|
+
expect(subject.dictionary_reference?).to eq true
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'when the dictionary reference DOES NOT exist' do
|
121
|
+
subject { create(:dictionary_cache_service, dictionary_key: es_es_dictionary_key, dictionary_cache: dictionary_cache) }
|
122
|
+
|
123
|
+
before do
|
124
|
+
create(:dictionary_cache_service, dictionary_key: en_us_dictionary_key, dictionary_cache: dictionary_cache)
|
125
|
+
.add_dictionary_source dictionary_source: memory_source
|
126
|
+
create(:dictionary_cache_service, dictionary_key: en_gb_dictionary_key, dictionary_cache: dictionary_cache)
|
127
|
+
.add_dictionary_source dictionary_source: dictionary_path_for(file_name: en_gb_dictionary_key.key)
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'returns false' do
|
131
|
+
expect(subject.dictionary_reference?).to eq false
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
describe 'maintains dictionary_cache object integrity' do
|
136
|
+
it_behaves_like 'the dictionary_cache object reference has not changed' do
|
137
|
+
let(:actual_dictionary_cache) { subject.dictionary_cache }
|
138
|
+
let(:expected_dictionary_cache) { dictionary_cache }
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
#add_dictionary_source dictionary_source: memory_source
|
144
|
+
describe '#add_dictionary_source dictionary_source: memory_source' do
|
145
|
+
subject! do
|
146
|
+
create(:dictionary_cache_service, dictionary_cache: dictionary_cache, dictionary_key: dictionary_key)
|
147
|
+
.add_dictionary_source dictionary_source: memory_source
|
148
|
+
end
|
149
|
+
|
150
|
+
context 'when a dictionary already exists' do
|
151
|
+
it 'raises an error' do
|
152
|
+
expect do
|
153
|
+
create(:dictionary_cache_service, dictionary_key: dictionary_key, dictionary_cache: subject.dictionary_cache)
|
154
|
+
.add_dictionary_source dictionary_source: memory_source
|
155
|
+
end.to raise_error "The dictionary source associated with key '#{key}' already exists."
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
context 'when a dictionary reference for the key DOES NOT already exist' do
|
160
|
+
let(:dictionary_cache_service) do
|
161
|
+
create(:dictionary_cache_service, dictionary_key: en_gb_dictionary_key, dictionary_cache: subject.dictionary_cache)
|
162
|
+
end
|
163
|
+
|
164
|
+
before do
|
165
|
+
dictionary_cache_service.add_dictionary_source dictionary_source: memory_source
|
166
|
+
end
|
167
|
+
|
168
|
+
let(:dictionary_source) { dictionary_cache_service.dictionary_source }
|
169
|
+
|
170
|
+
it 'creates a new dictionary reference' do
|
171
|
+
expect(dictionary_cache_service.dictionary_reference?).to eq true
|
172
|
+
end
|
173
|
+
|
174
|
+
it 'creates a unique memory source' do
|
175
|
+
expect(LittleWeasel::Modules::DictionarySourceable.memory_source? dictionary_source).to be_truthy
|
176
|
+
expect(dictionary_source == subject.dictionary_source).to_not eq true
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
describe 'maintains dictionary_cache object integrity' do
|
181
|
+
it_behaves_like 'the dictionary_cache object reference has not changed' do
|
182
|
+
subject { create(:dictionary_cache_service, dictionary_key: dictionary_key, dictionary_cache: dictionary_cache) }
|
183
|
+
let(:actual_dictionary_cache) { subject.add_dictionary_source(dictionary_source: memory_source).dictionary_cache }
|
184
|
+
let(:expected_dictionary_cache) { dictionary_cache }
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
#add_dictionary_source
|
190
|
+
describe '#add_dictionary_source' do
|
191
|
+
context 'when a dictionary reference for the key already exists' do
|
192
|
+
before do
|
193
|
+
create(:dictionary_cache_service, dictionary_cache: dictionary_cache, dictionary_key: dictionary_key)
|
194
|
+
.add_dictionary_source(dictionary_source: file)
|
195
|
+
end
|
196
|
+
|
197
|
+
it 'raises an error' do
|
198
|
+
expect do
|
199
|
+
create(:dictionary_cache_service, dictionary_cache: dictionary_cache, dictionary_key: dictionary_key, dictionary_cache: subject.dictionary_cache)
|
200
|
+
.add_dictionary_source(dictionary_source: file)
|
201
|
+
end.to raise_error "The dictionary source associated with key '#{key}' already exists."
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
context 'when a dictionary reference for the key DOES NOT already exist' do
|
206
|
+
subject! do
|
207
|
+
create(:dictionary_cache_service, dictionary_cache: dictionary_cache, dictionary_key: dictionary_key)
|
208
|
+
.add_dictionary_source(dictionary_source: file)
|
209
|
+
end
|
210
|
+
|
211
|
+
let(:en_gb_file) do
|
212
|
+
"#{ dictionary_path_for(file_name: en_gb_dictionary_key.key) }"
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'creates a new dictionary reference' do
|
216
|
+
dictionary_cache_service = create(:dictionary_cache_service, dictionary_key: en_gb_dictionary_key, dictionary_cache: subject.dictionary_cache)
|
217
|
+
dictionary_cache_service.add_dictionary_source dictionary_source: en_gb_file
|
218
|
+
expect(dictionary_cache_service.dictionary_reference?).to eq true
|
219
|
+
end
|
220
|
+
|
221
|
+
context 'when the file source already exists' do
|
222
|
+
it 'reuses the existing file source' do
|
223
|
+
dictionary_cache_service = create(:dictionary_cache_service, dictionary_key: en_gb_dictionary_key, dictionary_cache: subject.dictionary_cache)
|
224
|
+
# Note: We're using the same file as en-US dictionary key, so
|
225
|
+
# the file source is NOT unique and will be reused.
|
226
|
+
dictionary_cache_service.add_dictionary_source dictionary_source: file
|
227
|
+
expect(LittleWeasel::Modules::DictionarySourceable.file_source? dictionary_cache_service.dictionary_source).to be_truthy
|
228
|
+
expect(dictionary_cache_service.dictionary_source == subject.dictionary_source).to eq true
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
context 'when the file source DOES NOT already exists' do
|
233
|
+
it 'creates a unique file source' do
|
234
|
+
dictionary_cache_service = create(:dictionary_cache_service, dictionary_key: en_gb_dictionary_key, dictionary_cache: subject.dictionary_cache)
|
235
|
+
# Note: We're using file that is DIFFERENT from the file being used by the
|
236
|
+
# en-US dictionary key, the file source created will be unique.
|
237
|
+
dictionary_cache_service.add_dictionary_source dictionary_source: en_gb_file
|
238
|
+
expect(LittleWeasel::Modules::DictionarySourceable.file_source? dictionary_cache_service.dictionary_source).to be_truthy
|
239
|
+
expect(dictionary_cache_service.dictionary_source == subject.dictionary_source).to eq false
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
describe 'maintains dictionary_cache object integrity' do
|
245
|
+
it_behaves_like 'the dictionary_cache object reference has not changed' do
|
246
|
+
subject { create(:dictionary_cache_service, dictionary_key: dictionary_key, dictionary_cache: dictionary_cache) }
|
247
|
+
let(:actual_dictionary_cache) { subject.add_dictionary_source(dictionary_source: file).dictionary_cache }
|
248
|
+
let(:expected_dictionary_cache) { dictionary_cache }
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
#dictionary_file
|
254
|
+
describe '#dictionary_file' do
|
255
|
+
context 'when a file source is used' do
|
256
|
+
subject! { create(:dictionary_cache_service, dictionary_key: dictionary_key).add_dictionary_source(dictionary_source: file) }
|
257
|
+
|
258
|
+
it 'returns the dictionary file' do
|
259
|
+
expect(subject.dictionary_file).to eq file
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
context 'when a memory source is used' do
|
264
|
+
subject! { create(:dictionary_cache_service, dictionary_key: dictionary_key).add_dictionary_source dictionary_source: memory_source }
|
265
|
+
|
266
|
+
let(:dictionary_words) { dictionary_words_for(dictionary_file_path: file) }
|
267
|
+
|
268
|
+
it 'returns the dictionary file' do
|
269
|
+
expect(LittleWeasel::Modules::DictionarySourceable.memory_source? subject.dictionary_file).to be_truthy
|
270
|
+
end
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
#dictionary_id
|
275
|
+
describe '#dictionary_id' do
|
276
|
+
context 'when a dictionary id exists for the given dictionary key' do
|
277
|
+
before do
|
278
|
+
subject.add_dictionary_source dictionary_source: memory_source
|
279
|
+
end
|
280
|
+
|
281
|
+
it 'returns the key' do
|
282
|
+
expect(subject.dictionary_id).to_not be_nil
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
context 'when a dictionary id DOES NOT exist for the given dictionary key' do
|
287
|
+
it 'returns nil' do
|
288
|
+
expect(subject.dictionary_id).to be_nil
|
289
|
+
end
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
#dictionary_id!
|
294
|
+
describe '#dictionary_id!' do
|
295
|
+
context 'when a dictionary id associated with the dictionary key exists' do
|
296
|
+
subject { create(:dictionary_cache_service, dictionary_cache: dictionary_cache, dictionary_key: dictionary_key, dictionary_file_source: true) }
|
297
|
+
|
298
|
+
it 'returns the dictionary id' do
|
299
|
+
expect { subject.dictionary_id! }.to_not raise_error
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
context 'when a dictionary id associated with the dictionary key DOES NOT exist' do
|
304
|
+
subject { create(:dictionary_cache_service) }
|
305
|
+
|
306
|
+
it 'raises an error' do
|
307
|
+
expect { subject.dictionary_id! }.to raise_error "A dictionary id could not be found for key '#{key}'."
|
308
|
+
end
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
#dictionary_exist?
|
313
|
+
describe '#dictionary_exist?' do
|
314
|
+
context 'when the dictionary reference does not exist' do
|
315
|
+
subject { create(:dictionary_cache_service, dictionary_cache: dictionary_cache) }
|
316
|
+
|
317
|
+
it 'returns false' do
|
318
|
+
expect(subject.dictionary_exist?).to eq false
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
context 'when the dictionary is already loaded' do
|
323
|
+
subject { create(:dictionary_cache_service, dictionary_cache: dictionary_cache, dictionary_file_source: true, load: true) }
|
324
|
+
|
325
|
+
it 'returns true' do
|
326
|
+
expect(subject.dictionary_exist?).to eq true
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
context 'when the dictionary is NOT already loaded' do
|
331
|
+
subject { create(:dictionary_cache_service, dictionary_cache: dictionary_cache, dictionary_file_source: true) }
|
332
|
+
|
333
|
+
it 'returns false' do
|
334
|
+
expect(subject.dictionary_exist?).to eq false
|
335
|
+
end
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
#dictionary_object
|
340
|
+
describe '#dictionary_object' do
|
341
|
+
context 'when the dictionary object is already cached/loaded' do
|
342
|
+
subject { create(:dictionary_cache_service, dictionary_file_source: true, load: true) }
|
343
|
+
|
344
|
+
it 'returns the dictionary object' do
|
345
|
+
expect(subject.dictionary_object).to be_kind_of LittleWeasel::Dictionary
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
context 'when the dictionary object is NOT already cached/loaded' do
|
350
|
+
context 'when the dictionary reference exists' do
|
351
|
+
it 'returns an object that is not present?' do
|
352
|
+
expect(create(:dictionary_cache_service, dictionary_file_source: true).dictionary_object).to_not be_present
|
353
|
+
end
|
354
|
+
end
|
355
|
+
|
356
|
+
context 'when the dictionary reference DOES NOT exist' do
|
357
|
+
it 'returns an object that is not present?' do
|
358
|
+
expect(create(:dictionary_cache_service).dictionary_object).to_not be_present
|
359
|
+
end
|
360
|
+
end
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
#dictionary_object!
|
365
|
+
describe '#dictionary_object!' do
|
366
|
+
describe 'when the dictionary is NOT in the cache' do
|
367
|
+
it 'raises an error' do
|
368
|
+
expect { subject.dictionary_object! }.to raise_error("The dictionary object associated with argument key '#{key}' is not in the cache.")
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
372
|
+
describe 'when the dictionary is loaded/cached' do
|
373
|
+
subject { create(:dictionary_cache_service, dictionary_file_source: true, load: true) }
|
374
|
+
|
375
|
+
it 'returns the dictionary cache for the dictionary' do
|
376
|
+
expect(subject.dictionary_reference?).to eq true
|
377
|
+
expect(subject.dictionary_exist?).to eq true
|
378
|
+
expect(subject.dictionary_object!).to be_kind_of LittleWeasel::Dictionary
|
379
|
+
end
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
#dictionary_object=
|
384
|
+
describe '#dictionary_object=' do
|
385
|
+
context 'when the dictionary object passed is invalid' do
|
386
|
+
subject { create(:dictionary_cache_service) }
|
387
|
+
|
388
|
+
context 'when nil' do
|
389
|
+
it 'raises an error' do
|
390
|
+
expect { subject.dictionary_object = nil }.to raise_error 'Argument object is not a Dictionary object'
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
394
|
+
context 'when not a Dictionary object' do
|
395
|
+
it 'raises an error' do
|
396
|
+
expect { subject.dictionary_object = :wrong_object }.to raise_error 'Argument object is not a Dictionary object'
|
397
|
+
end
|
398
|
+
end
|
399
|
+
end
|
400
|
+
|
401
|
+
context 'when there is NO dictionary reference' do
|
402
|
+
before do
|
403
|
+
allow(dictionary_object).to receive(:is_a?).and_return(true)
|
404
|
+
end
|
405
|
+
|
406
|
+
subject { create(:dictionary_cache_service, dictionary_cache: dictionary_cache, dictionary_key: dictionary_key) }
|
407
|
+
|
408
|
+
let(:dictionary_object) { Object.new }
|
409
|
+
|
410
|
+
it 'raises an error' do
|
411
|
+
expect { subject.dictionary_object = dictionary_object }.to raise_error "The dictionary reference associated with key '#{key}' could not be found."
|
412
|
+
end
|
413
|
+
end
|
414
|
+
|
415
|
+
context 'when the dictionary is already loaded/cached and different from the dictionary object passed' do
|
416
|
+
subject { create(:dictionary_cache_service, dictionary_cache: dictionary_cache, dictionary_key: dictionary_key, dictionary_file_source: true, load: true) }
|
417
|
+
|
418
|
+
let(:dictionary) { create(:dictionary, dictionary_cache: dictionary_cache, dictionary_key: dictionary_key) }
|
419
|
+
|
420
|
+
it 'raises an error' do
|
421
|
+
expect { subject.dictionary_object = dictionary }.to raise_error "The dictionary is already loaded/cached for key '#{key}'; use #unload or #kill first."
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
context 'when the dictionary is already loaded/cached and the dictionary object is the same as the one that is loaded/cached' do
|
426
|
+
subject { create(:dictionary_cache_service, dictionary_file_source: true, load: true) }
|
427
|
+
|
428
|
+
it 'returns the same object' do
|
429
|
+
expect(subject.dictionary_object = subject.dictionary_object).to eq subject.dictionary_object
|
430
|
+
end
|
431
|
+
end
|
432
|
+
|
433
|
+
context 'when the dictionary is NOT already loaded/cached' do
|
434
|
+
subject { create(:dictionary_cache_service, dictionary_key: dictionary_key, dictionary_cache: dictionary_cache, dictionary_file_source: true) }
|
435
|
+
|
436
|
+
let(:dictionary) { create(:dictionary, dictionary_key: dictionary_key, dictionary_cache: dictionary_cache) }
|
437
|
+
|
438
|
+
it 'updates the dictionary object' do
|
439
|
+
expect { subject.dictionary_object = dictionary }.to_not raise_error
|
440
|
+
expect(subject.dictionary_object).to eq dictionary
|
441
|
+
end
|
442
|
+
end
|
443
|
+
end
|
444
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe LittleWeasel::Services::DictionaryCreatorService do
|
6
|
+
include_context 'dictionary keys'
|
7
|
+
include_context 'mock word filters'
|
8
|
+
include_context 'mock word preprocessors'
|
9
|
+
|
10
|
+
subject { create(:dictionary_creator_service, dictionary_key: dictionary_key, dictionary_cache: dictionary_cache, dictionary_metadata: dictionary_metadata, word_filters: word_filters, word_preprocessors: word_preprocessors) }
|
11
|
+
|
12
|
+
let(:language) { :en }
|
13
|
+
let(:region) { :us }
|
14
|
+
let(:tag) {}
|
15
|
+
let(:dictionary_key) { create(:dictionary_key, language: language, region: region, tag: tag) }
|
16
|
+
let(:key) { dictionary_key.key }
|
17
|
+
let(:file) { dictionary_path_for(file_name: key) }
|
18
|
+
let(:dictionary_cache) { {} }
|
19
|
+
let(:dictionary_metadata) { {} }
|
20
|
+
let(:word_filters) {}
|
21
|
+
let(:word_preprocessors) {}
|
22
|
+
|
23
|
+
shared_examples 'it should' do
|
24
|
+
context 'with word_filters' do
|
25
|
+
context 'when argument word_filters contains word filters' do
|
26
|
+
let(:word_filters) do
|
27
|
+
[
|
28
|
+
DollarSignFilter.new,
|
29
|
+
LittleWeasel::Filters::EnUs::NumericFilter.new,
|
30
|
+
LittleWeasel::Filters::EnUs::SingleCharacterWordFilter.new
|
31
|
+
]
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'creates a dictionary with the word filters passed' do
|
35
|
+
expect(dictionary).to be_kind_of LittleWeasel::Dictionary
|
36
|
+
expect(dictionary.word_results('$').success?).to eq true
|
37
|
+
expect(dictionary.word_results('1000').success?).to eq true
|
38
|
+
expect(dictionary.word_results('A').success?).to eq true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'with NO word_filters' do
|
44
|
+
context 'when argument word_filters is an empty Array' do
|
45
|
+
let(:word_filters) { [] }
|
46
|
+
|
47
|
+
it 'creates a dictionary that uses no word filters' do
|
48
|
+
expect(dictionary).to be_kind_of LittleWeasel::Dictionary
|
49
|
+
expect(dictionary.word_filters).to be_blank
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'when argument word_filters is nil' do
|
54
|
+
let(:word_filters) {}
|
55
|
+
|
56
|
+
it 'creates a dictionary that uses no word filters' do
|
57
|
+
expect(dictionary).to be_kind_of LittleWeasel::Dictionary
|
58
|
+
expect(dictionary.word_filters).to be_blank
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'with word_preprocessors' do
|
64
|
+
let(:word_preprocessors) { [UpcaseWordPreprocessor.new(order: 0)] }
|
65
|
+
let(:word) { 'a' }
|
66
|
+
|
67
|
+
context 'when argument word_preprocessors contains word preprocessors' do
|
68
|
+
it 'creates a dictionary with the word preprocessors passed' do
|
69
|
+
expect(dictionary).to be_kind_of LittleWeasel::Dictionary
|
70
|
+
preprocessed_words = dictionary.word_results(word).preprocessed_words
|
71
|
+
expect(preprocessed_words.original_word).to eq word
|
72
|
+
expect(preprocessed_words.preprocessed_words.count).to eq 1
|
73
|
+
expect(preprocessed_words.preprocessed_words[0].original_word).to eq word
|
74
|
+
expect(preprocessed_words.preprocessed_words[0].preprocessed_word).to eq word.upcase
|
75
|
+
expect(preprocessed_words.preprocessed_words[0].preprocessor).to eq word_preprocessors[0].to_sym
|
76
|
+
expect(preprocessed_words.preprocessed_words[0].preprocessed).to eq true
|
77
|
+
expect(preprocessed_words.preprocessed_words[0].preprocessor_order).to eq word_preprocessors[0].order
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context 'with NO word_preprocessors' do
|
83
|
+
context 'when argument word_preprocessors is an empty Array' do
|
84
|
+
let(:word_preprocessors) { [] }
|
85
|
+
|
86
|
+
it 'creates a dictionary that uses no word preprocessors' do
|
87
|
+
expect(dictionary).to be_kind_of LittleWeasel::Dictionary
|
88
|
+
expect(dictionary.word_preprocessors).to be_blank
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'when argument word_preprocessors is nil' do
|
93
|
+
let(:word_preprocessors) {}
|
94
|
+
|
95
|
+
it 'creates a dictionary that uses no word preprocessors' do
|
96
|
+
expect(dictionary).to be_kind_of LittleWeasel::Dictionary
|
97
|
+
expect(dictionary.word_preprocessors).to be_blank
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
#from_file_source
|
104
|
+
describe '#from_file_source' do
|
105
|
+
let(:dictionary) { subject.from_file_source file: file }
|
106
|
+
|
107
|
+
it_behaves_like 'it should'
|
108
|
+
end
|
109
|
+
|
110
|
+
#from_memory_source
|
111
|
+
describe '#from_memory_source' do
|
112
|
+
let(:dictionary) { subject.from_memory_source dictionary_words: dictionary_words }
|
113
|
+
let(:dictionary_words) do
|
114
|
+
dictionary_words_for dictionary_file_path: file
|
115
|
+
end
|
116
|
+
|
117
|
+
it_behaves_like 'it should'
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe LittleWeasel::Services::DictionaryFileLoaderService do
|
6
|
+
include_context 'dictionary cache'
|
7
|
+
|
8
|
+
subject! { create(:dictionary_file_loader_service, dictionary_key: dictionary_key, dictionary_cache: dictionary_cache) }
|
9
|
+
|
10
|
+
before(:each) do
|
11
|
+
LittleWeasel::Services::DictionaryCacheService.init dictionary_cache: dictionary_cache
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:dictionary_key) { create(:dictionary_key, language: :en, region: :us) }
|
15
|
+
let(:key) { dictionary_key.key }
|
16
|
+
let(:dictionary_cache) { {} }
|
17
|
+
|
18
|
+
#execute
|
19
|
+
describe '#execute' do
|
20
|
+
context 'when the dictionary is already loaded/cached' do
|
21
|
+
before do
|
22
|
+
create(:dictionary_cache_service, dictionary_key: dictionary_key, dictionary_cache: dictionary_cache, dictionary_file_source: true, load: true)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'raises an error' do
|
26
|
+
expect { subject.execute }.to raise_error "The dictionary associated with key '#{key}' already exists."
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'when the dictionary is NOT already loaded/cached' do
|
31
|
+
before do
|
32
|
+
create(:dictionary_cache_service, dictionary_key: dictionary_key, dictionary_cache: dictionary_cache, dictionary_file_source: true)
|
33
|
+
end
|
34
|
+
|
35
|
+
let(:expected_key) { create(:dictionary_key, language: :en, region: :us).key }
|
36
|
+
let(:expected_results) do
|
37
|
+
['apple',
|
38
|
+
'better',
|
39
|
+
'cat',
|
40
|
+
'dog',
|
41
|
+
'everyone',
|
42
|
+
'fat',
|
43
|
+
'game',
|
44
|
+
'help',
|
45
|
+
'italic',
|
46
|
+
'jasmine',
|
47
|
+
'kelp',
|
48
|
+
'love',
|
49
|
+
'man',
|
50
|
+
'nope',
|
51
|
+
'octopus',
|
52
|
+
'popeye',
|
53
|
+
'queue',
|
54
|
+
'ruby',
|
55
|
+
'stop',
|
56
|
+
'top',
|
57
|
+
'ultimate',
|
58
|
+
'very',
|
59
|
+
'was',
|
60
|
+
'xylophone',
|
61
|
+
'yes',
|
62
|
+
'zebra']
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'returns an Array of dictionary words loaded from the file' do
|
66
|
+
expect(key).to eq expected_key
|
67
|
+
expect(subject.execute).to eq expected_results
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|