Skip to content

Commit

Permalink
added tests for util class and color handler class
Browse files Browse the repository at this point in the history
  • Loading branch information
bhofmei committed Jun 23, 2017
1 parent d719da3 commit e4013b3
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .bowerrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"directory": "test"
}
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
test/*
!test/index.html
!test/spec
!test/run-jasmine.js
!test/data
built
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: node_js
node_js:
- 7
sudo: false
cache:
directories:
- $HOME/.cache/bower
before_install:
- pip install --user RangeHTTPServer
- npm install -g bower
install:
- bower install
before_script:
- python -m RangeHTTPServer &
script:
- phantomjs test/run-jasmine.js http:https://localhost:8000/test/
23 changes: 23 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "jbplugin-nucdens",
"homepage": "https://github.com/bhofmei/jbplugin-nucdens#readme",
"description": "JBrowse plugin for viewing nucleotide density",
"main": "js/main.js",
"keywords": [
"jbrowse",
"jbrowse-plugin",
"nucelotide density"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"src",
"test"
],
"devDependencies": {
"jasmine-core": "jasmine#^2.4.1",
"jbrowse": "git:https://github.com/GMOD/jbrowse.git#master"
}
}
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "jbplugin-smallrna",
"version": "1.3.1",
"description": "JBrowse plugin for viewing small RNA alignments",
"main": "js/main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},

"repository": {
"type": "git",
"url": "git+https://github.com/bhofmei/jbplugin-smallrna.git"
},
"keywords": [
"jbrowse",
"jbrowse-plugin",
"small rna"
],
"author": "Brigitte Hofmeister",
"license": "MIT",
"bugs": {
"url": "https://github.com/bhofmei/jbplugin-smallrna/issues"
},
"ignore": [
"node_modules",
"test"
],
"directories": {
"test": "test"
},
"homepage": "https://github.com/bhofmei/jbplugin-smallrna#readme"
}
47 changes: 47 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http:https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<title>Jasmine Spec Runner</title>

<link rel="stylesheet" href="jasmine-core/lib/jasmine-core/jasmine.css">
<script src="jasmine-core/lib/jasmine-core/jasmine.js"></script>
<script src="jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
<script src="jasmine-core/lib/jasmine-core/boot.js"></script>
<!--<script src ="jasmine-jquery/lib/jasmine-jquery.js"></script>
<script src="jquery/src/jquery.js"></script>-->

<!-- include source files here... -->
<script type="text/javascript" src="dojo/dojo.js" data-dojo-config="async: 1" ></script>

<script type="text/javascript">
require( { baseUrl: '.',
packages: [
'dojo',
'dijit',
'dojox',
'jszlib',
{ name: 'lazyload', location: 'lazyload', main: 'lazyload' },
'dgrid',
'xstyle',
'put-selector',
'FileSaver',
{ name: 'jDataView', location: 'jDataView/src', main: 'jdataview' },
{ name: 'JBrowse', location: 'jbrowse/src/JBrowse' },
{ name: 'NucleotideDensityPlugin', location: '../js' }
]
},
['dojo/ready','JBrowse/Browser','NucleotideDensityPlugin/View/Track/NucleotideDensity', 'NucleotideDensityPlugin/View/ColorHandler'],
function( ready,Browser,NucleotideDensity, colorHandler ) {
});
</script>
<script type="text/javascript" src="spec/NucleotideDensityPlugin.spec.js"></script>


</head>
<body>
<div id="sandbox" style="overflow:hidden; height:1px;"></div>

</body>
</html>
92 changes: 92 additions & 0 deletions test/run-jasmine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"use strict";
var system = require('system');

/**
* Wait until the test condition is true or a timeout occurs. Useful for waiting
* on a server response or for a ui change (fadeIn, etc.) to occur.
*
* @param testFx javascript condition that evaluates to a boolean,
* it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
* as a callback function.
* @param onReady what to do when testFx condition is fulfilled,
* it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
* as a callback function.
* @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.
*/
function waitFor(testFx, onReady, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3001, //< Default Max Timeout is 3s
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
} else {
if(!condition) {
// If condition still not fulfilled (timeout but condition is 'false')
console.log("'waitFor()' timeout");
phantom.exit(1);
} else {
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
clearInterval(interval); //< Stop this interval
}
}
}, 100); //< repeat check every 100ms
};


if (system.args.length !== 2) {
console.log('Usage: run-jasmine2.js URL');
phantom.exit(1);
}

var page = require('webpage').create();

// Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
page.onConsoleMessage = function(msg) {
console.log(msg);
};

