diff --git a/.travis.yml b/.travis.yml index 3574534..5b84490 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,5 +2,3 @@ language: node_js node_js: - "6" - "6.1" - - "5.11" - - "iojs" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..346fedf --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +test: + ./node_modules/.bin/mocha --reporter spec + +.PHONY: test \ No newline at end of file diff --git a/README.md b/README.md index daeee14..65638e1 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,7 @@ # Genetic algorithm framework built with JavaScript ES6 -[![Build Status](https://travis-ci.org/acupy/genetic-algorithm-es6.svg?branch=master)](https://travis-ci.org/acupy/genetic-algorithm-es6) - -[![NPM](https://nodei.co/npm/genetic-algorithm-fw.png?downloads=true)](https://npmjs.org/package/genetic-algorithm-fw) +[![Build Status](https://travis-ci.org/nitin42/genetic-algorithm-es6.svg?branch=master)](https://travis-ci.org/nitin42/genetic-algorithm-es6) +![coverage] (https://img.shields.io/badge/coverage-52.78%25-yellowgreen.svg) This package provides a framework for building applications where genetic algorithm (GA) is used for solving optimization problems based on a natural selection process that mimics biological evolution. diff --git a/package.json b/package.json index 08dab26..f154b93 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ }, "scripts": { "build": "babel src --presets babel-preset-es2015 --out-dir dist", - "prepublish": "npm run build" + "prepublish": "npm run build", + "test": "mocha --require babel-polyfill --compilers js:babel-register" }, "keywords": [ "genetic", @@ -27,6 +28,18 @@ "readmeFilename": "README.md", "devDependencies": { "babel-cli": "^6.18.0", - "babel-preset-es2015": "^6.18.0" + "babel-core": "^6.18.2", + "babel-istanbul": "^0.11.0", + "babel-polyfill": "^6.16.0", + "babel-preset-es2015": "^6.18.0", + "babel-preset-latest": "^6.16.0", + "babel-register": "^6.18.0", + "chai": "^3.5.0", + "mocha": "^3.2.0", + "should": "^11.1.1" + }, + "dependencies": { + "babel-istanbul": "^0.11.0", + "istanbul": "^0.4.5" } } diff --git a/src/index.js b/src/index.js index 7ad7598..e12b25b 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -export default class { +module.exports = class GeneticClass { constructor( mutationFunction = (phenotype) => phenotype, crossoverFunction = (a, b) => [a, b], @@ -15,6 +15,15 @@ export default class { this.population = population; this.populationSize = populationSize; this.chanceOfMutation = chanceOfMutation; + + this.populate = this.populate.bind(this); + this.mutate = this.mutate.bind(this); + this.crossover = this.crossover.bind(this); + this.isABetterThanB = this.isABetterThanB.bind(this); + this.compete = this.compete.bind(this); + this.mixPopulationOrder = this.mixPopulationOrder.bind(this); + this.evolve = this.evolve.bind(this); + this.best = this.best.bind(this); } populate () { @@ -101,3 +110,4 @@ export default class { return theBest; } } + diff --git a/test/src.js b/test/src.js new file mode 100644 index 0000000..aa3290c --- /dev/null +++ b/test/src.js @@ -0,0 +1,107 @@ +'use strict'; + +const chai = require('chai'); +const should = require('should'); + +let GeneticClass = require('../src/index.js'); + +let ga = new GeneticClass(); + +const expect = chai.expect; + +describe('Testing the GeneticClass', () => { + it('object should be instance of class', () => { + expect(ga).to.be.an.instanceof(GeneticClass); + }); + + it('Class should be ok', () => { + expect(GeneticClass).to.be.ok; + }); + + it('instance should be ok', () => { + expect(GeneticClass).to.be.ok; + }); + + it('class should exists', () => { + expect(GeneticClass).to.exist; + }); + + it('class should have populate method and not throw Error', () => { + expect(ga.populate).to.exist; + expect(ga.populate).to.not.throw(Error); + }); + + + it('class should have mutate method and not throw Error', () => { + expect(ga.mutate).to.exist; + expect(ga.mutate).to.not.throw(Error); + }); + + it('class should have crossover method and not throw Error', () => { + expect(ga.crossover).to.exist; + expect(ga.crossover).to.not.throw(Error); + }); + + it('class should have comparison method and not throw Error', () => { + expect(ga.isABetterThanB).to.exist; + expect(ga.isABetterThanB).to.not.throw(Error); + }); + + it('class should have compete method and not throw Error', () => { + expect(ga.compete).to.exist; + expect(ga.compete).to.not.throw(Error); + }); + + it('class should have mixPopulation method and not throw Error', () => { + expect(ga.mixPopulationOrder).to.exist; + expect(ga.mixPopulationOrder).to.not.throw(Error); + }); + + it('class should have evolve method and not throw Error', () => { + expect(ga.evolve).to.exist; + expect(ga.evolve).to.not.throw(Error); + }); + + it('class should have best method and not throw Error', () => { + expect(ga.best).to.exist; + expect(ga.best).to.not.throw(Error); + }); + + it('class should respond to its method', () => { + expect(GeneticClass).to.respondTo('populate'); + }); + + it('class should respond to its method', () => { + expect(GeneticClass).to.respondTo('crossover'); + }); + + it('class should respond to its method', () => { + expect(GeneticClass).to.respondTo('mutate'); + }); + + it('class should respond to its method', () => { + expect(GeneticClass).to.respondTo('isABetterThanB'); + }); + + it('class should respond to its method', () => { + expect(GeneticClass).to.respondTo('compete'); + }); + + it('class should respond to its method', () => { + expect(GeneticClass).to.respondTo('mixPopulationOrder'); + }); + + it('class should respond to its method', () => { + expect(GeneticClass).to.respondTo('evolve'); + }); + + it('class should respond to its method', () => { + expect(GeneticClass).to.respondTo('best'); + }); + + it('some methods should expect arguments', () => { + ga.mutate(expect(arguments).to.be.arguments); + ga.crossover(expect(arguments).to.be.arguments); + ga.isABetterThanB(expect(arguments).to.be.arguments); + }); +})