Skip to content

hychen/ke-e

Repository files navigation

Build Status License

ké--ê

ké--ê is a property-based testing library, inspired by QuickCheck, Hypothesis, JSVerify and faker.js.

It is also available to generate fake data for many purpose like faker.js.

What is ké--ê?

假的 (ké--ê) is a Taiwanese word. the meaning is fake things in English.

Features

  • Property-based test utilities library.
  • Repeatable random fake data generators.
  • Mocha test framwork integration.
  • Support for multiple localities.
  • Support for browser and Node.js.

check documents for more details.

Property-Based Testing

Property-based tests make statements about the output of your code based on the input, and these statements are verified for many different possible inputs.

By default, ke.hold runs 50 tests. it reports a counter example does not satisfy the property if any test fail.

for example, the following test.

describe('Int', () => {
  ke.hold(
    '> 0',       // property name.
    x => x > 0   // predicate.
  )
  .over(0)       // first especially case.
  .over(2)       // second especially case.
  .over(ke.int)  // universal case.
})

will report:

1) Int > 0:

      AssertionError: > 0 doesn't hold, counter example: -5, tried: 1/100
      + expected - actual

      -false
      +true

Exception-Based Testing

You can still use chai to do the testing with random test values generated by ke-e.

describe('Nat', () => {
  it('>= 0',  () => {
    ke.forall(ke.nat).eval((n) => {
      expect(n >= 0).eq(true);
    });
  }
})

Repeatable

test results are repeatable.

$ mocha
1) Int >0:
AssertionError: >0 doesn't hold, seed: -1764850555, counter example: -5265798245849472, tried: 3/3`

Seed=-1764850555 mocha
1) Int >0:
AssertionError: >0 doesn't hold, seed: -1764850555, counter example: -5265798245849472, tried: 3/3`

Localization

This project follows unicode CLDR specification. en is default locale id.

// Generate English first name.
ke.person.firstName.random.

// Generate Transitional Chinese first name.
ke.person.firstName.locale('zh-Hant-TW').random.

Node.js REPL

$ ke
> ke.int.random
123124343

Arbitraries

The purpose of a arbitraries is random generation and shrinking of values in property-based testing, but you could use them to generate fake data individually.

some of definitions of arbitraries took from faker.js project.

Primitive

  • any - produce any primitive types.
  • boolean — produce true and false with equal probability.
  • falsy — produce falsy values: false, null, undefined, '', 0, and NaN.
  • nat — produce a natural number.
  • int — produce a integer.
  • pint — produce a positive integer.
  • nint — produce a negative integer.
  • num — produce a float numbers.
  • pnum — produce a positive float numbers.
  • nnum — produce negative float numbers.
  • char — produce a unicode character.
  • asciichar — produce an ascii character.
  • string — produce a unicode string.
  • nestring - produce a non-empty unicode string.
  • asciistring — produce an ascii string.
  • neasciistring — produce an non-empty ascii string.

Combinator

  • constant — produce a constant value.
  • elements — produce one of the given values.
  • regex - produce a value from the regular expression.
  • frequency - Choose one of the given arbitraries, with a weighted random distribution.
  • oneOf — randomly uses one of the given generators.
  • maybe - produce a value or null.
  • array — produce an array.
  • nearray — produce a non-empty array.
  • sequence — produce an array of given arbitraries in order.
  • object — produce an object.
  • impl - produce a instance of class C.
  • call - produces a value of function with given arbitrary arguments.
  • func — produce a function.
  • genfunc - produce a generator.
  • promise - produce a promise.
  • recursive - produce a recursive value.

Locale

  • locale
  • localeids — produce a locale id.
  • country
  • timezone

Datetime

  • date — produce a Date.

Literate

  • literate
  • word
  • words
  • sentence
  • sentences
  • paragraph
  • paragraphs
  • lines
  • text

Person

  • person - produce an object to reprsent a person.
  • name - produce the name of a person.
  • firstName - produce the first name of a person.
  • lastName - produce the last name of a person.
  • gender - produce the gender of a person.

Internet

  • internet
  • avatar - produce an avatar uri.
  • userName - produce a internet username.
  • password - produce a password string.
  • email - produce an email.
  • url - produce an url.

Location

  • location
  • address
  • streetAddress
  • state
    • abbr
  • zipCode
  • county
  • city
  • street
  • buildingNumber
  • latitude - produce an latitude.
  • longitude - produce an longitude.
  • coordinates - produce a coordinates.

Grahpic

  • graphic
    • color
      • rgba
      • hsla
      • hsva
      • hex
      • hexCode
    • image
      • imageURL - an image url.
      • grayImageURL - a gray image url.

Installation

Node.js

install the module with: npm install ke-e

import ke from `ke-e`

describe('Integer', () => {
  ke.hold(
    'x + y = y + x'
    (x, y) => x + y === y + x
    ).over(ke.int, ke.int);
});

Browser using script tag

Download ke-e.js and place it in your project, then add it as the follwoing.

dones not work now.

<script src="http://hychen.me/ke-e/ke-e.js"</script>
<script>
describe('Integer', () => {
  ke.hold(
    'x + y = y + x'
    (x, y) => x + y === y + x
    ).over(ke.int, ke.int);
});
</script>

Get Involved

License

The MIT License (MIT)

About

假的 (ké--ê) is a property-based testing library, inspired by QuickCheck, Hypothesis, JSVerify and faker.js.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published