From edcf20ce30b4f962d73a74035c20033cb468cace Mon Sep 17 00:00:00 2001 From: cirquit Date: Thu, 5 Jan 2017 18:23:20 +0100 Subject: [PATCH] fixed imports, removed utilTest from old html5Report --- jgiven-html-app/src/js/util.js | 28 ++++------ jgiven-html-app/src/test/utilTest.js | 81 ++++++++++++++++++++++++++-- 2 files changed, 86 insertions(+), 23 deletions(-) diff --git a/jgiven-html-app/src/js/util.js b/jgiven-html-app/src/js/util.js index e5a90dab8f..c37974d849 100644 --- a/jgiven-html-app/src/js/util.js +++ b/jgiven-html-app/src/js/util.js @@ -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); @@ -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; @@ -150,7 +141,7 @@ export function ownProperties (obj) { } export function deselectAll (options) { - _.forEach(options, function (option) { + forEach(options, function (option) { option.selected = false; }); } @@ -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 } ; @@ -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) { @@ -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]; diff --git a/jgiven-html-app/src/test/utilTest.js b/jgiven-html-app/src/test/utilTest.js index 504955e20f..961a371a98 100644 --- a/jgiven-html-app/src/test/utilTest.js +++ b/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 () { @@ -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"); @@ -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() {