cognition 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b4789202824b62224c48afa61b4ed08722cc3d7
4
- data.tar.gz: f932bcd68552af1c176ee98bd8ebd25b9a7acf90
3
+ metadata.gz: 5b556dd703f6cedceb65c61a882701d00470e75c
4
+ data.tar.gz: 7290e4591bf836a68d124e7c2655a2b10fdff48c
5
5
  SHA512:
6
- metadata.gz: 381d41220b889ab675bfb1907433dc77630a082343cb0d39427ad083bcd605e7a221b55b02deba7f6bc09921922eeb49f654c0259df039a6fcf25f23c2982bfe
7
- data.tar.gz: fc1bf5ac97807a89fc7684ecb3ec045f38e6aff7b58ccc1043ddc00434fdb8e3932901bf9dc3c5c1a658eae6d7464aa2807f3f3008f881d2303df56c3d451994
6
+ metadata.gz: 46abe2f2530cc77c9e36004ff1821d9af9031a4f06107125e66687ab178014bdcc3a69b9eca8038bd2794b8e6ec7da5a5880d880ac1fe2a975da426bbb3d2825
7
+ data.tar.gz: d8c46b14fefa4d54e44e2afeed0f4d3eee41a50d0171296adde5540e40fdd8f083769fc424ada51a33ebd3affa965ac77002bc1b8c347c160b5e0f72308c5c73
data/Rakefile CHANGED
@@ -1 +1,9 @@
1
1
  require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc 'Run tests'
9
+ task :default => :test
@@ -1,3 +1,3 @@
1
1
  module Cognition
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
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("\n")}"
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,7 @@
1
+ class Hello < Cognition::Plugins::Base
2
+ match 'hello', 'hello: Returns Hello World', :hello
3
+
4
+ def hello(_msg)
5
+ 'Hello World'
6
+ end
7
+ 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
@@ -0,0 +1,11 @@
1
+ require 'minitest/autorun'
2
+ require 'cognition'
3
+
4
+ class PluginTest < Minitest::Test
5
+ def test_sets_matchers
6
+ require_relative "fixtures/hello"
7
+ hello = Hello.new
8
+
9
+ assert_equal 1, hello.matchers.count
10
+ end
11
+ end
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.1
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