GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - nhaarman/expect.kt: Readable test assertions in Kotlin

BranchesTags
Open more actions menu

Latest commit

 

History

55 Commits
55 Commits

Folders and files

NameName
Last commit message
Last commit date
.idea
.idea
 
 
expect.kt
expect.kt
 
 
gradle
gradle
 
 
.gitignore
.gitignore
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
build.gradle
build.gradle
 
 
gradle.properties
gradle.properties
 
 
gradlew
gradlew
 
 
gradlew.bat
gradlew.bat
 
 
settings.gradle
settings.gradle
 
 

Repository files navigation

Expect.kt

Expect.kt allows you to write test assertions in a readable way:

expect(3 + 2).toBeEqualTo(5)
expect(1 == 2).toBe(false)
expect(1 + 2).toBeGreaterThan(2)

You can also provide custom messages:

expect("expect.kt").toContain("pec") { "String did not contain 'pec'" }

Failure messages will be descriptive:

expect(3 + 2).toBeEqualTo(6)

java.lang.AssertionError: 
	Expected 5 to be equal to 6.

And:

val actual: String? = null
expect(actual).toContain("test")

java.lang.AssertionError: 
	Expected value to contain "test", but the actual value was null.

Extensions

Due to Kotlin's extensions, we can easily add functionality to an existing matcher.

fun StringMatcher.toBeEmpty() {
  if (actual?.isEmpty() != true) {
    fail("Expected \"$actual\" to be empty.")
  }
}

We can now easily use this function in our tests:

expect("test").toBeEmpty()

java.lang.AssertionError: 
	Expected value to be empty, but actual value was "test".

Custom matchers

You can also write matchers for custom types:

data class MyType(val name: String)

fun expect(actual: MyType?) : MyTypeMatcher {
  return MyTypeMatcher(actual)
}

class MyTypeMatcher(actual: MyType?) : Matcher<MyType>(actual) {

  fun toBeNamed(expected: String) {
    if(actual?.name != expected) {
      fail("Expected ${actual?.name} to be $expected")
    }
  }
}

Setup

Expect.kt is hosted on Maven Central. To use Expect.kt in your gradle test builds, add the following to your build.gradle:

testCompile 'com.nhaarman:expect.kt:x.x.x'

License

Copyright 2016 Niek Haarman

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Readable test assertions in Kotlin

Resources

Readme
Apache-2.0 license
Activity

Stars

46 stars

Watchers

3 watching

Forks

8 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.