├── .github └── workflows │ ├── ci.yml │ └── clean.yml ├── .gitignore ├── .jvmopts ├── .scalafmt.conf ├── LICENSE.txt ├── README.md ├── build.sbt ├── core ├── js-native │ └── src │ │ └── main │ │ └── scala │ │ └── cats │ │ └── effect │ │ └── testing │ │ └── RuntimePlatform.scala ├── jvm │ └── src │ │ └── main │ │ └── scala │ │ └── cats │ │ └── effect │ │ └── testing │ │ └── RuntimePlatform.scala └── shared │ └── src │ └── main │ └── scala │ └── cats │ └── effect │ └── testing │ └── UnsafeRun.scala ├── minitest └── shared │ └── src │ ├── main │ └── scala │ │ └── cats │ │ └── effect │ │ └── testing │ │ └── minitest │ │ ├── BaseIOTestSuite.scala │ │ ├── DeterministicIOTestSuite.scala │ │ └── IOTestSuite.scala │ └── test │ └── scala │ └── cats │ └── effect │ └── testing │ └── minitest │ ├── TestDetSuite.scala │ └── TestNondetSuite.scala ├── project ├── build.properties └── plugins.sbt ├── scalatest ├── js-native │ └── src │ │ └── main │ │ └── scala │ │ └── cats │ │ └── effect │ │ └── testing │ │ └── scalatest │ │ └── AsyncIOSpec.scala ├── jvm │ └── src │ │ ├── main │ │ └── scala │ │ │ └── cats │ │ │ └── effect │ │ │ └── testing │ │ │ └── scalatest │ │ │ └── AsyncIOSpec.scala │ │ └── test │ │ └── scala │ │ └── cats │ │ └── effect │ │ └── testing │ │ └── scalatest │ │ └── DeadlockTest.scala └── shared │ └── src │ ├── main │ └── scala │ │ └── cats │ │ └── effect │ │ └── testing │ │ └── scalatest │ │ ├── AssertingSyntax.scala │ │ ├── CatsResource.scala │ │ ├── CatsResourceIO.scala │ │ └── EffectTestSupport.scala │ └── test │ └── scala │ └── cats │ └── effect │ └── testing │ └── scalatest │ ├── CatsResourceErrorSpecs.scala │ ├── CatsResourceSpecs.scala │ ├── IOTest.scala │ └── IOTestAsyncFlatSpecLike.scala ├── specs2 ├── js-native │ └── src │ │ └── test │ │ └── scala │ │ └── cats │ │ └── effect │ │ └── testing │ │ └── specs2 │ │ └── CatsEffectSpecsPlatform.scala ├── jvm │ └── src │ │ └── test │ │ └── scala │ │ └── cats │ │ └── effect │ │ └── testing │ │ └── specs2 │ │ └── CatsEffectSpecsPlatform.scala └── shared │ └── src │ ├── main │ └── scala │ │ └── cats │ │ └── effect │ │ └── testing │ │ └── specs2 │ │ ├── CatsEffect.scala │ │ └── CatsResource.scala │ └── test │ └── scala │ └── cats │ └── effect │ └── testing │ └── specs2 │ ├── CatsEffectSpecs.scala │ ├── CatsResourceErrorSpecs.scala │ ├── CatsResourceParallelSpecs.scala │ └── CatsResourceSpecs.scala └── utest └── shared └── src ├── main └── scala │ └── cats │ └── effect │ └── testing │ └── utest │ ├── DeterministicIOTestSuite.scala │ └── EffectTestSuite.scala └── test └── scala └── cats └── effect └── testing └── utest ├── TestDetSuite.scala └── TestNondetSuite.scala /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | # This file was automatically generated by sbt-github-actions using the 2 | # githubWorkflowGenerate task. You should add and commit this file to 3 | # your git repository. It goes without saying that you shouldn't edit 4 | # this file by hand! Instead, if you wish to make changes, you should 5 | # change your sbt build configuration to revise the workflow description 6 | # to meet your needs, then regenerate this file. 7 | 8 | name: Continuous Integration 9 | 10 | on: 11 | pull_request: 12 | branches: ['**', '!update/**', '!pr/**'] 13 | push: 14 | branches: ['**', '!update/**', '!pr/**'] 15 | tags: [v*] 16 | 17 | env: 18 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 19 | 20 | 21 | concurrency: 22 | group: ${{ github.workflow }} @ ${{ github.ref }} 23 | cancel-in-progress: true 24 | 25 | jobs: 26 | build: 27 | name: Test 28 | strategy: 29 | matrix: 30 | os: [ubuntu-22.04] 31 | scala: [3, 2.12, 2.13] 32 | java: [temurin@8] 33 | project: [rootJS, rootJVM, rootNative] 34 | runs-on: ${{ matrix.os }} 35 | timeout-minutes: 60 36 | steps: 37 | - name: Checkout current branch (full) 38 | uses: actions/checkout@v4 39 | with: 40 | fetch-depth: 0 41 | 42 | - name: Setup sbt 43 | uses: sbt/setup-sbt@v1 44 | 45 | - name: Setup Java (temurin@8) 46 | id: setup-java-temurin-8 47 | if: matrix.java == 'temurin@8' 48 | uses: actions/setup-java@v4 49 | with: 50 | distribution: temurin 51 | java-version: 8 52 | cache: sbt 53 | 54 | - name: sbt update 55 | if: matrix.java == 'temurin@8' && steps.setup-java-temurin-8.outputs.cache-hit == 'false' 56 | run: sbt +update 57 | 58 | - name: Check that workflows are up to date 59 | run: sbt githubWorkflowCheck 60 | 61 | - name: Check headers and formatting 62 | if: matrix.java == 'temurin@8' && matrix.os == 'ubuntu-22.04' 63 | run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' headerCheckAll scalafmtCheckAll 'project /' scalafmtSbtCheck 64 | 65 | - name: scalaJSLink 66 | if: matrix.project == 'rootJS' 67 | run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' Test/scalaJSLinkerResult 68 | 69 | - name: nativeLink 70 | if: matrix.project == 'rootNative' 71 | run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' Test/nativeLink 72 | 73 | - name: Test 74 | run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' test 75 | 76 | - name: Check binary compatibility 77 | if: matrix.java == 'temurin@8' && matrix.os == 'ubuntu-22.04' 78 | run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' mimaReportBinaryIssues 79 | 80 | - name: Generate API documentation 81 | if: matrix.java == 'temurin@8' && matrix.os == 'ubuntu-22.04' 82 | run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' doc 83 | 84 | - name: Make target directories 85 | if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/series/1.x') 86 | run: mkdir -p specs2/js/target minitest/jvm/target minitest/js/target utest/native/target core/native/target utest/js/target core/js/target core/jvm/target specs2/jvm/target scalatest/native/target utest/jvm/target scalatest/js/target scalatest/jvm/target specs2/native/target project/target 87 | 88 | - name: Compress target directories 89 | if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/series/1.x') 90 | run: tar cf targets.tar specs2/js/target minitest/jvm/target minitest/js/target utest/native/target core/native/target utest/js/target core/js/target core/jvm/target specs2/jvm/target scalatest/native/target utest/jvm/target scalatest/js/target scalatest/jvm/target specs2/native/target project/target 91 | 92 | - name: Upload target directories 93 | if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/series/1.x') 94 | uses: actions/upload-artifact@v4 95 | with: 96 | name: target-${{ matrix.os }}-${{ matrix.java }}-${{ matrix.scala }}-${{ matrix.project }} 97 | path: targets.tar 98 | 99 | publish: 100 | name: Publish Artifacts 101 | needs: [build] 102 | if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/series/1.x') 103 | strategy: 104 | matrix: 105 | os: [ubuntu-22.04] 106 | java: [temurin@8] 107 | runs-on: ${{ matrix.os }} 108 | steps: 109 | - name: Checkout current branch (full) 110 | uses: actions/checkout@v4 111 | with: 112 | fetch-depth: 0 113 | 114 | - name: Setup sbt 115 | uses: sbt/setup-sbt@v1 116 | 117 | - name: Setup Java (temurin@8) 118 | id: setup-java-temurin-8 119 | if: matrix.java == 'temurin@8' 120 | uses: actions/setup-java@v4 121 | with: 122 | distribution: temurin 123 | java-version: 8 124 | cache: sbt 125 | 126 | - name: sbt update 127 | if: matrix.java == 'temurin@8' && steps.setup-java-temurin-8.outputs.cache-hit == 'false' 128 | run: sbt +update 129 | 130 | - name: Download target directories (3, rootJS) 131 | uses: actions/download-artifact@v4 132 | with: 133 | name: target-${{ matrix.os }}-${{ matrix.java }}-3-rootJS 134 | 135 | - name: Inflate target directories (3, rootJS) 136 | run: | 137 | tar xf targets.tar 138 | rm targets.tar 139 | 140 | - name: Download target directories (3, rootJVM) 141 | uses: actions/download-artifact@v4 142 | with: 143 | name: target-${{ matrix.os }}-${{ matrix.java }}-3-rootJVM 144 | 145 | - name: Inflate target directories (3, rootJVM) 146 | run: | 147 | tar xf targets.tar 148 | rm targets.tar 149 | 150 | - name: Download target directories (3, rootNative) 151 | uses: actions/download-artifact@v4 152 | with: 153 | name: target-${{ matrix.os }}-${{ matrix.java }}-3-rootNative 154 | 155 | - name: Inflate target directories (3, rootNative) 156 | run: | 157 | tar xf targets.tar 158 | rm targets.tar 159 | 160 | - name: Download target directories (2.12, rootJS) 161 | uses: actions/download-artifact@v4 162 | with: 163 | name: target-${{ matrix.os }}-${{ matrix.java }}-2.12-rootJS 164 | 165 | - name: Inflate target directories (2.12, rootJS) 166 | run: | 167 | tar xf targets.tar 168 | rm targets.tar 169 | 170 | - name: Download target directories (2.12, rootJVM) 171 | uses: actions/download-artifact@v4 172 | with: 173 | name: target-${{ matrix.os }}-${{ matrix.java }}-2.12-rootJVM 174 | 175 | - name: Inflate target directories (2.12, rootJVM) 176 | run: | 177 | tar xf targets.tar 178 | rm targets.tar 179 | 180 | - name: Download target directories (2.12, rootNative) 181 | uses: actions/download-artifact@v4 182 | with: 183 | name: target-${{ matrix.os }}-${{ matrix.java }}-2.12-rootNative 184 | 185 | - name: Inflate target directories (2.12, rootNative) 186 | run: | 187 | tar xf targets.tar 188 | rm targets.tar 189 | 190 | - name: Download target directories (2.13, rootJS) 191 | uses: actions/download-artifact@v4 192 | with: 193 | name: target-${{ matrix.os }}-${{ matrix.java }}-2.13-rootJS 194 | 195 | - name: Inflate target directories (2.13, rootJS) 196 | run: | 197 | tar xf targets.tar 198 | rm targets.tar 199 | 200 | - name: Download target directories (2.13, rootJVM) 201 | uses: actions/download-artifact@v4 202 | with: 203 | name: target-${{ matrix.os }}-${{ matrix.java }}-2.13-rootJVM 204 | 205 | - name: Inflate target directories (2.13, rootJVM) 206 | run: | 207 | tar xf targets.tar 208 | rm targets.tar 209 | 210 | - name: Download target directories (2.13, rootNative) 211 | uses: actions/download-artifact@v4 212 | with: 213 | name: target-${{ matrix.os }}-${{ matrix.java }}-2.13-rootNative 214 | 215 | - name: Inflate target directories (2.13, rootNative) 216 | run: | 217 | tar xf targets.tar 218 | rm targets.tar 219 | 220 | - name: Import signing key 221 | if: env.PGP_SECRET != '' && env.PGP_PASSPHRASE == '' 222 | env: 223 | PGP_SECRET: ${{ secrets.PGP_SECRET }} 224 | PGP_PASSPHRASE: ${{ secrets.PGP_PASSPHRASE }} 225 | run: echo $PGP_SECRET | base64 -d -i - | gpg --import 226 | 227 | - name: Import signing key and strip passphrase 228 | if: env.PGP_SECRET != '' && env.PGP_PASSPHRASE != '' 229 | env: 230 | PGP_SECRET: ${{ secrets.PGP_SECRET }} 231 | PGP_PASSPHRASE: ${{ secrets.PGP_PASSPHRASE }} 232 | run: | 233 | echo "$PGP_SECRET" | base64 -d -i - > /tmp/signing-key.gpg 234 | echo "$PGP_PASSPHRASE" | gpg --pinentry-mode loopback --passphrase-fd 0 --import /tmp/signing-key.gpg 235 | (echo "$PGP_PASSPHRASE"; echo; echo) | gpg --command-fd 0 --pinentry-mode loopback --change-passphrase $(gpg --list-secret-keys --with-colons 2> /dev/null | grep '^sec:' | cut --delimiter ':' --fields 5 | tail -n 1) 236 | 237 | - name: Publish 238 | env: 239 | SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }} 240 | SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} 241 | SONATYPE_CREDENTIAL_HOST: ${{ secrets.SONATYPE_CREDENTIAL_HOST }} 242 | run: sbt tlCiRelease 243 | 244 | dependency-submission: 245 | name: Submit Dependencies 246 | if: github.event.repository.fork == false && github.event_name != 'pull_request' 247 | strategy: 248 | matrix: 249 | os: [ubuntu-22.04] 250 | java: [temurin@8] 251 | runs-on: ${{ matrix.os }} 252 | steps: 253 | - name: Checkout current branch (full) 254 | uses: actions/checkout@v4 255 | with: 256 | fetch-depth: 0 257 | 258 | - name: Setup sbt 259 | uses: sbt/setup-sbt@v1 260 | 261 | - name: Setup Java (temurin@8) 262 | id: setup-java-temurin-8 263 | if: matrix.java == 'temurin@8' 264 | uses: actions/setup-java@v4 265 | with: 266 | distribution: temurin 267 | java-version: 8 268 | cache: sbt 269 | 270 | - name: sbt update 271 | if: matrix.java == 'temurin@8' && steps.setup-java-temurin-8.outputs.cache-hit == 'false' 272 | run: sbt +update 273 | 274 | - name: Submit Dependencies 275 | uses: scalacenter/sbt-dependency-submission@v2 276 | with: 277 | modules-ignore: rootjs_3 rootjs_2.12 rootjs_2.13 rootjvm_3 rootjvm_2.12 rootjvm_2.13 rootnative_3 rootnative_2.12 rootnative_2.13 278 | configs-ignore: test scala-tool scala-doc-tool test-internal 279 | -------------------------------------------------------------------------------- /.github/workflows/clean.yml: -------------------------------------------------------------------------------- 1 | # This file was automatically generated by sbt-github-actions using the 2 | # githubWorkflowGenerate task. You should add and commit this file to 3 | # your git repository. It goes without saying that you shouldn't edit 4 | # this file by hand! Instead, if you wish to make changes, you should 5 | # change your sbt build configuration to revise the workflow description 6 | # to meet your needs, then regenerate this file. 7 | 8 | name: Clean 9 | 10 | on: push 11 | 12 | jobs: 13 | delete-artifacts: 14 | name: Delete Artifacts 15 | runs-on: ubuntu-latest 16 | env: 17 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 18 | steps: 19 | - name: Delete artifacts 20 | run: | 21 | # Customize those three lines with your repository and credentials: 22 | REPO=${GITHUB_API_URL}/repos/${{ github.repository }} 23 | 24 | # A shortcut to call GitHub API. 25 | ghapi() { curl --silent --location --user _:$GITHUB_TOKEN "$@"; } 26 | 27 | # A temporary file which receives HTTP response headers. 28 | TMPFILE=/tmp/tmp.$$ 29 | 30 | # An associative array, key: artifact name, value: number of artifacts of that name. 31 | declare -A ARTCOUNT 32 | 33 | # Process all artifacts on this repository, loop on returned "pages". 34 | URL=$REPO/actions/artifacts 35 | while [[ -n "$URL" ]]; do 36 | 37 | # Get current page, get response headers in a temporary file. 38 | JSON=$(ghapi --dump-header $TMPFILE "$URL") 39 | 40 | # Get URL of next page. Will be empty if we are at the last page. 41 | URL=$(grep '^Link:' "$TMPFILE" | tr ',' '\n' | grep 'rel="next"' | head -1 | sed -e 's/.*.*//') 42 | rm -f $TMPFILE 43 | 44 | # Number of artifacts on this page: 45 | COUNT=$(( $(jq <<<$JSON -r '.artifacts | length') )) 46 | 47 | # Loop on all artifacts on this page. 48 | for ((i=0; $i < $COUNT; i++)); do 49 | 50 | # Get name of artifact and count instances of this name. 51 | name=$(jq <<<$JSON -r ".artifacts[$i].name?") 52 | ARTCOUNT[$name]=$(( $(( ${ARTCOUNT[$name]} )) + 1)) 53 | 54 | id=$(jq <<<$JSON -r ".artifacts[$i].id?") 55 | size=$(( $(jq <<<$JSON -r ".artifacts[$i].size_in_bytes?") )) 56 | printf "Deleting '%s' #%d, %'d bytes\n" $name ${ARTCOUNT[$name]} $size 57 | ghapi -X DELETE $REPO/actions/artifacts/$id 58 | done 59 | done 60 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | .idea/ 3 | # vim 4 | *.sw? 5 | 6 | # Ignore [ce]tags files 7 | tags 8 | 9 | .metals 10 | .bloop 11 | .bsp 12 | project/metals.sbt 13 | .vscode 14 | -------------------------------------------------------------------------------- /.jvmopts: -------------------------------------------------------------------------------- 1 | -Xms1G 2 | -Xmx4G 3 | -XX:+UseG1GC 4 | -------------------------------------------------------------------------------- /.scalafmt.conf: -------------------------------------------------------------------------------- 1 | version = "3.9.4" 2 | runner.dialect = scala213source3 3 | project.includePaths = [] # disables formatting 4 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cats-effect-testing 2 | 3 | A quickie little utility which makes it easier to write tests using [specs2](https://specs2.org) (mutable or functional), [scalatest](http://scalatest.org), [µTest](https://github.com/lihaoyi/utest) or [minitest](https://github.com/monix/minitest) where the examples are effectful within `cats.effect.IO`. 4 | 5 | ## Specs2 6 | 7 | ```scala 8 | import cats.effect.IO 9 | import cats.effect.testing.specs2.CatsEffect 10 | import org.specs2.mutable.Specification 11 | 12 | // for some reason, only class works here; object will not be detected by sbt 13 | class ExampleSpec extends Specification with CatsEffect { 14 | "examples" should { 15 | "do the things" in IO { 16 | true must beTrue 17 | } 18 | } 19 | } 20 | ``` 21 | 22 | The above compiles and runs exactly as you would expect. 23 | 24 | By default, tests run with a 10 second timeout. If you wish to override this, simply override the inherited `Timeout` val: 25 | 26 | ```scala 27 | override val Timeout = 5.seconds 28 | ``` 29 | 30 | If you need an `ExecutionContext`, one is available in the `executionContext` val. 31 | 32 | And if you need to share Cats Effect's Resource between test examples you can extend `CatsResource`, like so: 33 | 34 | ```scala 35 | import cats.effect.{IO, Ref, Resource} 36 | import org.specs2.mutable.SpecificationLike 37 | 38 | class CatsResourceSpecs extends CatsResource[IO, Ref[IO, Int]] with SpecificationLike { 39 | sequential 40 | 41 | val resource: Resource[IO, Ref[IO, Int]] = 42 | Resource.make(Ref[IO].of(0))(_.set(Int.MinValue)) 43 | 44 | "cats resource support" should { 45 | "run a resource modification" in withResource { ref => 46 | ref.modify{ a => 47 | (a + 1, a) 48 | }.map( 49 | _ must_=== 0 50 | ) 51 | } 52 | 53 | "be shared between tests" in withResource { ref => 54 | ref.modify{ a => 55 | (a + 1, a) 56 | }.map( 57 | _ must_=== 1 58 | ) 59 | } 60 | } 61 | } 62 | ``` 63 | 64 | The Resource is acquired before the tests are run and released afterwards. A more realistic example would be to share a Resource that takes a long time to start up. 65 | 66 | ### Usage 67 | 68 | ```sbt 69 | libraryDependencies += "org.typelevel" %% "cats-effect-testing-specs2" % "" % Test 70 | ``` 71 | 72 | Published for Scala 3.1+, 2.13, 2.12, as well as ScalaJS 1.7+. Depends on Cats Effect 3.1+ and Specs2 4.13.x. Specs2 5.0 is not yet supported. 73 | 74 | Early versions (`0.x.y`) were published under the `com.codecommit` groupId. 75 | 76 | ## ScalaTest 77 | 78 | ```scala 79 | import cats.effect._ 80 | import cats.effect.testing.scalatest.AsyncIOSpec 81 | import org.scalatest.matchers.should.Matchers 82 | import org.scalatest.freespec.AsyncFreeSpec 83 | 84 | class MySpec extends AsyncFreeSpec with AsyncIOSpec with Matchers { 85 | 86 | "My Code " - { 87 | "works" in { 88 | IO(1).asserting(_ shouldBe 1) 89 | } 90 | } 91 | 92 | ``` 93 | ### Usage 94 | 95 | ```sbt 96 | libraryDependencies += "org.typelevel" %% "cats-effect-testing-scalatest" % "" % Test 97 | ``` 98 | 99 | Published for Scala 3.1+, 2.13, 2.12, as well as ScalaJS 1.7.x. Depends on Cats Effect 3.1+ and scalatest 3.2.6. 100 | 101 | Early versions (`0.x.y`) were published under the `com.codecommit` groupId. 102 | 103 | ## µTest 104 | 105 | ```scala 106 | import scala.concurrent.duration._ 107 | import utest._ 108 | import cats.syntax.all._ 109 | import cats.effect.IO 110 | import cats.effect.testing.utest.IOTestSuite 111 | 112 | object SimpleSuite extends IOTestSuite { 113 | override val timeout = 1.second // Default timeout is 10 seconds 114 | 115 | val tests = Tests { 116 | test("do the thing") { 117 | IO(assert(true)) 118 | } 119 | } 120 | } 121 | 122 | ``` 123 | 124 | ### Usage 125 | 126 | ```sbt 127 | libraryDependencies += "org.typelevel" %% "cats-effect-testing-utest" % "" % Test 128 | ``` 129 | 130 | Published for Scala 3.1+, 2.13, 2.12, as well as ScalaJS 1.7.x. Depends on Cats Effect 3.1+ and µTest 0.7.9. 131 | 132 | Early versions (`0.x.y`) were published under the `com.codecommit` groupId. 133 | 134 | ## Minitest 135 | 136 | Minitest is very similar to uTest, but being strongly typed, there's no need to support 137 | non-IO tests 138 | 139 | ```scala 140 | import scala.concurrent.duration._ 141 | import cats.syntax.all._ 142 | import cats.effect.IO 143 | import cats.effect.testing.minitest.IOTestSuite 144 | 145 | object SimpleSuite extends IOTestSuite { 146 | override val timeout = 1.second // Default timeout is 10 seconds 147 | 148 | test("do the thing") { 149 | IO(assert(true)) 150 | } 151 | } 152 | ``` 153 | 154 | ### Usage 155 | 156 | ```sbt 157 | libraryDependencies += "org.typelevel" %% "cats-effect-testing-minitest" % "" % Test 158 | ``` 159 | 160 | Published for Scala 3.1+, 2.13, 2.12, as well as ScalaJS 1.7.x. Depends on Cats Effect 3.1+ and minitest 2.9.5. 161 | 162 | Early versions (`0.x.y`) were published under the `com.codecommit` groupId. 163 | 164 | ## Similar projects 165 | 166 | ### scalacheck-effect 167 | 168 | [scalacheck-effect](https://github.com/typelevel/scalacheck-effect) is a library that extends the functionality of [ScalaCheck](https://scalacheck.org) to support "effectful" properties. An effectful property is one that evaluates each sample in some type constructor `F[_]`. 169 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2021 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | name := "cats-effect-testing" 18 | 19 | ThisBuild / tlBaseVersion := "1.6" 20 | ThisBuild / startYear := Some(2020) 21 | ThisBuild / developers += tlGitHubDev("djspiewak", "Daniel Spiewak") 22 | 23 | ThisBuild / crossScalaVersions := Seq("3.3.6", "2.12.20", "2.13.16") 24 | 25 | ThisBuild / tlVersionIntroduced := Map("3" -> "1.1.1") 26 | 27 | ThisBuild / tlCiReleaseBranches := Seq("series/1.x") 28 | 29 | val CatsEffectVersion = "3.6.1" 30 | val ScalaTestVersion = "3.2.18" 31 | 32 | lazy val root = tlCrossRootProject 33 | .aggregate(core, specs2, utest, minitest, scalatest) 34 | 35 | lazy val core = crossProject(JSPlatform, JVMPlatform, NativePlatform) 36 | .in(file("core")) 37 | .settings( 38 | name := "cats-effect-testing-core", 39 | tlVersionIntroduced := List("2.12", "2.13", "3").map(_ -> "1.3.0").toMap, 40 | libraryDependencies += "org.typelevel" %%% "cats-effect" % CatsEffectVersion) 41 | .nativeSettings(tlVersionIntroduced := List("2.12", "2.13", "3").map(_ -> "1.5.0").toMap) 42 | 43 | lazy val specs2 = crossProject(JSPlatform, JVMPlatform, NativePlatform) 44 | .in(file("specs2")) 45 | .dependsOn(core) 46 | .settings( 47 | name := "cats-effect-testing-specs2", 48 | libraryDependencies += "org.specs2" %%% "specs2-core" % "4.20.5") 49 | .nativeSettings(tlVersionIntroduced := List("2.12", "2.13", "3").map(_ -> "1.5.0").toMap) 50 | 51 | lazy val scalatest = crossProject(JSPlatform, JVMPlatform, NativePlatform) 52 | .in(file("scalatest")) 53 | .dependsOn(core) 54 | .settings( 55 | name := "cats-effect-testing-scalatest", 56 | 57 | libraryDependencies ++= Seq( 58 | "org.scalatest" %%% "scalatest-core" % ScalaTestVersion, 59 | "org.scalatest" %%% "scalatest-shouldmatchers" % ScalaTestVersion % Test, 60 | "org.scalatest" %%% "scalatest-mustmatchers" % ScalaTestVersion % Test, 61 | "org.scalatest" %%% "scalatest-freespec" % ScalaTestVersion % Test, 62 | "org.scalatest" %%% "scalatest-wordspec" % ScalaTestVersion % Test)) 63 | .nativeSettings(tlVersionIntroduced := List("2.12", "2.13", "3").map(_ -> "1.5.0").toMap) 64 | 65 | lazy val utest = crossProject(JSPlatform, JVMPlatform, NativePlatform) 66 | .in(file("utest")) 67 | .dependsOn(core) 68 | .settings( 69 | name := "cats-effect-testing-utest", 70 | 71 | testFrameworks += new TestFramework("utest.runner.Framework"), 72 | 73 | libraryDependencies ++= Seq( 74 | "org.typelevel" %%% "cats-effect-testkit" % CatsEffectVersion, 75 | "com.lihaoyi" %%% "utest" % "0.8.2"), 76 | 77 | Test / scalacOptions -= "-Xfatal-warnings") 78 | .nativeSettings(tlVersionIntroduced := List("2.12", "2.13", "3").map(_ -> "1.5.0").toMap) 79 | 80 | lazy val minitest = crossProject(JSPlatform, JVMPlatform) 81 | .in(file("minitest")) 82 | .dependsOn(core) 83 | .settings( 84 | name := "cats-effect-testing-minitest", 85 | testFrameworks += new TestFramework("minitest.runner.Framework"), 86 | 87 | libraryDependencies ++= Seq( 88 | "org.typelevel" %%% "cats-effect-testkit" % CatsEffectVersion, 89 | "io.monix" %%% "minitest" % "2.9.6"), 90 | 91 | Test / scalacOptions -= "-Xfatal-warnings") 92 | -------------------------------------------------------------------------------- /core/js-native/src/main/scala/cats/effect/testing/RuntimePlatform.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect 18 | package testing 19 | 20 | import scala.concurrent.ExecutionContext 21 | 22 | private[testing] trait RuntimePlatform { 23 | private[testing] def createIORuntime(delegate: ExecutionContext): unsafe.IORuntime = 24 | unsafe.IORuntime( 25 | delegate, 26 | unsafe.IORuntime.global.blocking, 27 | unsafe.IORuntime.global.scheduler, 28 | unsafe.IORuntime.global.shutdown, 29 | unsafe.IORuntime.global.config) 30 | } 31 | -------------------------------------------------------------------------------- /core/jvm/src/main/scala/cats/effect/testing/RuntimePlatform.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect 18 | package testing 19 | 20 | import scala.concurrent.ExecutionContext 21 | 22 | private[testing] trait RuntimePlatform { 23 | private[testing] def createIORuntime(ec: ExecutionContext): unsafe.IORuntime = { 24 | val (blocking, blockingSD) = unsafe.IORuntime.createDefaultBlockingExecutionContext() 25 | val (scheduler, schedulerSD) = unsafe.IORuntime.createDefaultScheduler() 26 | unsafe.IORuntime(ec, blocking, scheduler, { () => blockingSD(); schedulerSD(); }, unsafe.IORuntimeConfig()) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/cats/effect/testing/UnsafeRun.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect 18 | package testing 19 | 20 | import scala.annotation.nowarn 21 | import scala.concurrent.Future 22 | import scala.concurrent.duration.FiniteDuration 23 | 24 | trait UnsafeRun[F[_]] { 25 | def unsafeToFuture[A](fa: F[A]): Future[A] 26 | def unsafeToFuture[A](fa: F[A], @nowarn("msg=never used") timeout: Option[FiniteDuration]): Future[A] 27 | = unsafeToFuture(fa) // For binary compatibility 28 | } 29 | 30 | object UnsafeRun { 31 | 32 | def apply[F[_]](implicit F: UnsafeRun[F]): UnsafeRun[F] = F 33 | 34 | implicit object unsafeRunForCatsIO extends UnsafeRun[IO] { 35 | import unsafe.implicits.global 36 | 37 | override def unsafeToFuture[A](ioa: IO[A]): Future[A] = 38 | unsafeToFuture(ioa, None) 39 | 40 | // TODO is it worth isolating runtimes between test runs? 41 | override def unsafeToFuture[A](ioa: IO[A], timeout: Option[FiniteDuration]): Future[A] = 42 | timeout.fold(ioa)(ioa.timeout).unsafeToFuture() 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /minitest/shared/src/main/scala/cats/effect/testing/minitest/BaseIOTestSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.minitest 18 | 19 | import scala.concurrent.ExecutionContext 20 | 21 | import cats.effect.IO 22 | import minitest.api._ 23 | 24 | 25 | private[effect] abstract class BaseIOTestSuite[Ec <: ExecutionContext] extends AbstractTestSuite with Asserts { 26 | protected def makeExecutionContext(): Ec 27 | 28 | private[effect] lazy val executionContext: Ec = makeExecutionContext() 29 | protected[effect] implicit def suiteEc: ExecutionContext = executionContext 30 | 31 | protected[effect] def mkSpec(name: String, ec: Ec, io: => IO[Unit]): TestSpec[Unit, Unit] 32 | 33 | def test(name: String)(f: => IO[Unit]): Unit = 34 | synchronized { 35 | if (isInitialized) throw new AssertionError("Cannot define new tests after TestSuite was initialized") 36 | propertiesSeq :+= mkSpec(name, executionContext, f) 37 | } 38 | 39 | lazy val properties: Properties[_] = 40 | synchronized { 41 | if (!isInitialized) isInitialized = true 42 | Properties[Unit](() => (), _ => Void.UnitRef, () => (), () => (), propertiesSeq) 43 | } 44 | 45 | private[this] var propertiesSeq = Vector.empty[TestSpec[Unit, Unit]] 46 | private[this] var isInitialized = false 47 | } 48 | -------------------------------------------------------------------------------- /minitest/shared/src/main/scala/cats/effect/testing/minitest/DeterministicIOTestSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.minitest 18 | 19 | import scala.concurrent.ExecutionContext 20 | 21 | import cats.effect.{unsafe, IO} 22 | import cats.effect.testkit.TestContext 23 | 24 | import minitest.api.{DefaultExecutionContext, TestSpec} 25 | 26 | import scala.concurrent.duration._ 27 | 28 | @deprecated("use TestControl from cats-effect-testkit", "1.4.0") 29 | abstract class DeterministicIOTestSuite extends BaseIOTestSuite[TestContext] { 30 | 31 | override protected final def makeExecutionContext(): TestContext = TestContext() 32 | 33 | override protected[effect] implicit def suiteEc: ExecutionContext = DefaultExecutionContext 34 | 35 | override protected[effect] def mkSpec(name: String, ec: TestContext, io: => IO[Unit]): TestSpec[Unit, Unit] = 36 | TestSpec.sync(name, _ => { 37 | val scheduler = new unsafe.Scheduler { 38 | 39 | def sleep(delay: FiniteDuration, action: Runnable): Runnable = { 40 | val cancel = ec.schedule(delay, action) 41 | new Runnable { def run() = cancel() } 42 | } 43 | 44 | def nowMillis() = ec.now().toMillis 45 | def monotonicNanos() = ec.now().toNanos 46 | } 47 | 48 | implicit val runtime: unsafe.IORuntime = 49 | unsafe.IORuntime(ec, ec, scheduler, () => (), unsafe.IORuntimeConfig()) 50 | 51 | val f = io.unsafeToFuture() 52 | ec.tickAll() 53 | f.value match { 54 | case Some(value) => value.get 55 | case None => throw new RuntimeException( 56 | s"The IO in ${this.getClass.getName}.$name did not terminate.\n" + 57 | "It's possible that you are using a ContextShift that is backed by other ExecutionContext or" + 58 | "the test code is waiting indefinitely." 59 | ) 60 | } 61 | }) 62 | } 63 | -------------------------------------------------------------------------------- /minitest/shared/src/main/scala/cats/effect/testing/minitest/IOTestSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.minitest 18 | 19 | import cats.effect.{unsafe, IO} 20 | import cats.effect.testing.RuntimePlatform 21 | 22 | import minitest.api._ 23 | 24 | import scala.concurrent.ExecutionContext 25 | import scala.concurrent.duration._ 26 | 27 | abstract class IOTestSuite extends BaseIOTestSuite[ExecutionContext] with RuntimePlatform { 28 | protected def makeExecutionContext(): ExecutionContext = DefaultExecutionContext 29 | 30 | protected def timeout: FiniteDuration = 10.seconds 31 | 32 | protected[effect] def mkSpec(name: String, ec: ExecutionContext, io: => IO[Unit]): TestSpec[Unit, Unit] = { 33 | TestSpec.async[Unit](name, { _ => 34 | // TODO cleanup 35 | implicit val runtime: unsafe.IORuntime = createIORuntime(ec) 36 | io.timeout(timeout).unsafeToFuture() 37 | }) 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /minitest/shared/src/test/scala/cats/effect/testing/minitest/TestDetSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.minitest 18 | 19 | import cats.effect.IO 20 | import scala.concurrent.duration._ 21 | 22 | @deprecated("use TestControl instead", since = "1.5.0") 23 | object TestDetSuite extends DeterministicIOTestSuite { 24 | test("IO values should work") { 25 | IO(true).flatMap(b => IO(assert(b))) 26 | } 27 | 28 | test("Using Timer should work") { 29 | IO.sleep(1.day) >> IO(assert(true)).timeout(1.second) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /minitest/shared/src/test/scala/cats/effect/testing/minitest/TestNondetSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.minitest 18 | 19 | import cats.effect.IO 20 | import scala.concurrent.duration._ 21 | 22 | object TestNondetSuite extends IOTestSuite { 23 | 24 | test("IO values should work") { 25 | IO(true).flatMap(b => IO(assert(b))) 26 | } 27 | 28 | test("Timer and ContextShift should be available for respective operations") { 29 | IO.sleep(1.second) >> IO.cede >> IO(assert(true)).timeout(1.second) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=1.11.1 2 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("org.typelevel" % "sbt-typelevel" % "0.8.0") 2 | addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.18.2") 3 | addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.17") 4 | addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.3.2") 5 | -------------------------------------------------------------------------------- /scalatest/js-native/src/main/scala/cats/effect/testing/scalatest/AsyncIOSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.scalatest 18 | 19 | import cats.effect.IO 20 | import cats.effect.testing.RuntimePlatform 21 | import cats.effect.unsafe.IORuntime 22 | import org.scalactic.source.Position 23 | import org.scalatest.AsyncTestSuite 24 | import org.scalatest.enablers.Retrying 25 | import org.scalatest.time.Span 26 | 27 | trait AsyncIOSpec extends AssertingSyntax with EffectTestSupport with RuntimePlatform { asyncTestSuite: AsyncTestSuite => 28 | 29 | implicit lazy val ioRuntime: IORuntime = createIORuntime(executionContext) 30 | 31 | implicit def ioRetrying[T]: Retrying[IO[T]] = new Retrying[IO[T]] { 32 | override def retry(timeout: Span, interval: Span, pos: Position)(fun: => IO[T]): IO[T] = 33 | IO.fromFuture( 34 | IO(Retrying.retryingNatureOfFutureT[T](IORuntime.global.compute).retry(timeout, interval, pos)(fun.unsafeToFuture())), 35 | ) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /scalatest/jvm/src/main/scala/cats/effect/testing/scalatest/AsyncIOSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.scalatest 18 | 19 | import cats.effect.IO 20 | import cats.effect.testing.RuntimePlatform 21 | import cats.effect.unsafe.IORuntime 22 | import org.scalactic.source.Position 23 | import org.scalatest.AsyncTestSuite 24 | import org.scalatest.enablers.Retrying 25 | import org.scalatest.time.Span 26 | 27 | trait AsyncIOSpec extends AssertingSyntax with EffectTestSupport with RuntimePlatform { asyncTestSuite: AsyncTestSuite => 28 | 29 | implicit lazy val ioRuntime: IORuntime = IORuntime.global 30 | 31 | implicit def ioRetrying[T]: Retrying[IO[T]] = new Retrying[IO[T]] { 32 | override def retry(timeout: Span, interval: Span, pos: Position)(fun: => IO[T]): IO[T] = 33 | IO.fromFuture( 34 | IO(Retrying.retryingNatureOfFutureT[T](IORuntime.global.compute).retry(timeout, interval, pos)(fun.unsafeToFuture())), 35 | ) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /scalatest/jvm/src/test/scala/cats/effect/testing/scalatest/DeadlockTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.scalatest 18 | 19 | import cats.effect.IO 20 | import org.scalatest.freespec.AsyncFreeSpec 21 | 22 | class DeadlockTest extends AsyncFreeSpec with AsyncIOSpec { 23 | 24 | "should not deadlock" in { 25 | IO.blocking {}.unsafeRunSync() 26 | succeed 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /scalatest/shared/src/main/scala/cats/effect/testing/scalatest/AssertingSyntax.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.scalatest 18 | 19 | import cats.Functor 20 | import cats.effect.Sync 21 | import org.scalatest.{Assertion, Assertions, Succeeded} 22 | import cats.syntax.all._ 23 | 24 | /** 25 | * Copied from FS2 26 | * https://github.com/functional-streams-for-scala/fs2/blob/188a37883d7bbdf22bc4235a3a1223b14dc10b6c/core/shared/src/test/scala/fs2/Fs2Spec.scala#L73-L120 27 | */ 28 | trait AssertingSyntax { 29 | self: Assertions => 30 | /** Provides various ways to make test assertions on an `F[A]`. */ 31 | implicit class Asserting[F[_], A](private val self: F[A]) { 32 | 33 | /** 34 | * Asserts that the `F[A]` completes with an `A` which passes the supplied function. 35 | * 36 | * @example {{{ 37 | * IO(1).asserting(_ shouldBe 1) 38 | * }}} 39 | */ 40 | def asserting(f: A => Assertion)(implicit F: Sync[F]): F[Assertion] = 41 | self.flatMap(a => F.delay(f(a))) 42 | 43 | /** 44 | * Asserts that the `F[A]` completes with an `A` and no exception is thrown. 45 | */ 46 | def assertNoException(implicit F: Functor[F]): F[Assertion] = 47 | self.as(Succeeded) 48 | 49 | /** 50 | * Asserts that the `F[A]` fails with an exception of type `E`. 51 | */ 52 | def assertThrows[E <: Throwable](implicit F: Sync[F], ct: reflect.ClassTag[E]): F[Assertion] = 53 | assertThrowsError[E](_ => succeed) 54 | 55 | /** 56 | * Asserts that the `F[A]` fails with an exception of type `E` and an expected error. 57 | */ 58 | def assertThrowsError[E <: Throwable](test: E => Assertion)(implicit F: Sync[F], ct: reflect.ClassTag[E]): F[Assertion] = 59 | self.attempt.flatMap { 60 | case Left(e: E) => 61 | F.delay(test(e)) 62 | case Left(t) => 63 | F.delay( 64 | fail( 65 | s"Expected an exception of type ${ct.runtimeClass.getName} but got an exception: $t" 66 | ) 67 | ) 68 | case Right(a) => 69 | F.delay( 70 | fail(s"Expected an exception of type ${ct.runtimeClass.getName} but got a result: $a") 71 | ) 72 | } 73 | 74 | /** 75 | * Asserts that the `F[A]` fails with an exception of type `E` and an expected error message. 76 | */ 77 | def assertThrowsWithMessage[E <: Throwable](expectedMessage: String)(implicit F: Sync[F], ct: reflect.ClassTag[E]): F[Assertion] = 78 | assertThrowsError[E] { e => 79 | if (e.getMessage == expectedMessage) 80 | succeed 81 | else 82 | fail( 83 | s"Expected exception to have message '$expectedMessage' but got: ${e.getMessage}" 84 | ) 85 | } 86 | 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /scalatest/shared/src/main/scala/cats/effect/testing/scalatest/CatsResource.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing 18 | package scalatest 19 | 20 | import cats.effect.{Async, Deferred, Resource, Sync} 21 | import cats.syntax.all.* 22 | 23 | import org.scalatest.{BeforeAndAfterAll, FixtureAsyncTestSuite, FutureOutcome, Outcome} 24 | 25 | import scala.concurrent.duration.* 26 | 27 | trait CatsResource[F[_], A] extends BeforeAndAfterAll { this: FixtureAsyncTestSuite => 28 | 29 | def ResourceAsync: Async[F] 30 | private[this] implicit def _ResourceAsync: Async[F] = ResourceAsync 31 | 32 | def ResourceUnsafeRun: UnsafeRun[F] 33 | private[this] implicit def _ResourceUnsafeRun: UnsafeRun[F] = ResourceUnsafeRun 34 | 35 | val resource: Resource[F, A] 36 | 37 | protected val ResourceTimeout: Duration = 10.seconds 38 | protected def finiteResourceTimeout: Option[FiniteDuration] = 39 | Some(ResourceTimeout) collect { 40 | case fd: FiniteDuration => fd 41 | } 42 | 43 | // we use the gate to prevent further step execution 44 | // this isn't *ideal* because we'd really like to block the specs from even starting 45 | // but it does work on scalajs 46 | @volatile 47 | private var gate: Option[Deferred[F, Unit]] = None 48 | @volatile 49 | private var value: Option[Either[Throwable, A]] = None 50 | @volatile 51 | private var shutdown: F[Unit] = ().pure[F] 52 | 53 | override def beforeAll(): Unit = { 54 | val toRun = for { 55 | d <- Deferred[F, Unit] 56 | _ <- Sync[F] delay { 57 | gate = Some(d) 58 | } 59 | 60 | pair <- resource.attempt.allocated 61 | (a, shutdownAction) = pair 62 | 63 | _ <- Sync[F] delay { 64 | value = Some(a) 65 | shutdown = shutdownAction 66 | } 67 | 68 | _ <- d.complete(()) 69 | } yield () 70 | 71 | UnsafeRun[F].unsafeToFuture(toRun, finiteResourceTimeout) 72 | () 73 | } 74 | 75 | override def afterAll(): Unit = { 76 | UnsafeRun[F].unsafeToFuture(shutdown, finiteResourceTimeout) 77 | 78 | gate = None 79 | value = None 80 | shutdown = ().pure[F] 81 | } 82 | 83 | override type FixtureParam = A 84 | 85 | override def withFixture(test: OneArgAsyncTest): FutureOutcome = { 86 | lazy val toRun: F[Outcome] = Sync[F] defer { 87 | gate match { 88 | case Some(g) => 89 | g.get *> (Async[F] fromFuture { 90 | Sync[F] delay { 91 | withFixture { 92 | test.toNoArgAsyncTest { 93 | value match { 94 | case Some(Right(x)) => x 95 | case Some(Left(ex)) => fail("Failed during fixture acquisition", ex) 96 | case None => fail("Resource Not Initialized When Trying to Use") 97 | } 98 | } 99 | }.toFuture 100 | } 101 | }) 102 | 103 | case None => 104 | // just... loop I guess? sometimes we can hit this before scalatest has run the earlier action 105 | toRun 106 | } 107 | } 108 | 109 | new FutureOutcome(UnsafeRun[F].unsafeToFuture(toRun, finiteResourceTimeout)) 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /scalatest/shared/src/main/scala/cats/effect/testing/scalatest/CatsResourceIO.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing 18 | package scalatest 19 | 20 | import cats.effect.{Async, IO} 21 | import cats.effect.unsafe.IORuntime 22 | 23 | import org.scalatest.FixtureAsyncTestSuite 24 | 25 | import scala.concurrent.Future 26 | import scala.concurrent.duration._ 27 | 28 | trait CatsResourceIO[A] extends CatsResource[IO, A] with RuntimePlatform { this: FixtureAsyncTestSuite => 29 | 30 | final def ResourceAsync = Async[IO] 31 | 32 | final def ResourceUnsafeRun = _ResourceUnsafeRun 33 | 34 | private lazy val _ResourceUnsafeRun = 35 | new UnsafeRun[IO] { 36 | private implicit val runtime: IORuntime = createIORuntime(executionContext) 37 | 38 | override def unsafeToFuture[B](ioa: IO[B]): Future[B] = 39 | unsafeToFuture(ioa, None) 40 | 41 | override def unsafeToFuture[B](ioa: IO[B], timeout: Option[FiniteDuration]): Future[B] = 42 | timeout.fold(ioa)(ioa.timeout).unsafeToFuture() 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /scalatest/shared/src/main/scala/cats/effect/testing/scalatest/EffectTestSupport.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.scalatest 18 | 19 | import cats.effect.{unsafe, IO, SyncIO} 20 | 21 | import org.scalatest.{Assertion, Succeeded} 22 | 23 | import scala.concurrent.{ExecutionContext, Future} 24 | 25 | /** 26 | * Copied from FS2 27 | * https://github.com/functional-streams-for-scala/fs2/blob/188a37883d7bbdf22bc4235a3a1223b14dc10b6c/core/shared/src/test/scala/fs2/EffectTestSupport.scala 28 | */ 29 | trait EffectTestSupport { 30 | 31 | implicit def ioRuntime: unsafe.IORuntime 32 | implicit def executionContext: ExecutionContext 33 | 34 | implicit def syncIoToFutureAssertion(io: SyncIO[Assertion]): Future[Assertion] = 35 | Future(io.unsafeRunSync()) 36 | implicit def ioToFutureAssertion(io: IO[Assertion]): Future[Assertion] = 37 | io.unsafeToFuture() 38 | implicit def syncIoUnitToFutureAssertion(io: SyncIO[Unit]): Future[Assertion] = 39 | Future(io.as(Succeeded).unsafeRunSync()) 40 | implicit def ioUnitToFutureAssertion(io: IO[Unit]): Future[Assertion] = 41 | io.as(Succeeded).unsafeToFuture() 42 | } 43 | -------------------------------------------------------------------------------- /scalatest/shared/src/test/scala/cats/effect/testing/scalatest/CatsResourceErrorSpecs.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.scalatest 18 | 19 | import cats.effect.{Outcome as _, *} 20 | import org.scalatest.* 21 | import org.scalatest.wordspec.FixtureAsyncWordSpec 22 | 23 | import java.util.concurrent.TimeoutException 24 | import scala.concurrent.duration.* 25 | 26 | case class BlowUpResourceException() extends RuntimeException("boom") 27 | 28 | class CatsResourceErrorSpecs 29 | extends FixtureAsyncWordSpec 30 | with CatsResourceIO[Int] { 31 | 32 | private val expectedException = BlowUpResourceException() 33 | 34 | override protected val ResourceTimeout: Duration = 10.millis 35 | override val resource: Resource[IO, Int] = 36 | IO.raiseError(expectedException).toResource 37 | 38 | override def withFixture(test: OneArgAsyncTest): FutureOutcome = 39 | new FutureOutcome(super.withFixture(test).toFuture.recover { 40 | case TestFailedException(`expectedException`) => 41 | Succeeded 42 | case ex: TimeoutException => 43 | fail("Timeout received, probably because of unreported resource acquisition failure", ex) 44 | }) 45 | 46 | "cats resource specifications" should { 47 | "report errors in resource acquisition" in { i => 48 | fail(s"should not get here, but received $i") 49 | } 50 | } 51 | } 52 | 53 | object TestFailedException { 54 | def unapply(tfEx: org.scalatest.exceptions.TestFailedException): Option[Throwable] = 55 | Option(tfEx.getCause) 56 | } 57 | -------------------------------------------------------------------------------- /scalatest/shared/src/test/scala/cats/effect/testing/scalatest/CatsResourceSpecs.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.scalatest 18 | 19 | import cats.effect.{IO, Ref, Resource} 20 | import org.scalatest.matchers.must.Matchers._ 21 | import org.scalatest.wordspec.FixtureAsyncWordSpec 22 | 23 | class CatsResourceSpecs extends FixtureAsyncWordSpec with AsyncIOSpec with CatsResourceIO[Ref[IO, Int]] { 24 | 25 | override val resource: Resource[IO, Ref[IO, Int]] = 26 | Resource.make(Ref[IO].of(0))(_.set(Int.MinValue)) 27 | 28 | "cats resource specifications" should { 29 | "run a resource modification" in { ref => 30 | ref 31 | .modify { a => 32 | (a + 1, a) 33 | } 34 | .map( 35 | _ must be(0) 36 | ) 37 | } 38 | 39 | "be shared between tests" in { ref => 40 | ref 41 | .modify { a => 42 | (a + 1, a) 43 | } 44 | .map( 45 | _ must be(1) 46 | ) 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /scalatest/shared/src/test/scala/cats/effect/testing/scalatest/IOTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.scalatest 18 | 19 | import cats.effect.{IO, SyncIO} 20 | import org.scalatest.matchers.should.Matchers 21 | import org.scalatest.freespec.AsyncFreeSpec 22 | 23 | class IOSpecTests extends AsyncFreeSpec with AsyncIOSpec with Matchers { 24 | 25 | "Asserting Syntax " - { 26 | "IO Asserting" in { 27 | IO(1).asserting(_ shouldBe 1) 28 | } 29 | 30 | "SyncIO Asserting" in { 31 | SyncIO(1).asserting(_ shouldBe 1) 32 | } 33 | 34 | "IO assert no exception" in { 35 | IO(()).assertNoException 36 | } 37 | 38 | "IO assert Exception" in { 39 | IO.raiseError(AError).assertThrows[AError.type] 40 | } 41 | 42 | "IO assert Exception with message" in { 43 | val expectedMessage = "foo" 44 | IO.raiseError(AErrorWithMessage(expectedMessage)) 45 | .assertThrowsWithMessage[AErrorWithMessage](expectedMessage) 46 | } 47 | 48 | } 49 | 50 | "Effect assertions" - { 51 | "IO Assertion" in { 52 | IO(1 shouldBe 1) 53 | } 54 | 55 | "SyncIO Assertion" in { 56 | SyncIO(1 shouldBe 1) 57 | } 58 | 59 | "Successful IO[Unit] treated as success" in { 60 | IO(()) 61 | } 62 | 63 | "Successful SyncIO[Unit] treated as success" in { 64 | SyncIO(()) 65 | } 66 | } 67 | } 68 | 69 | case object AError extends Throwable 70 | case class AErrorWithMessage(message: String) extends Throwable { 71 | override def getMessage: String = message 72 | } 73 | -------------------------------------------------------------------------------- /scalatest/shared/src/test/scala/cats/effect/testing/scalatest/IOTestAsyncFlatSpecLike.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.scalatest 18 | 19 | import cats.effect.IO 20 | import org.scalatest.freespec.AsyncFreeSpecLike 21 | import org.scalatest.matchers.should.Matchers 22 | 23 | class IOSpecAsyncFlatSpecTests extends AsyncIOSpec with AsyncFreeSpecLike with Matchers { 24 | 25 | "Asserting Syntax " - { 26 | "IO Asserting" in { 27 | IO(1).asserting(_ shouldBe 1) 28 | } 29 | } 30 | 31 | "Effect assertions" - { 32 | "IO Assertion" in { 33 | IO(1 shouldBe 1) 34 | } 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /specs2/js-native/src/test/scala/cats/effect/testing/specs2/CatsEffectSpecsPlatform.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.specs2 18 | 19 | trait CatsEffectSpecsPlatform { this: CatsEffectSpecs => 20 | def platformSpecs = "really execute effects" in skipped 21 | } 22 | -------------------------------------------------------------------------------- /specs2/jvm/src/test/scala/cats/effect/testing/specs2/CatsEffectSpecsPlatform.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.specs2 18 | 19 | import cats.effect.IO 20 | 21 | trait CatsEffectSpecsPlatform { this: CatsEffectSpecs => 22 | def platformSpecs = { 23 | "really execute effects" in { 24 | var gate = false 25 | 26 | "forcibly attempt to get the deferred value" in { 27 | IO.cede.untilM_(IO(gate)).as(ok) 28 | } 29 | 30 | "complete the deferred value inside IO context" in { 31 | (IO { gate = true }).as(ok) 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /specs2/shared/src/main/scala/cats/effect/testing/specs2/CatsEffect.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing 18 | package specs2 19 | 20 | import cats.effect.{MonadCancel, Resource} 21 | import cats.syntax.all._ 22 | 23 | import org.specs2.execute.AsResult 24 | import org.specs2.specification.core.{AsExecution, Execution} 25 | 26 | import scala.concurrent.duration._ 27 | 28 | trait CatsEffect { 29 | 30 | protected val Timeout: Duration = 10.seconds 31 | protected def finiteTimeout: Option[FiniteDuration] = 32 | Some(Timeout) collect { 33 | case fd: FiniteDuration => fd 34 | } 35 | 36 | implicit def effectAsExecution[F[_]: UnsafeRun, R](implicit R: AsResult[R]): AsExecution[F[R]] = new AsExecution[F[R]] { 37 | def execute(t: => F[R]): Execution = 38 | Execution 39 | .withEnvAsync(_ => UnsafeRun[F].unsafeToFuture(t, finiteTimeout)) 40 | .copy(timeout = finiteTimeout) 41 | } 42 | 43 | implicit def resourceAsExecution[F[_]: UnsafeRun, R](implicit F: MonadCancel[F, Throwable], R: AsResult[R]): AsExecution[Resource[F, R]] = new AsExecution[Resource[F, R]] { 44 | def execute(t: => Resource[F, R]): Execution = 45 | effectAsExecution[F, R].execute(t.use(_.pure[F])) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /specs2/shared/src/main/scala/cats/effect/testing/specs2/CatsResource.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing 18 | package specs2 19 | 20 | import cats.effect._ 21 | import cats.effect.syntax.all._ 22 | import cats.syntax.all._ 23 | import org.specs2.specification.BeforeAfterAll 24 | 25 | import scala.concurrent.duration._ 26 | 27 | abstract class CatsResource[F[_]: Async: UnsafeRun, A] extends BeforeAfterAll with CatsEffect { 28 | 29 | val resource: Resource[F, A] 30 | 31 | protected val ResourceTimeout: Duration = 10.seconds 32 | protected def finiteResourceTimeout: Option[FiniteDuration] = 33 | Some(ResourceTimeout) collect { 34 | case fd: FiniteDuration => fd 35 | } 36 | 37 | // we use the gate to prevent further step execution 38 | // this isn't *ideal* because we'd really like to block the specs from even starting 39 | // but it does work on scalajs 40 | @volatile 41 | private var gate: Option[Deferred[F, Unit]] = None 42 | private var value: Option[Either[Throwable, A]] = None 43 | private var shutdown: F[Unit] = ().pure[F] 44 | 45 | override def beforeAll(): Unit = { 46 | val toRun = for { 47 | d <- Deferred[F, Unit] 48 | _ <- Sync[F] delay { 49 | gate = Some(d) 50 | } 51 | 52 | pair <- resource.attempt.allocated 53 | (a, shutdownAction) = pair 54 | 55 | _ <- Sync[F] delay { 56 | value = Some(a) 57 | shutdown = shutdownAction 58 | } 59 | 60 | _ <- d.complete(()) 61 | } yield () 62 | 63 | UnsafeRun[F].unsafeToFuture(toRun, finiteResourceTimeout) 64 | () 65 | } 66 | 67 | override def afterAll(): Unit = { 68 | UnsafeRun[F].unsafeToFuture(shutdown, finiteResourceTimeout) 69 | 70 | gate = None 71 | value = None 72 | shutdown = ().pure[F] 73 | } 74 | 75 | def withResource[R](f: A => F[R]): F[R] = 76 | gate match { 77 | case Some(g) => 78 | finiteResourceTimeout.foldl(g.get)(_.timeout(_)) *> Sync[F].delay(value.get).rethrow.flatMap(f) 79 | 80 | // specs2's runtime should prevent this case 81 | case None => 82 | Spawn[F].cede >> withResource(f) 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /specs2/shared/src/test/scala/cats/effect/testing/specs2/CatsEffectSpecs.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.specs2 18 | 19 | import cats.effect.{IO, Ref, Resource} 20 | import cats.syntax.all._ 21 | import org.specs2.mutable.Specification 22 | 23 | class CatsEffectSpecs extends Specification with CatsEffect with CatsEffectSpecsPlatform { 24 | 25 | "cats effect specifications" should { 26 | "run a non-effectful test" in { 27 | true must beTrue 28 | } 29 | 30 | "run a simple effectful test" in IO { 31 | true must beTrue 32 | false must beFalse 33 | } 34 | 35 | "run a simple resource test" in { 36 | true must beTrue 37 | }.pure[Resource[IO, *]] 38 | 39 | "resource must be live for use" in { 40 | Resource.make(Ref[IO].of(true))(_.set(false)) evalMap { r => 41 | r.get.map(_ must beTrue) 42 | } 43 | } 44 | 45 | platformSpecs 46 | 47 | // "timeout a failing test" in (IO.never: IO[Boolean]) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /specs2/shared/src/test/scala/cats/effect/testing/specs2/CatsResourceErrorSpecs.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.specs2 18 | 19 | import cats.effect.{IO, Resource} 20 | import org.specs2.execute.Result 21 | import org.specs2.mutable.SpecificationLike 22 | 23 | case class BlowUpResourceException() extends RuntimeException("boom") 24 | 25 | class CatsResourceErrorSpecs 26 | extends CatsResource[IO, Unit] 27 | with SpecificationLike { 28 | 29 | private val expectedException = BlowUpResourceException() 30 | 31 | val resource: Resource[IO, Unit] = 32 | Resource.eval(IO.raiseError(expectedException)) 33 | 34 | "cats resource support" should { 35 | "report failure when the resource acquisition fails" in withResource[Result] { _ => 36 | IO(failure("we shouldn't get here if an exception was raised")) 37 | } 38 | .recover[Result] { 39 | case ex: RuntimeException => 40 | ex must beEqualTo(expectedException) 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /specs2/shared/src/test/scala/cats/effect/testing/specs2/CatsResourceParallelSpecs.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect 18 | package testing.specs2 19 | 20 | import org.specs2.mutable.SpecificationLike 21 | 22 | import scala.concurrent.duration._ 23 | 24 | class CatsResourceParallelSpecs extends CatsResource[IO, Unit] with SpecificationLike { 25 | // *not* sequential 26 | 27 | val resource = Resource.eval(IO.sleep(500.millis)) 28 | 29 | "cats resource parallel test support" should { 30 | "await the resource availability" in withResource(_ => IO.pure(ok)) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /specs2/shared/src/test/scala/cats/effect/testing/specs2/CatsResourceSpecs.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.specs2 18 | 19 | import cats.effect.{IO, Ref, Resource} 20 | import org.specs2.mutable.SpecificationLike 21 | 22 | class CatsResourceSpecs extends CatsResource[IO, Ref[IO, Int]] with SpecificationLike { 23 | sequential 24 | 25 | val resource: Resource[IO, Ref[IO, Int]] = 26 | Resource.make(Ref[IO].of(0))(_.set(Int.MinValue)) 27 | 28 | "cats resource support" should { 29 | "run a resource modification" in withResource { ref => 30 | ref.modify{a => 31 | (a + 1, a) 32 | }.map( 33 | _ must_=== 0 34 | ) 35 | } 36 | 37 | "be shared between tests" in withResource {ref => 38 | ref.modify{a => 39 | (a + 1, a) 40 | }.map( 41 | _ must_=== 1 42 | ) 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /utest/shared/src/main/scala/cats/effect/testing/utest/DeterministicIOTestSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.utest 18 | 19 | import cats.effect.{unsafe, IO} 20 | import cats.effect.testkit.TestContext 21 | 22 | import utest.TestSuite 23 | 24 | import scala.concurrent.{ExecutionContext, Future} 25 | import scala.concurrent.duration._ 26 | 27 | @deprecated("use TestControl from cats-effect-testkit", "1.4.0") 28 | abstract class DeterministicIOTestSuite extends TestSuite { 29 | protected val testContext: TestContext = TestContext() 30 | protected def allowNonIOTests: Boolean = false 31 | 32 | override def utestWrap(path: Seq[String], runBody: => Future[Any])(implicit ec: ExecutionContext): Future[Any] = { 33 | val scheduler = new unsafe.Scheduler { 34 | 35 | def sleep(delay: FiniteDuration, action: Runnable): Runnable = { 36 | val cancel = testContext.schedule(delay, action) 37 | new Runnable { def run() = cancel() } 38 | } 39 | 40 | def nowMillis() = testContext.now().toMillis 41 | def monotonicNanos() = testContext.now().toNanos 42 | } 43 | 44 | implicit val runtime: unsafe.IORuntime = 45 | unsafe.IORuntime(testContext, testContext, scheduler, () => (), unsafe.IORuntimeConfig()) 46 | 47 | runBody.flatMap { 48 | case io: IO[Any] => 49 | val f = io.unsafeToFuture() 50 | testContext.tickAll() 51 | assert(testContext.state.tasks.isEmpty) 52 | f.value match { 53 | case Some(_) => f 54 | case None => throw new RuntimeException(s"The IO in ${path.mkString(".")} did not terminate.") 55 | } 56 | case other if allowNonIOTests => Future.successful(other) 57 | case other => 58 | throw new RuntimeException(s"Test body must return an IO value. Got $other") 59 | }(new ExecutionContext { 60 | def execute(runnable: Runnable): Unit = runnable.run() 61 | def reportFailure(cause: Throwable): Unit = throw cause 62 | }) 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /utest/shared/src/main/scala/cats/effect/testing/utest/EffectTestSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing 18 | package utest 19 | 20 | import cats.effect.Temporal 21 | 22 | import scala.annotation.nowarn 23 | import scala.concurrent.duration._ 24 | import scala.concurrent.{ExecutionContext, Future} 25 | import scala.reflect.ClassTag 26 | 27 | @nowarn 28 | abstract class EffectTestSuite[F[_]: Temporal: UnsafeRun](implicit Tag: ClassTag[F[Any]]) 29 | extends _root_.utest.TestSuite { 30 | 31 | protected def timeout: FiniteDuration = 10.seconds 32 | protected def allowNonIOTests: Boolean = false 33 | 34 | override def utestWrap(path: Seq[String], runBody: => Future[Any])(implicit ec: ExecutionContext): Future[Any] = { 35 | runBody flatMap { 36 | case Tag(io) => UnsafeRun[F].unsafeToFuture(io, Some(timeout)) 37 | case other if allowNonIOTests => Future.successful(other) 38 | case other => throw new RuntimeException(s"Test body must return an IO value. Got $other") 39 | } 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /utest/shared/src/test/scala/cats/effect/testing/utest/TestDetSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.utest 18 | 19 | import cats.effect.IO 20 | import scala.concurrent.duration._ 21 | import utest._ 22 | 23 | @deprecated("use TestControl instead", since = "1.5.0") 24 | object TestDetSuite extends DeterministicIOTestSuite { 25 | val tests = Tests { 26 | test("IO values should work") { 27 | IO(true).flatMap(b => IO(assert(b))) 28 | } 29 | 30 | test("Using Timer should work") { 31 | IO.sleep(1.day) >> IO(assert(true)).timeout(1.second) 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /utest/shared/src/test/scala/cats/effect/testing/utest/TestNondetSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Typelevel 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cats.effect.testing.utest 18 | 19 | import cats.effect.IO 20 | import utest.{Tests, assert, test} 21 | import scala.concurrent.duration._ 22 | 23 | object TestNondetSuite extends EffectTestSuite[IO] { 24 | 25 | override val timeout: FiniteDuration = 2.seconds 26 | override val allowNonIOTests: Boolean = true 27 | 28 | val tests = Tests { 29 | test("IO values should work") { 30 | IO(true).flatMap(b => IO(assert(b))) 31 | } 32 | 33 | test("Timer and ContextShift should be available for respective operations") { 34 | IO.sleep(1.second) >> IO(assert(true)).timeout(1.second) 35 | } 36 | 37 | test("Non-IO values should be OK if allowNonIOTests is overriden") { 38 | assert(1 == 1) 39 | } 40 | } 41 | } 42 | --------------------------------------------------------------------------------