From 91a39a8f11f540d180ca0b97666991aee346da31 Mon Sep 17 00:00:00 2001 From: Charles de Bueger Date: Sun, 5 Apr 2020 23:36:51 +0100 Subject: [PATCH] first stabs at build word chunks --- app/models/text_sample.rb | 12 +++++++ app/models/word_chunk.rb | 2 ++ spec/examples.txt | 61 ++++++++++++++++++++------------- spec/factories/text_sample.rb | 13 +++++-- spec/models/text_sample_spec.rb | 35 ++++++++++++++++--- spec/spec_helper.rb | 13 ++++--- spec/support/factory_bot.rb | 4 +-- 7 files changed, 102 insertions(+), 38 deletions(-) diff --git a/app/models/text_sample.rb b/app/models/text_sample.rb index 2b4d435..1a2da82 100644 --- a/app/models/text_sample.rb +++ b/app/models/text_sample.rb @@ -1,4 +1,16 @@ +# frozen_string_literal: true + class TextSample < ApplicationRecord validates :description, presence: true validates :text, presence: true + + def build_word_chunks + chunk_sizes = 2..8 + + chunk_sizes.each do |chunk_size| + build_word_chunks_of_size(chunk_size) unless text.size < chunk_size + end + end + + def build_word_chunks_of_size(chunk_size); end end diff --git a/app/models/word_chunk.rb b/app/models/word_chunk.rb index 8913c67..86b1344 100644 --- a/app/models/word_chunk.rb +++ b/app/models/word_chunk.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class WordChunk < ApplicationRecord belongs_to :text_sample diff --git a/spec/examples.txt b/spec/examples.txt index 0a2573a..ada0501 100644 --- a/spec/examples.txt +++ b/spec/examples.txt @@ -1,24 +1,37 @@ -example_id | status | run_time | ---------------------------------------------- | ------- | --------------- | -./spec/models/text_sample_spec.rb[1:1:1] | passed | 0.02027 seconds | -./spec/models/text_sample_spec.rb[1:1:2] | passed | 0.0024 seconds | -./spec/models/text_sample_spec.rb[1:2:1] | passed | 0.03488 seconds | -./spec/models/text_sample_spec.rb[1:2:2] | pending | 0.00001 seconds | -./spec/models/word_chunk_spec.rb[1:1:1] | passed | 0.00441 seconds | -./spec/models/word_chunk_spec.rb[1:1:2] | passed | 0.00293 seconds | -./spec/models/word_chunk_spec.rb[1:1:3] | passed | 0.00311 seconds | -./spec/models/word_chunk_spec.rb[1:1:4] | passed | 0.0458 seconds | -./spec/requests/text_samples_spec.rb[1:1:1] | passed | 0.01542 seconds | -./spec/requests/text_samples_spec.rb[1:2:1] | passed | 0.01541 seconds | -./spec/requests/text_samples_spec.rb[1:3:1] | passed | 0.01303 seconds | -./spec/requests/text_samples_spec.rb[1:4:1] | passed | 2.21 seconds | -./spec/requests/text_samples_spec.rb[1:5:1:1] | passed | 0.00878 seconds | -./spec/requests/text_samples_spec.rb[1:5:1:2] | failed | 0.00064 seconds | -./spec/requests/text_samples_spec.rb[1:5:1:3] | passed | 0.00876 seconds | -./spec/requests/text_samples_spec.rb[1:5:2:1] | passed | 0.016 seconds | -./spec/requests/text_samples_spec.rb[1:5:2:2] | passed | 0.01428 seconds | -./spec/requests/text_samples_spec.rb[1:6:1:1] | pending | 0.00949 seconds | -./spec/requests/text_samples_spec.rb[1:6:1:2] | passed | 0.01169 seconds | -./spec/requests/text_samples_spec.rb[1:6:2:1] | passed | 0.0159 seconds | -./spec/requests/text_samples_spec.rb[1:7:1] | passed | 0.01494 seconds | -./spec/requests/text_samples_spec.rb[1:7:2] | passed | 0.01152 seconds | +example_id | status | run_time | +----------------------------------------------------- | ------- | --------------- | +./spec/helpers/text_samples_helper_spec.rb[1:1] | pending | 0.00003 seconds | +./spec/models/text_sample_spec.rb[1:1:1] | passed | 0.00371 seconds | +./spec/models/text_sample_spec.rb[1:1:2] | passed | 0.00463 seconds | +./spec/models/text_sample_spec.rb[1:2:1:1] | passed | 0.03304 seconds | +./spec/models/text_sample_spec.rb[1:2:1:2] | passed | 0.0036 seconds | +./spec/models/word_chunk_spec.rb[1:1:1] | passed | 0.00654 seconds | +./spec/models/word_chunk_spec.rb[1:1:2] | passed | 0.0232 seconds | +./spec/models/word_chunk_spec.rb[1:1:3] | passed | 0.00385 seconds | +./spec/models/word_chunk_spec.rb[1:1:4] | passed | 0.0031 seconds | +./spec/requests/text_samples_spec.rb[1:1:1] | passed | 0.02365 seconds | +./spec/requests/text_samples_spec.rb[1:2:1] | passed | 0.01393 seconds | +./spec/requests/text_samples_spec.rb[1:3:1] | passed | 2.24 seconds | +./spec/requests/text_samples_spec.rb[1:4:1] | passed | 0.03572 seconds | +./spec/requests/text_samples_spec.rb[1:5:1:1] | passed | 0.00784 seconds | +./spec/requests/text_samples_spec.rb[1:5:1:2] | failed | 0.01819 seconds | +./spec/requests/text_samples_spec.rb[1:5:1:3] | passed | 0.00772 seconds | +./spec/requests/text_samples_spec.rb[1:5:2:1] | passed | 0.01638 seconds | +./spec/requests/text_samples_spec.rb[1:5:2:2] | passed | 0.01606 seconds | +./spec/requests/text_samples_spec.rb[1:6:1:1] | pending | 0.01364 seconds | +./spec/requests/text_samples_spec.rb[1:6:1:2] | passed | 0.01511 seconds | +./spec/requests/text_samples_spec.rb[1:6:2:1] | passed | 0.04019 seconds | +./spec/requests/text_samples_spec.rb[1:7:1] | passed | 0.01801 seconds | +./spec/requests/text_samples_spec.rb[1:7:2] | passed | 0.01515 seconds | +./spec/routing/text_samples_routing_spec.rb[1:1:1] | passed | 0.00081 seconds | +./spec/routing/text_samples_routing_spec.rb[1:1:2] | passed | 0.00114 seconds | +./spec/routing/text_samples_routing_spec.rb[1:1:3] | passed | 0.00078 seconds | +./spec/routing/text_samples_routing_spec.rb[1:1:4] | passed | 0.00087 seconds | +./spec/routing/text_samples_routing_spec.rb[1:1:5] | passed | 0.00143 seconds | +./spec/routing/text_samples_routing_spec.rb[1:1:6] | passed | 0.00099 seconds | +./spec/routing/text_samples_routing_spec.rb[1:1:7] | passed | 0.0039 seconds | +./spec/routing/text_samples_routing_spec.rb[1:1:8] | passed | 0.00101 seconds | +./spec/views/text_samples/edit.html.erb_spec.rb[1:1] | passed | 0.00769 seconds | +./spec/views/text_samples/index.html.erb_spec.rb[1:1] | passed | 0.01657 seconds | +./spec/views/text_samples/new.html.erb_spec.rb[1:1] | passed | 0.02457 seconds | +./spec/views/text_samples/show.html.erb_spec.rb[1:1] | passed | 0.01983 seconds | diff --git a/spec/factories/text_sample.rb b/spec/factories/text_sample.rb index 7873528..ec305f6 100644 --- a/spec/factories/text_sample.rb +++ b/spec/factories/text_sample.rb @@ -1,7 +1,14 @@ +# frozen_string_literal: true + # This will guess the TextSample class FactoryBot.define do - factory :text_sample_simple, class: TextSample do - description { 'Simple' } - text { 'The man in the mirror' } + factory :text_sample_two_chars, class: TextSample do + description { 'Two characters' } + text { 'At' } + end + + factory :text_sample_three_chars, class: TextSample do + description { 'Three characters' } + text { 'The' } end end diff --git a/spec/models/text_sample_spec.rb b/spec/models/text_sample_spec.rb index a62c060..5e234fc 100644 --- a/spec/models/text_sample_spec.rb +++ b/spec/models/text_sample_spec.rb @@ -1,14 +1,41 @@ +# frozen_string_literal: true + require 'rails_helper' -RSpec.describe TextSample, type: :model do +RSpec.describe TextSample, type: :model do # rubocop:disable Metrics/BlockLength describe 'validations' do it { should validate_presence_of(:description) } it { should validate_presence_of(:text) } end + # describe '::class_method' do describe '#build_word_chunks' do - let(:text_sample){ create(:text_sample_simple) } - it 'counts word chunks' - it 'assigns the word chunks to this text sample' + describe 'builds different sized chunks' do + it 'handles 2 character chunks' do + text_sample = create(:text_sample_two_chars) + allow(text_sample).to receive(:build_word_chunks_of_size) + text_sample.build_word_chunks + + expect(text_sample).to( + have_received(:build_word_chunks_of_size).with(2).once + ) + expect(text_sample).not_to( + have_received(:build_word_chunks_of_size).with(3) + ) + end + + it 'handles 3 character chunks' do + text_sample = create(:text_sample_three_chars) + allow(text_sample).to receive(:build_word_chunks_of_size) + text_sample.build_word_chunks + + expect(text_sample).to( + have_received(:build_word_chunks_of_size).with(2).once + ) + expect(text_sample).to( + have_received(:build_word_chunks_of_size).with(3).once + ) + end + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 72785f2..25fa303 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This file was generated by the `rails generate rspec:install` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause @@ -44,8 +46,8 @@ # triggering implicit auto-inclusion in groups with matching metadata. config.shared_context_metadata_behavior = :apply_to_host_groups -# The settings below are suggested to provide a good initial experience -# with RSpec, but feel free to customize to your heart's content. + # The settings below are suggested to provide a good initial experience + # with RSpec, but feel free to customize to your heart's content. # This allows you to limit a spec run to individual examples or groups # you care about by tagging them with `:focus` metadata. When nothing # is tagged with `:focus`, all examples get run. RSpec also provides @@ -56,7 +58,7 @@ # Allows RSpec to persist some state between runs in order to support # the `--only-failures` and `--next-failure` CLI options. We recommend # you configure your source control system to ignore this file. - config.example_status_persistence_file_path = "spec/examples.txt" + config.example_status_persistence_file_path = 'spec/examples.txt' # Limits the available syntax to the non-monkey patched syntax that is # recommended. For more details, see: @@ -72,7 +74,7 @@ # Use the documentation formatter for detailed output, # unless a formatter has already been configured # (e.g. via a command-line flag). - config.default_formatter = "doc" + config.default_formatter = 'doc' end # Print the 10 slowest examples and example groups at the @@ -91,4 +93,7 @@ # test failures related to randomization by passing the same `--seed` value # as the one that triggered the failure. Kernel.srand config.seed + + # CdeB custom changes + config.default_formatter = 'doc' end diff --git a/spec/support/factory_bot.rb b/spec/support/factory_bot.rb index 387af78..c7890e4 100644 --- a/spec/support/factory_bot.rb +++ b/spec/support/factory_bot.rb @@ -1,5 +1,3 @@ -puts "ljksfdlkfjlkjsdlkfdklfj" - RSpec.configure do |config| config.include FactoryBot::Syntax::Methods -end \ No newline at end of file +end