]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
plugin(points): +/- must come after, closes #34
authorMatthias Hecker <mail@apoc.cc>
Mon, 6 Apr 2020 19:08:55 +0000 (21:08 +0200)
committerMatthias Hecker <mail@apoc.cc>
Mon, 6 Apr 2020 19:08:55 +0000 (21:08 +0200)
This modifies the karma/points plugin to ignore increment/
decrement suffixes. `--SOMETHING` is more trouble than its worth,
people will write --NAME as a signature, or paste a command
line argument, e.g. `ls --sort time` which causes issues.

I also added tests for the points plugin, the plan is
to make the plugin testing easier more "rubionic"

data/rbot/plugins/points.rb
lib/rbot/language.rb
test/mock.rb
test/plugins/test_points.rb

index 21157f3d00af7cf26b15d9592337aa7d1029082e..58f24501c096ad0dc8c74deb7991ca3249830c23 100644 (file)
@@ -78,13 +78,17 @@ class PointsPlugin < Plugin
     op = nil
     ac = Hash.new
     m.message.split.each_with_index do |tok, i|
+      # ignore preceeding +/-
+      if op && arg.nil?
+        op = nil
+      end
       tok.sub!(/[:,]$/, '') if i == 0
       catch :me_if_you_can do
         if m.channel.users[tok].nil?
-          if (tok =~ /^(?:--)(.*[^-].*)$/) || (tok =~ /^(.*[^-].*)(?:--)$/)
+          if tok =~ /^(.*[^-].*)(?:--)$/
             op, arg = '--', $1
             next
-          elsif (tok =~ /^(?:\+\+)(.*[^+].*)$/)||(tok =~ /^(.*[^+].*)(?:\+\+)$/)
+          elsif tok =~ /^(.*[^+].*)(?:\+\+)$/
             op, arg = '++', $1
             next
           end
index 7193c39e3577240e8219d089ea6f91585ab22ced..8055ea0e8e986ae588ff624408617daef803d0f0 100644 (file)
@@ -11,6 +11,9 @@ module Irc
 class Bot
   class Language
 
+    # Access needed for tests:
+    attr_reader :strings
+
     # This constant hash holds the mapping
     # from long language names to the usual POSIX
     # locale specifications
index f1ce92dda40d81678f22c196488a60f7c6dbab29..511daaba414c5cba844e34fe484791baa50da957 100644 (file)
@@ -1,6 +1,7 @@
 $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
 $:.unshift File.join(File.dirname(__FILE__), '..')
-
+#require 'rbot/logger'
+#Irc::Bot::LoggerManager.instance.set_level(5)
 
 module Irc
 class Bot
@@ -55,7 +56,7 @@ class MockMessage
   def initialize(message='', source='user')
     @message = message
     @sourcenick = source
-    @channel = Irc::Channel.new('#test', '', [], server: nil)
+    @channel = Irc::Channel.new('#test', '', ['bob'], server: nil)
     @replies = []
   end
 
index f1a7479dd49402093ebc9e9f39e95909652bd000..83018e2eb65131c02bafe17099ce2f493b573040 100644 (file)
@@ -21,6 +21,10 @@ class PointsPluginTest < Test::Unit::TestCase
     assert_not_nil(@plugin)
     assert_not_empty(@plugin.help(nil))
 
+    m = MockMessage.new('', 'user')
+    @plugin.points(m, key: 'linux')
+    assert_equal('linux has zero points', m.replies.first)
+
     m = MockMessage.new('linux++', 'user')
     @plugin.message(m)
     assert_equal('linux now has 1 points!', m.replies.first)
@@ -40,5 +44,31 @@ class PointsPluginTest < Test::Unit::TestCase
     m = MockMessage.new('', 'linux')
     @plugin.points(m, {})
     assert_equal('points for linux: 2', m.replies.first)
+
+    m = MockMessage.new('alice++', 'user')
+    @plugin.message(m)
+    assert_equal('alice now has 1 points!', m.replies.first)
+
+    ignored = [
+      '++alice',
+      '--alice',
+      'something something --github',
+      'ls --sort time',
+      '-- foo',
+      '++ foo',
+    ]
+    ignored.each do |ignore|
+      m = MockMessage.new(ignore, 'user')
+      @plugin.message(m)
+      assert_empty(m.replies, "message should've been ignored: #{ignore.inspect}")
+    end
+
+    m = MockMessage.new('bob++', 'user')
+    @plugin.message(m)
+    assert_equal('bob now has 1 points!', m.replies.first)
+
+    m = MockMessage.new('bot++', 'user')
+    @plugin.message(m)
+    assert_include(MockBot.new.lang.strings['thanks'], m.replies.first)
   end
 end