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 utilitis 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 100 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`
KESeed=-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 Tranditional Chinese first name.
ke.person.firstName.locale('zh-Hant-TW').random.
// Set global locale.
// ke.globalLocale = 'zh-Hant-TW';
Node.js REPL
$ ke
> ke.int.random.
123124343
Arbitraries
The pupose 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 difinitions of arbitraries took from faker.js project.
Primitive
- any - produce any primtive 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.
- number — produce a float numbers.
- pnumber — produce a positive float numbers.
- nnumber — 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 contant value.
- elements — produce one of the given values.
- regex - produce a value from the regualr expression.
- frequency - Choose one of the given arbitraries, with a weighted random distribution.
- suchThat — produce a value that satisfies a predicate.
- oneOf — randomly uses one of the given generators.
- maybe - produce a value or null.
- pair — produce a pair of two arbitraries.
- array — produce an array.
- nearray — produce a non-empty array.
- sequence — produce an array of given arbitraries in order.
- object — produce an object.
- objectOf - transform a class to an object arbitrary.
- func — produce a function.
- genfunc - produce a generator.
- promise - produce a promise.
- small - produce smaller version of a arbitrary.
- recursive - produce a recursive value.
- variant - produce valid or invalid value of an arbitrary .
- data - produce a test dataset.
Locale
- locale
- localeids — produce a locale id.
- country
- timezone
Datetime
- date — produce a Date.
Literate
- literate
- word
- words
- sentence
- sentences
- pagraph
- pagraphs
- 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.
- longtitude - produce an longtitude.
- coordinates - produce a coordinates.
Grahpic
- grahpic
- color
- rgba
- hsla
- hsva
- hex
- hexCode
- image
- imageURL - an image url.
- grayImageURL - a gray image url.
- color
Installation
Node.js
install the module with: npm install ke-e
;
Browser using script tag
Download ke-e.js and place it in your project, then add it as the follwoing.
Get Involved
- source code : github.com/hychen/ke-e.
License
The MIT License (MIT)