Skip to content

Commit

Permalink
Adds more tests and fixes problems (#37)
Browse files Browse the repository at this point in the history
* Adds more tests and fixes problems

* more tests and more bug fixes

* more promise tests

* moore tests

* escape key and value values

* implemented printer & logger from whatwg spec

* temp version of format specifiers
  • Loading branch information
nikolayemrikh authored Sep 23, 2018
1 parent 9d712dd commit 1838135
Show file tree
Hide file tree
Showing 28 changed files with 650 additions and 182 deletions.
37 changes: 20 additions & 17 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,13 @@ gulp.task(`build-scripts`, () => {
jsnext: true,
browser: true
}),
commonjs({
include: `node_modules/**`
}),
commonjs(),
json(),
babel({
babelrc: false,
exclude: [`node_modules/**`, `js/tests/**`],
presets: [
[`@babel/preset-env`, {modules: false}]
[`@babel/preset-env`, {modules: false, useBuiltIns: `entry`}]
]
})
]
Expand All @@ -111,9 +109,7 @@ gulp.task(`build-prompt`, () => {
jsnext: true,
browser: true
}),
commonjs({
include: `node_modules/**`
}),
commonjs(),
json(),
babel({
babelrc: false,
Expand Down Expand Up @@ -168,18 +164,25 @@ gulp.task(`build-tests`, () => {
});

gulp.task(`test`, function (done) {
// return gulp
// .src([`js/**/*.test.js`], {
// read: false
// })
// .pipe(mocha({
// compilers: [`js:babel-register`],
// reporter: `spec`
// }));
let testOnlyFiles;
if (process.env.TEST_ONLY_FILES) {
testOnlyFiles = process.env.TEST_ONLY_FILES
.trim()
.split(/\s*\,\s*|\s+/)
.join(`|`);
}
const testsGlob = `build/js/tests/**/${testOnlyFiles ? `+(${testOnlyFiles})` : `*`}.test.js`;

new KarmaServer({
configFile: __dirname + `/karma.conf.js`,
// singleRun: true,
debug: true
singleRun: process.env.TEST_DEBUG !== `true`,
debug: true,
files: [
`node_modules/chai/chai.js`,
`karma-chai-adapter.js`,
`build/js/index.js`,
testsGlob
]
}, done).start();
});

Expand Down
5 changes: 3 additions & 2 deletions index-silent.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
<script>
const obj = {};
const arr = [obj];
arr.push(arr);
// arr.fn = fn;
const fn = (bar = 123) => {
return bar;
};
arr.push(arr);
arr.fn = fn;
fn.arr = arr;
fn.obj = obj;
fn.fn = fn;
obj.obj = obj;
obj.arr = arr;
obj.fn = fn;
Expand Down
6 changes: 4 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
});
const promptContainer = document.querySelector(`.prompt-container`);
const prompt = new Prompt(promptContainer, `jsConsole`);
window.jsConsole.onAny = (appendedElHeight) => {
window.jsConsole.onAny = (rowEl) => {
const appendedElHeight = rowEl.offsetHeight;
const promptOffsetBottom = promptContainer.offsetTop + prompt.viewHeight;
if (promptOffsetBottom < document.body.clientHeight) {
window.scroll(0, document.body.scrollTop + appendedElHeight);
Expand All @@ -72,9 +73,10 @@
return bar;
};
arr.push(arr);
// arr.fn = fn;
arr.fn = fn;
fn.arr = arr;
fn.obj = obj;
fn.fn = fn;
obj.obj = obj;
obj.arr = arr;
obj.fn = fn;
Expand Down
3 changes: 3 additions & 0 deletions js/array/array-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export default class ArrayView extends TypeView {
!this._parentView) {
this.toggleItalic(true);
}

this._state.isOpened = this.isOpeningAllowed;
}

_getStateDescriptors() {
Expand Down Expand Up @@ -178,6 +180,7 @@ export default class ArrayView extends TypeView {
addedKeysCounter++;
}
if (!inHead) {
fragment.appendChild(this._createGettersEntriesFragment());
TypeView.appendEntryElIntoFragment(
this._createTypedEntryEl({obj: arr, key: `length`, mode, notCheckDescriptors: true}),
fragment
Expand Down
133 changes: 96 additions & 37 deletions js/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {Mode, ViewType, Env} from './enums';

const DEFAULT_MAX_FIELDS_IN_HEAD = 5;

// const getSpecifiresRE = () => /%s|%d|%i|%f|%o|%O/g;

/**
* Console
* @class
Expand Down Expand Up @@ -110,6 +112,78 @@ export default class Console {
return params;
}

/**
* This is implementation of https://console.spec.whatwg.org/#logger
* @param {{}} opts
* @param {[]} entries
*/
_log(opts, entries) {
if (!entries.length) {
return;
}

// if (entries.length > 1 && getSpecifiresRE().test(entries[0])) {
// this._print(opts, this._format(entries));
// return;
// }

this._print(opts, entries);
}

// _format(entries) {
// let targetStr = entries.shift();
//
// const re = getSpecifiresRE();
//
// let match;
// while ((match = re.exec(targetStr)) !== null) {
// const substitution = entries.shift();
// const specifier = match[0];
// let convertedSubstitution;
// switch (specifier) {
// case `%s`:
// convertedSubstitution = substitution;
// break;
// case `%d`:
// case `%i`:
// if (typeof substitution === `symbol`) {
// convertedSubstitution = Number.NaN;
// } else {
// convertedSubstitution = Number.parseInt(substitution, 10);
// }
// break;
// case `%f`:
// if (typeof substitution === `symbol`) {
// convertedSubstitution = Number.NaN;
// } else {
// convertedSubstitution = Number.parseFloat(substitution);
// }
// break;
// case `%o`:
//
// break;
// case `%O`:
//
// break;
// }
// targetStr = targetStr.replace(specifier, convertedSubstitution);
// }
// entries.unshift(targetStr);
// return entries;
// }

_print({mode, modifier, onPrint}, values) {
const rowEl = getElement(`<div class="console__row ${modifier ? `console__row--${modifier}` : ``}"></div>`);
values.forEach((val) => {
rowEl.appendChild(this.createTypedView(val, mode).el);
});
this._el.appendChild(rowEl);
if (onPrint) {
onPrint(rowEl);
}
this.onAny(rowEl);
}

/**
* Subscribe on any event fired
* @abstract
Expand All @@ -120,80 +194,65 @@ export default class Console {
* Subscribe on log event fired
* @abstract
**/
onlog() {}
onLog() {}

/**
* Subscribe on logHTML event fired
* @abstract
**/
onlogHTML() {}
onLogHTML() {}

/**
* Subscribe on dir event fired
* @abstract
**/
ondir() {}
onDir() {}

/**
* Subscribe on error event fired
* @abstract
**/
onerror() {}
onError() {}

/**
* Equivalent to console.log
* Push rest of arguments into container
*/
log(...rest) {
const rowEl = this._getRowEl(rest, Mode.LOG);
this._el.appendChild(rowEl);
this.onlog();
this.onAny(rowEl.offsetHeight);
log(...entries) {
this._log({mode: Mode.LOG, onPrint: this.onLog}, entries);
}
/**
* Equivalent to this.log but marks row as output
*/
logOutput(...rest) {
const rowEl = this._getRowEl(rest, Mode.LOG, `output`);
this._el.appendChild(rowEl);
this.onlog();
this.onAny(rowEl.offsetHeight);
logOutput(...entries) {
this._log({mode: Mode.LOG, modifier: `output`, onPrint: this.onLog}, entries);
}

/**
* Equivalent to console.log but special charachters in strings won't be excaped
* Push rest of arguments into container
*/
logHTML(...rest) {
this._el.appendChild(this._getRowEl(rest, Mode.LOG_HTML));
this.onlogHTML();
this.onAny();
logHTML(...entries) {
this._log({mode: Mode.LOG_HTML, onPrint: this.onLogHTML}, entries);
}

/**
* Equivalent to console.error
* Push single value into conainer
* @param {*} val — value
*/
error(val) {
const el = getElement(`<div class="console__row console__row--error"></div>`);
el.appendChild(this.createTypedView(val, Mode.ERROR).el);
this._el.appendChild(el);
this.onerror();
this.onAny();
error(...entries) {
this._log({mode: Mode.ERROR, modifier: `error`, onPrint: this.onError}, entries);
}

/**
* Equivalent to console.dir
* Push single value into conainer
* @param {*} val — value
* Push single value into container
*/
dir(val) {
const el = getElement(`<div class="console__row"></div>`);
el.appendChild(this.createTypedView(val, Mode.DIR).el);
this._el.appendChild(el);
this.ondir();
this.onAny();
dir(...entries) {
if (!entries.length) {
return;
}
this._print({mode: Mode.DIR, onPrint: this.onDir}, [entries[0]]);
}

/**
Expand All @@ -202,10 +261,10 @@ export default class Console {
* @param {string} markup
*/
prompt(markup) {
const el = getElement(`<div class="console__row console__row--input"><pre class="console__item item"></pre></div>`);
el.querySelector(`.console__item`).innerHTML = markup;
this._el.appendChild(el);
this.onAny(el.offsetHeight);
const rowEl = getElement(`<div class="console__row console__row--input"><pre class="console__item item"></pre></div>`);
rowEl.querySelector(`.console__item`).innerHTML = markup;
this._el.appendChild(rowEl);
this.onAny(rowEl);
}

/**
Expand Down
20 changes: 0 additions & 20 deletions js/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,6 @@ export const ViewType = {
PRIMITIVE: `primitive`
};

/**
* CSS classes
* @enum {string}
*/
export const Class = {
CONSOLE_ITEM_HEAD: `item__head`,
CONSOLE_ITEM_POINTER: `item_pointer`,
CONSOLE_ITEM_HEAD_SHOW: `item__head_show`,
ENTRY_CONTAINER_BRACED: `entry-container_braced`,
ENTRY_CONTAINER_OVERSIZE: `entry-container_oversize`,
CONSOLE_ITEM_HEAD_PARENTHESED: `item__head_parenthesed`,
CONSOLE_ITEM_HEAD_INFO: `item__head-info`,
CONSOLE_ITEM_HEAD_ELEMENTS: `item__head-elements`,
CONSOLE_ITEM_HEAD_ELEMENTS_SHOW: `item__head-elements_show`,
CONSOLE_ITEM_CONTENT_CONTAINTER: `item-content-container`,
CONSOLE_ITEM_CONTENT_CONTAINTER_SHOW: `item-content-container_show`,
CONSOLE_ITEM_HEAD_ELEMENTS_LENGTH: `item__head-elements-length`,
CONSOLE_ITEM_HEAD_ELEMENTS_LENGTH_SHOW: `item__head-elements-length_show`
};

/**
* Console environment
* @enum {string}
Expand Down
7 changes: 6 additions & 1 deletion js/function/function-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export default class FunctionView extends TypeView {
_afterRender() {
this._state.isOpeningDisabled = this.isDisableOpening;

this._state.isOpened = this.isOpeningAllowed;

if (this._mode === Mode.LOG || this._mode === Mode.LOG_HTML || this._mode === Mode.ERROR) {
this._headContentEl.addEventListener(`click`, () => {
this._headContentEl.classList.toggle(`nowrap`);
Expand Down Expand Up @@ -153,7 +155,7 @@ ${this._fnType === FnType.ARROW ? ` => ` : ` `}${bodyLines.join(`\n`)}`;
return bodyContent;
}

createContent(fn) {
createContent(fn, inHead) {
const fragment = document.createDocumentFragment();
const entriesKeys = this.contentEntriesKeys;
for (let key of entriesKeys) {
Expand All @@ -162,6 +164,9 @@ ${this._fnType === FnType.ARROW ? ` => ` : ` `}${bodyLines.join(`\n`)}`;
fragment
);
}
if (!inHead) {
fragment.appendChild(this._createGettersEntriesFragment());
}
TypeView.appendEntryElIntoFragment(
this._createTypedEntryEl({obj: fn, key: `__proto__`, mode: Mode.PROP, notCheckDescriptors: true}),
fragment
Expand Down
2 changes: 1 addition & 1 deletion js/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import '@babel/polyfill';
import '@babel/polyfill/dist/polyfill';
import Console from './console';

window.Console = Console;
2 changes: 1 addition & 1 deletion js/object/map-entry-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class MapEntryView extends TypeView {
this._state.isBraced = this._mode !== Mode.PREVIEW;
this._state.isHeadContentShowed = true;
this._state.isOpeningDisabled = this._mode === Mode.PREVIEW;
this._state.isOpened = this._mode !== Mode.PREVIEW;
this._state.isOpened = this.isOpeningAllowed;
}

_getStateDescriptors() {
Expand Down
Loading

0 comments on commit 1838135

Please sign in to comment.