Skip to content

Commit

Permalink
Some specs for comment filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jabbany committed Nov 29, 2016
1 parent 2469643 commit 0319e51
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 26 deletions.
35 changes: 24 additions & 11 deletions dist/CommentCoreLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -1133,26 +1133,32 @@ var CommentFilter = (function () {
return true;
}
switch (rule.op) {
case '<':
return extracted < rule.value;
case '>':
return extracted > rule.value;
case '~':
case 'regexp':
return (new RegExp(rule.value)).test(extracted.toString());
case '=':
case 'eq':
return rule.value === extracted.toString();
return rule.value ===
((typeof extracted === 'number') ?
extracted : extracted.toString());
case 'NOT':
return !_match(rule.value, cmtData);
return !_match(rule.value, extracted);
case 'AND':
if (Array.isArray(rule.value)) {
return rule.value.every(function (r) {
return _match(r, cmtData);
return _match(r, extracted);
});
} else {
return false;
}
case 'OR':
if (Array.isArray(rule.value)) {
return rule.value.some(function (r) {
return _match(r, cmtData);
return _match(r, extracted);
});
} else {
return false;
Expand All @@ -1179,24 +1185,28 @@ var CommentFilter = (function () {
}

CommentFilter.prototype.doModify = function (cmt) {
for (var k=0; k < this.modifiers.length; k++) {
cmt = this.modifiers[k](cmt);
}
return cmt;
return this.modifiers.reduce(function (c, f) {
return f(c);
}, cmt);
};

CommentFilter.prototype.beforeSend = function (cmt) {
return cmt;
}
};

CommentFilter.prototype.doValidate = function (cmtData) {
if (cmtData.mode.toString() in this.allowTypes &&
if ((!this.allowUnknownTypes ||
cmtData.mode.toString() in this.allowTypes) &&
!this.allowTypes[cmtData.mode.toString()]) {
return false;
}
return this.rules.every(function (rule) {
// Decide if matched
var matched = _match(rule, cmtData);
try {
var matched = _match(rule, cmtData);
} catch (e) {
var matched = false;
}
return rule.mode === 'accept' ? matched : !matched;
});
};
Expand All @@ -1209,6 +1219,9 @@ var CommentFilter = (function () {
};

CommentFilter.prototype.addModifier = function (f) {
if (typeof f !== 'function') {
throw new Error('Modifiers need to be functions.');
}
this.modifiers.push(f);
};

Expand Down
3 changes: 2 additions & 1 deletion dist/CommentCoreLibrary.min.js

Large diffs are not rendered by default.

168 changes: 168 additions & 0 deletions spec/filter/CommentFilter_spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
'use strict'
describe 'CommentFilter', ->
filter = null
comment =
'mode': 1
'stime': 0
'text': 'Foo Bar Baz'
'date': 1000
'size': 25
'color': 0xffffff
commentAlt =
'mode': 1
'stime': 0
'text': 'Foo Bar Baz Foz'
'date': 2000
'size': 50
'color': 0xff0000

ruleAccept =
'mode': 'accept'
'subject': 'size'
'op': '<'
'value': 50
ruleReject =
'mode': 'reject'
'subject': 'color'
'op': '='
'value': 0xffffff
ruleNot =
'mode': 'accept'
'subject': ''
'op': 'NOT'
'value':
'subject': 'text.length'
'op': '<'
'value': 15
ruleOr =
'mode': 'accept'
'subject': ''
'op': 'OR'
'value': [
{
'subject': 'date'
'op': '<'
'value': 2000
}
{
'subject': 'text'
'op': '~'
'value': '.oz'
}
]

describe 'instance API', ->
beforeEach ->
filter = new CommentFilter()

'doModify beforeSend doValidate addRule
addModifier'.split(/\s/).forEach (method)->

it "has method: '#{method}'", ->
expect(typeof filter[method]).toBe 'function'

describe 'defaults', ->
it 'does not have any modifiers', ->
expect(filter.modifiers.length).toBe 0

it 'does not have any rules', ->
expect(filter.rules.length).toBe 0

describe '.doModify', ->
beforeEach ->
filter = new CommentFilter()

it 'does nothing if no modifier', ->
expect(filter.doModify comment).toBe comment

it 'executes modifier', ->
spy = sinon.stub().returns('Foo')
filter.addModifier spy
expect(filter.doModify comment).toBe 'Foo'
expect(spy).toHaveBeenCalledWith(comment)

describe '.beforeSend', ->
it 'does nothing', ->
expect(filter.beforeSend comment).toBe comment

describe '.doValidate', ->
alienModeComment =
'mode': 1000
'stime': 10
'text': 'BAZ'
'size': 10

beforeEach ->
filter = new CommentFilter()

it 'passes valiadation valid mode', ->
expect(filter.doValidate comment).toBe true

it 'fails validation invalid mode', ->
filter.allowTypes['1'] = false
expect(filter.doValidate comment).toBe false

it 'passes validation unknown mode', ->
expect(filter.doValidate alienModeComment).toBe true

it 'fails validation if allowUnknownTypes false', ->
filter.allowUnknownTypes = false
expect(filter.doValidate alienModeComment).toBe false

it 'executes accept rules', ->
filter.addRule ruleAccept
expect(filter.doValidate comment).toBe true
expect(filter.doValidate commentAlt).toBe false

it 'executes reject rules', ->
filter.addRule ruleReject
expect(filter.rules.length).toBe 1
expect(filter.doValidate comment).toBe false
expect(filter.doValidate commentAlt).toBe true

it 'implicitly ANDs rules', ->
filter.addRule ruleAccept
filter.addRule ruleReject
expect(filter.doValidate comment).toBe false
expect(filter.doValidate commentAlt).toBe false

it 'matches OR rules', ->
filter.addRule ruleOr
expect(filter.doValidate comment).toBe true
expect(filter.doValidate commentAlt).toBe true

it 'matches NOT rules', ->
filter.addRule ruleNot
expect(filter.doValidate comment).toBe false
expect(filter.doValidate commentAlt).toBe true

describe '.addRule', ->
rule =
'mode': 'reject',
'subject': 'text',
'op': '=',
'value': 'Foo'

it 'adds a valid rule', ->
filter.addRule rule
expect(filter.rules.length).toBe 1
expect(filter.rules[0]).toBe rule

it 'rejects adding invalid rule', ->
expect( =>
filter.addRule
'mode': '???').toThrow()

describe '.addModifier', ->
modifier = (cmt) ->
cmt.color = 0xffffff
cmt

it 'adds a valid modifier', ->
filter.addModifier modifier
expect(filter.modifiers.length).toBe 1
expect(filter.modifiers[0]).toBe modifier

it 'rejects invalid modifier', ->
expect( => filter.addModifier 'Boo').toThrow()

3 changes: 0 additions & 3 deletions spec/filter/SimpleFilter_spec.coffee

This file was deleted.

62 changes: 51 additions & 11 deletions src/filter/CommentFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
*/
var CommentFilter = (function () {

/**
* Matches a rule against an input that could be the full or a subset of
* the comment data.
*
* @param rule - rule object to match
* @param cmtData - full or portion of comment data
* @return boolean indicator of match
*/
function _match (rule, cmtData) {
var path = rule.subject.split('.');
var extracted = cmtData;
Expand All @@ -26,26 +34,32 @@ var CommentFilter = (function () {
return true;
}
switch (rule.op) {
case '<':
return extracted < rule.value;
case '>':
return extracted > rule.value;
case '~':
case 'regexp':
return (new RegExp(rule.value)).test(extracted.toString());
case '=':
case 'eq':
return rule.value === extracted.toString();
return rule.value ===
((typeof extracted === 'number') ?
extracted : extracted.toString());
case 'NOT':
return !_match(rule.value, cmtData);
return !_match(rule.value, extracted);
case 'AND':
if (Array.isArray(rule.value)) {
return rule.value.every(function (r) {
return _match(r, cmtData);
return _match(r, extracted);
});
} else {
return false;
}
case 'OR':
if (Array.isArray(rule.value)) {
return rule.value.some(function (r) {
return _match(r, cmtData);
return _match(r, extracted);
});
} else {
return false;
Expand All @@ -71,25 +85,48 @@ var CommentFilter = (function () {
};
}

/**
* Runs all modifiers against current comment
*
* @param cmt - comment to run modifiers on
* @return modified comment
*/
CommentFilter.prototype.doModify = function (cmt) {
for (var k=0; k < this.modifiers.length; k++) {
cmt = this.modifiers[k](cmt);
}
return cmt;
return this.modifiers.reduce(function (c, f) {
return f(c);
}, cmt);
};

/**
* Executes a method defined to be executed right before the comment object
* (built from commentData) is placed onto the runline.
*
* @deprecated
* @param cmt - comment data
* @return cmt
*/
CommentFilter.prototype.beforeSend = function (cmt) {
return cmt;
}
};

/**
* Performs validation of the comment data before it is allowed to get sent
*
* @param cmtData - comment data
*/
CommentFilter.prototype.doValidate = function (cmtData) {
if (cmtData.mode.toString() in this.allowTypes &&
if ((!this.allowUnknownTypes ||
cmtData.mode.toString() in this.allowTypes) &&
!this.allowTypes[cmtData.mode.toString()]) {
return false;
}
return this.rules.every(function (rule) {
// Decide if matched
var matched = _match(rule, cmtData);
try {
var matched = _match(rule, cmtData);
} catch (e) {
var matched = false;
}
return rule.mode === 'accept' ? matched : !matched;
});
};
Expand All @@ -102,6 +139,9 @@ var CommentFilter = (function () {
};

CommentFilter.prototype.addModifier = function (f) {
if (typeof f !== 'function') {
throw new Error('Modifiers need to be functions.');
}
this.modifiers.push(f);
};

Expand Down

0 comments on commit 0319e51

Please sign in to comment.