page.open(system.args[1], function(status){
if (status !== "success") {
console.log("Unable to access network");
phantom.exit();
} else {
waitFor(function(){
return page.evaluate(function(){
return (document.body.querySelector('.symbolSummary .pending') === null &&
document.body.querySelector('.jasmine-duration') !== null);
});
}, function(){
var exitCode = page.evaluate(function(){
console.log('');

var title = 'Jasmine';
var version = document.body.querySelector('.jasmine-version').innerText;
var duration = document.body.querySelector('.jasmine-duration').innerText;
var banner = title + ' ' + version + ' ' + duration;
console.log(banner);

var list = document.body.querySelectorAll('.jasmine-results > .jasmine-failures > .jasmine-spec-detail.jasmine-failed');
if (list && list.length > 0) {
console.log('');
console.log(list.length + ' test(s) FAILED:');
for (i = 0; i < list.length; ++i) {
var el = list[i],
desc = el.querySelector('.jasmine-description'),
msg = el.querySelector('.jasmine-messages > .jasmine-result-message');
console.log('');
console.log(desc.innerText);
console.log(msg.innerText);
console.log('');
}
return 1;
} else {
console.log(document.body.querySelector('.jasmine-alert > .jasmine-bar,.jasmine-passed > .bar.skipped').innerText);
return 0;
}
});
phantom.exit(exitCode);
});
}
});
112 changes: 112 additions & 0 deletions test/spec/NucleotideDensityPlugin.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
require([
'dojo/_base/declare',
'dojo/_base/array',
'JBrowse/Browser',
'NucleotideDensityPlugin/View/ColorHandler',
'NucleotideDensityPlugin/View/Track/NucleotideDensity',
'NucleotideDensityPlugin/Store/SeqFeature/NucDensity',
'NucleotideDensityPlugin/Store/Util'
], function(
declare,
array,
Browser,
ColorHandler,
nucDensTrack,
nucDensStore,
nucDensUtil
) {

describe( 'Initial test', function() {
var test = true;
it('jasmine is working', function() {
expect(test).toBe(true);
});
}); // end initial test

describe( 'Test degenerate nucleotide utilities', function(){
it('non-degenerate test, forward only', function(){
var cgAr = nucDensUtil.getPossibilities('CG',false);
var gttAr = nucDensUtil.getPossibilities('GTT',false);
expect(cgAr.length).toBe(1);
expect(gttAr.length).toBe(1);
});

it('non-degenerate test, both strands', function(){
var cgAr = nucDensUtil.getPossibilities('CG',true);
var gttAr = nucDensUtil.getPossibilities('GTT',true);
expect(cgAr.length).toBe(2);
expect(gttAr.length).toBe(2);
});

it('degenerate test, forward only', function(){
var cnAr = nucDensUtil.getPossibilities('CN',false);
var chhAr = nucDensUtil.getPossibilities('CHH',false);
var gbrAr = nucDensUtil.getPossibilities('GBR',false);
var nnAr = nucDensUtil.getPossibilities('NN',false);
var nnnAr = nucDensUtil.getPossibilities('NNN',false);
expect(cnAr.length).toBe(4);
expect(chhAr.length).toBe(9);
expect(gbrAr.length).toBe(6);
expect(nnAr.length).toBe(16);
expect(nnnAr.length).toBe(64);
});

it('degenerate test, both strands', function(){
var cnAr = nucDensUtil.getPossibilities('CN',true);
var chhAr = nucDensUtil.getPossibilities('CHH',true);
var gbrAr = nucDensUtil.getPossibilities('GBR',true);
var nnAr = nucDensUtil.getPossibilities('NN',true);
expect(cnAr.length).toBe(8);
expect(chhAr.length).toBe(18);
expect(gbrAr.length).toBe(12);
expect(nnAr.length).toBe(32);
});
}); // end test util

describe('test color handler utility', function(){
it('generate n colors', function(){
var clr5 = ColorHandler.generateNColors(5);
expect(clr5.length).toBe(5);
expect(clr5).toEqual(["#ff00a2", "#ea8500", "#00bb20", "#00c2ff", "#0097ff"]);
});

it('generate random colors', function(){
var clr2 = ColorHandler.generateRandomColors(["CG","CH"]);
expect(clr2).toEqual({CG:"#ff00a2",CH:'#00c29b'});
});

it('get font color', function(){
var cr1 = ColorHandler.getFontColor('#284996');
expect(cr1).toBe('#F0F0F0');
var cr2 = ColorHandler.getFontColor('#f1b982');
expect(cr2).toBe('#010101');
});

it('get color from list', function(){
var colorList = ['blue','red','pink','purple']

var clr2 = ColorHandler.intToColorFromList(1, colorList, true);
expect(clr2).toBe('red');

var clr2 = ColorHandler.contextToColorFromList('CN', ['TA','WCN','CN','NY'],['blue','red','pink','purple'], true);
expect(clr2).toBe('pink');
});

it('get color from list not defined', function(){
var un = ColorHandler.contextToColorFromList('NN',['NH','GT','AC'],['blue','red','pink'], false);
expect(un).not.toBeDefined()
});

it('get color from repeatable list', function(){
var colorList = ['blue','red','pink'];
var ctxList = ['AA','AC','AT','CC','CG','CT','GA','GC','TT'];

var clr1 = ColorHandler.contextToColorFromList('CC',ctxList, colorList, true);
expect(clr1).toBe('blue');
var clr2 = ColorHandler.contextToColorFromList('TT',ctxList, colorList, true);
expect(clr2).toBe('pink');
});
}); // end test color handler

});

0 comments on commit e4013b3

Please sign in to comment.