diff --git a/.gitignore b/.gitignore index 3c3629e..a62b65f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,6 @@ node_modules + +lib +dist + +*.log diff --git a/package.json b/package.json index c6dcaff..babfa85 100644 --- a/package.json +++ b/package.json @@ -2,10 +2,15 @@ "name": "mafmt", "version": "1.0.1", "description": "A multiaddr validator", - "main": "src/index.js", + "main": "lib/index.js", + "jsnext:main": "src/index.js", "scripts": { - "lint": "standard", - "test": "node tests/test.js" + "lint": "dignified-lint", + "build": "dignified-build", + "test": "dignified-test", + "test:node": "dignified-test node", + "test:browser": "dignified-test browser", + "release": "dignified-release" }, "pre-commit": [ "lint", @@ -25,9 +30,9 @@ }, "homepage": "https://github.com/whyrusleeping/js-mafmt#readme", "devDependencies": { - "pre-commit": "^1.1.2", - "standard": "^6.0.8", - "tape": "^4.4.0" + "chai": "^3.5.0", + "dignified.js": "github:dignifiedquire/dignified.js", + "pre-commit": "^1.1.2" }, "dependencies": { "multiaddr": "^1.3.0" diff --git a/src/index.js b/src/index.js index d52a42f..e157a40 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,5 @@ +'use strict' + var multiaddr = require('multiaddr') var IP = or(base('ip4'), base('ip6')) diff --git a/test/index.spec.js b/test/index.spec.js new file mode 100644 index 0000000..e0265dd --- /dev/null +++ b/test/index.spec.js @@ -0,0 +1,95 @@ +/* eslint-env mocha */ +'use strict' + +var expect = require('chai').expect +var mafmt = require('./../src') + +describe('multiaddr validation', function () { + it('basic stuff works', function () { + var good_ip = [ + '/ip4/0.0.0.0', + '/ip6/fc00::' + ] + + var bad_ip = [ + '/ip4/0.0.0.0/tcp/555', + '/udp/789/ip6/fc00::' + ] + + var good_tcp = [ + '/ip4/0.0.7.6/tcp/1234', + '/ip6/::/tcp/0' + ] + + var bad_tcp = [ + '/tcp/12345', + '/ip6/fc00::/udp/5523/tcp/9543' + ] + + var good_udp = [ + '/ip4/0.0.7.6/udp/1234', + '/ip6/::/udp/0' + ] + + var bad_udp = [ + '/udp/12345', + '/ip6/fc00::/tcp/5523/udp/9543' + ] + + var good_utp = [ + '/ip4/1.2.3.4/udp/3456/utp', + '/ip6/::/udp/0/utp' + ] + + var bad_utp = [ + '/ip4/0.0.0.0/tcp/12345/utp', + '/ip6/::/ip4/0.0.0.0/udp/1234/utp' + ] + + var good_websockets = [ + '/ip4/1.2.3.4/tcp/3456/websockets', + '/ip6/::/tcp/0/websockets' + ] + + var bad_websockets = [ + '/ip4/0.0.0.0/tcp/12345/udp/2222/websockets', + '/ip6/::/ip4/0.0.0.0/udp/1234/websockets' + ] + + function assertMatches (p) { + var tests = Array.from(arguments).slice(1) + tests.forEach(function (test) { + test.forEach(function (testcase) { + expect(p.matches(testcase)).to.be.eql(true) + }) + }) + } + + function assertMismatches (p) { + var tests = Array.from(arguments).slice(1) + tests.forEach(function (test) { + test.forEach(function (testcase) { + expect(p.matches(testcase)).to.be.eql(false) + }) + }) + } + + assertMatches(mafmt.IP, good_ip) + assertMismatches(mafmt.IP, bad_ip, good_tcp) + + assertMatches(mafmt.TCP, good_tcp) + assertMismatches(mafmt.TCP, bad_tcp, good_ip) + + assertMatches(mafmt.UDP, good_udp) + assertMismatches(mafmt.UDP, bad_udp, good_ip, good_tcp, good_utp) + + assertMatches(mafmt.UTP, good_utp) + assertMismatches(mafmt.UTP, bad_utp, good_ip, good_tcp, good_udp) + + assertMatches(mafmt.Reliable, good_utp, good_tcp) + assertMismatches(mafmt.Reliable, good_ip, good_udp) + + assertMatches(mafmt.WebSockets, good_websockets) + assertMismatches(mafmt.WebSockets, good_ip, good_udp, bad_websockets) + }) +}) diff --git a/tests/test.js b/tests/test.js deleted file mode 100644 index 3a57710..0000000 --- a/tests/test.js +++ /dev/null @@ -1,94 +0,0 @@ -var test = require('tape') -var mafmt = require('./../src') - -test('basic stuff works', function (t) { - var good_ip = [ - '/ip4/0.0.0.0', - '/ip6/fc00::' - ] - - var bad_ip = [ - '/ip4/0.0.0.0/tcp/555', - '/udp/789/ip6/fc00::' - ] - - var good_tcp = [ - '/ip4/0.0.7.6/tcp/1234', - '/ip6/::/tcp/0' - ] - - var bad_tcp = [ - '/tcp/12345', - '/ip6/fc00::/udp/5523/tcp/9543' - ] - - var good_udp = [ - '/ip4/0.0.7.6/udp/1234', - '/ip6/::/udp/0' - ] - - var bad_udp = [ - '/udp/12345', - '/ip6/fc00::/tcp/5523/udp/9543' - ] - - var good_utp = [ - '/ip4/1.2.3.4/udp/3456/utp', - '/ip6/::/udp/0/utp' - ] - - var bad_utp = [ - '/ip4/0.0.0.0/tcp/12345/utp', - '/ip6/::/ip4/0.0.0.0/udp/1234/utp' - ] - - var good_websockets = [ - '/ip4/1.2.3.4/tcp/3456/websockets', - '/ip6/::/tcp/0/websockets' - ] - - var bad_websockets = [ - '/ip4/0.0.0.0/tcp/12345/udp/2222/websockets', - '/ip6/::/ip4/0.0.0.0/udp/1234/websockets' - ] - - function assertMatches (p) { - var tests = Array.from(arguments).slice(1) - tests.forEach(function (test) { - test.forEach(function (testcase) { - if (!p.matches(testcase)) { - t.fail('should have matched: ' + testcase + ' ' + p) - } - }) - }) - } - - function assertMismatches (p) { - var tests = Array.from(arguments).slice(1) - tests.forEach(function (test) { - test.forEach(function (testcase) { - t.equal(p.matches(testcase), false, 'should not have matched: ' + testcase + ' ' + test) - }) - }) - } - - assertMatches(mafmt.IP, good_ip) - assertMismatches(mafmt.IP, bad_ip, good_tcp) - - assertMatches(mafmt.TCP, good_tcp) - assertMismatches(mafmt.TCP, bad_tcp, good_ip) - - assertMatches(mafmt.UDP, good_udp) - assertMismatches(mafmt.UDP, bad_udp, good_ip, good_tcp, good_utp) - - assertMatches(mafmt.UTP, good_utp) - assertMismatches(mafmt.UTP, bad_utp, good_ip, good_tcp, good_udp) - - assertMatches(mafmt.Reliable, good_utp, good_tcp) - assertMismatches(mafmt.Reliable, good_ip, good_udp) - - assertMatches(mafmt.WebSockets, good_websockets) - assertMismatches(mafmt.WebSockets, good_ip, good_udp, bad_websockets) - - t.end() -})