summaryrefslogtreecommitdiff
path: root/test/plugins/test_points.rb
blob: b3386ed4769196bdecc3cb61ec6b10215c327124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
$:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
$:.unshift File.join(File.dirname(__FILE__), '..', '..')

require 'test/unit'
require 'test/mock'

require 'rbot/ircbot'
require 'rbot/registry'
require 'rbot/plugins'
require 'rbot/language'

class PointsPluginTest < Test::Unit::TestCase
  def setup
    manager = Irc::Bot::Plugins.manager
    manager.bot_associate(MockBot.new)
    manager.load_botmodule_file('./data/rbot/plugins/points.rb')
    @plugin = manager.get_plugin('points')
  end

  def test_points
    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)

    m = MockMessage.new('linux++', 'user')
    @plugin.message(m)
    assert_equal('linux now has 2 points!', m.replies.first)

    m = MockMessage.new('linux++', 'linux')
    @plugin.message(m)
    assert_empty(m.replies)

    m = MockMessage.new('', 'user')
    @plugin.points(m, key: 'linux')
    assert_equal('points for linux: 2', m.replies.first)

    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)

    # assign to multiple things
    m = MockMessage.new('hello linux++ hello torvalds++', 'user')
    @plugin.message(m)
    assert_equal(m.replies.length, 2)
    assert_equal('linux now has 3 points!', m.replies[0])
    assert_equal('torvalds now has 1 points!', m.replies[1])

    ignored = [
      '++alice',
      '--alice',
      'something something --github',
      'ls --sort time',
      '-- foo',
      '++ foo',
      'test ++',
      'test --',
      '<-- pointing',
      'pointing -->',
      '&++',
      ' ++',
      ' --',
      '++ --',
      '-- ++',
      '+rw+r++r++',
      '-rw-r--r--',
      'https://linux.slashdot.org/story/20/04/12/2138205/how-red-hats-new-ceo-handles-life-under-ibm----and-a-global-pandemic'
    ]
    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_equal('thanks :)', m.replies.first)
  end
end