• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

json-schema-org/JSON-Schema-Test-Suite: A language agnostic test suite for the J ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

json-schema-org/JSON-Schema-Test-Suite

开源软件地址:

https://github.com/json-schema-org/JSON-Schema-Test-Suite

开源编程语言:

Python 100.0%

开源软件介绍:

JSON Schema Test Suite

Contributor Covenant Project Status: Active – The project has reached a stable, usable state and is being actively developed. Financial Contributors on Open Collective

Build Status

This repository contains a set of JSON objects that implementers of JSON Schema validation libraries can use to test their validators.

It is meant to be language agnostic and should require only a JSON parser. The conversion of the JSON objects into tests within a specific language and test framework of choice is left to be done by the validator implementer.

Coverage

All JSON Schema specification releases should be well covered by this suite, including drafts 2020-12, 2019-09, 07, 06, 04 and 03. Drafts 04 and 03 are considered "frozen" in that less effort is put in to backport new tests to these versions.

Additional coverage is always welcome, particularly for bugs encountered in real-world implementations. If you see anything missing or incorrect, please feel free to file an issue or submit a PR.

Introduction to the Test Suite Structure

The tests in this suite are contained in the tests directory at the root of this repository. Inside that directory is a subdirectory for each released version of the specification.

The structure and contents of each file in these directories is described below.

In addition to the version-specific subdirectories, two additional directories are present:

  1. draft-next/: containing tests for the next version of the specification whilst it is in development
  2. latest/: a symbolic link which points to the directory which is the most recent release (which may be useful for implementations providing specific entry points for validating against the latest version of the specification)

Inside each version directory there are a number of .json files each containing a collection of related tests. Often the grouping is by property under test, but not always. In addition to the .json files, each version directory contains one or more special subdirectories whose purpose is described below, and which contain additional .json files.

Each .json file consists of a single JSON array of test cases.

Terminology

For clarity, we first define this document's usage of some testing terminology:

term definition
test suite the entirety of the contents of this repository, containing tests for multiple different releases of the JSON Schema specification
test case a single schema, along with a description and an array of tests
test within a test case, a single test example, containing a description, instance and a boolean indicating whether the instance is valid under the test case schema
test runner a program, external to this repository and authored by a user of this suite, which is executing each of the tests in the suite

An example illustrating this structure is immediately below, and a JSON Schema containing a formal definition of the contents of test cases can be found alongside this README.

Sample Test Case

Here is a single test case, containing one or more tests:

{
    "description": "The test case description",
    "schema": { "type": "string" },
    "tests": [
        {
            "description": "a test with a valid instance",
            "data": "a string",
            "valid": true
        },
        {
            "description": "a test with an invalid instance",
            "data": 15,
            "valid": false
        }
    ]
}

Subdirectories Within Each Draft

There is currently only one additional subdirectory that may exist within each draft test directory.

This is:

  1. optional/: Contains tests that are considered optional.

Note, the optional/ subdirectory today conflates many reasons why a test may be optional -- it may be because tests within a particular file are indeed not required by the specification but still potentially useful to an implementer, or it may be because tests within it only apply to programming languages with particular functionality (in which case they are not truly optional in such a language). In the future this directory structure will be made richer to reflect these differences more clearly.

Using the Suite to Test a Validator Implementation

The test suite structure was described above.

If you are authoring a new validator implementation, or adding support for an additional version of the specification, this section describes:

  1. How to implement a test runner which passes tests to your validator
  2. Assumptions the suite makes about how the test runner will configure your validator
  3. Invariants the test suite claims to hold for its tests

How to Implement a Test Runner

Presented here is a possible implementation of a test runner. The precise steps described do not need to be followed exactly, but the results of your own procedure should produce the same effects.

To test a specific version:

  • Load any remote references described below and configure your implementation to retrieve them via their URIs

  • Walk the filesystem tree for that version's subdirectory and for each .json file found:

    • if the file is located in the root of the version directory:

      • for each test case present in the file:

        • load the schema from the "schema" property

        • load (or log) the test case description from the "description" property for debugging or outputting

        • for each test in the "tests" property:

          • load the instance to be tested from the "data" property

          • load (or log) the individual test description from the "description" property for debugging or outputting

          • use the schema loaded above to validate whether the instance is considered valid under your implementation

          • if the result from your implementation matches the value found in the "valid" property, your implementation correctly implements the specific example

          • if the result does not match, or your implementation errors or crashes, your implementation does not correctly implement the specific example

    • otherwise it is located in a special subdirectory as described above. Follow the additional assumptions and restrictions for the containing subdirectory, then run the test case as above.

If your implementation supports multiple versions, run the above procedure for each version supported, configuring your implementation as appropriate to call each version individually.

Additional Assumptions

  1. The suite, notably in its refRemote.json file in each draft, expects a number of remote references to be configured. These are JSON documents, identified by URI, which are used by the suite to test the behavior of the $ref keyword (and related keywords). Depending on your implementation, you may configure how to "register" these either by retrieving them from the remotes/ directory at the root of the repository, or you may execute bin/jsonschema_suite remotes using the executable in the bin/ directory, which will output a JSON object containing all of the remotes combined.

  2. Test cases found within special subdirectories may require additional configuration to run. In particular, tests within the optional/format subdirectory may require implementations to change the way they treat the "format"keyword (particularly on older drafts which did not have a notion of vocabularies).

Invariants & Guarantees

The test suite guarantees a number of things about tests it defines. Any deviation from the below is generally considered a bug. If you suspect one, please file an issue:

  1. All files containing test cases are valid JSON.

  2. The contents of the "schema" property in a test case are always valid JSON Schemas under the corresponding specification.

    The rationale behind this is that we are testing instances in a test's "data" element, and not the schema itself. A number of tests do test the validity of a schema itself, but do so by representing the schema as an instance inside a test, with the associated meta-schema in the "schema" property (via the "$ref" keyword):

    {
        "description": "Test the \"type\" schema keyword",
        "schema": {
            "$ref": "https://json-schema.org/draft/2019-09/schema"
         },
        "tests": [
            {
                "description": "Valid: string",
                "data": {
                    "type": "string"
                },
                "valid": true
            },
            {
                "description": "Invalid: null",
                "data": {
                    "type": null
                },
                "valid": false
            }
        ]
    }

    See below for some known limitations.

Known Limitations

This suite expresses its assertions about the behavior of an implementation within JSON Schema itself. Each test is the application of a schema to a particular instance. This means that the suite of tests can test against any behavior a schema can describe, and conversely cannot test against any behavior which a schema is incapable of representing, even if the behavior is mandated by the specification.

For example, a schema can require that a string is a URI-reference and even that it matches a certain pattern, but even though the specification contains recommendations about URIs being normalized, a JSON schema cannot today represent this assertion within the core vocabularies of the specifications, so no test covers this behavior.

Who Uses the Test Suite

This suite is being used by:

Clojure

Coffeescript

Common Lisp

C++

Dart

Elixir

Erlang

Go

Haskell

Java

JavaScript

Node.js

For node.js developers, the suite is also available as an npm package.

Node-specific support is maintained in a separate repository which also welcomes your contributions!

.NET

Perl

PHP

PostgreSQL

Python

Ruby

Rust

Scala

Swift

If you use it as well, please fork and send a pull request adding yourself to the list :).

Contributing

If you see something missing or incorrect, a pull request is most welcome!

There are some sanity checks in place for testing the test suite. You can run them with bin/jsonschema_suite check or tox. They will be run automatically by GitHub Actions as well.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap