aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormms <git@sapka.me>2024-11-12 23:36:48 +0100
committermms <git@sapka.me>2024-11-12 23:36:48 +0100
commit6d3ab5f655c83f78905dccede27e5b1da22cc97c (patch)
treea19a544d059e1efe43650672a7842945a361548d
parentc65e4885b999e90732bf37ee954540bae58a4880 (diff)
test: add specs to Mesage class
-rwxr-xr-xbin/chotto2
-rw-r--r--lib/chotto.rb2
-rw-r--r--lib/chotto/database.rb2
-rw-r--r--lib/chotto/message.rb15
-rw-r--r--lib/chotto/tags.rb12
-rw-r--r--spec/message_spec.rb46
-rw-r--r--spec/messages_spec.rb6
7 files changed, 60 insertions, 25 deletions
diff --git a/bin/chotto b/bin/chotto
index 0dabd48..3d66d4e 100755
--- a/bin/chotto
+++ b/bin/chotto
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby
-require_relative "../lib/chotto.rb"
+require_relative '../lib/chotto'
CONFIG_FILEPATH = 'chotto/config.rb'
XDG_HOME = ENV.fetch('XDG_CONFIG_HOME', "#{Dir.home}/.config")
diff --git a/lib/chotto.rb b/lib/chotto.rb
index 47a94c0..6afcf35 100644
--- a/lib/chotto.rb
+++ b/lib/chotto.rb
@@ -36,5 +36,3 @@ def eval_rules
rule_set.run
end
end
-
-
diff --git a/lib/chotto/database.rb b/lib/chotto/database.rb
index 78b80fe..2379b0b 100644
--- a/lib/chotto/database.rb
+++ b/lib/chotto/database.rb
@@ -5,7 +5,7 @@ module Chotto
attr_reader :db
def initialize(path:)
- @db = ::Notmuch::Database.new(path, mode: Notmuch::MODE_READ_WRITE)
+ @db = ::Notmuch::Database.new(path, mode: Notmuch::MODE_READ_WRITE)
end
def query(query)
diff --git a/lib/chotto/message.rb b/lib/chotto/message.rb
index ce7c067..ff382f8 100644
--- a/lib/chotto/message.rb
+++ b/lib/chotto/message.rb
@@ -2,6 +2,7 @@
module Chotto
class Message
+ attr_accessor :tags
attr_reader :message
def initialize(msg:)
@@ -9,22 +10,14 @@ module Chotto
@tags = @message.tags
end
- def method_missing(method_name, *_args)
- handle_get(Chotto::Helpers.header_name_from_dsl(method_name))
- end
+ def method_missing(method_name, *_args)
+ handle_get(Chotto::Helpers.header_name_from_dsl(method_name))
+ end
def handle_get(header_name)
message.header(header_name) if message.header(header_name)
end
- def tags
- @tags
- end
-
- def tags=(new_tags)
- @tags = new_tags
- end
-
def save!
message.remove_all_tags
tags.each do |tag|
diff --git a/lib/chotto/tags.rb b/lib/chotto/tags.rb
index 3a4fb14..52caeec 100644
--- a/lib/chotto/tags.rb
+++ b/lib/chotto/tags.rb
@@ -1,18 +1,16 @@
module Chotto
class Tags
- include Enumerable
+ include Enumerable
- attr_accessor :tags, :message
+ attr_accessor :tags, :message
def initialize(tags:, message:)
@tags = tags
@messae = message
end
- def each(&block)
- tags.each(&block)
- end
+ def each(&block)
+ tags.each(&block)
+ end
end
-
-
end
diff --git a/spec/message_spec.rb b/spec/message_spec.rb
new file mode 100644
index 0000000..5564d34
--- /dev/null
+++ b/spec/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)
+ end
+
+ subject.tags = tags
+ subject.save!
+ end
+ end
+end
diff --git a/spec/messages_spec.rb b/spec/messages_spec.rb
index 6a84bc3..ec43379 100644
--- a/spec/messages_spec.rb
+++ b/spec/messages_spec.rb
@@ -1,10 +1,10 @@
# frozen_string_literal: true
require 'rspec'
-require_relative '../lib/some/messages'
+require_relative '../lib/chotto/messages'
-RSpec.describe Some::Messages do
- let(:subject) { Some::Messages.new }
+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)') }