Skip to content

Commit

Permalink
Proper error checking for parsing, naming changes, readme updates
Browse files Browse the repository at this point in the history
- DOMParser did not throw errors, so extra error checking functionality was needed
- changes argument names to be more clearer
- adds new test cases to handle error checking for invalid inputs
  • Loading branch information
d2fong committed Jan 11, 2017
1 parent 70047fc commit 54907db
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,25 @@ fetch('some-sbgnml-file.xml').then( fileString => {

For a holistic view on how to use this module, take a look at the [example](https://github.com/PathwayCommons/sbgnml-to-cytoscape/tree/master/example) folder.

## Tests
## Errors

Feeding invalid sbgnml text to the converter will result in an error being thrown.

```js
let convert = require('sbgnml-to-cytoscape')

let graph = convert(null); // error: Could not convert the following text to xml: null
```

## Commands

#### Development
Run the following commands to spin up a test server:
```sh
gulp
```

#### Tests
Run the tests with:
```sh
npm test
Expand Down
10 changes: 8 additions & 2 deletions src/sbgnmlConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ var sbgnmlConverter = {
var parser = new DOMParser();
doc = parser.parseFromString(text, 'text/xml');
}

var parseError = doc.getElementsByTagName('parsererror');
if (parseError.length > 0) {
throw new Error('Could not convert the following text to xml: ' + JSON.stringify(text));
}

return doc;
},
sbgnmlTags: {
Expand Down Expand Up @@ -411,12 +417,12 @@ var sbgnmlConverter = {
var cytoscapeJsEdge = {data: edgeObj};
jsonArray.push(cytoscapeJsEdge);
},
convert: function (filestring) {
convert: function (sbgnmlText) {
var self = this;
var cytoscapeJsNodes = [];
var cytoscapeJsEdges = [];

var xmlObject = this.loadXMLFromString(filestring);
var xmlObject = this.loadXMLFromString(sbgnmlText);

var compartments = self.getAllCompartments(xmlObject);

Expand Down
24 changes: 22 additions & 2 deletions test/sbgnmlConverter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ var fixtureFiles = [
'./fixtures/input/mapk_cascade.xml',
'./fixtures/input/neuronal_muscle_signalling.xml',
'./fixtures/input/polyq_proteins_interference.xml',
'./fixtures/input/vitamins_b6_activation_to_pyridoxal_phosphate.xml'
'./fixtures/input/vitamins_b6_activation_to_pyridoxal_phosphate.xml',


'./fixtures/input/broken.xml'
];

var outputFiles = [
Expand All @@ -25,7 +28,10 @@ var outputFiles = [
require('./fixtures/output/mapk_cascade.json'),
require('./fixtures/output/neuronal_muscle_signalling.json'),
require('./fixtures/output/polyq_proteins_interference.json'),
require('./fixtures/output/vitamins_b6_activation_to_pyridoxal_phosphate.json')
require('./fixtures/output/vitamins_b6_activation_to_pyridoxal_phosphate.json'),


require('./fixtures/output/broken.json')
];

var getFileText = function (filename) {
Expand Down Expand Up @@ -53,4 +59,18 @@ describe('sbgnmlConverter', function () {
assert.deepEqual(expected, actual);
}
});
it('should throw an error when the text to xml conversion function fails', function () {
var garbageInputs = [null, 'blah', true, false, {'stuff': 'stuff'}];

var c = function (input) {
converter.convert(input);
};

/* eslint-disable */
for (var i = 0; i < garbageInputs.length; i++) {
assert.throws(function () { c(garbageInputs[i]) }, Error); // show invalid argument in the error message
}

/* eslint-enable */
});
});

0 comments on commit 54907db

Please sign in to comment.