Permalink
Browse files

starts yaml parser + tests

  • Loading branch information...
1 parent 564767a commit fd33cec4a77a2b5fd1453560125cde68803cf9f8 @Najaf Najaf committed Feb 27, 2013
View
3 lib/dumb-answers.rb
@@ -1,4 +1,7 @@
require "dumb-answers/version"
module DumbAnswers
+ autoload :YamlParser, 'dumb-answers/yaml_parser'
+ autoload :Question, 'dumb-answers/question'
+ autoload :Ending, 'dumb-answers/ending'
end
View
4 lib/dumb-answers/ending.rb
@@ -0,0 +1,4 @@
+module DumbAnswers
+ class Ending
+ end
+end
View
5 lib/dumb-answers/question.rb
@@ -0,0 +1,5 @@
+module DumbAnswers
+ class Question
+ end
+end
+
View
15 lib/dumb-answers/yaml_parser.rb
@@ -0,0 +1,15 @@
+module DumbAnswers
+ module YamlParser
+ TYPES = {
+ 'question' => Question,
+ 'ending' => Ending
+ }
+
+ def self.load(data)
+ data.reduce({}) do |memo, node|
+ memo[node['id']] = TYPES[node['type']].new
+ memo
+ end
+ end
+ end
+end
View
15 spec/data/parser-test.yml
@@ -0,0 +1,15 @@
+- id: 'test-question'
+ type: question
+ text: "Do you like cheese?"
+ additional_text: "Some more text"
+ answers:
+ a: 'yes'
+ b: 'no'
+ transitions:
+ - conditions: { 'test-question-1': 'a' }
+ id: 'test-ending'
+ - conditions: { 'test-question-1': 'b' }
+ id: 'test-ending'
+- id: 'test-ending'
+ type: 'ending'
+ text: 'Good for you!'
View
6 spec/spec_helper.rb
@@ -2,10 +2,16 @@
require 'minitest/autorun'
require 'minitest/pride'
require 'turn'
+require 'yaml'
Turn.config.format = :outline
require 'dumb-answers'
include DumbAnswers
require 'minitest/matchers'
+
+def test_data(name)
+ data_dir = File.join(File.dirname(__FILE__), 'data')
+ YAML.load_file "#{data_dir}/#{name}.yml"
+end
View
22 spec/yaml_parser_spec.rb
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+describe YamlParser do
+ describe '.load' do
+ before do
+ @nodes = YamlParser.load test_data('parser-test')
+ end
+
+ it 'returns a hash expected size and keys' do
+ @nodes.length.must_equal 2
+ end
+
+ it 'returns hash keyed by node ids' do
+ @nodes.keys.must_equal %w(test-question test-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
+ end
+ end
+end

0 comments on commit fd33cec

Please sign in to comment.