From 1b79faa3333a79c5fb0520b0c2efec95dbb2a726 Mon Sep 17 00:00:00 2001 From: Mark Griffiths Date: Mon, 5 Nov 2018 11:52:56 +0000 Subject: [PATCH] Add object and builtin tests --- .travis.yml | 1 + index.js | 6 +++--- index.mjs | 6 +++--- src/index.js | 6 +++--- test/obejcts.js | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 test/obejcts.js diff --git a/.travis.yml b/.travis.yml index a9b51fe..e784aab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ branches: language: node_js node_js: - stable + - 10 - 9 - 8 sudo: false diff --git a/index.js b/index.js index 5dd49f4..d9a4d44 100644 --- a/index.js +++ b/index.js @@ -179,11 +179,11 @@ const consoleFactory = function (options = {}) { sOut.write(format(inspect(obj, options))); }, - pretty(obj, depth = 0) { + pretty(obj, depth = 0, color = true) { sOut.write(format('Content: %s\n', inspect(obj, { depth, - colors: termNG.color.basic - }).slice(0, -1).replace(/^{/, 'Object\n ').replace(/^\[/, 'Array\n ').replace(/^(\w+) {/, '$1').replace(/:/g, ' ▸').replace(/,\n/g, '\n'))); + colors: color && termNG.color.basic + }).slice(0, -1).replace(/^{/, 'Object\n ').replace(/^\[/, 'Array\n ').replace(/^(\w+) {/, '$1').replace(/(\w+):/g, '$1 ▸').replace(/,\n/g, '\n'))); }, yargs(obj) { diff --git a/index.mjs b/index.mjs index 3ecdf8e..d51647a 100644 --- a/index.mjs +++ b/index.mjs @@ -173,11 +173,11 @@ const consoleFactory = function (options = {}) { sOut.write(format(inspect(obj, options))); }, - pretty(obj, depth = 0) { + pretty(obj, depth = 0, color = true) { sOut.write(format('Content: %s\n', inspect(obj, { depth, - colors: termNG.color.basic - }).slice(0, -1).replace(/^{/, 'Object\n ').replace(/^\[/, 'Array\n ').replace(/^(\w+) {/, '$1').replace(/:/g, ' ▸').replace(/,\n/g, '\n'))); + colors: color && termNG.color.basic + }).slice(0, -1).replace(/^{/, 'Object\n ').replace(/^\[/, 'Array\n ').replace(/^(\w+) {/, '$1').replace(/(\w+):/g, '$1 ▸').replace(/,\n/g, '\n'))); }, yargs(obj) { diff --git a/src/index.js b/src/index.js index 2d90a9f..3405631 100644 --- a/src/index.js +++ b/src/index.js @@ -303,16 +303,16 @@ const consoleFactory = function (options = {}) { * canWrite ▸ [Function] * ... */ - pretty(obj, depth = 0) { + pretty(obj, depth = 0, color = true) { sOut.write(format('Content: %s\n', inspect(obj, { depth, - colors: termNG.color.basic + colors: color && termNG.color.basic }) .slice(0, -1) .replace(/^{/, 'Object\n ') .replace(/^\[/, 'Array\n ') .replace(/^(\w+) {/, '$1') - .replace(/:/g, ' ▸') + .replace(/(\w+):/g, '$1 ▸') .replace(/,\n/g, '\n') )) }, diff --git a/test/obejcts.js b/test/obejcts.js new file mode 100644 index 0000000..2e8a8c5 --- /dev/null +++ b/test/obejcts.js @@ -0,0 +1,35 @@ +'use strict' + +import stream from 'stream' +import test from 'ava' +import {createConsole} from '..' + +const StreamProxy = new stream.PassThrough() +StreamProxy.setEncoding('utf8') + +const testConsole = createConsole({outStream: StreamProxy}) + +test('Pretty print', t => { + testConsole.pretty({ + test: 'Testing', + another: { + level: 'deep' + } + }, 2, false) + const result = StreamProxy.read() + t.is(result, `Content: Object\n test ▸ 'Testing', another ▸ { level ▸ 'deep' } \n`) +}) + +test('Object dir print', t => { + testConsole.dir({ + test: 'Testing', + another: { + level: 'deep' + } + }, { + depth: 2, + colors: false + }) + const result = StreamProxy.read() + t.is(result, `{ test: 'Testing', another: { level: 'deep' } }`) +})