Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed https://markdown-it.github.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.7 KiB

import { assert } from 'chai'
import Ruler from '../lib/ruler.mjs'
10 years ago
describe('Ruler', function () {
it('should replace rule (.at)', function () {
const ruler = new Ruler()
let res = 0
10 years ago
ruler.push('test', function foo() { res = 1 })
ruler.at('test', function bar() { res = 2 })
10 years ago
const rules = ruler.getRules('')
10 years ago
assert.strictEqual(rules.length, 1)
rules[0]()
assert.strictEqual(res, 2)
})
10 years ago
it('should inject before/after rule', function () {
const ruler = new Ruler()
let res = 0
10 years ago
ruler.push('test', function foo() { res = 1 })
ruler.before('test', 'before_test', function fooBefore() { res = -10 })
ruler.after('test', 'after_test', function fooAfter() { res = 10 })
10 years ago
const rules = ruler.getRules('')
10 years ago
assert.strictEqual(rules.length, 3)
rules[0]()
assert.strictEqual(res, -10)
rules[1]()
assert.strictEqual(res, 1)
rules[2]()
assert.strictEqual(res, 10)
})
10 years ago
it('should enable/disable rule', function () {
const ruler = new Ruler()
let rules
10 years ago
ruler.push('test', function foo() {})
ruler.push('test2', function bar() {})
10 years ago
rules = ruler.getRules('')
assert.strictEqual(rules.length, 2)
10 years ago
ruler.disable('test')
rules = ruler.getRules('')
assert.strictEqual(rules.length, 1)
ruler.disable('test2')
rules = ruler.getRules('')
assert.strictEqual(rules.length, 0)
10 years ago
ruler.enable('test')
rules = ruler.getRules('')
assert.strictEqual(rules.length, 1)
ruler.enable('test2')
rules = ruler.getRules('')
assert.strictEqual(rules.length, 2)
})
10 years ago
it('should enable/disable multiple rule', function () {
const ruler = new Ruler()
let rules
10 years ago
ruler.push('test', function foo() {})
ruler.push('test2', function bar() {})
10 years ago
ruler.disable([ 'test', 'test2' ])
rules = ruler.getRules('')
assert.strictEqual(rules.length, 0)
ruler.enable([ 'test', 'test2' ])
rules = ruler.getRules('')
assert.strictEqual(rules.length, 2)
})
10 years ago
it('should enable rules by whitelist', function () {
const ruler = new Ruler()
10 years ago
ruler.push('test', function foo() {})
ruler.push('test2', function bar() {})
10 years ago
ruler.enableOnly('test')
const rules = ruler.getRules('')
assert.strictEqual(rules.length, 1)
})
10 years ago
it('should support multiple chains', function () {
const ruler = new Ruler()
let rules
10 years ago
ruler.push('test', function foo() {})
ruler.push('test2', function bar() {}, { alt: [ 'alt1' ] })
ruler.push('test2', function bar() {}, { alt: [ 'alt1', 'alt2' ] })
10 years ago
rules = ruler.getRules('')
assert.strictEqual(rules.length, 3)
rules = ruler.getRules('alt1')
assert.strictEqual(rules.length, 2)
rules = ruler.getRules('alt2')
assert.strictEqual(rules.length, 1)
})
10 years ago
it('should fail on invalid rule name', function () {
const ruler = new Ruler()
10 years ago
ruler.push('test', function foo() {})
10 years ago
assert.throws(function () {
ruler.at('invalid name', function bar() {})
})
10 years ago
assert.throws(function () {
ruler.before('invalid name', function bar() {})
})
10 years ago
assert.throws(function () {
ruler.after('invalid name', function bar() {})
})
10 years ago
assert.throws(function () {
ruler.enable('invalid name')
})
10 years ago
assert.throws(function () {
ruler.disable('invalid name')
})
})
10 years ago
it('should not fail on invalid rule name in silent mode', function () {
const ruler = new Ruler()
ruler.push('test', function foo() {})
assert.doesNotThrow(function () {
ruler.enable('invalid name', true)
})
assert.doesNotThrow(function () {
ruler.enableOnly('invalid name', true)
})
assert.doesNotThrow(function () {
ruler.disable('invalid name', true)
})
})
})