Skip to content

Commit

Permalink
Refactor test files
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Szakacs committed Nov 1, 2017
1 parent b6a0115 commit 50ce8e9
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 90 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"bump": "npm run gulp -- bump",
"build": "npm run gulp -- build",
"build:doc": "npm run build && jsdoc2md --no-gfm dist/*.js > doc/API.md",
"test": "npm run lint && npm run build:doc && tap test/*.js --100",
"test:dev": "npm run build && tap test/*.js --100",
"test": "npm run lint && npm run build:doc && tap test/*.test.js --100",
"test:dev": "npm run build && tap test/*.test.js --100",
"precommit": "npm run test",
"test:ci": "npm run test"
},
Expand Down
21 changes: 21 additions & 0 deletions test/algorithm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const tap = require('tap')
const utils = require('./utils')
const getSRI = utils.getSRI

tap.test('algorithm', function (t) {
var hash

hash = getSRI('-', getSRI.SHA256, true)
t.equal(hash.indexOf('sha256-'), 0)

hash = getSRI('-', getSRI.SHA384, true)
t.equal(hash.indexOf('sha384-'), 0)

hash = getSRI('-', getSRI.SHA512, true)
t.equal(hash.indexOf('sha512-'), 0)

hash = getSRI('-', 'no-hash', true)
t.equal(hash.indexOf('sha256-'), 0)

t.end()
})
30 changes: 30 additions & 0 deletions test/error.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const tap = require('tap')
const utils = require('./utils')
const getSRI = utils.getSRI

tap.test('error', function (t) {
var message

try {
getSRI()
} catch (error) {
message = error.message
}
t.equal(message, 'The string parameter must be a string type, got: undefined')

try {
getSRI(0)
} catch (error) {
message = error.message
}
t.equal(message, 'The string parameter must be a string type, got: number')

try {
getSRI('')
} catch (error) {
message = error.message
}
t.equal(message, 'The string parameter must have a .length > 0')

t.end()
})
31 changes: 31 additions & 0 deletions test/files.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const tap = require('tap')
const path = require('path')
const fs = require('fs')
const utils = require('./utils')
const getSRI = utils.getSRI

tap.test('prefix', function (t) {
const fixtures = path.normalize(path.join(process.cwd(), '/test/fixtures/'))
const files = [
'bootstrap.min.css',
'sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO',

'bootstrap.min.js',
'sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy',

'jquery-3.3.1.slim.min.js',
'sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo',

'popper.min.js',
'sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49'
]

for (var i = 0, len = files.length; i < len; i += 2) {
const content = fs.readFileSync(path.join(fixtures, files[i])).toString()
const hash = files[i + 1]

t.equal(getSRI(content, getSRI.SHA384, true), hash)
}

t.end()
})
88 changes: 0 additions & 88 deletions test/index.test.js

This file was deleted.

11 changes: 11 additions & 0 deletions test/prefix.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const tap = require('tap')
const utils = require('./utils')
const getSRI = utils.getSRI

tap.test('prefix', function (t) {
t.equal(getSRI('-', getSRI.SHA256), 'OXPgIukyIPkhLBjQ0MVDrnwwnkZkDak6SgMU3pmfURI=')
t.equal(getSRI('-', getSRI.SHA256, false), 'OXPgIukyIPkhLBjQ0MVDrnwwnkZkDak6SgMU3pmfURI=')
t.equal(getSRI('-', getSRI.SHA256, true), 'sha256-OXPgIukyIPkhLBjQ0MVDrnwwnkZkDak6SgMU3pmfURI=')

t.end()
})
8 changes: 8 additions & 0 deletions test/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

const path = require('path')
const pkg = require('../../package')

module.exports = {
getSRI: require(path.normalize(path.join(__dirname, '../../', pkg.main)))
}

0 comments on commit 50ce8e9

Please sign in to comment.