commit f7d0ed400cb93af46f47df1cb9eed1a81a49a327
parent 796312ec3ec3a5098ea25a0f291711d31c0f97cf
Author: Jared Tobin <jared@jtobin.io>
Date: Mon, 8 Oct 2018 10:02:44 +1300
Add property tests for hex <-> patq.
Diffstat:
2 files changed, 48 insertions(+), 7 deletions(-)
diff --git a/package.json b/package.json
@@ -4,7 +4,7 @@
"description": "utilities to convert urbit ship names back and forth between @p and @ud",
"main": "dist/index.js",
"scripts": {
- "test": "jest",
+ "test": "jest test/*.test.js && mocha test/property.js",
"build": "gulp"
},
"keywords": [
@@ -15,21 +15,24 @@
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0-beta.34",
- "isomorphic-webcrypto": "^1.6.0",
+ "babel-jest": "^23.4.2",
+ "chai": "^4.2.0",
"gulp": "github:gulpjs/gulp#4.0",
+ "isomorphic-webcrypto": "^1.6.0",
+ "jest": "^23.5.0",
+ "jsverify": "^0.8.3",
+ "mocha": "^5.2.0",
+ "regenerator-runtime": "^0.12.1",
"rollup-plugin-babel": "github:rollup/rollup-plugin-babel",
"rollup-plugin-commonjs": "^8.3.0",
"rollup-plugin-json": "^3.0.0",
"rollup-plugin-node-builtins": "^2.1.2",
+ "rollup-plugin-node-globals": "^1.2.1",
"rollup-plugin-node-resolve": "^3.3.0",
"rollup-plugin-replace": "^2.0.0",
"rollup-plugin-root-import": "^0.2.3",
"rollup-stream": "^1.24.1",
- "vinyl-source-stream": "^2.0.0",
- "babel-jest": "^23.4.2",
- "jest": "^23.5.0",
- "regenerator-runtime": "^0.12.1",
- "rollup-plugin-node-globals": "^1.2.1"
+ "vinyl-source-stream": "^2.0.0"
},
"dependencies": {
"babel-plugin-lodash": "^3.3.4",
diff --git a/test/property.js b/test/property.js
@@ -0,0 +1,38 @@
+
+const expect = require('chai').expect
+const jsc = require('jsverify')
+const ob = require('../src')
+const bn = require('bn.js')
+
+removeLeadingZeroBytes = str =>
+ str.slice(0, 2) === '00'
+ ? removeLeadingZeroBytes(str.slice(2))
+ : str
+
+eqModLeadingZeroBytes = (s, t) =>
+ removeLeadingZeroBytes(s) === removeLeadingZeroBytes(t)
+
+describe('@q encoding', () => {
+ let hexString = jsc.string.smap(
+ x => Buffer.from(x).toString('hex'),
+ x => Buffer.from(x, 'hex').toString()
+ )
+
+ let patq = hexString.smap(
+ hex => ob.patq(new bn(hex, 'hex')),
+ pq => ob.patq2hex(pq)
+ )
+
+ it('patq2hex and hex2patq are inverses', () => {
+ let iso0 = jsc.forall(hexString, hex =>
+ eqModLeadingZeroBytes(ob.patq2hex(ob.hex2patq(hex)), hex)
+ )
+
+ let iso1 = jsc.forall(patq, str =>
+ ob.hex2patq(ob.patq2hex(str)) === str
+ )
+
+ jsc.assert(iso0)
+ jsc.assert(iso1)
+ })
+})