Sha256: 93a726629c3a65db1980df8c92d5ba11b1d94017594b6e46089ac34597a1f247

Contents?: true

Size: 1.79 KB

Versions: 11

Compression:

Stored size: 1.79 KB

Contents

require 'test/unit'
require File.dirname(__FILE__) + '/../lib/json-schema'

class JSONSchemaTest < Test::Unit::TestCase
  
  #
  # These tests are ONLY run if there is an appropriate JSON backend parser available
  #
  
  def test_schema_from_file
    data = {"a" => 5}
    assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
    data = {"a" => "bad"}
    assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
  end
  
  def test_data_from_file
    schema = {"type" => "object", "properties" => {"a" => {"type" => "integer"}}}
    assert(JSON::Validator.validate_uri(schema,File.join(File.dirname(__FILE__),"data/good_data_1.json")))
    assert(!JSON::Validator.validate_uri(schema,File.join(File.dirname(__FILE__),"data/bad_data_1.json")))
  end
  
  def test_data_from_json
    if JSON::Validator.json_backend != nil
      schema = {"type" => "object", "properties" => {"a" => {"type" => "integer"}}}
      assert(JSON::Validator.validate_json(schema, %Q({"a" : 5})))
      assert(!JSON::Validator.validate_json(schema, %Q({"a" : "poop"})))
    end
  end
  
  def test_both_from_file
    assert(JSON::Validator.validate_uri(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/good_data_1.json")))
    assert(!JSON::Validator.validate_uri(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/bad_data_1.json")))
  end
    
  def test_file_ref
    data = {"b" => {"a" => 5}}
    assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_2.json"),data))

    data = {"b" => {"a" => "boo"}}
    assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
json-schema-1.2.1 test/test_files.rb
json-schema-1.2.0 test/test_files.rb
json-schema-1.1.1 test/test_files.rb
json-schema-1.1.0 test/test_files.rb
json-schema-1.0.12 test/test_files.rb
json-schema-1.0.11 test/test_files.rb
json-schema-1.0.10 test/test_files.rb
json-schema-1.0.9 test/test_files.rb
json-schema-1.0.8 test/test_files.rb
json-schema-1.0.7 test/test_files.rb
json-schema-1.0.6 test/test_files.rb