Skip to content

Commit

Permalink
fixed imports, removed utilTest from old html5Report
Browse files Browse the repository at this point in the history
  • Loading branch information
cirquit authored and janschaefer committed Jan 20, 2017
1 parent bd21ea6 commit 8dceea8
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 23 deletions.
28 changes: 9 additions & 19 deletions jgiven-html-app/src/js/util.js
Expand Up @@ -2,16 +2,7 @@
* Utility functions
*/

// let _ = require('lodash');

import forEach from "lodash/fp/forEach";
import curry from "lodash/fp/curry";
import filter from "lodash/fp/filter";
import flow from "lodash/fp/flow";

// this is somehow needed, because functions from "lodash/fp/..." behave
// very differently than from the "_" object
import _ from 'lodash';
import { forEach, flow, curry, filter, indexOf, sortBy, takeWhile, drop, partial } from "lodash";

String.prototype.capitalize = function () {
return this.charAt(0).toUpperCase() + this.slice(1);
Expand Down Expand Up @@ -114,7 +105,7 @@ export function getScenarioId (scenario) {
}

export function sortByDescription (scenarios) {
var sortedScenarios = _.forEach(_.sortBy(scenarios, function (x) {
var sortedScenarios = forEach(sortBy(scenarios, function (x) {
return x.description.toLowerCase();
}), function (x) {
x.expanded = false;
Expand Down Expand Up @@ -150,7 +141,7 @@ export function ownProperties (obj) {
}

export function deselectAll (options) {
_.forEach(options, function (option) {
forEach(options, function (option) {
option.selected = false;
});
}
Expand All @@ -169,15 +160,15 @@ export function getArgumentInfos(wordArray) {
}

flow(
filter(isArgument),
forEach(curry(updateContainer)(nameArray, enumArray))
partial(filter, _, isArgument),
partial(forEach, _, curry(updateContainer)(nameArray, enumArray))
)(wordArray);

return [enumArray, nameArray];
}

export function parseNextInt(arr) {
var numbers = _.takeWhile(arr, function (c) { return !isNaN(c) && c !== " "; }).join("");
var numbers = takeWhile(arr, function (c) { return !isNaN(c) && c !== " "; }).join("");
var parsedInt = parseInt(numbers);
var result = { integer : isNaN(parsedInt) ? undefined : parsedInt
, length : numbers.length } ;
Expand All @@ -186,8 +177,8 @@ export function parseNextInt(arr) {

export function parseNextArgumentName(string) {
var stopChars = [" ", ",", ";", ":","\"","%","!","[","]","(",")","-","_"];
var isNonStopChar = function (c) { return _.indexOf(stopChars, c) === -1; };
return _.takeWhile(string, isNonStopChar).join("");
var isNonStopChar = function (c) { return indexOf(stopChars, c) === -1; };
return takeWhile(string, isNonStopChar).join("");
}

export function replaceArguments(string, enumArray, nameArray) {
Expand All @@ -204,10 +195,9 @@ export function replaceArguments(string, enumArray, nameArray) {
var argumentLen = 0;

if (isSeparator && !escaped) {
var substring = _.drop(string, i+1);
var substring = drop(string, i+1);
var argName = parseNextArgumentName(substring);
var argIndex = parseNextInt(substring);

// named placeholder '$[argumentname]'
if (nameArray[argName] !== undefined) {
argument = nameArray[argName];
Expand Down
81 changes: 77 additions & 4 deletions jgiven-html-app/src/test/utilTest.js
@@ -1,7 +1,5 @@
import { nanosToReadableUnit, splitClassName, parseNextInt, replaceArguments, getArgumentInfos } from '../js/util.js'

let _ = require('lodash');

describe("Util", function () {

describe("nanosToReadableUnit", function () {
Expand Down Expand Up @@ -42,7 +40,83 @@ describe("Util", function () {
expect(t.packageName).toEqual("");
});
});


describe("getArgumentInfos", function() {
it("works for words which are arguments", function() {
function argumentName(str) { return { "argumentName" : str }; }

var words1 = [ { "value" : 1, "argumentInfo": argumentName("i") } ];
var words2 = [ { "value" : 2, "argumentInfo": argumentName("i") }
, { "value" : "str", "argumentInfo" : argumentName("string") }
, { "value" : -10, "argumentInfo" : argumentName("neg_integer") }
, { "value" : "", "argumentInfo" : argumentName("empty") }
];
var enumRes1 = [], nameRes1 = [], enumRes2 = [], nameRes2 = [];

var [enumRes1, nameRes1] = getArgumentInfos(words1);
expect(enumRes1.length).toEqual(1);
expect(nameRes1["i"]).toEqual(1);


var [enumRes2, nameRes2] = getArgumentInfos(words2);
expect(enumRes2.length).toEqual(4);
expect(enumRes2[0]).toEqual(2);
expect(enumRes2[1]).toEqual("str");
expect(enumRes2[2]).toEqual(-10);
expect(enumRes2[3]).toEqual("");

expect(nameRes2["i"]).toEqual(2);
expect(nameRes2["string"]).toEqual("str");
expect(nameRes2["neg_integer"]).toEqual(-10);
expect(nameRes2["empty"]).toEqual("");
});

it("works for words which are not arguments", function() {
var words1 = [ { "value" : 1 } ];
var words2 = [ { "value" : 1 }
, { "value" : "str" }
, { "value" : -10 }
, { "value" : "" }
];
var enumRes1 = [], nameRes1 = [], enumRes2 = [], nameRes2 = [];
var [enumRes1, nameRes1] = getArgumentInfos(words1);
expect(enumRes1.length).toEqual(0);

var [enumRes2, nameRes2] = getArgumentInfos(words2);
expect(enumRes1.length).toEqual(0);
});

it("works for mixed words and arguments", function() {
function argumentName(str) { return { "argumentName" : str }; }

var words1 = [ { "value" : 1, "argumentInfo": argumentName("i") }
, { "value" : " " }
];
var words2 = [ { "value" : "Given"}
, { "value" : 2, "argumentInfo": argumentName("i") }
, { "value" : "then do something..." }
, { "value" : -10, "argumentInfo" : argumentName("neg_integer") }
, { "value" : "and after that it returns"}
, { "value" : "nothing", "argumentInfo" : argumentName("empty") }
];
var enumRes1 = [], nameRes1 = [], enumRes2 = [], nameRes2 = [];

[enumRes1, nameRes1] = getArgumentInfos(words1);
expect(enumRes1[0]).toEqual(1);
expect(nameRes1["i"]).toEqual(1);

[enumRes2, nameRes2] = getArgumentInfos(words2);
expect(enumRes2.length).toEqual(3);
expect(enumRes2[0]).toEqual(2);
expect(enumRes2[1]).toEqual(-10);
expect(enumRes2[2]).toEqual("nothing");

expect(nameRes2["i"]).toEqual(2);
expect(nameRes2["neg_integer"]).toEqual(-10);
expect(nameRes2["empty"]).toEqual("nothing");
});
});

describe("parseNextInt", function() {
it("works for digits", function() {
var res1 = parseNextInt("0");
Expand Down Expand Up @@ -182,7 +256,6 @@ describe("Util", function () {

var res = replaceArguments("Referencing arguments per name - int : $i, bool : $bool", enumArray, nameArray);
expect(res).toEqual("Referencing arguments per name - int : 0, bool : false");

});

it("works for named placeholder", function() {
Expand Down

0 comments on commit 8dceea8

Please sign in to comment.