Skip to content

Commit

Permalink
Add tests and update bower config
Browse files Browse the repository at this point in the history
  • Loading branch information
jabbany committed Nov 5, 2014
1 parent fdd4281 commit 9bd7d64
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.9.2",
"main": ["build/CommentCoreLibrary.js","build/style.css"],
"ignore": [
"tests/**/*",
"test/**/*",
"experimental/**/*",
"demo/**/*"
],
Expand Down
22 changes: 11 additions & 11 deletions build/CommentCoreLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -951,10 +951,10 @@ var CommentManager = (function() {
})();

/**
AcFun Format
Licensed Under MIT License
An alternative format comment parser
**/
* AcFun Format Parser
* @license MIT License
* An alternative format comment parser
*/
function AcfunParser(jsond){
var list = [];
try{
Expand Down Expand Up @@ -1049,29 +1049,29 @@ function AcfunParser(jsond){
}

/**
Bilibili Format
Licensed Under MIT License
Takes in an XMLDoc/LooseXMLDoc and parses that into a Generic Comment List
**/
* Bilibili Format Parser
* @license MIT License
* Takes in an XMLDoc/LooseXMLDoc and parses that into a Generic Comment List
**/
function BilibiliParser(xmlDoc, text, warn){
function format(string){
//Format the bili output to be json-valid
// Format the comment text to be JSON Valid.
return string.replace(/\t/,"\\t");
}

if(xmlDoc !== null){
var elems = xmlDoc.getElementsByTagName('d');
}else{
if(!document || !document.createElement){
//Maybe we are in a restricted context
// Maybe we are in a restricted context? Bail.
return [];
}
if(warn){
if(!confirm("XML Parse Error. \n Allow tag soup parsing?\n[WARNING: This is unsafe.]")){
return [];
}
}else{
// clobber some potentially bad things
// TODO: Make this safer in the future
text = text.replace(new RegExp("</([^d])","g"), "</disabled $1");
text = text.replace(new RegExp("</(\S{2,})","g"), "</disabled $1");
text = text.replace(new RegExp("<([^d/]\W*?)","g"), "<disabled $1");
Expand Down
59 changes: 55 additions & 4 deletions spec/core/CommentSpaceAllocator_spec.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,58 @@
'use strict'
describe 'CommentSpaceAllocators', ->
scrollCSA = anchorCSA = manager = stage = c1 = c2 = s1 = s2 = null

describe 'AnchorCommentSpaceAllocator', ->
xit('TODO: add some examples to (or delete) it')
describe 'instance ISpaceAllocator and descendants', ->
beforeEach ->
scrollCSA = new CommentSpaceAllocator(400,300)
anchorCSA = new AnchorCommentSpaceAllocator(400,300)
stage = document.createElement 'DIV'
manager = new CommentManager(stage)
manager.init()
c1 = new CoreComment(manager, {})
c2 = new CoreComment(manager, {})
s1 = new ScrollComment(manager, {})
s2 = new ScrollComment(manager, {})
c1.init();
c2.init();
s1.init();
s2.init();

describe 'CommentSpaceAllocator', ->
xit('TODO: add some examples to (or delete) it')
'add remove setBounds'.split(' ').forEach (method)->
it "Has method: '#{method}'", ->
expect(typeof scrollCSA[method]).toBe 'function'
expect(typeof anchorCSA[method]).toBe 'function'

'willCollide pathCheck'.split(' ').forEach (method)->
it "Has path-based allocation method: '#{method}'", ->
expect(typeof scrollCSA[method]).toBe 'function'
expect(typeof anchorCSA[method]).toBe 'function'

it 'successful initialization of comment manager', ->
expect(manager).not.toBe null

describe 'AnchorCommentSpaceAllocator', ->
it 'comments always collide', ->
expect(anchorCSA.willCollide(c1, c2)).toBe true
it 'same comment must collide with self', ->
expect(anchorCSA.willCollide(c1, c1)).toBe true
expect(anchorCSA.willCollide(c2, c2)).toBe true
it 'path check passes for y = 0 in empty pool', ->
expect(anchorCSA.pathCheck(0, c1, [])).toBe true
it 'path check fails for y = 0 in self pool', ->
expect(anchorCSA.pathCheck(0, c1, [c1])).toBe false
it 'path check fails for y = height + 1 in self pool', ->
expect(anchorCSA.pathCheck(c1.height + 1, c1, [c1])).toBe true
# TODO: We need more extensive test cases

describe 'CommentSpaceAllocator', ->
it 'same comment must collide with self', ->
expect(scrollCSA.willCollide(s1, s1)).toBe true
expect(scrollCSA.willCollide(s2, s2)).toBe true
it 'path check passes for y = 0 in empty pool', ->
expect(scrollCSA.pathCheck(0, s1, [])).toBe true
it 'path check passes for y = 0 in self pool', ->
expect(scrollCSA.pathCheck(0, s1, [s1])).toBe false
it 'path check passes for y = height + 1 in self pool', ->
expect(scrollCSA.pathCheck(s1.height + 1, s1, [s1])).toBe true
# TODO: We need more extensive test cases
9 changes: 9 additions & 0 deletions spec/parsers/BilibiliFormat_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
describe 'BilibiliFormat', ->
jasmine.getFixtures().fixturesPath = "test/"
it 'parses normal comments', ->
# TODO: Update testing to pass in an XML object instead of
# relying on the unsafe innerHTML.
xml_text = readFixtures 'av207527.xml'
comments = BilibiliParser(null, xml_text)
expect(comments.length).toBe 12546
Expand All @@ -24,3 +26,10 @@ describe 'BilibiliFormat', ->
expect(comments.length).toBe 654
expect(comments[0].mode).toEqual 7
expect(comments[653].mode).toEqual 8

it 'parses advanced comments', ->
xml_text = readFixtures 'boss.xml'
comments = BilibiliParser(null, xml_text)
expect(comments.length).toBe 1000
expect(comments[0].mode).toEqual 7
expect(comments[0].motion).not.toBe null
8 changes: 4 additions & 4 deletions src/parsers/AcfunFormat.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
AcFun Format
Licensed Under MIT License
An alternative format comment parser
**/
* AcFun Format Parser
* @license MIT License
* An alternative format comment parser
*/
function AcfunParser(jsond){
var list = [];
try{
Expand Down
14 changes: 7 additions & 7 deletions src/parsers/BilibiliFormat.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/**
Bilibili Format
Licensed Under MIT License
Takes in an XMLDoc/LooseXMLDoc and parses that into a Generic Comment List
**/
* Bilibili Format Parser
* @license MIT License
* Takes in an XMLDoc/LooseXMLDoc and parses that into a Generic Comment List
**/
function BilibiliParser(xmlDoc, text, warn){
function format(string){
//Format the bili output to be json-valid
// Format the comment text to be JSON Valid.
return string.replace(/\t/,"\\t");
}

if(xmlDoc !== null){
var elems = xmlDoc.getElementsByTagName('d');
}else{
if(!document || !document.createElement){
//Maybe we are in a restricted context
// Maybe we are in a restricted context? Bail.
return [];
}
if(warn){
if(!confirm("XML Parse Error. \n Allow tag soup parsing?\n[WARNING: This is unsafe.]")){
return [];
}
}else{
// clobber some potentially bad things
// TODO: Make this safer in the future
text = text.replace(new RegExp("</([^d])","g"), "</disabled $1");
text = text.replace(new RegExp("</(\S{2,})","g"), "</disabled $1");
text = text.replace(new RegExp("<([^d/]\W*?)","g"), "<disabled $1");
Expand Down
1 change: 0 additions & 1 deletion test/boss.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<?xml version="1.0" encoding="UTF-8"?><i><chatserver>chat.bilibili.com</chatserver><chatid>2196678</chatid><mission>0</mission><source>k-v</source><d p="8.2299995422363,7,25,16751001,1408888765,0,c1cd5522,576934780">[0,0,"1-1",3.8,"《霸道总裁爱上我》(SMAP-[SHAKE])",0,0,0,0,1000,0,true,"微软雅黑",1,"M67,264L66,264L230,263"]</d>
<d p="12.039999961853,7,25,16751001,1408888786,0,c1cd5522,576935381">[0,0,"1-1",3.7,"演唱/填词/念白/后期/压制:翘课、少恭",0,0,0,0,1000,0,true,"微软雅黑",1,"M67,264L66,264L230,263"]</d>
<d p="15.770000457764,7,25,16751001,1408888807,0,c1cd5522,576936074">[0,0,"1-1",3.8,"字幕:择日",0,0,0,0,1000,0,true,"微软雅黑",1,"M67,264L66,264L230,263"]</d>
Expand Down

0 comments on commit 9bd7d64

Please sign in to comment.