diff --git a/.gitignore b/.gitignore index b512c09..3091757 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +coverage \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index f52bbce..aafbc82 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,10 +4,11 @@ node_js: addons: chrome: stable +services: + - xvfb before_script: - "export DISPLAY=:99.0" - - "sh -e /etc/init.d/xvfb start" - sleep 3 # give xvfb some time to start before_install: diff --git a/karma.conf.js b/karma.conf.js index 0747749..f829f0d 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -30,13 +30,17 @@ module.exports = function(config) { // preprocess matching files before serving them to the browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { + 'quz/*.js': ['coverage'] + }, + coverageReporter: { + type : 'html', + dir : 'coverage/' }, - // test results reporter to use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter - reporters: ['progress'], + reporters: ['progress', 'coverage'], // web server port diff --git a/package.json b/package.json index e80646a..895ecd0 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,9 @@ }, "homepage": "https://github.com/FE-star/homework1#readme", "devDependencies": { - "karma": "^1.7.0", + "karma": "^1.7.1", "karma-chrome-launcher": "^2.2.0", + "karma-coverage": "^1.1.2", "karma-mocha": "^1.3.0", "mocha": "^3.5.0", "should": "^11.2.1" diff --git a/quz/quz.js b/quz/quz.js index 624e7de..f088b77 100755 --- a/quz/quz.js +++ b/quz/quz.js @@ -8,7 +8,16 @@ * @returns {List} */ function dcate(A, B) { - /** Fill in here **/ + function getListLast (list) { + if (!list.tail) { + return list + } else { + return getListLast(list.tail) + } + } + let lastA = getListLast(A) + lastA.tail = B + return A } /** @@ -23,5 +32,19 @@ function dcate(A, B) { * @returns {List} */ function sub(L, start, len) { - /** Fill in here **/ + if (!L || (!start && start !== 0) || (!len && len !== 0)) { + throw new Error('缺少参数') + } + function getListByIndex (list, index, from = 0) { + if (from === index) { + return list + } else { + from ++ + return list.tail && getListByIndex(list.tail, index, from) + } + } + let startList = getListByIndex(L, start) + let endList = getListByIndex(startList, (start + len - 1), start) + endList && (endList.tail = null) + return startList } diff --git a/test/test.js b/test/test.js index 317a346..a1a7f6d 100755 --- a/test/test.js +++ b/test/test.js @@ -16,6 +16,9 @@ describe('unit test for quz.js', function () { it('should get the sublist consisting of LEN items from list L', function () { C.toString().should.equal('[ 19 8 7 3 2 ]'); sub(C, 3, 2).toString().should.equal('[ 3 2 ]'); + should.throws(() => sub(C, 3), function (err) { + return err.message.includes('缺少参数') + }); C.toString().should.equal('[ 19 8 7 3 2 ]'); }); });