Skip to content

Commit

Permalink
U: complete testing
Browse files Browse the repository at this point in the history
  • Loading branch information
fool2fish committed Aug 9, 2013
1 parent 65cd21f commit 12e17cb
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 36 deletions.
17 changes: 11 additions & 6 deletions lib/index.js
Expand Up @@ -47,7 +47,14 @@ exports.readCfgFile = function(p) {
exports.writeCfgFile = function(p, cfg) {
var dir = path.dirname(p)
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
dir.split(path.sep).reduce(function(parts, part) {
parts += part + '/'
var subpath = path.resolve(parts)
if (!fs.existsSync(subpath)) {
fs.mkdirSync(subpath)
}
return parts;
}, '');
}
fs.writeFileSync(p, JSON.stringify(cfg))
}
Expand Down Expand Up @@ -144,13 +151,11 @@ exports.getExternalIpAddress = function() {
var interfaces = require('os').networkInterfaces()
var addresses = []
Object.keys(interfaces).forEach(function(name) {
var iface = interfaces[name]
for (var i in iface) {
var node = iface[i]
interfaces[name].forEach(function(node) {
if (node.family === 'IPv4' && node.internal === false) {
addresses = addresses.concat(node)
addresses.push(node)
}
}
})
})
if (addresses.length > 0) {
return addresses[0].address
Expand Down
174 changes: 144 additions & 30 deletions tests/index-spec.js
@@ -1,5 +1,7 @@
'use strict';

var path = require('path')
var fs = require('fs')
var expect = require('expect.js')
var commander = require('commander')

Expand All @@ -9,77 +11,189 @@ var common = require('../index')
describe('totoro-common', function(){

describe('split', function() {
it('should return a split array if a string is passed in', function() {

it('pass a string and return a split array', function() {
var str = 'mac/chrome/10.0.0.1,firefox,sarafi/3.0'
var rt = common.split(str)
expect(rt.length).to.be(3)
expect(rt[0]).to.be('mac/chrome/10.0.0.1')
})

it('should return an empty array if not a string is passed in', function() {
it('pass any of other type and return an empty array', function() {
expect(common.split().length).to.be(0)
expect(common.split(false).length).to.be(0)
expect(common.split([1,2,3]).length).to.be(0)
expect(common.split({key:'value'}).length).to.be(0)
})
})

describe('getCfg', function() {
it('should extract command line options from commander object', function() {
commander
.description('a commander')
.option('--nick [s]', 'a name')
.option('--favorite [s]', 'a favorite')
.parse(['node', 'scriptpath', '--nick=fool2fish', '--favorite=imax'])
var rt = common.getCfg(commander)
expect(Object.keys(rt).length).to.be(2)
expect(rt.nick).to.be('fool2fish')
expect(rt.favorite).to.be('imax')
})
it('getCfg', function() {
commander
.description('a commander')
.option('--nick [s]', 'a name')
.option('--favorite [s]', 'a favorite')
.parse(['node', 'scriptpath', '--nick=fool2fish', '--favorite=imax'])
var rt = common.getCfg(commander)
expect(Object.keys(rt).length).to.be(2)
expect(rt.nick).to.be('fool2fish')
expect(rt.favorite).to.be('imax')
})

describe('readCfgFile', function() {

it('pass a not exist file and return an empty plain object', function() {
var p = path.join(__dirname, 'path', 'to', 'not-existed-file.json')
var rt = common.readCfgFile(p)
expect(Object.keys(rt).length).to.be(0)
expect(JSON.stringify(rt)).to.be('{}')
})

it('pass a not proper file and return an empty plain object', function() {
var p = path.join(__dirname, 'not-proper-file.txt')
fs.writeFileSync(p, 'some text')

var rt = common.readCfgFile(p)
expect(Object.keys(rt).length).to.be(0)
expect(JSON.stringify(rt)).to.be('{}')

fs.unlinkSync(p)
})

it('pass a proper file and return a plain object', function() {
var p = path.join(__dirname, 'proper-file.json')
fs.writeFileSync(p, '{"nick": "fool2fish", "job": "web developer"}')

var rt = common.readCfgFile(p)
expect(Object.keys(rt).length).to.be(2)
expect(rt.nick).to.be('fool2fish')
expect(rt.job).to.be('web developer')

fs.unlinkSync(p)
})

})

describe('writeCfgFile', function() {

})
it('create it first if the file not exist', function() {
var p = path.join(__dirname, 'path', 'to', 'not-existed-config.json')
common.writeCfgFile(p, {nick: 'fool2fish', blog: 'fool2fish.cn'})

describe('camelcase', function() {
it('should turn aa-bb-cc into aaBbCc', function() {
expect(common.camelcase('totoro')).to.be('totoro')
expect(common.camelcase('totoro-server')).to.be('totoroServer')
expect(common.camelcase('totoro-Common')).to.be('totoroCommon')
var cfg = require(p)
expect(Object.keys(cfg).length).to.be(2)
expect(cfg.nick).to.be('fool2fish')
expect(cfg.blog).to.be('fool2fish.cn')

fs.unlinkSync(p)
fs.rmdirSync(path.join(__dirname, 'path', 'to'))
fs.rmdirSync(path.join(__dirname, 'path'))
})

})
it('cover old content', function() {
var p = path.join(__dirname, 'existed-config.json')
fs.writeFileSync(p, '{"nick": "fool2fish", "job": "web developer"}')

common.writeCfgFile(p, {nick: 'fool2fish', twitter: 'fool2fish'})

describe('unCamelcase', function() {
it('should turn aaBbCc into aa-bb-cc', function() {
expect(common.unCamelcase('totoro')).to.be('totoro')
expect(common.unCamelcase('totoroServer')).to.be('totoro-server')
expect(common.unCamelcase('TotoroCommon')).to.be('-totoro-common')
var cfg = require(p)
expect(Object.keys(cfg).length).to.be(2)
expect(cfg.nick).to.be('fool2fish')
expect(cfg.twitter).to.be('fool2fish')
expect(cfg.blog).to.be(undefined)

fs.unlinkSync(p)
})

})

describe('isUrl', function() {
it('camelcase', function() {
expect(common.camelcase('totoro')).to.be('totoro')
expect(common.camelcase('totoro-server')).to.be('totoroServer')
expect(common.camelcase('totoro-Common')).to.be('totoroCommon')
})

it('unCamelcase', function() {
expect(common.unCamelcase('totoro')).to.be('totoro')
expect(common.unCamelcase('totoroServer')).to.be('totoro-server')
expect(common.unCamelcase('TotoroCommon')).to.be('-totoro-common')
})

describe('isKeyword', function() {
it('isUrl', function() {
expect(common.isUrl('http://www.taobao.com')).to.be(true)
expect(common.isUrl('https://www.alipay.com')).to.be(true)
expect(common.isUrl('www.taobao.com')).to.be(false)
expect(common.isUrl('keyword')).to.be(false)
expect(common.isUrl(path.join('path', 'to', 'file'))).to.be(false)
expect(common.isUrl()).to.be(false)
})

it('isKeyword', function() {
expect(common.isKeyword('mocha')).to.be(true)
expect(common.isKeyword(path.join('path', 'to', 'file'))).to.be(false)
expect(common.isKeyword('http://www.taobao.com')).to.be(false)
expect(common.isKeyword('www.taobao.com')).to.be(false)
})

describe('isExistedFile', function() {
it('isExistedFile', function() {
var existedFile = __filename
var notExistedFile = path.join('path', 'to', 'not-existed-file.json')

expect(common.isExistedFile(existedFile)).to.be(true)
expect(common.isExistedFile(existedFile + '?querystring#hash')).to.be(true)
expect(common.isExistedFile(existedFile + '?querystring')).to.be(true)
expect(common.isExistedFile(existedFile + '#hash')).to.be(true)
expect(common.isExistedFile(notExistedFile)).to.be(false)
})

describe('mix', function() {

describe('overwrite', function() {

it('not overwrite', function() {
var rt = common.mix({}, {a:'first'}, {a:'second'}, false)
expect(rt.a).to.be('first')
})

it('overwrite', function() {
var rt = common.mix({}, {a:'first'}, {a:'second'}, true)
expect(rt.a).to.be('second')
})
})

it('any of arguments could be null', function() {
var rt1 = common.mix(null, {a: 'first'}, false)
expect(Object.keys(rt1).length).to.be(1)
expect(rt1.a).to.be('first')

var rt2 = common.mix({a: 'zero'}, null, {a: 'second'}, true)
expect(Object.keys(rt2).length).to.be(1)
expect(rt2.a).to.be('second')

var rt3 = common.mix({a: 'zero'}, {a: 'first'}, {a: 'second', b: 'second'})
expect(Object.keys(rt3).length).to.be(2)
expect(rt3.a).to.be('zero')
expect(rt3.b).to.be('second')
})

})

describe('getExternalIpAddress', function() {
it('getExternalIpAddress', function() {
var rt = common.getExternalIpAddress()
var interfaces = require('os').networkInterfaces()
var addresses = []
Object.keys(interfaces).forEach(function(name) {
addresses = addresses.concat(interfaces[name])
})
var addresses = addresses.filter(function(node) {
if (node.family === 'IPv4' && node.internal === false) {
return true
}
})

if (addresses.length) {
expect(rt).to.be(addresses[0].address)
} else {
expect(rt).to.be(null)
}
})
})

0 comments on commit 12e17cb

Please sign in to comment.