Permalink
Browse files

adds conditional endings class + tests

  • Loading branch information...
1 parent 5bb7173 commit 2e97a2a82c456432a661324690fb656cd94b40c9 @Najaf Najaf committed Mar 4, 2013
View
@@ -1,9 +1,10 @@
require "dumb-answers/version"
module DumbAnswers
- autoload :YamlParser, 'dumb-answers/yaml_parser'
- autoload :Question, 'dumb-answers/question'
- autoload :Ending, 'dumb-answers/ending'
- autoload :State, 'dumb-answers/state'
- autoload :Tree, 'dumb-answers/tree'
+ autoload :YamlParser, 'dumb-answers/yaml_parser'
+ autoload :Question, 'dumb-answers/question'
+ autoload :Ending, 'dumb-answers/ending'
+ autoload :State, 'dumb-answers/state'
+ autoload :Tree, 'dumb-answers/tree'
+ autoload :ConditionalEnding, 'dumb-answers/conditional_ending'
end
@@ -0,0 +1,26 @@
+module DumbAnswers
+ class ConditionalEnding
+ attr_accessor :id, :conditional_texts
+ def initialize(attributes)
+ @id, @conditional_texts = *attributes.values_at('id', 'conditional_texts')
+ end
+
+ def to_ending(history)
+ Ending.new(
+ 'id' => @id,
+ 'text' => text_for(history)
+ )
+ end
+
+ private
+ def text_for(history)
+ applicable_endings = @conditional_texts.select do |conditional_text|
+ conditional_text['conditions'].each_pair.all? do |pair|
+ history.include? pair
+ end
+ end
+
+ applicable_endings == [] ? "" : applicable_endings.first['text']
+ end
+ end
+end
@@ -1,8 +1,9 @@
module DumbAnswers
module YamlParser
TYPES = {
- 'question' => Question,
- 'ending' => Ending
+ 'question' => Question,
+ 'ending' => Ending,
+ 'conditional_ending' => ConditionalEnding
}
def self.load(data)
@@ -0,0 +1,40 @@
+require 'spec_helper'
+
+describe ConditionalEnding do
+ describe '#initialize' do
+ before do
+ @ending = ConditionalEnding.new('id' => 'test-id', 'conditional_texts' => [
+ { 'conditions' => { '1' => 'a', '2' => 'a' }, 'text' => "Hello, world" }
+ ])
+ end
+ it 'sets id and conditional texts' do
+ @ending.id.must_equal 'test-id'
+ @ending.conditional_texts.must_equal [{ 'conditions' => { '1' => 'a', '2' => 'a' }, 'text' => "Hello, world" }]
+ end
+ end
+
+ describe '#to_ending' do
+ before do
+ @conditional_ending = ConditionalEnding.new('id' => 'to-ending-test-id', 'conditional_texts' => [
+ { 'conditions' => { '1' => 'a', '2' => 'a', '3' => 'a' }, 'text' => 'All as' },
+ { 'conditions' => { '1' => 'a', '2' => 'b', '3' => 'a' }, 'text' => 'Mix' },
+ { 'conditions' => { '1' => 'b', '2' => 'b', '3' => 'b' }, 'text' => 'All bs' }
+ ])
+
+ @valid_history = [ %w(1 a), %w(2 b), %w(3 a) ]
+ @invalid_history = [ %w(foo bar), %w(baz bong), %w(bar foo) ]
+ end
+
+ it 'returns an ending with the same id' do
+ @conditional_ending.to_ending(@valid_history).id.must_equal 'to-ending-test-id'
+ end
+
+ it 'returns an ending with the correct text' do
+ @conditional_ending.to_ending(@valid_history).text.must_equal 'Mix'
+ end
+
+ it 'returns empty text on non-relevant history' do
+ @conditional_ending.to_ending(@invalid_history).text.must_equal ''
+ end
+ end
+end
No changes.
@@ -13,3 +13,10 @@
- id: 'test-ending'
type: 'ending'
text: 'Good for you!'
+- id: 'test-conditional-ending'
+ type: 'conditional_ending'
+ conditional_texts:
+ - conditions: { 'test-question': 'a' }
+ ending: "You like cheese"
+ - conditions: { 'test-question': 'b' }
+ ending: "You don't like cheese... wierdo!"
View
@@ -7,7 +7,7 @@
end
it 'returns node array for given keys' do
- Tree.get('parser-test').length.must_equal 2
+ Tree.get('parser-test').length.must_equal 3
Tree.get('state-test').length.must_equal 4
end
end
View
@@ -7,16 +7,17 @@
end
it 'returns a hash expected size and keys' do
- @nodes.length.must_equal 2
+ @nodes.length.must_equal 3
end
it 'returns hash keyed by node ids' do
- @nodes.keys.must_equal %w(test-question test-ending)
+ @nodes.keys.must_equal %w(test-question test-ending test-conditional-ending)
end
it 'adds question/ending instances based on type' do
@nodes['test-question'].is_a?(Question).must_equal true
@nodes['test-ending'].is_a?(Ending).must_equal true
+ @nodes['test-conditional-ending'].is_a?(ConditionalEnding).must_equal true
end
end
end

0 comments on commit 2e97a2a

Please sign in to comment.