Skip to content

Commit

Permalink
Separate tests from the build script
Browse files Browse the repository at this point in the history
  • Loading branch information
realityking committed Feb 25, 2019
1 parent c898267 commit 96ad844
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 39 deletions.
29 changes: 7 additions & 22 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,15 @@

var fs = require('fs')
var charmap = require('./charmap')


exports.sort = function () {
function sortCharmap () {
var charmap = JSON.parse(fs.readFileSync('charmap.json', 'utf8'))
var sorted = charmap.sort(function (a, b) {
return a.key > b.key ? 1 : a.key < b.key ? -1 : 0
})
fs.writeFileSync('charmap.json', JSON.stringify(sorted, null, 2), 'utf8')
}

exports.duplicates = function () {
var cache = {}
var duplicates = []
charmap.forEach(function (pair) {
if (!cache[pair.key]) {
cache[pair.key] = pair
}
else {
duplicates.push(pair)
}
})
return {
cache: cache,
duplicates: duplicates
}
}

exports.replace = function () {
function replaceCharmap () {
var charmap = JSON.parse(fs.readFileSync('charmap.json', 'utf8'))
var obj = charmap.reduce(function (obj, pair) {
obj[pair.key] = pair.value
return obj
Expand All @@ -44,3 +26,6 @@ exports.replace = function () {

fs.writeFileSync('index.js', source, 'utf8')
}

sortCharmap()
replaceCharmap()
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
],
"types": "index.d.ts",
"scripts": {
"build": "node build.js && echo Build completed",
"lint": "eslint index.js test.js build.js && echo Lint Passed",
"test": "npm run lint && npm run test:ci && echo Tests Passed",
"test:ci": "mocha",
"test:cov": "istanbul cover _mocha"
"test:ci": "npm run build && mocha",
"test:cov": "npm run build && istanbul cover _mocha"
},
"engines": {
"node": ">=4.0.0"
Expand Down
34 changes: 19 additions & 15 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
/* eslint-disable node/no-deprecated-api */
var t = require('assert')
var slugify = require('./')
var charmap = require('./charmap.json')

describe('slugify', function () {
var build = require('./build')
var slugify

before(function () {
build.replace()
slugify = require('./')
})

it('throws', function () {
try {
slugify(undefined)
Expand All @@ -20,11 +14,25 @@ describe('slugify', function () {
})

it('duplicates characters are not allowed', function () {
var result = build.duplicates()
function getDuplicates (charmap) {
var cache = {}
var duplicates = []
charmap.forEach(function (pair) {
if (!cache[pair.key]) {
cache[pair.key] = pair
}
else {
duplicates.push(pair)
}
})
return duplicates
}

var result = getDuplicates(charmap)
t.equal(
result.duplicates.length,
result.length,
0,
'duplicates: ' + result.duplicates
'duplicates: ' + result
.map(function (pair) {
return pair.key
})
Expand Down Expand Up @@ -277,8 +285,4 @@ describe('slugify', function () {
)
})
})

after(function () {
build.sort()
})
})

0 comments on commit 96ad844

Please sign in to comment.