cognition 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +8 -0
- data/lib/cognition/version.rb +1 -1
- data/lib/cognition.rb +7 -6
- data/test/fixtures/hello.rb +7 -0
- data/test/test_cognition.rb +30 -0
- data/test/test_matcher.rb +53 -0
- data/test/test_plugin.rb +11 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b556dd703f6cedceb65c61a882701d00470e75c
|
4
|
+
data.tar.gz: 7290e4591bf836a68d124e7c2655a2b10fdff48c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46abe2f2530cc77c9e36004ff1821d9af9031a4f06107125e66687ab178014bdcc3a69b9eca8038bd2794b8e6ec7da5a5880d880ac1fe2a975da426bbb3d2825
|
7
|
+
data.tar.gz: d8c46b14fefa4d54e44e2afeed0f4d3eee41a50d0171296adde5540e40fdd8f083769fc424ada51a33ebd3affa965ac77002bc1b8c347c160b5e0f72308c5c73
|
data/Rakefile
CHANGED
data/lib/cognition/version.rb
CHANGED
data/lib/cognition.rb
CHANGED
@@ -9,9 +9,14 @@ module Cognition
|
|
9
9
|
|
10
10
|
attr_accessor :plugins, :matchers
|
11
11
|
|
12
|
+
def reset
|
13
|
+
@matchers = []
|
14
|
+
@plugins = []
|
15
|
+
register(Cognition::Plugins::Ping)
|
16
|
+
end
|
17
|
+
|
12
18
|
def register(klass)
|
13
19
|
return false if plugin_names.include? klass.to_s
|
14
|
-
|
15
20
|
plugins << klass.new
|
16
21
|
end
|
17
22
|
|
@@ -27,17 +32,13 @@ module Cognition
|
|
27
32
|
end
|
28
33
|
|
29
34
|
def help
|
30
|
-
"No such command:\n\n #{matchers.map(&:help).join(
|
35
|
+
"No such command:\n\n #{matchers.map(&:help).join('\n')}"
|
31
36
|
end
|
32
37
|
|
33
38
|
def matchers
|
34
39
|
plugins.collect(&:matchers).flatten.compact
|
35
40
|
end
|
36
41
|
|
37
|
-
def matcher_triggers
|
38
|
-
matchers.map(&:trigger)
|
39
|
-
end
|
40
|
-
|
41
42
|
def plugin_names
|
42
43
|
plugins.map { |p| p.class.name }
|
43
44
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'cognition'
|
3
|
+
require_relative 'fixtures/hello'
|
4
|
+
|
5
|
+
class CognitionTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
Cognition.reset
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_registers_plugins
|
11
|
+
Cognition.register(Hello)
|
12
|
+
|
13
|
+
assert_equal 2, Cognition.plugins.count
|
14
|
+
assert_instance_of Hello, Cognition.plugins.last
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_processes_messages
|
18
|
+
msg = Cognition::Message.new('ping')
|
19
|
+
assert_equal 'PONG', Cognition.process_message(msg)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_shows_help_if_no_matches
|
23
|
+
Cognition.register(Hello)
|
24
|
+
msg = Cognition::Message.new('pong')
|
25
|
+
output = Cognition.process_message(msg)
|
26
|
+
assert_match 'No such command:', output
|
27
|
+
assert_match 'ping: Returns "PONG"', output
|
28
|
+
assert_match 'hello: Returns Hello World', output
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'cognition'
|
3
|
+
|
4
|
+
class MatcherTest < Minitest::Test
|
5
|
+
def test_raises_error_without_a_trigger
|
6
|
+
assert_raises ArgumentError do
|
7
|
+
_ = Cognition::Matcher.new(action: 'foo')
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_raises_error_without_an_action
|
12
|
+
assert_raises ArgumentError do
|
13
|
+
_ = Cognition::Matcher.new(trigger: 'foo')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_matches_string
|
18
|
+
msg = Cognition::Message.new('help')
|
19
|
+
matcher = Cognition::Matcher.new('help', 'test', &Proc.new {})
|
20
|
+
assert matcher.matches?(msg)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_string_fails_with_invalid_message
|
24
|
+
msg = Cognition::Message.new('Help')
|
25
|
+
matcher = Cognition::Matcher.new('help', 'test', &Proc.new {})
|
26
|
+
refute matcher.matches?(msg)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_matches_regexp
|
30
|
+
msg = Cognition::Message.new('ping')
|
31
|
+
matcher = Cognition::Matcher.new(/ping/, 'test', &Proc.new {})
|
32
|
+
assert matcher.matches?(msg)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_regexp_fails_with_invalid_message
|
36
|
+
msg = Cognition::Message.new('pink')
|
37
|
+
matcher = Cognition::Matcher.new(/ping/, 'test', &Proc.new {})
|
38
|
+
refute matcher.matches?(msg)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_sets_response_on_attemp_if_matches
|
42
|
+
msg = Cognition::Message.new('ping')
|
43
|
+
matcher = Cognition::Matcher.new(/ping/, 'test', &Proc.new {'PONG'})
|
44
|
+
matcher.attempt(msg)
|
45
|
+
assert_equal 'PONG', matcher.response
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_returns_false_on_attemp_if_no_match
|
49
|
+
msg = Cognition::Message.new('pink')
|
50
|
+
matcher = Cognition::Matcher.new(/ping/, 'test', &Proc.new {'PONG'})
|
51
|
+
refute matcher.attempt(msg)
|
52
|
+
end
|
53
|
+
end
|
data/test/test_plugin.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cognition
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Anderson
|
@@ -71,6 +71,10 @@ files:
|
|
71
71
|
- lib/cognition/plugins/base.rb
|
72
72
|
- lib/cognition/plugins/ping.rb
|
73
73
|
- lib/cognition/version.rb
|
74
|
+
- test/fixtures/hello.rb
|
75
|
+
- test/test_cognition.rb
|
76
|
+
- test/test_matcher.rb
|
77
|
+
- test/test_plugin.rb
|
74
78
|
homepage: https://github.com/anoldguy/Cognition
|
75
79
|
licenses:
|
76
80
|
- MIT
|
@@ -95,4 +99,8 @@ rubygems_version: 2.4.5
|
|
95
99
|
signing_key:
|
96
100
|
specification_version: 4
|
97
101
|
summary: A rules engine for running commands.
|
98
|
-
test_files:
|
102
|
+
test_files:
|
103
|
+
- test/fixtures/hello.rb
|
104
|
+
- test/test_cognition.rb
|
105
|
+
- test/test_matcher.rb
|
106
|
+
- test/test_plugin.rb
|