Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

A: add some testing script

  • Loading branch information...
commit 65cd21f29fe1e8ed60442dcc249069e6e4d4c2c9 1 parent cb6f74c
@fool2fish fool2fish authored
View
2  .gitignore
@@ -4,3 +4,5 @@
node_modules/
*.swp
logs/
+lib-cov/
+coverage.html
View
29 .jshintrc
@@ -0,0 +1,29 @@
+{
+ "eqeqeq": true,
+ "forin": false,
+ "latedef": false,
+ "newcap": true,
+ "quotmark": true,
+ "undef": false,
+ "unused": false,
+ "trailing": true,
+ "lastsemic": false,
+ "asi": true,
+ "boss": true,
+ "expr": true,
+ "strict": true,
+ "funcscope": true,
+ "loopfunc": true,
+ "multistr": true,
+ "proto": false,
+ "smarttabs": true,
+ "shadow": false,
+ "sub": true,
+ "node": true,
+ "passfail": false,
+ "white": false,
+ "globals": {
+ "describe": false,
+ "it": false
+ }
+}
View
21 Makefile
@@ -0,0 +1,21 @@
+all: test coverage jshint
+
+test:
+ @mocha tests
+ @$(MAKE) coverage cov-args=json-cov | node scripts/coverage.js
+
+cov-args = html-cov > coverage.html
+coverage:
+ @jscoverage lib lib-cov
+ @mv lib lib-bak
+ @mv lib-cov lib
+ @mocha tests -R $(cov-args)
+ @rm -rf lib
+ @mv lib-bak lib
+
+jshint:
+ @jshint lib/*.js
+ @jshint tests/*.js
+
+
+.PHONY: all test coverage jshint
View
160 index.js
@@ -1,159 +1,3 @@
-var path = require('path')
-var fs = require('fs')
+'use strict';
-var logger = require('./logger').getLog()
-
-
-exports.home = process.platform === 'win32' ? process.env.USERPROFILE : process.env.HOME
-
-
-exports.split = function(str) {
- return str ? str.split(',') : []
-}
-
-
-/**
- * extract config from commander
- */
-exports.getCfg = function(commander) {
- var cfg = {}
- commander.options.forEach(function(item) {
- var key = camelcase(item.name())
- if(key in commander) {
- if (typeof commander[key] !== 'function') {
- cfg[key] = commander[key]
- }
- }
- })
- return cfg
-}
-
-
-/**
- * read json config file
- */
-exports.readCfgFile = function(p) {
- try {
- return require(path.resolve(p))
- } catch(e) {
- logger.debug('Fail to read config file<' + p + '>')
- return {}
- }
-}
-
-
-exports.writeCfgFile = function(p, cfg) {
- var dir = path.dirname(p)
- if (!fs.existsSync(dir)) {
- fs.mkdirSync(dir)
- }
- fs.writeFileSync(p, JSON.stringify(cfg))
-}
-
-
-function camelcase(str) {
- return str.split('-').reduce(function(str, word){
- return str + word[0].toUpperCase() + word.slice(1)
- })
-}
-exports.camelcase = camelcase
-
-
-exports.unCamelcase = function(str) {
- return str.split(/([A-Z])/).reduce(function(str, word) {
- if (/[A-Z]/.test(word)) {
- return str + '-' + word.toLowerCase()
- } else {
- return str + word
- }
- })
-}
-
-
-exports.isUrl = function(p) {
- /* jshint -W092 */
- return /^https?:\/\//.test(p)
-}
-
-
-/**
- * see if specified adapter is a keyword
- */
-exports.isKeyword = function(p) {
- return p.indexOf('.') === -1 && p.indexOf(path.sep) === -1
-}
-
-
-exports.isExistedFile = function(p){
- // if path contains query string
- var reg = /\?|#/
- if (reg.test(p)) {
- p = p.split(reg)[0]
- }
-
- return p && fs.existsSync(p) && fs.statSync(p).isFile()
-}
-
-
-/**
- * mix properties from src to target
- * multiple src be allowed
- * e.g. var target = mix(target, src1, src2, src3)
- */
-exports.mix = function(target, src, overwrite) {
- target = target || {}
- /*
- * NOTE
- *
- * can't modify overwrite directly!!!
- *
- * if you assign a new value to overwrite
- * when method in the same file call mix()
- * arguments is not modified
- * but if one method out of this file call it
- * arguments will be modified
- */
- var ow
- var len = arguments.length
- var srcEnd = len - 1
- var lastArg = arguments[len - 1]
-
- if ( typeof lastArg === 'boolean' || typeof lastArg === 'number') {
- ow = lastArg
- srcEnd--
- } else {
- ow = false
- }
-
- for (var i = 1; i <= srcEnd; i++) {
- var current = arguments[i] || {}
- for (var j in current) {
- if (ow || typeof target[j] === 'undefined') {
- target[j] = current[j]
- }
- }
- }
-
- return target
-}
-
-
-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]
- if (node.family === 'IPv4' && node.internal === false) {
- addresses = addresses.concat(node)
- }
- }
- })
- if (addresses.length > 0) {
- return addresses[0].address
- }
-}
-
-
-exports.logger = require('./logger')
+module.exports = require('./lib')
View
161 lib/index.js
@@ -0,0 +1,161 @@
+"use strict";
+
+var path = require('path')
+var fs = require('fs')
+
+var logger = require('./logger').getLog()
+
+
+exports.home = process.platform === 'win32' ? process.env.USERPROFILE : process.env.HOME
+
+
+exports.split = function(str) {
+ return str && (typeof str === 'string') ? str.split(',') : []
+}
+
+
+/**
+ * extract config from commander
+ */
+exports.getCfg = function(commander) {
+ var cfg = {}
+ commander.options.forEach(function(item) {
+ var key = camelcase(item.name())
+ if(key in commander) {
+ if (typeof commander[key] !== 'function') {
+ cfg[key] = commander[key]
+ }
+ }
+ })
+ return cfg
+}
+
+
+/**
+ * read json config file
+ */
+exports.readCfgFile = function(p) {
+ try {
+ return require(path.resolve(p))
+ } catch(e) {
+ logger.debug('Fail to read config file<' + p + '>')
+ return {}
+ }
+}
+
+
+exports.writeCfgFile = function(p, cfg) {
+ var dir = path.dirname(p)
+ if (!fs.existsSync(dir)) {
+ fs.mkdirSync(dir)
+ }
+ fs.writeFileSync(p, JSON.stringify(cfg))
+}
+
+
+function camelcase(str) {
+ return str.split('-').reduce(function(str, word){
+ return str + word[0].toUpperCase() + word.slice(1)
+ })
+}
+exports.camelcase = camelcase
+
+
+exports.unCamelcase = function(str) {
+ return str.split(/([A-Z])/).reduce(function(str, word) {
+ if (/[A-Z]/.test(word)) {
+ return str + '-' + word.toLowerCase()
+ } else {
+ return str + word
+ }
+ })
+}
+
+
+exports.isUrl = function(p) {
+ /* jshint -W092 */
+ return /^https?:\/\//.test(p)
+}
+
+
+/**
+ * see if specified adapter is a keyword
+ */
+exports.isKeyword = function(p) {
+ return p.indexOf('.') === -1 && p.indexOf(path.sep) === -1
+}
+
+
+exports.isExistedFile = function(p){
+ // if path contains query string
+ var reg = /\?|#/
+ if (reg.test(p)) {
+ p = p.split(reg)[0]
+ }
+
+ return p && fs.existsSync(p) && fs.statSync(p).isFile()
+}
+
+
+/**
+ * mix properties from src to target
+ * multiple src be allowed
+ * e.g. var target = mix(target, src1, src2, src3)
+ */
+exports.mix = function(target, src, overwrite) {
+ target = target || {}
+ /*
+ * NOTE
+ *
+ * can't modify overwrite directly!!!
+ *
+ * if you assign a new value to overwrite
+ * when method in the same file call mix()
+ * arguments is not modified
+ * but if one method out of this file call it
+ * arguments will be modified
+ */
+ var ow
+ var len = arguments.length
+ var srcEnd = len - 1
+ var lastArg = arguments[len - 1]
+
+ if ( typeof lastArg === 'boolean' || typeof lastArg === 'number') {
+ ow = lastArg
+ srcEnd--
+ } else {
+ ow = false
+ }
+
+ for (var i = 1; i <= srcEnd; i++) {
+ var current = arguments[i] || {}
+ for (var j in current) {
+ if (ow || typeof target[j] === 'undefined') {
+ target[j] = current[j]
+ }
+ }
+ }
+
+ return target
+}
+
+
+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]
+ if (node.family === 'IPv4' && node.internal === false) {
+ addresses = addresses.concat(node)
+ }
+ }
+ })
+ if (addresses.length > 0) {
+ return addresses[0].address
+ }
+}
+
+
+exports.logger = require('./logger')
View
0  logger.js → lib/logger.js
File renamed without changes
View
7 package.json
@@ -20,7 +20,12 @@
"colorful": "2.1.0",
"dateformat": "*"
},
- "devDependencies": {},
+ "devDependencies": {
+ "commander": "1.2.0",
+ "mocha": "1.12.0",
+ "expect.js": "0.2.0",
+ "sinon": "1.7.3"
+ },
"repository": {
"type": "git",
"url": "git@github.com:totorojs/totoro-common.git"
View
27 scripts/coverage.js
@@ -0,0 +1,27 @@
+var format = require('util').format;
+var data = ''
+
+process.stdin.on('data', function(chunk) {
+ data += chunk
+})
+
+process.stdin.on('end', function() {
+ data = data.replace(/^[^{]*({)/g, '$1')
+ data = JSON.parse(data)
+
+ console.log('Coverage:')
+ data.files.sort(function(a, b) {
+ return a.coverage - b.coverage
+ }).forEach(function(item) {
+ console.log(format('%s%% - %s', pad(item.coverage), item.filename))
+ })
+ console.log()
+})
+
+
+function pad(num) {
+ num = num.toFixed(2)
+ var len = 9 - num.length
+ return new Array(len).join(' ') + num
+}
+
View
85 tests/index-spec.js
@@ -0,0 +1,85 @@
+'use strict';
+
+var expect = require('expect.js')
+var commander = require('commander')
+
+var common = require('../index')
+
+
+describe('totoro-common', function(){
+
+ describe('split', function() {
+ it('should return a split array if a string is passed in', 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() {
+ 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')
+ })
+ })
+
+ describe('readCfgFile', function() {
+
+ })
+
+ describe('writeCfgFile', function() {
+
+ })
+
+ 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')
+ })
+
+ })
+
+ 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')
+ })
+ })
+
+ describe('isUrl', function() {
+
+ })
+
+ describe('isKeyword', function() {
+
+ })
+
+ describe('isExistedFile', function() {
+
+ })
+
+ describe('mix', function() {
+
+ })
+
+ describe('getExternalIpAddress', function() {
+
+ })
+})
Please sign in to comment.
Something went wrong with that request. Please try again.