diff options
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/database_spec.rb | 31 | ||||
-rw-r--r-- | spec/lib/helpers_spec.rb | 15 | ||||
-rw-r--r-- | spec/lib/message_spec.rb | 46 | ||||
-rw-r--r-- | spec/lib/messages_spec.rb | 71 | ||||
-rw-r--r-- | spec/lib/ruleset_spec.rb | 27 |
5 files changed, 190 insertions, 0 deletions
diff --git a/spec/lib/database_spec.rb b/spec/lib/database_spec.rb new file mode 100644 index 0000000..8da04e4 --- /dev/null +++ b/spec/lib/database_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rspec' +require_relative '../../lib/chotto/database' + +RSpec.describe Chotto::Database do + let(:db_double) { double('Notmuch::Database') } + let(:db_instance_double) { double('Notmuch::Database') } + let(:query_double) { double('Notmuch::Query') } + let(:query) { 'from:baltar@battlestar.com' } + let(:path) { '/var/db/mail' } + let(:subject) { Chotto::Database } + + before do + expect(db_double).to receive(:new).with(path, mode: Notmuch::MODE_READ_WRITE).and_return(db_instance_double) + end + + it 'initializes the database instance' do + subject.new(path: path, db_class: db_double) + end + + describe '#search_messages' do + it 'uses the correct method chain' do + expect(db_instance_double).to receive(:query).with(query).and_return(query_double) + expect(query_double).to receive(:search_messages) + + db = subject.new(path: path, db_class: db_double) + db.search_messages(query) + end + end +end diff --git a/spec/lib/helpers_spec.rb b/spec/lib/helpers_spec.rb new file mode 100644 index 0000000..5b841e4 --- /dev/null +++ b/spec/lib/helpers_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'rspec' +require_relative '../../lib/chotto/helpers' + +RSpec.describe Chotto::Helpers do + let(:subject) { Chotto::Helpers } + describe '#header_name_from_dsl' do + it { expect(subject.header_name_from_dsl('word')).to eq('Word') } + it { expect(subject.header_name_from_dsl('wOrd')).to eq('Word') } + it { expect(subject.header_name_from_dsl('Word')).to eq('Word') } + + it { expect(subject.header_name_from_dsl('many_words')).to eq('Many-Words') } + end +end diff --git a/spec/lib/message_spec.rb b/spec/lib/message_spec.rb new file mode 100644 index 0000000..2605117 --- /dev/null +++ b/spec/lib/message_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require 'rspec' +require_relative '../../lib/chotto/message' +require_relative '../../lib/chotto/helpers' + +RSpec.describe Chotto::Message do + let(:tags) { [] } + let(:msg) { double('Notmuch::Message', tags: tags) } + + let(:subject) { Chotto::Message.new(msg: msg) } + + it 'reads headers' do + expect(msg).to receive(:header).with('A-Header') + + subject.a_header + end + + describe 'tags' do + it 'keeps tags state in memory' do + subject.tags << 'tag' + expect(subject.tags).to match ['tag'] + + subject.tags << 'tag2' + expect(subject.tags).to match %w[tag tag2] + end + + it 'allows to overwritte entire tags array' do + subject.tags = [1, 2, 3, 4] + expect(subject.tags).to match [1, 2, 3, 4] + end + end + + describe 'save!' do + let(:tags) { [1, 2, 3] } + it 'saves current tag array to db' do + expect(msg).to receive(:remove_all_tags) + tags.each do |tag| + expect(msg).to receive(:add_tag).with(tag.to_s) + end + + subject.tags = tags + subject.save! + end + end +end diff --git a/spec/lib/messages_spec.rb b/spec/lib/messages_spec.rb new file mode 100644 index 0000000..7550991 --- /dev/null +++ b/spec/lib/messages_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require 'rspec' +require_relative '../../lib/chotto/messages' + +RSpec.describe Chotto::Messages do + let(:subject) { Chotto::Messages.new(db: double('Notmuch')) } + + describe 'direct filters' do + it { expect(subject.filter('from:baltar@battlestar.com').query_string).to eq(' (from:baltar@battlestar.com)') } + + it { + expect(subject.filter('from:baltar@battlestar.com').filter('ship:galactica').query_string) + .to eq(' (from:baltar@battlestar.com) and (ship:galactica)') + } + end + + describe 'conjuction change' do + it { + expect(subject.filter('from:baltar@battlestar.com') + .or.filter('ship:galactica').query_string).to eq(' (from:baltar@battlestar.com) or (ship:galactica)') + } + + it { + expect(subject.filter('from:baltar@battlestar.com') + .or.filter('ship:galactica') + .filter('hair:long').query_string) + .to eq(' (from:baltar@battlestar.com) or (ship:galactica) or (hair:long)') + } + + it { + expect(subject.filter('from:baltar@battlestar.com') + .or.filter('ship:galactica') + .and.filter('hair:long').query_string) + .to eq(' (from:baltar@battlestar.com) or (ship:galactica) and (hair:long)') + } + end + + describe 'hash filters' do + context 'with singular values' do + it { expect(subject.filter(from: 'baltar@battlestar.com').query_string).to eq(' (from:baltar@battlestar.com)') } + + it { + expect(subject.filter(from: 'baltar@battlestar.com', ship: 'galactica').query_string) + .to eq(' (from:baltar@battlestar.com) and (ship:galactica)') + } + + it { + expect(subject.or.filter(from: 'baltar@battlestar.com', ship: 'galactica').query_string) + .to eq(' (from:baltar@battlestar.com) or (ship:galactica)') + } + end + + context 'with set of values' do + it { + expect(subject.filter(from: ['baltar@battlestar.com', + 'adama@battestar.com']).query_string) + .to eq(' (from:baltar@battlestar.com) or (from:adama@battestar.com)') + } + end + end + + context 'advanced filters' do + it { + expect(subject.filter(from: ['baltar@battlestar.com', 'adama@battestar.com']) + .and.filter(ship: 'battlestar') + .or.filter(ship: 'pegasus').query_string) + .to eq(' (from:baltar@battlestar.com) or (from:adama@battestar.com) and (ship:battlestar) or (ship:pegasus)') + } + end +end diff --git a/spec/lib/ruleset_spec.rb b/spec/lib/ruleset_spec.rb new file mode 100644 index 0000000..e7a6904 --- /dev/null +++ b/spec/lib/ruleset_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require 'rspec' +require_relative '../../lib/chotto/ruleset' +require_relative '../../lib/chotto/messages' + +RSpec.describe Chotto::RuleSet do + let(:db) { instance_double('Chotto::Database') } + let(:messages) { double('Chotto::Messages', mth: true) } + let(:rule) do + proc do + messages.mth + end + end + let(:subject) do + Chotto::RuleSet.new('a name', db, rule) + end + + describe '#run' do + it 'evaluates the run block' do + expect(Chotto::Messages).to receive(:new).with(db: db).and_return(messages) + expect(messages).to receive(:mth) + + subject.run + end + end +end |