{"id":16766459,"url":"https://github.com/yugui/jsonnetunit","last_synced_at":"2025-03-21T23:33:29.988Z","repository":{"id":38814239,"uuid":"64484992","full_name":"yugui/jsonnetunit","owner":"yugui","description":"Unit testing framework for Jsonnet","archived":false,"fork":false,"pushed_at":"2018-05-01T06:13:31.000Z","size":27,"stargazers_count":90,"open_issues_count":3,"forks_count":8,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-14T06:06:38.006Z","etag":null,"topics":["jsonnet","testing","xunit"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yugui.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-29T14:01:17.000Z","updated_at":"2024-07-11T20:51:18.000Z","dependencies_parsed_at":"2022-09-18T11:10:49.989Z","dependency_job_id":null,"html_url":"https://github.com/yugui/jsonnetunit","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yugui%2Fjsonnetunit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yugui%2Fjsonnetunit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yugui%2Fjsonnetunit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yugui%2Fjsonnetunit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yugui","download_url":"https://codeload.github.com/yugui/jsonnetunit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221820606,"owners_count":16886222,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jsonnet","testing","xunit"],"created_at":"2024-10-13T06:06:34.028Z","updated_at":"2024-10-28T11:15:16.605Z","avatar_url":"https://github.com/yugui.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JsonnetUnit\n\n[![Build Status](https://travis-ci.org/yugui/jsonnetunit.svg?branch=master)](https://travis-ci.org/yugui/jsonnetunit)\n\nJsonnetunit is a unit test framework for [Jsonnet](http://jsonnet.org/).\n\n[TOC]\n\n## Examples\n\n`example_test.jsonnet`:\n```jsonnet\nlocal test = import \"jsonnetunit/test.libsonnet\";\n\ntest.suite({\n    testIdentity: {actual: 1, expect: 1},\n    testNeg:      {actual: \"YAML\", expectNot: \"Markup Language\"},\n    testFact: {\n        local fact(n) = if n == 0 then 1 else n * fact(n-1),\n\n        actual: fact(10),\n        expect: 3628800\n    },\n})\n```\n\nThen, just evaluate your test file with `jsonnet`.\n```console\n$ jsonnet -J path/to/jsonnetunit example_test.jsonnet\n{\n    \"verify\": \"Passed 3 test cases\"\n}\n```\n\nOn failure, it emits an error report and exits with non-zero status.\n```console\n$ jsonnet -J path/to/jsonnetunit example_test.jsonnet\nRUNTIME ERROR: Failed 11/11 test cases:\ntestFoo: Expected 1 to be 2\ntestBar: Expected 1 to satisfy the function\ntestBaz: Expected 1 to satisfy the condition that the value is between 2 and 3\n      path/to/jsonnetunit/jsonnetunit/test.libsonnet:40:13-25   object \u003canonymous\u003e\n      During manifestation\n```\n\nSee `jsonnet/test/std_matchers_test.jsonnet` for more examples of other matchers.\n\n## How to install\n\nJust `git clone`.\n\n```console\n$ git clone https://github.com/yugui/jsonnetunit.git\n```\n\n## Getting started\n\n### How to write tests\n\n1.  Create a test file\n\n    Test files must be `.jsonnet` files which manifestize a result of `test.suite` function.\n\n    ```jsonnet\n    local test = import \"path/to/jsonnetunit/test.libsonnet\";\n    test.suite({\n    })\n    ```\n\n2.  Add test cases\n\n    `test.suite` function takes an object which contains fields prefixed with `test`.\n    You can add arbitrary number of such fields.  `test.suite` does not directly use any other fields.\n\n    Individual test fields must have at least two fields:\n\n    * `actual` field: There must be a field named `actual`. This is the actual value to be verified.\n    * expectation field: There must be another field which describes an expectation.  This expectation is used to verify the `actual` value.\n\n      ```jsonnet\n      test.suite({\n          testFoo: {\n              actual: std.length('foo'),\n              expect: 3,\n          },\n      })\n      ```\n\n      The interpretation of the expectation depends on the name of the expectation field. The name `expect` in the example means that it expects that `actual` field is equal to the given value.\n\n### Simple expectations\n\nExpectation Field Name | Description               | Example\n-----------------------|---------------------------|------------------------------\n`expect`               | value equality            | `{actual: 1+1, expect: 2}`\n`expectNot`            | value inequality          | `{actual: 1+1, expectNot: 3}`\n`expectLt`             | less than                 | `{actual: 1+1, expectLt: 3}`\n`expectLe`             | less than or equal to     | `{actual: 1+1, expectLe: 3}`\n`expectGt`             | greater than              | `{actual: 1+1, expectGt: 1}`\n`expectGe`             | greater than or equal to  | `{actual: 1+1, expectGe: 1}`\n\n### expectThat\n\nYou can describe an abitrary expectation with `expectThat`.\nThis expectation field takes a unary function or an object.\n\nThe function must take an actual value and it must return if the value satisfies your expectation in a boolean value.\n\n```jsonnet\n{\n  actual: ultimateAnswerToLifeTheUniverseAndEverything(),\n  expectThat: function(x) x == 42,\n}\n```\n\nWhen you pass an object, the object must have two fields `actual` and `result`.\nThe first field `actual` is overridden with the `actual` value of the test case on evaluation.\nThe second field `result` must be an boolean which describes whether `actual` field satisfies your expectation.\nIn this case, you can optionally specifies a custom `description` of the expectation.  This is used as a part of error message when the test case fails.\n\n```jsonnet\n{\n  actual: ultimateAnswerToLifeTheUniverseAndEverything(),\n  expectThat: {\n    actual: error \"to be overridden\",\n    result: self.actual == 42,\n    description: \"Expect %d to be equal to the known value\" % self.actual,\n  },\n}\n```\n\n### Custom expectation matcher\n\nYou can also define your own expectation matcher.\n\n1.  Define a binary function which takes `actual` and `expected` values. This function must return an object\n    derived from `matcher.jsonnet` and must have the following three fields.\n\n    * `satisfied`: (boolean) Returns if `self.actual` satisfies your expectation\n    * `positiveMessage`: (string) Returns an error message to be returned when `self.actual` does not satisfies your expectation.\n    * `negativeMessage`: (string) Returns an error message to be returned when `self.actual` does not satisfies the negation of the expectation.\n\n    e.g.\n    ```jsonnet\n    local setMatcher(actual, expected) = import \"jsonnetunit/matcher.libsonnet\" {\n        satisfied: std.set(actual) == std.set(expected),\n        positiveMessage: \"Expected \" + actual + \" to be equal to \" + expected + \" as a set\",\n        negativeMessage: \"Expected \" + actual + \" not to be equal to \" + expected + \" as a set\",\n    };\n    ```\n2.  Define your expectation field name in the `matchers` field of the test suite.\n\n    e.g.\n    ```jsonnet\n    test.suite({\n        testEq: {\n            actual: [6, 7, 2, 3, 7],\n            expectSetEq: [2, 3, 6, 7],\n        },\n        testNe: {\n            actual: [6, 7, 2, 3, 7],\n            expectSetNe: [1, 2, 3, 4, 5],\n         }\n    }) {\n        matchers+: {\n            // Define a new expectation field name \"expectSetEq\" for set equality\n            expectSetEq: {\n                matcher: setMatcher,\n                expectationType: true,\n            },\n             // Define a new expectation field name \"expectSetNe\" for set inequality\n            expectSetNe: {\n                matcher: setMatcher,\n                expectationType: false,\n            },\n        },\n    }\n    ```\n    \n### Running with [Bazel](https://bazel.build/)\nUse `jsonnet_test` rule.\n* `WORKSPACE`:\n  \n  ```bzl\n  ...\n  git_repository(\n      name = \"com_github_yugui_jsonnetunit\",\n      remote = \"https://github.com/yugui/jsonnetunit.git\",\n      tag = \"0.2.0\",\n  )\n  ```\n  \n* `BUILD`:\n  \n  ```bzl\n  load(\"@com_github_yugui_jsonnetunit//jsonnetunit:jsonnetunit.bzl\", \"jsonnet_test\")\n  \n  jsonnet_test(\n      name = \"your_test\",\n      src = \"your_test.jsonnet\",\n  )   \n  ```\n\n# Copyright\n\nCopyright 2016 Yuki Yugui Sonoda\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyugui%2Fjsonnetunit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyugui%2Fjsonnetunit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyugui%2Fjsonnetunit/lists"}