├── .gitignore ├── .travis.yml ├── .travis └── install-bazel.sh ├── BUILD.bazel ├── LICENSE ├── README.md ├── WORKSPACE └── jsonnetunit ├── BUILD.bazel ├── jsonnetunit.bzl ├── matcher.libsonnet ├── std_matchers.libsonnet ├── test.libsonnet └── test ├── BUILD.bazel ├── failure_test.jsonnet ├── golden ├── failure_test.golden ├── std_matchers_failure_test.golden └── test_test.golden ├── std_matchers_failure_test.jsonnet ├── std_matchers_test.jsonnet └── test_test.jsonnet /.gitignore: -------------------------------------------------------------------------------- 1 | /bazel-* 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | os: linux 2 | dist: trusty 3 | sudo: false 4 | language: java 5 | jdk: oraclejdk8 6 | 7 | branches: 8 | only: 9 | - master 10 | 11 | before_cache: 12 | - rm -rf $HOME/.cache/bazel/_bazel_travis/install/ 13 | cache: 14 | directories: 15 | - $HOME/local 16 | - $HOME/.bazel 17 | - $HOME/.cache/bazel 18 | 19 | addons: 20 | apt: 21 | sources: 22 | - ubuntu-toolchain-r-test 23 | packages: 24 | - libstdc++6 25 | - build-essential 26 | - g++ 27 | 28 | before_install: 29 | - .travis/install-bazel.sh 0.13.0 30 | - bazel version 31 | - bazel info 32 | - bazel fetch --curses=no -- "//... -//:jsonnetunit-docs" 33 | install: 34 | - bazel build $BAZEL_BUILD_OPTS --show_progress_rate_limit 0 -- //... -//:jsonnetunit-docs 35 | - bazel build $BAZEL_BUILD_OPTS -- //:jsonnetunit-docs 36 | script: 37 | - bazel test $BAZEL_BUILD_OPTS --show_progress_rate_limit 0 --test_output=streamed -- //... 38 | 39 | env: 40 | global: 41 | - PATH=$PATH:$HOME/local/bin 42 | - BAZEL_BUILD_OPTS="--curses=no --genrule_strategy=standalone --spawn_strategy=standalone --verbose_failures -j 20" 43 | -------------------------------------------------------------------------------- /.travis/install-bazel.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -eu 2 | VERSION=$1 3 | if [ -z "$VERSION" ]; then 4 | echo "Usage: install-bazel.sh BAZEL_VERSION" >&2 5 | exit 1 6 | fi 7 | 8 | INSTALLER=bazel-$VERSION-installer-linux-x86_64.sh 9 | RELEASE_BASE_URI=https://github.com/bazelbuild/bazel/releases/download/$VERSION 10 | 11 | REQUIRE_INSTALL=true 12 | if test -x $HOME/local/$INSTALLER; then 13 | cd $HOME/local 14 | if curl $RELEASE_BASE_URI/$INSTALLER.sha256 | sha256sum -c; then 15 | REQUIRE_INSTALL=false 16 | fi 17 | fi 18 | 19 | if $REQUIRE_INSTALL; then 20 | rm -rf $HOME/local 21 | mkdir $HOME/local 22 | cd $HOME/local 23 | wget $RELEASE_BASE_URI/$INSTALLER 24 | chmod +x $INSTALLER 25 | ./$INSTALLER --prefix=$HOME/local --base=$HOME/.bazel 26 | fi 27 | -------------------------------------------------------------------------------- /BUILD.bazel: -------------------------------------------------------------------------------- 1 | load("@io_bazel_skydoc//skylark:skylark.bzl", "skylark_doc") 2 | 3 | licenses(["notice"]) # Apache 2.0 4 | 5 | skylark_doc( 6 | name = "jsonnetunit-docs", 7 | srcs = ["//jsonnetunit:jsonnetunit.bzl"], 8 | ) 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JsonnetUnit 2 | 3 | [![Build Status](https://travis-ci.org/yugui/jsonnetunit.svg?branch=master)](https://travis-ci.org/yugui/jsonnetunit) 4 | 5 | Jsonnetunit is a unit test framework for [Jsonnet](http://jsonnet.org/). 6 | 7 | [TOC] 8 | 9 | ## Examples 10 | 11 | `example_test.jsonnet`: 12 | ```jsonnet 13 | local test = import "jsonnetunit/test.libsonnet"; 14 | 15 | test.suite({ 16 | testIdentity: {actual: 1, expect: 1}, 17 | testNeg: {actual: "YAML", expectNot: "Markup Language"}, 18 | testFact: { 19 | local fact(n) = if n == 0 then 1 else n * fact(n-1), 20 | 21 | actual: fact(10), 22 | expect: 3628800 23 | }, 24 | }) 25 | ``` 26 | 27 | Then, just evaluate your test file with `jsonnet`. 28 | ```console 29 | $ jsonnet -J path/to/jsonnetunit example_test.jsonnet 30 | { 31 | "verify": "Passed 3 test cases" 32 | } 33 | ``` 34 | 35 | On failure, it emits an error report and exits with non-zero status. 36 | ```console 37 | $ jsonnet -J path/to/jsonnetunit example_test.jsonnet 38 | RUNTIME ERROR: Failed 11/11 test cases: 39 | testFoo: Expected 1 to be 2 40 | testBar: Expected 1 to satisfy the function 41 | testBaz: Expected 1 to satisfy the condition that the value is between 2 and 3 42 | path/to/jsonnetunit/jsonnetunit/test.libsonnet:40:13-25 object 43 | During manifestation 44 | ``` 45 | 46 | See `jsonnet/test/std_matchers_test.jsonnet` for more examples of other matchers. 47 | 48 | ## How to install 49 | 50 | Just `git clone`. 51 | 52 | ```console 53 | $ git clone https://github.com/yugui/jsonnetunit.git 54 | ``` 55 | 56 | ## Getting started 57 | 58 | ### How to write tests 59 | 60 | 1. Create a test file 61 | 62 | Test files must be `.jsonnet` files which manifestize a result of `test.suite` function. 63 | 64 | ```jsonnet 65 | local test = import "path/to/jsonnetunit/test.libsonnet"; 66 | test.suite({ 67 | }) 68 | ``` 69 | 70 | 2. Add test cases 71 | 72 | `test.suite` function takes an object which contains fields prefixed with `test`. 73 | You can add arbitrary number of such fields. `test.suite` does not directly use any other fields. 74 | 75 | Individual test fields must have at least two fields: 76 | 77 | * `actual` field: There must be a field named `actual`. This is the actual value to be verified. 78 | * expectation field: There must be another field which describes an expectation. This expectation is used to verify the `actual` value. 79 | 80 | ```jsonnet 81 | test.suite({ 82 | testFoo: { 83 | actual: std.length('foo'), 84 | expect: 3, 85 | }, 86 | }) 87 | ``` 88 | 89 | 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. 90 | 91 | ### Simple expectations 92 | 93 | Expectation Field Name | Description | Example 94 | -----------------------|---------------------------|------------------------------ 95 | `expect` | value equality | `{actual: 1+1, expect: 2}` 96 | `expectNot` | value inequality | `{actual: 1+1, expectNot: 3}` 97 | `expectLt` | less than | `{actual: 1+1, expectLt: 3}` 98 | `expectLe` | less than or equal to | `{actual: 1+1, expectLe: 3}` 99 | `expectGt` | greater than | `{actual: 1+1, expectGt: 1}` 100 | `expectGe` | greater than or equal to | `{actual: 1+1, expectGe: 1}` 101 | 102 | ### expectThat 103 | 104 | You can describe an abitrary expectation with `expectThat`. 105 | This expectation field takes a unary function or an object. 106 | 107 | The function must take an actual value and it must return if the value satisfies your expectation in a boolean value. 108 | 109 | ```jsonnet 110 | { 111 | actual: ultimateAnswerToLifeTheUniverseAndEverything(), 112 | expectThat: function(x) x == 42, 113 | } 114 | ``` 115 | 116 | When you pass an object, the object must have two fields `actual` and `result`. 117 | The first field `actual` is overridden with the `actual` value of the test case on evaluation. 118 | The second field `result` must be an boolean which describes whether `actual` field satisfies your expectation. 119 | In 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. 120 | 121 | ```jsonnet 122 | { 123 | actual: ultimateAnswerToLifeTheUniverseAndEverything(), 124 | expectThat: { 125 | actual: error "to be overridden", 126 | result: self.actual == 42, 127 | description: "Expect %d to be equal to the known value" % self.actual, 128 | }, 129 | } 130 | ``` 131 | 132 | ### Custom expectation matcher 133 | 134 | You can also define your own expectation matcher. 135 | 136 | 1. Define a binary function which takes `actual` and `expected` values. This function must return an object 137 | derived from `matcher.jsonnet` and must have the following three fields. 138 | 139 | * `satisfied`: (boolean) Returns if `self.actual` satisfies your expectation 140 | * `positiveMessage`: (string) Returns an error message to be returned when `self.actual` does not satisfies your expectation. 141 | * `negativeMessage`: (string) Returns an error message to be returned when `self.actual` does not satisfies the negation of the expectation. 142 | 143 | e.g. 144 | ```jsonnet 145 | local setMatcher(actual, expected) = import "jsonnetunit/matcher.libsonnet" { 146 | satisfied: std.set(actual) == std.set(expected), 147 | positiveMessage: "Expected " + actual + " to be equal to " + expected + " as a set", 148 | negativeMessage: "Expected " + actual + " not to be equal to " + expected + " as a set", 149 | }; 150 | ``` 151 | 2. Define your expectation field name in the `matchers` field of the test suite. 152 | 153 | e.g. 154 | ```jsonnet 155 | test.suite({ 156 | testEq: { 157 | actual: [6, 7, 2, 3, 7], 158 | expectSetEq: [2, 3, 6, 7], 159 | }, 160 | testNe: { 161 | actual: [6, 7, 2, 3, 7], 162 | expectSetNe: [1, 2, 3, 4, 5], 163 | } 164 | }) { 165 | matchers+: { 166 | // Define a new expectation field name "expectSetEq" for set equality 167 | expectSetEq: { 168 | matcher: setMatcher, 169 | expectationType: true, 170 | }, 171 | // Define a new expectation field name "expectSetNe" for set inequality 172 | expectSetNe: { 173 | matcher: setMatcher, 174 | expectationType: false, 175 | }, 176 | }, 177 | } 178 | ``` 179 | 180 | ### Running with [Bazel](https://bazel.build/) 181 | Use `jsonnet_test` rule. 182 | * `WORKSPACE`: 183 | 184 | ```bzl 185 | ... 186 | git_repository( 187 | name = "com_github_yugui_jsonnetunit", 188 | remote = "https://github.com/yugui/jsonnetunit.git", 189 | tag = "0.2.0", 190 | ) 191 | ``` 192 | 193 | * `BUILD`: 194 | 195 | ```bzl 196 | load("@com_github_yugui_jsonnetunit//jsonnetunit:jsonnetunit.bzl", "jsonnet_test") 197 | 198 | jsonnet_test( 199 | name = "your_test", 200 | src = "your_test.jsonnet", 201 | ) 202 | ``` 203 | 204 | # Copyright 205 | 206 | Copyright 2016 Yuki Yugui Sonoda 207 | 208 | Licensed under the Apache License, Version 2.0 (the "License"); 209 | you may not use this file except in compliance with the License. 210 | You may obtain a copy of the License at 211 | 212 | http://www.apache.org/licenses/LICENSE-2.0 213 | 214 | Unless required by applicable law or agreed to in writing, software 215 | distributed under the License is distributed on an "AS IS" BASIS, 216 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 217 | See the License for the specific language governing permissions and 218 | limitations under the License. 219 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 1 | workspace(name = "com_github_yugui_jsonnetunit") 2 | 3 | # jsonnet_test refers an external repository @com_github_yugui_jsonnetunit. 4 | local_repository( 5 | name = "com_github_yugui_jsonnetunit", 6 | path = ".", 7 | ) 8 | 9 | git_repository( 10 | name = "io_bazel_rules_jsonnet", 11 | commit = "b44bf8d76e0702ae06fd5422e8397f8162d7f5ca", 12 | remote = "https://github.com/bazelbuild/rules_jsonnet.git", 13 | ) 14 | 15 | load("@io_bazel_rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_repositories") 16 | 17 | jsonnet_repositories() 18 | 19 | git_repository( 20 | name = "io_bazel_rules_sass", 21 | remote = "https://github.com/bazelbuild/rules_sass.git", 22 | tag = "0.0.3", 23 | ) 24 | 25 | load("@io_bazel_rules_sass//sass:sass.bzl", "sass_repositories") 26 | 27 | sass_repositories() 28 | 29 | git_repository( 30 | name = "io_bazel_skydoc", 31 | remote = "https://github.com/bazelbuild/skydoc.git", 32 | tag = "0.1.4", 33 | ) 34 | 35 | load("@io_bazel_skydoc//skylark:skylark.bzl", "skydoc_repositories") 36 | 37 | skydoc_repositories() 38 | -------------------------------------------------------------------------------- /jsonnetunit/BUILD.bazel: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | licenses(["notice"]) # Apache 2.0 4 | 5 | load("@io_bazel_rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_library") 6 | 7 | exports_files(["jsonnetunit.bzl"]) 8 | 9 | jsonnet_library( 10 | name = "jsonnetunit", 11 | srcs = [ 12 | "matcher.libsonnet", 13 | "std_matchers.libsonnet", 14 | "test.libsonnet", 15 | ], 16 | visibility = ["//visibility:public"], 17 | ) 18 | -------------------------------------------------------------------------------- /jsonnetunit/jsonnetunit.bzl: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_to_json_test") 2 | 3 | def jsonnet_test(name, src, deps=[], **kwargs): 4 | """Runs a jsonnetunit test 5 | 6 | Args: 7 | name: A unique name for this rule 8 | src: A `.jsonnet` which contains a jsonnetunit test suite 9 | deps: List of `jsonnet_library` rules which `src` depends on. 10 | 11 | ### Note 12 | This rule also accepts other attributes defined in `jsonnet_to_json_test` rule 13 | except `golden`, `error` or `regex`. 14 | """ 15 | for key in ["golden", "error", "regex"]: 16 | if key in kwargs: 17 | fail("no such attribute in jsonnet_test", key) 18 | 19 | jsonnet_to_json_test( 20 | name = name, 21 | src = src, 22 | deps = deps + ["@com_github_yugui_jsonnetunit//jsonnetunit"], 23 | **kwargs 24 | ) 25 | 26 | -------------------------------------------------------------------------------- /jsonnetunit/matcher.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | satisfied: error 'must implement satisfied in concrete matcher', 3 | positiveMessage: error 'must implement positiveMessage in concrete matcher', 4 | negativeMessage: error 'must implement negativeMessage in concreteMatcher', 5 | 6 | matches(expectationType):: self.satisfied == expectationType, 7 | message(expectationType):: 8 | if expectationType then 9 | self.positiveMessage 10 | else 11 | self.negativeMessage, 12 | } 13 | -------------------------------------------------------------------------------- /jsonnetunit/std_matchers.libsonnet: -------------------------------------------------------------------------------- 1 | local baseMatcher = import 'matcher.libsonnet'; 2 | 3 | local equalMatcher(actual, expectation) = baseMatcher { 4 | satisfied: actual == expectation, 5 | positiveMessage: 'Expected ' + actual + ' to be ' + expectation, 6 | negativeMessage: 'Expected ' + actual + ' not to be ' + expectation, 7 | }; 8 | 9 | local ltMatcher(actual, expectation) = baseMatcher { 10 | satisfied: actual < expectation, 11 | positiveMessage: 'Expected ' + actual + ' to be less than ' + expectation, 12 | }; 13 | 14 | local leMatcher(actual, expectation) = baseMatcher { 15 | satisfied: actual <= expectation, 16 | positiveMessage: 'Expected ' + actual + 17 | ' to be less than or equal to ' + expectation, 18 | }; 19 | 20 | local gtMatcher(actual, expectation) = baseMatcher { 21 | satisfied: actual > expectation, 22 | positiveMessage: 'Expected ' + actual + 23 | ' to be greater than ' + expectation, 24 | }; 25 | 26 | local geMatcher(actual, expectation) = baseMatcher { 27 | satisfied: actual >= expectation, 28 | positiveMessage: 'Expected ' + actual + 29 | ' to be greater than or equal to ' + expectation, 30 | }; 31 | 32 | local thatMatcher(actual, expectation) = baseMatcher { 33 | satisfied: ( 34 | if std.type(expectation) == 'function' then 35 | expectation(actual) 36 | else 37 | (expectation { actual: actual }).result 38 | ), 39 | positiveMessage: 'Expected ' + actual + ' to satisfy ' + self.description, 40 | description:: ( 41 | if std.type(expectation) == 'function' then 42 | 'the function' 43 | else 44 | local evaluation = expectation { actual: actual }; 45 | if std.objectHas(evaluation, 'description') then 46 | evaluation.description 47 | else 48 | evaluation 49 | ), 50 | }; 51 | 52 | { 53 | expect: { 54 | matcher: equalMatcher, 55 | expectationType: true, 56 | }, 57 | expectNot: { 58 | matcher: equalMatcher, 59 | expectationType: false, 60 | }, 61 | expectLt: { 62 | matcher: ltMatcher, 63 | expectationType: true, 64 | }, 65 | expectLe: { 66 | matcher: leMatcher, 67 | expectationType: true, 68 | }, 69 | expectGt: { 70 | matcher: gtMatcher, 71 | expectationType: true, 72 | }, 73 | expectGe: { 74 | matcher: geMatcher, 75 | expectationType: true, 76 | }, 77 | expectThat: { 78 | matcher: thatMatcher, 79 | expectationType: true, 80 | }, 81 | } 82 | -------------------------------------------------------------------------------- /jsonnetunit/test.libsonnet: -------------------------------------------------------------------------------- 1 | local stdMatchers = import 'std_matchers.libsonnet'; 2 | 3 | local testCase(matchers, name, spec) = ( 4 | local candidates = [ 5 | { 6 | name: name, 7 | matcher: matchers[matcherName].matcher(spec.actual, spec[matcherName]), 8 | expectationType: matchers[matcherName].expectationType, 9 | } 10 | for matcherName in std.objectFields(matchers) 11 | if std.objectHas(spec, matcherName) 12 | ]; 13 | if std.length(candidates) == 0 then 14 | error 'Unrecognized spec ' + spec + ' ' 15 | else if std.length(candidates) > 1 then 16 | error 'Ambiguous expectation in spec ' + spec 17 | else 18 | candidates[0] 19 | ); 20 | 21 | local suite(tests) = { 22 | matchers:: stdMatchers, 23 | 24 | result:: [ 25 | testCase(self.matchers, t, tests[t]) 26 | for t in std.objectFields(tests) 27 | if std.startsWith(t, 'test') 28 | ], 29 | 30 | verify: ( 31 | local failures = [ 32 | tc 33 | for tc in self.result 34 | if !tc.matcher.matches(tc.expectationType) 35 | ]; 36 | if std.length(failures) > 0 then 37 | local message = 'Failed %d/%d test cases:\n' % [ 38 | std.length(failures), 39 | std.length(self.result), 40 | ] + std.join('\n', [ 41 | '%s: %s' % [tc.name, tc.matcher.message(tc.expectationType)] 42 | for tc in failures 43 | ]); 44 | error message 45 | else 46 | 'Passed %d test cases' % std.length(self.result) 47 | ), 48 | }; 49 | 50 | { 51 | suite: suite, 52 | } 53 | -------------------------------------------------------------------------------- /jsonnetunit/test/BUILD.bazel: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:private"]) 2 | 3 | licenses(["notice"]) # Apache 2.0 4 | 5 | load("@io_bazel_rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_to_json_test") 6 | load("@com_github_yugui_jsonnetunit//jsonnetunit:jsonnetunit.bzl", "jsonnet_test") 7 | 8 | # Tests jsonnet_test rule 9 | jsonnet_test( 10 | name = "test_test", 11 | src = "test_test.jsonnet", 12 | ) 13 | 14 | # Tests :jsonnetunit 15 | jsonnet_to_json_test( 16 | name = "test_golden_test", 17 | src = "test_test.jsonnet", 18 | golden = "golden/test_test.golden", 19 | deps = ["@com_github_yugui_jsonnetunit//jsonnetunit"], 20 | ) 21 | 22 | jsonnet_to_json_test( 23 | name = "failure_golden_test", 24 | src = "failure_test.jsonnet", 25 | error = 1, 26 | golden = "golden/failure_test.golden", 27 | regex = 1, 28 | deps = ["@com_github_yugui_jsonnetunit//jsonnetunit"], 29 | ) 30 | 31 | jsonnet_test( 32 | name = "std_matchers_test", 33 | src = "std_matchers_test.jsonnet", 34 | ) 35 | 36 | jsonnet_to_json_test( 37 | name = "std_matchers_failure_test", 38 | src = "std_matchers_failure_test.jsonnet", 39 | error = 1, 40 | golden = "golden/std_matchers_failure_test.golden", 41 | regex = 1, 42 | deps = ["@com_github_yugui_jsonnetunit//jsonnetunit"], 43 | ) 44 | -------------------------------------------------------------------------------- /jsonnetunit/test/failure_test.jsonnet: -------------------------------------------------------------------------------- 1 | local test = import 'jsonnetunit/test.libsonnet'; 2 | 3 | test.suite({ 4 | testFailure: { actual: 1 + 1, expect: 3 }, 5 | }) 6 | -------------------------------------------------------------------------------- /jsonnetunit/test/golden/failure_test.golden: -------------------------------------------------------------------------------- 1 | RUNTIME ERROR: Failed 1/1 test cases: 2 | testFailure: Expected 2 to be 3 3 | -------------------------------------------------------------------------------- /jsonnetunit/test/golden/std_matchers_failure_test.golden: -------------------------------------------------------------------------------- 1 | testEq: Expected 1 to be 2 2 | testGe: Expected 1 to be greater than or equal to 2 3 | testGt: Expected 1 to be greater than 2 4 | testGtEq: Expected 1 to be greater than 1 5 | testLe: Expected 2 to be less than or equal to 1 6 | testLt: Expected 2 to be less than 1 7 | testLtEq: Expected 2 to be less than 2 8 | testNe: Expected 1 not to be 1 9 | testThatFunction: Expected 1 to satisfy the function 10 | testThatObject: Expected 1 to satisfy \{"actual": 1, "result": false\} 11 | testThatObjectDesc: Expected 1 to satisfy the condition that the value is 2 12 | -------------------------------------------------------------------------------- /jsonnetunit/test/golden/test_test.golden: -------------------------------------------------------------------------------- 1 | { 2 | "verify": "Passed 1 test cases" 3 | } 4 | -------------------------------------------------------------------------------- /jsonnetunit/test/std_matchers_failure_test.jsonnet: -------------------------------------------------------------------------------- 1 | local test = import 'jsonnetunit/test.libsonnet'; 2 | 3 | test.suite({ 4 | testEq: { actual: 1, expect: 2 }, 5 | testNe: { actual: 1, expectNot: 1 }, 6 | 7 | testLt: { actual: 2, expectLt: 1 }, 8 | testLtEq: { actual: 2, expectLt: 2 }, 9 | testLe: { actual: 2, expectLe: 1 }, 10 | 11 | testGt: { actual: 1, expectGt: 2 }, 12 | testGtEq: { actual: 1, expectGt: 1 }, 13 | testGe: { actual: 1, expectGe: 2 }, 14 | 15 | testThatFunction: { 16 | actual: 1, 17 | expectThat: function(actual) actual == 2, 18 | }, 19 | testThatObject: { 20 | actual: 1, 21 | expectThat: { 22 | actual: error 'to be provided', 23 | result: self.actual == 2, 24 | }, 25 | }, 26 | testThatObjectDesc: { 27 | actual: 1, 28 | expectThat: { 29 | actual: error 'to be provided', 30 | result: self.actual == 2, 31 | description: 'the condition that the value is 2', 32 | }, 33 | }, 34 | }) 35 | -------------------------------------------------------------------------------- /jsonnetunit/test/std_matchers_test.jsonnet: -------------------------------------------------------------------------------- 1 | local test = import 'jsonnetunit/test.libsonnet'; 2 | 3 | test.suite({ 4 | testExpectNull: { actual: null, expect: null }, 5 | testExpectBool: { actual: true, expect: true }, 6 | testExpectString: { actual: 'str', expect: 'str' }, 7 | testExpectNumber: { actual: 1, expect: 1 }, 8 | testExpectArray: { 9 | actual: [null, true, 'str', 1], 10 | expect: [null, true, 'str', 1], 11 | }, 12 | testExpectObject: { 13 | actual: { a: null, b: true, c: 'str', d: 1 }, 14 | expect: { a: null, b: true, c: 'str', d: 1 }, 15 | }, 16 | } + { 17 | testExpectNotNull: { actual: 1, expectNot: null }, 18 | testExpectNotStringInt: { actual: '1', expectNot: 1 }, 19 | testExpectNotBoolInt: { actual: true, expectNot: 1 }, 20 | testExpectNotArray: { 21 | actual: [1, 2, 3], 22 | expectNot: [1, 4, 3], 23 | }, 24 | testExpectNotObject: { 25 | actual: { a: 1, b: 2, c: 3 }, 26 | expectNot: { a: 1, b: 4, c: 3 }, 27 | }, 28 | } + { 29 | testExpectLt: { 30 | actual: 1, 31 | expectLt: 2, 32 | }, 33 | testExpectLe: { 34 | actual: 1, 35 | expectLe: 2, 36 | }, 37 | testExpectLeEq: { 38 | actual: 1, 39 | expectLe: 1, 40 | }, 41 | testExpectGt: { 42 | actual: 2, 43 | expectGt: 1, 44 | }, 45 | testExpectGe: { 46 | actual: 2, 47 | expectGe: 1, 48 | }, 49 | testExpectGeEq: { 50 | actual: 2, 51 | expectGe: 2, 52 | }, 53 | } + { 54 | testExpectThatFunction: { 55 | actual: 1, 56 | expectThat: function(actual) actual == 1, 57 | }, 58 | testExpectThatObject: { 59 | actual: 1, 60 | expectThat: { 61 | actual: error 'to be provided', 62 | result: self.actual == 1, 63 | }, 64 | }, 65 | }) 66 | -------------------------------------------------------------------------------- /jsonnetunit/test/test_test.jsonnet: -------------------------------------------------------------------------------- 1 | local matcher = import 'jsonnetunit/matcher.libsonnet'; 2 | local test = import 'jsonnetunit/test.libsonnet'; 3 | 4 | local stubMatcher(actual, expected) = matcher { 5 | satisfied: true, 6 | }; 7 | 8 | test.suite({ 9 | testDummy: { actual: 'something', expectStub: 'something' }, 10 | }) { 11 | matchers+: { 12 | expectStub: { 13 | matcher: stubMatcher, 14 | expectationType: true, 15 | }, 16 | }, 17 | } 18 | --------------------------------------------------------------------------------