Sha256: 5bd89992302e602c658512f520456a815ecd3fbf88598e03727c74f96a9cc4f3

Contents?: true

Size: 1.8 KB

Versions: 3

Compression:

Stored size: 1.8 KB

Contents

require 'test/unit'
require 'rubygems'
require 'mocha/setup'
require 'parsers/parser'
require 'parsers/json_parser'
require 'helpers'

# Test case for the <tt>ChainReactor::Parsers::JsonParser</tt> class.
class TestJsonParser < Test::Unit::TestCase
  include ChainReactor::TestHelpers

  # Test that a ParseError is raised when an invalid JSON string is
  # passed.
  def test_parse_invalid_json_raises_error
    parser = ChainReactor::Parsers::JsonParser.new get_logger
    ex = assert_raise ChainReactor::Parsers::ParseError do
      parser.parse("not a json",[],false)
    end
    assert_match( /not a valid JSON/, ex.message)
  end

  # Test that a ParseError is raised when a JSON with invalid keys is
  # passed.
  def test_parse_wrong_json_raises_error
    parser = ChainReactor::Parsers::JsonParser.new get_logger
    ex = assert_raise ChainReactor::Parsers::RequiredKeyError do
      parser.parse('{"key1":"value","key2":"value"}',['monkey'],false)
    end
    assert_match(/Required key 'monkey'/, ex.message)
  end

  # Test that a valid JSON string returns a cause object with correct
  # data.
  def test_parse_valid_json_returns_hash
    name = "A name"
    type = "A type"
    json = "{\"name\":\"#{name}\",\"type\":\"#{type}\"}"
    parser = ChainReactor::Parsers::JsonParser.new get_logger
    cause = parser.parse(json,[],false)
    assert_equal name, cause['name']
    assert_equal type, cause['type']
  end

  # Test that a valid JSON string returns a cause object with correct
  # data.
  def test_parse_valid_json_returns_hash_with_symbol_keys
    name = "A name"
    type = "A type"
    json = "{\"name\":\"#{name}\",\"type\":\"#{type}\"}"
    parser = ChainReactor::Parsers::JsonParser.new get_logger
    cause = parser.parse(json,[],true)
    assert_equal name, cause[:name]
    assert_equal type, cause[:type]
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
chain-reactor-0.2.2 test/test_json_parser.rb
chain-reactor-0.2.1 test/test_json_parser.rb
chain-reactor-0.2.0 test/test_json_parser.rb