Use dignified.js

This commit is contained in:
dignifiedquire 2016-03-23 15:07:25 +01:00
parent 05dc53989d
commit b04c5112fc
5 changed files with 113 additions and 100 deletions

5
.gitignore vendored
View File

@ -1 +1,6 @@
node_modules
lib
dist
*.log

View File

@ -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"

View File

@ -1,3 +1,5 @@
'use strict'
var multiaddr = require('multiaddr')
var IP = or(base('ip4'), base('ip6'))

95
test/index.spec.js Normal file
View File

@ -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)
})
})

View File

@ -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()
})