From 3fbc3fffb28c64511f7a7b4b950ac29b6d02954d Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Sun, 12 May 2024 20:29:48 +0300 Subject: [PATCH] verbose + more tests --- .../src/objects/org/eolang/int$div.js | 2 +- eo2js-runtime/src/runtime/bytes-of.js | 15 +++++++- eo2js-runtime/src/runtime/package.js | 2 +- eo2js-runtime/test/runtime/bytes-of.test.js | 38 ++++++++++++++----- eo2js/src/commands/link.js | 35 ++++++++++++----- eo2js/src/eo2js.js | 1 + eo2js/src/resources/js/__main__.js | 3 +- eo2js/src/resources/xsl/to-js.xsl | 4 +- eo2js/test/commands/dataize.test.js | 16 ++++++-- eo2js/test/commands/link.test.js | 10 ++++- eo2js/test/resources/dataize/app.js | 21 ++++++++++ 11 files changed, 117 insertions(+), 30 deletions(-) create mode 100644 eo2js/test/resources/dataize/app.js diff --git a/eo2js-runtime/src/objects/org/eolang/int$div.js b/eo2js-runtime/src/objects/org/eolang/int$div.js index b8d5060..c76fc07 100644 --- a/eo2js-runtime/src/objects/org/eolang/int$div.js +++ b/eo2js-runtime/src/objects/org/eolang/int$div.js @@ -15,7 +15,7 @@ const int$div = function() { obj.assets[LAMBDA] = function(self) { const arg = dataized(self.take('x'), INT) if (arg === 0) { - // todo: error + // fix: error } return data.toObject( dataized(self.take(RHO), INT) / arg diff --git a/eo2js-runtime/src/runtime/bytes-of.js b/eo2js-runtime/src/runtime/bytes-of.js index 4a8d102..88a8742 100644 --- a/eo2js-runtime/src/runtime/bytes-of.js +++ b/eo2js-runtime/src/runtime/bytes-of.js @@ -27,7 +27,7 @@ const hexToInt = function(bytes) { /** * Bytes of. * @param {string|number|boolean|array.|array.} data - Data to cast to bytes - * @return {{asInt: (function(): number), asBool: (function(): boolean), asString: (function(): string), asFloat: (function(): number), asBytes: (function(): array.)}} + * @return {{asInt: (function(): number), asBool: (function(): boolean), asString: (function(): string), asFloat: (function(): number), asBytes: (function(): array.), verbose: (function(): string)}} */ const bytesOf = function(data) { let bytes @@ -73,6 +73,19 @@ const bytesOf = function(data) { throw new Error(`Byte array must be 1 byte long to convert to bool (${bytes})`) } return bytes[0] !== 0 + }, + verbose: function() { + let str + if (bytes.length === 0) { + str = '[]' + } else if (bytes.length === 1) { + str = bytes[0] ? '[1]' : '[0]' + } else if (bytes.length === 8) { + str = `[${this.asBytes()}] = ${this.asInt()}, or ${this.asFloat()}, or "${this.asString()}"` + } else { + str = `[${this.asBytes()}] = "${this.asString()}"` + } + return str } } } diff --git a/eo2js-runtime/src/runtime/package.js b/eo2js-runtime/src/runtime/package.js index 3fd3541..4103471 100644 --- a/eo2js-runtime/src/runtime/package.js +++ b/eo2js-runtime/src/runtime/package.js @@ -48,7 +48,7 @@ const found = function(name, full) { obj = tryFind.call(this, '../../../..', name, split) } if (obj == null) { - throw new Error(`Couldn't find object ${name} from ${full}`) + throw new Error(`Couldn't find object '${name}' from '${full}'`) } return obj } diff --git a/eo2js-runtime/test/runtime/bytes-of.test.js b/eo2js-runtime/test/runtime/bytes-of.test.js index 4fe5eb5..639608c 100644 --- a/eo2js-runtime/test/runtime/bytes-of.test.js +++ b/eo2js-runtime/test/runtime/bytes-of.test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const bytesOf = require('../../temp/runtime/bytes-of'); +const assert = require('assert') +const bytesOf = require('../../temp/runtime/bytes-of') describe('bytesOf', function() { describe('int', function() { @@ -14,10 +14,10 @@ describe('bytesOf', function() { const value = 1234 const bytes = bytesOf(value).asBytes() assert.equal(bytesOf(bytes).asInt(), value) - }); + }) it('should fail if not 8 bytes given', function() { assert.throws(() => bytesOf([0, 0, 0, 48, 57]).asInt()) - }); + }) }) describe('float', function() { it('should return the same float', function() { @@ -26,15 +26,15 @@ describe('bytesOf', function() { }) it('should return valid float bytes', function() { assert.deepEqual(bytesOf(374.9).asBytes(), [64, 119, 110, 102, 102, 102, 102, 102]) - }); + }) it('should convert to bytes and back', function() { const value = 9412.21 const bytes = bytesOf(value).asBytes() assert.equal(bytesOf(bytes).asFloat(), value) - }); + }) it('should fail if not 8 bytes given', function() { assert.throws(() => bytesOf([64, 119, 110, 102]).asFloat()) - }); + }) }) describe('string', function() { it('should return the same string', function() { @@ -65,7 +65,7 @@ describe('bytesOf', function() { it('should return valid bool from bytes', function() { assert.equal(bytesOf([1]).asBool(), true) assert.equal(bytesOf([0]).asBool(), false) - }); + }) it('should fail if not 1 byte given', function() { assert.throws(() => bytesOf([1, 1]).asBool()) }) @@ -87,6 +87,26 @@ describe('bytesOf', function() { it('should fail while converting wrong format bytes', function() { const wrong = [1, 2, 'hello'] assert.throws(() => bytesOf(wrong)) - }); + }) + }) + describe('#verbose()', function() { + describe('returns valid array as string if', function() { + it('length is 0', function() { + assert.equal(bytesOf([]).verbose(), '[]') + }) + it('length is 1', function() { + assert.equal(bytesOf([1]).verbose(), '[1]') + assert.equal(bytesOf([0]).verbose(), '[0]') + }) + it('length is 8', function() { + assert.ok(bytesOf(5).verbose().startsWith('[0,0,0,0,0,0,0,5] = 5, or ')) + assert.ok(bytesOf(7.3).verbose().includes(', or 7.3, or')) + assert.ok(bytesOf('abcdefgh').verbose().includes('abcdefgh')) + }) + it('length is not 8', function() { + assert.ok(bytesOf('Hello, world!').verbose().includes('Hello, world!')) + assert.ok(bytesOf('some').verbose().includes('some')) + }) + }) }) }) diff --git a/eo2js/src/commands/link.js b/eo2js/src/commands/link.js index 5e8f825..1766215 100644 --- a/eo2js/src/commands/link.js +++ b/eo2js/src/commands/link.js @@ -11,30 +11,45 @@ const main = '__main__.js' /** * Data to insert to package.json file. - * @type {{author: string, name: string, version: string, dependencies: {}}} + * If path to local dependency is present - eo2js-runtime dependency won't be added to + * package.json file. + * @param {String} [runtime] - path to local eo-runtime dependency + * @return {{author: string, name: string, version: string}} */ -const pckg = { - name: 'project', - version: '1.0.0', - author: 'eoc', - // dependencies: { todo - // 'eo2js-runtime': 'latest' - // }, +const pckg = function(runtime) { + const def = { + name: 'project', + version: '1.0.0', + author: 'eoc' + } + if (!runtime) { + def.dependencies = { + 'eo2js-runtime': 'latest' + } + } + return def } /** * Build npm project. - * @param {{target: String, project: String, resources: String}} options - Program options + * @param {{target: String, project: String, resources: String, dependency: ?String}} options - Program options */ const link = function(options) { options = {...program.opts(), ...options} const project = path.resolve(options.target, options.project) - fs.writeFileSync(path.resolve(project, 'package.json'), JSON.stringify(pckg)) + fs.writeFileSync(path.resolve(project, 'package.json'), JSON.stringify(pckg(options.dependency))) execSync('npm install', {cwd: project}) fs.copyFileSync( path.resolve(options.resources, `js/${main}`), path.resolve(project, main) ) + if (options.dependency) { + fs.cpSync( + options.dependency, + path.resolve(project, 'node_modules/eo2js-runtime'), + {recursive: true} + ) + } } module.exports = link diff --git a/eo2js/src/eo2js.js b/eo2js/src/eo2js.js index 3d27edd..1dd8622 100644 --- a/eo2js/src/eo2js.js +++ b/eo2js/src/eo2js.js @@ -16,6 +16,7 @@ program .option('-t, --target ', 'Target directory with all generated files', '.eoc') .option('-p, --project ', 'Path to result JavaScript project', 'project') .option('-r, --resources ', 'Path to the resources', 'src/resources') + .option('-d --dependency ', 'Path to local eo2js-runtime dependency') .option('--alone', 'Just run a single command without dependencies') // .option('--hash ', 'Hash in objectionary/home to compile against', parser) // .option('--parser ', 'Set the version of EO parser to use', parser) diff --git a/eo2js/src/resources/js/__main__.js b/eo2js/src/resources/js/__main__.js index 1ba2ec6..195eabf 100644 --- a/eo2js/src/resources/js/__main__.js +++ b/eo2js/src/resources/js/__main__.js @@ -1,5 +1,6 @@ const phi = require('eo2js-runtime/src/runtime/phi') const dataized = require('eo2js-runtime/src/runtime/dataized') +const bytesOf = require('eo2js-runtime/src/runtime/bytes-of') const data = require('eo2js-runtime/src/runtime/data') const ErAbstract = require('eo2js-runtime/src/runtime/error/ErAbstract') @@ -20,7 +21,7 @@ const main = function() { }) app = app.with({0: args}) } - console.log(dataized(app)) + console.log(bytesOf(dataized(app)).verbose()) } catch (error) { if (error instanceof ErAbstract) { console.error(error.message) diff --git a/eo2js/src/resources/xsl/to-js.xsl b/eo2js/src/resources/xsl/to-js.xsl index e99faf5..f9a222e 100644 --- a/eo2js/src/resources/xsl/to-js.xsl +++ b/eo2js/src/resources/xsl/to-js.xsl @@ -48,7 +48,7 @@ SOFTWARE. - + @@ -59,7 +59,7 @@ SOFTWARE. - + diff --git a/eo2js/test/commands/dataize.test.js b/eo2js/test/commands/dataize.test.js index 613593e..d9aaf8e 100644 --- a/eo2js/test/commands/dataize.test.js +++ b/eo2js/test/commands/dataize.test.js @@ -1,16 +1,26 @@ const path = require('path'); const fs = require('fs'); -const {ok} = require('assert'); +const {runSync} = require('../helpers'); +const assert = require('assert'); describe('dataize', function() { const home = path.resolve('temp/test-dataize') const target = path.resolve(home, 'target') const project = path.resolve(target, 'project') + const runtime = path.resolve('../eo2js-runtime') beforeEach('clear home', function() { fs.rmSync(home, {recursive: true, force: true}) fs.mkdirSync(project, {recursive: true}) }) - it('should execute node run', function() { - ok(true) // todo + it('should dataize app object', function() { + runSync(['link', '-t', target, '-p project', '--alone', '-d', runtime]) + fs.copyFileSync( + path.resolve('test/resources/dataize/app.js'), + path.resolve(project, 'app.js') + ) + const log = runSync([ + 'dataize', 'app', '--alone', '-t', target, '-p project', '-d', runtime + ]) + assert.ok(log.includes('= "Hello, world!"')) }) }) diff --git a/eo2js/test/commands/link.test.js b/eo2js/test/commands/link.test.js index 00e08c0..489a68d 100644 --- a/eo2js/test/commands/link.test.js +++ b/eo2js/test/commands/link.test.js @@ -15,13 +15,19 @@ describe('link', function() { * @return {String} - Stdout. */ const link = function() { - return runSync(['link', '-t', target, '-p project --alone']) + return runSync([ + 'link', + '-t', target, + '-p project', + '--alone', + '-d', path.resolve('../eo2js-runtime') + ]) } it('should create all necessary files and install npm project', function(done) { assertFilesExist(link(), project, [ 'package.json', 'package-lock.json', - // 'node_modules/eo2js-runtime', todo + 'node_modules/eo2js-runtime', '__main__.js' ]) done() diff --git a/eo2js/test/resources/dataize/app.js b/eo2js/test/resources/dataize/app.js new file mode 100644 index 0000000..aba83ea --- /dev/null +++ b/eo2js/test/resources/dataize/app.js @@ -0,0 +1,21 @@ +/** + * Object for testing dataization. + * @return {Object} object - Test object + */ +const app = function() { + const object = require('eo2js-runtime/src/runtime/object') + const {PHI, DELTA} = require('eo2js-runtime/src/runtime/attribute/specials') + const attr = require('eo2js-runtime/src/runtime/attribute/attr') + const bytesOf = require('eo2js-runtime/src/runtime/bytes-of') + const obj = object('app') + obj.attrs[PHI] = attr.lambda( + obj, (_) => { + const inner = object('app$phi') + inner.assets[DELTA] = bytesOf('Hello, world!').asBytes() + return inner + } + ) + return obj +} + +module.exports = app