diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/chotto_spec.rb | 52 | ||||
-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 (renamed from spec/message_spec.rb) | 6 | ||||
-rw-r--r-- | spec/lib/messages_spec.rb (renamed from spec/messages_spec.rb) | 2 | ||||
-rw-r--r-- | spec/lib/ruleset_spec.rb | 27 |
6 files changed, 129 insertions, 4 deletions
diff --git a/spec/chotto_spec.rb b/spec/chotto_spec.rb new file mode 100644 index 0000000..9e8650b --- /dev/null +++ b/spec/chotto_spec.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require 'rspec' +require_relative '../lib/chotto' + +describe Chotto do + let(:db_double) { double('::Notmuch::Database') } + let(:db_instance_double) { double('::Notmuch::Database') } + let(:db_query_double) { double('::Notmuch::Query') } + let(:message_double) { double('::Notmuch::Message') } + + before do + $db_double = db_double # we need to access this double within block evaled elsewhere + + allow(db_double).to receive(:new).with('/path', mode: Notmuch::MODE_READ_WRITE).and_return(db_instance_double) + + allow(db_instance_double).to receive(:query).with(' (from:baltar@galactica.com)').and_return(db_query_double) + allow(db_query_double).to receive(:search_messages).and_return([message_double]) + + Chotto.configure do + config.database_path = '/path' + config.db_class = $db_double + end + + Chotto.rule_set 'sample' do + messages.or.filter(from: 'baltar@galactica.com').each do |msg| + msg.tags = msg.subject == 'Number Six' ? [:todo] : [:spam] + msg.save! + end + end + end + + it 'sends the rules to Notmuch - scenario 1' do + expect(message_double).to receive(:tags).and_return([]) + expect(message_double).to receive(:header).with('Subject').and_return('Number Six') + expect(message_double).to receive(:remove_all_tags) + expect(message_double).to receive(:add_tag).with('todo') + expect(db_instance_double).to receive(:close) + + eval_rules + end + + it 'sends the rules to Notmuch - scenario 2' do + expect(message_double).to receive(:tags).and_return([]) + expect(message_double).to receive(:header).with('Subject').and_return('Boomer') + expect(message_double).to receive(:remove_all_tags) + expect(message_double).to receive(:add_tag).with('spam') + expect(db_instance_double).to receive(:close) + + eval_rules + end +end 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/message_spec.rb b/spec/lib/message_spec.rb index 5564d34..2605117 100644 --- a/spec/message_spec.rb +++ b/spec/lib/message_spec.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true require 'rspec' -require_relative '../lib/chotto/message' -require_relative '../lib/chotto/helpers' +require_relative '../../lib/chotto/message' +require_relative '../../lib/chotto/helpers' RSpec.describe Chotto::Message do let(:tags) { [] } @@ -36,7 +36,7 @@ RSpec.describe Chotto::Message do 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) + expect(msg).to receive(:add_tag).with(tag.to_s) end subject.tags = tags diff --git a/spec/messages_spec.rb b/spec/lib/messages_spec.rb index ec43379..7550991 100644 --- a/spec/messages_spec.rb +++ b/spec/lib/messages_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require 'rspec' -require_relative '../lib/chotto/messages' +require_relative '../../lib/chotto/messages' RSpec.describe Chotto::Messages do let(:subject) { Chotto::Messages.new(db: double('Notmuch')) } 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 |