Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

starts yaml parser + tests

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

0 notes on commit fd33cec

Please sign in to comment.
Something went wrong with that request. Please try again.