Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate tests from the build script #52

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 20 additions & 15 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
/* eslint-disable node/no-deprecated-api */
var t = require('assert')
var slugify = require('./')

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 +13,27 @@ describe('slugify', function () {
})

it('duplicates characters are not allowed', function () {
var result = build.duplicates()
var charmap = require('./charmap.json')

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 +286,4 @@ describe('slugify', function () {
)
})
})

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