Skip to content

Commit

Permalink
updating unit test. 100% code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
skratchdot committed Nov 28, 2015
1 parent 6c001b5 commit a36f79c
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 216 deletions.
67 changes: 67 additions & 0 deletions .eslintrc
@@ -0,0 +1,67 @@
// adapted from: https://github.com/Khan/style-guides/pull/25
{
"plugins": [
],
"ecmaFeatures": {
"modules": true
},
"env": {
"browser": true,
"node": true,
"mocha": true
},
"rules": {
"arrow-parens": [2, "always"],
"arrow-spacing": 2,
"brace-style": 2,
"camelcase": [2, {
"properties": "always"
}],
"comma-dangle": [1, "always-multiline"],
"comma-dangle": [2, "never"],
"comma-spacing": [2, {
"before": false,
"after": true
}],
"eol-last": [0],
"guard-for-in": 2,
//"indent": [2, 4],
"linebreak-style": [2, "unix"],
/*
"max-len": [2, 80, 4, {
"ignoreUrls": true,
"ignorePattern": "^\\s*const\\s.+=\\s*require\\s*\\("
}],
*/
"no-alert": 2,
"no-array-constructor": 2,
"no-const-assign": 2,
"no-debugger": 2,
"no-dupe-keys": 2,
"no-new-object": 2,
"no-spaced-func": 2,
"no-this-before-super": 2,
"no-throw-literal": 2,
"no-trailing-spaces": 2,
"no-unreachable": 2,
"no-unused-vars": 1,
"no-var": 0,
//"object-curly-spacing": [2, "always"],
"one-var": [2, "never"],
"prefer-const": 1,
"prefer-template": 2,
"quotes": [2, "single"],
"semi": [2, "always"],
"space-after-keywords": [2, "always"],
"space-before-blocks": 2,
"space-before-function-paren": [2, {
"anonymous": "always",
"named": "never"
}],
"space-infix-ops": 2,
"space-return-throw-case": 2,
"strict": [0, "never"],
"valid-jsdoc": 2
},
"extends": "eslint:recommended"
}
7 changes: 6 additions & 1 deletion .gitignore
@@ -1 +1,6 @@
/node_modules/
_ignore/
coverage/
node_modules/
.DS_Store
.npm-debug.log
.project
14 changes: 0 additions & 14 deletions .jshintrc

This file was deleted.

2 changes: 1 addition & 1 deletion .npmignore
@@ -1,7 +1,7 @@
.git*
_ignore/
test/
.DS_Store
.gitignore
.npm-debug.log
.project
.travis.yml
13 changes: 13 additions & 0 deletions .tonic.example.js
@@ -0,0 +1,13 @@
var Picker = require('random-picker').Picker;
var picker = new Picker(); // or pass in a seed: var picker = new Picker(seed)
picker.option('male');
picker.option('female');
var r1 = picker.pick(); // 50% chance of being 'male' or 'female'
picker.option('male', 22);
picker.option('female', 78);
var r2 = picker.pick(); // now there's a 22% chance of being 'male', and a 78% change of being 'female'
picker.removeAll();
picker.option('good', 3.42);
picker.option('bad', 3.42);
var r3 = picker.pick(); // now there's a 50% chance of being 'good' or 'bad'
console.log(r1, r2, r3);
15 changes: 12 additions & 3 deletions .travis.yml
@@ -1,5 +1,14 @@
language: node_js
node_js:
- "0.10"
before_install: npm install -g grunt-cli
install: npm install
- 'stable'
- '4.2'
- '4.1'
- '4.0'
- '0.12'
- '0.11'
- '0.10'
before_script:
- npm install -g gulp
script:
- gulp test
- cat ./coverage/lcov.info | ./node_modules/.bin/coveralls --verbose
48 changes: 0 additions & 48 deletions Gruntfile.js

This file was deleted.

8 changes: 7 additions & 1 deletion README.md
@@ -1,10 +1,14 @@
# random-picker

