Skip to content

Commit

Permalink
verbose + more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed May 12, 2024
1 parent c359f2e commit 3fbc3ff
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 30 deletions.
2 changes: 1 addition & 1 deletion eo2js-runtime/src/objects/org/eolang/int$div.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion eo2js-runtime/src/runtime/bytes-of.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const hexToInt = function(bytes) {
/**
* Bytes of.
* @param {string|number|boolean|array.<string>|array.<number>} data - Data to cast to bytes
* @return {{asInt: (function(): number), asBool: (function(): boolean), asString: (function(): string), asFloat: (function(): number), asBytes: (function(): array.<number>)}}
* @return {{asInt: (function(): number), asBool: (function(): boolean), asString: (function(): string), asFloat: (function(): number), asBytes: (function(): array.<number>), verbose: (function(): string)}}
*/
const bytesOf = function(data) {
let bytes
Expand Down Expand Up @@ -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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion eo2js-runtime/src/runtime/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
38 changes: 29 additions & 9 deletions eo2js-runtime/test/runtime/bytes-of.test.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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())
})
Expand All @@ -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'))
})
})
})
})
35 changes: 25 additions & 10 deletions eo2js/src/commands/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions eo2js/src/eo2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ program
.option('-t, --target <path>', 'Target directory with all generated files', '.eoc')
.option('-p, --project <path>', 'Path to result JavaScript project', 'project')
.option('-r, --resources <path>', 'Path to the resources', 'src/resources')
.option('-d --dependency <path>', 'Path to local eo2js-runtime dependency')
.option('--alone', 'Just run a single command without dependencies')
// .option('--hash <hex>', 'Hash in objectionary/home to compile against', parser)
// .option('--parser <version>', 'Set the version of EO parser to use', parser)
Expand Down
3 changes: 2 additions & 1 deletion eo2js/src/resources/js/__main__.js
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions eo2js/src/resources/xsl/to-js.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ SOFTWARE.
<xsl:value-of select="$TAB"/>
</xsl:for-each>
</xsl:function>
<!-- Clean name todo -->
<!-- Clean name -->
<xsl:function name="eo:clean" as="xs:string">
<xsl:param name="n" as="xs:string"/>
<xsl:value-of select="replace(replace(replace(replace($n, '_', '__'), '-', '_'), '@', 'φ'), 'α', '_')"/>
Expand All @@ -59,7 +59,7 @@ SOFTWARE.
<xsl:param name="s2"/>
<xsl:value-of select="concat(concat($s1, '_'), $s2)"/>
</xsl:function>
<!-- Construct valid object name todo -->
<!-- Construct valid object name -->
<xsl:function name="eo:object-name" as="xs:string">
<xsl:param name="name" as="xs:string"/>
<xsl:param name="alt" as="xs:string"/>
Expand Down
16 changes: 13 additions & 3 deletions eo2js/test/commands/dataize.test.js
Original file line number Diff line number Diff line change
@@ -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!"'))
})
})
10 changes: 8 additions & 2 deletions eo2js/test/commands/link.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 21 additions & 0 deletions eo2js/test/resources/dataize/app.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3fbc3ff

Please sign in to comment.