[![NPM version](https://badge.fury.io/js/random-picker.svg)](http://badge.fury.io/js/random-picker)
[![Build Status](https://travis-ci.org/skratchdot/random-picker.png?branch=master)](https://travis-ci.org/skratchdot/random-picker)
[![Coverage Status](https://coveralls.io/repos/skratchdot/random-picker/badge.png)](https://coveralls.io/r/skratchdot/random-picker)
[![Code Climate](https://codeclimate.com/github/skratchdot/random-picker.png)](https://codeclimate.com/github/skratchdot/random-picker)
[![Coverage Status](https://coveralls.io/repos/skratchdot/random-picker/badge.svg?branch=master&service=github)](https://coveralls.io/github/skratchdot/random-picker?branch=master)
[![Dependency Status](https://david-dm.org/skratchdot/random-picker.svg)](https://david-dm.org/skratchdot/random-picker)
[![devDependency Status](https://david-dm.org/skratchdot/random-picker/dev-status.svg)](https://david-dm.org/skratchdot/random-picker#info=devDependencies)

[![NPM](https://nodei.co/npm/random-picker.png)](https://npmjs.org/package/random-picker)


## Description

Expand All @@ -30,6 +34,8 @@ picker.option('bad', 3.42)
picker.pick(); // now there's a 50% chance of being 'good' or 'bad'
```

- [Live example on Tonic](https://tonicdev.com/npm/random-picker)


## Documentation

Expand Down
62 changes: 62 additions & 0 deletions gulpfile.js
@@ -0,0 +1,62 @@
/*eslint no-console: 0 */
'use strict';
var gulp = require('gulp');
var eslint = require('gulp-eslint');
var isparta = require('isparta');
var istanbul = require('gulp-istanbul');
var mocha = require('gulp-mocha');
var files = {
lint: ['*.js'],
src: ['index.js'],
test: ['test.js']
};

gulp.task('lint', function () {
return gulp.src(files.lint)
// eslint() attaches the lint output to the eslint property
// of the file object so it can be used by other modules.
.pipe(eslint())
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(eslint.format('stylish'))
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
.pipe(eslint.failAfterError());
});

gulp.task('test', function () {
return gulp.src(files.src)
.pipe(istanbul({
instrumenter: isparta.Instrumenter,
includeUntested: true
}))
// Force `require` to return covered files
.pipe(istanbul.hookRequire())
.on('finish', function () {
gulp.src(files.test, {read: false})
// gulp-mocha needs filepaths so you can't have any plugins before it
.pipe(mocha({
reporter: 'spec'
}))
.on('error', function (err) {
console.error(err.toString());
this.emit('end');
})
.pipe(istanbul.writeReports({
dir: './coverage',
reporters: ['lcov', 'json', 'text-summary']
}));
});
});

gulp.task('watch', function () {
gulp.watch([files.lint], ['lint', 'test']);
});

// setup default task
gulp.task('default', ['lint', 'test', 'watch']);

// handle errors
process.on('uncaughtException', function (e) {
console.error(e);
});
17 changes: 9 additions & 8 deletions lib/random-picker.js → index.js
Expand Up @@ -10,7 +10,7 @@
var randomGenerator = require('random-seed');

var cleanScore = function (score) {
var num = score === 0 ? 0 : parseFloat(score) || 1;
var num = parseFloat(score) || 1;
if (!Number.isFinite(num) || num < 0) {
num = 0;
}
Expand All @@ -23,10 +23,10 @@ var Option = function (value, score) {
};

var Picker = function (seed) {
var api = this,
rand = randomGenerator.create(seed),
total = 0,
options = [];
var api = this;
var rand = randomGenerator.create(seed);
var total = 0;
var options = [];

var recalculateTotal = function () {
total = 0;
Expand All @@ -36,8 +36,8 @@ var Picker = function (seed) {
};

api.pick = function () {
var runningTotal = 0,
randomValue = rand.floatBetween(0, total);
var runningTotal = 0;
var randomValue = rand.floatBetween(0, total);
for (var i = 0; i < options.length; i++) {
runningTotal += options[i].score;
if (randomValue < runningTotal) {
Expand All @@ -48,7 +48,8 @@ var Picker = function (seed) {
};

api.option = function (value, score) {
var option = new Option(value, score), added = false;
var option = new Option(value, score);
var added = false;
if (option.score <= 0) {
api.remove(value);
} else {
Expand Down

0 comments on commit a36f79c

Please sign in to comment.