Skip to content

Commit 9bd7d64

Browse files
committed
Add tests and update bower config
1 parent fdd4281 commit 9bd7d64

7 files changed

Lines changed: 87 additions & 28 deletions

File tree

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.9.2",
44
"main": ["build/CommentCoreLibrary.js","build/style.css"],
55
"ignore": [
6-
"tests/**/*",
6+
"test/**/*",
77
"experimental/**/*",
88
"demo/**/*"
99
],

build/CommentCoreLibrary.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -951,10 +951,10 @@ var CommentManager = (function() {
951951
})();
952952

953953
/**
954-
AcFun Format
955-
Licensed Under MIT License
956-
An alternative format comment parser
957-
**/
954+
* AcFun Format Parser
955+
* @license MIT License
956+
* An alternative format comment parser
957+
*/
958958
function AcfunParser(jsond){
959959
var list = [];
960960
try{
@@ -1049,29 +1049,29 @@ function AcfunParser(jsond){
10491049
}
10501050

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

10621062
if(xmlDoc !== null){
10631063
var elems = xmlDoc.getElementsByTagName('d');
10641064
}else{
10651065
if(!document || !document.createElement){
1066-
//Maybe we are in a restricted context
1066+
// Maybe we are in a restricted context? Bail.
10671067
return [];
10681068
}
10691069
if(warn){
10701070
if(!confirm("XML Parse Error. \n Allow tag soup parsing?\n[WARNING: This is unsafe.]")){
10711071
return [];
10721072
}
10731073
}else{
1074-
// clobber some potentially bad things
1074+
// TODO: Make this safer in the future
10751075
text = text.replace(new RegExp("</([^d])","g"), "</disabled $1");
10761076
text = text.replace(new RegExp("</(\S{2,})","g"), "</disabled $1");
10771077
text = text.replace(new RegExp("<([^d/]\W*?)","g"), "<disabled $1");
Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,58 @@
11
'use strict'
2+
describe 'CommentSpaceAllocators', ->
3+
scrollCSA = anchorCSA = manager = stage = c1 = c2 = s1 = s2 = null
24

3-
describe 'AnchorCommentSpaceAllocator', ->
4-
xit('TODO: add some examples to (or delete) it')
5+
describe 'instance ISpaceAllocator and descendants', ->
6+
beforeEach ->
7+
scrollCSA = new CommentSpaceAllocator(400,300)
8+
anchorCSA = new AnchorCommentSpaceAllocator(400,300)
9+
stage = document.createElement 'DIV'
10+
manager = new CommentManager(stage)
11+
manager.init()
12+
c1 = new CoreComment(manager, {})
13+
c2 = new CoreComment(manager, {})
14+
s1 = new ScrollComment(manager, {})
15+
s2 = new ScrollComment(manager, {})
16+
c1.init();
17+
c2.init();
18+
s1.init();
19+
s2.init();
520

6-
describe 'CommentSpaceAllocator', ->
7-
xit('TODO: add some examples to (or delete) it')
21+
'add remove setBounds'.split(' ').forEach (method)->
22+
it "Has method: '#{method}'", ->
23+
expect(typeof scrollCSA[method]).toBe 'function'
24+
expect(typeof anchorCSA[method]).toBe 'function'
25+
26+
'willCollide pathCheck'.split(' ').forEach (method)->
27+
it "Has path-based allocation method: '#{method}'", ->
28+
expect(typeof scrollCSA[method]).toBe 'function'
29+
expect(typeof anchorCSA[method]).toBe 'function'
30+
31+
it 'successful initialization of comment manager', ->
32+
expect(manager).not.toBe null
33+
34+
describe 'AnchorCommentSpaceAllocator', ->
35+
it 'comments always collide', ->
36+
expect(anchorCSA.willCollide(c1, c2)).toBe true
37+
it 'same comment must collide with self', ->
38+
expect(anchorCSA.willCollide(c1, c1)).toBe true
39+
expect(anchorCSA.willCollide(c2, c2)).toBe true
40+
it 'path check passes for y = 0 in empty pool', ->
41+
expect(anchorCSA.pathCheck(0, c1, [])).toBe true
42+
it 'path check fails for y = 0 in self pool', ->
43+
expect(anchorCSA.pathCheck(0, c1, [c1])).toBe false
44+
it 'path check fails for y = height + 1 in self pool', ->
45+
expect(anchorCSA.pathCheck(c1.height + 1, c1, [c1])).toBe true
46+
# TODO: We need more extensive test cases
47+
48+
describe 'CommentSpaceAllocator', ->
49+
it 'same comment must collide with self', ->
50+
expect(scrollCSA.willCollide(s1, s1)).toBe true
51+
expect(scrollCSA.willCollide(s2, s2)).toBe true
52+
it 'path check passes for y = 0 in empty pool', ->
53+
expect(scrollCSA.pathCheck(0, s1, [])).toBe true
54+
it 'path check passes for y = 0 in self pool', ->
55+
expect(scrollCSA.pathCheck(0, s1, [s1])).toBe false
56+
it 'path check passes for y = height + 1 in self pool', ->
57+
expect(scrollCSA.pathCheck(s1.height + 1, s1, [s1])).toBe true
58+
# TODO: We need more extensive test cases

spec/parsers/BilibiliFormat_spec.coffee

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
describe 'BilibiliFormat', ->
33
jasmine.getFixtures().fixturesPath = "test/"
44
it 'parses normal comments', ->
5+
# TODO: Update testing to pass in an XML object instead of
6+
# relying on the unsafe innerHTML.
57
xml_text = readFixtures 'av207527.xml'
68
comments = BilibiliParser(null, xml_text)
79
expect(comments.length).toBe 12546
@@ -24,3 +26,10 @@ describe 'BilibiliFormat', ->
2426
expect(comments.length).toBe 654
2527
expect(comments[0].mode).toEqual 7
2628
expect(comments[653].mode).toEqual 8
29+
30+
it 'parses advanced comments', ->
31+
xml_text = readFixtures 'boss.xml'
32+
comments = BilibiliParser(null, xml_text)
33+
expect(comments.length).toBe 1000
34+
expect(comments[0].mode).toEqual 7
35+
expect(comments[0].motion).not.toBe null

src/parsers/AcfunFormat.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
AcFun Format
3-
Licensed Under MIT License
4-
An alternative format comment parser
5-
**/
2+
* AcFun Format Parser
3+
* @license MIT License
4+
* An alternative format comment parser
5+
*/
66
function AcfunParser(jsond){
77
var list = [];
88
try{

src/parsers/BilibiliFormat.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
/**
2-
Bilibili Format
3-
Licensed Under MIT License
4-
Takes in an XMLDoc/LooseXMLDoc and parses that into a Generic Comment List
5-
**/
2+
* Bilibili Format Parser
3+
* @license MIT License
4+
* Takes in an XMLDoc/LooseXMLDoc and parses that into a Generic Comment List
5+
**/
66
function BilibiliParser(xmlDoc, text, warn){
77
function format(string){
8-
//Format the bili output to be json-valid
8+
// Format the comment text to be JSON Valid.
99
return string.replace(/\t/,"\\t");
1010
}
1111

1212
if(xmlDoc !== null){
1313
var elems = xmlDoc.getElementsByTagName('d');
1414
}else{
1515
if(!document || !document.createElement){
16-
//Maybe we are in a restricted context
16+
// Maybe we are in a restricted context? Bail.
1717
return [];
1818
}
1919
if(warn){
2020
if(!confirm("XML Parse Error. \n Allow tag soup parsing?\n[WARNING: This is unsafe.]")){
2121
return [];
2222
}
2323
}else{
24-
// clobber some potentially bad things
24+
// TODO: Make this safer in the future
2525
text = text.replace(new RegExp("</([^d])","g"), "</disabled $1");
2626
text = text.replace(new RegExp("</(\S{2,})","g"), "</disabled $1");
2727
text = text.replace(new RegExp("<([^d/]\W*?)","g"), "<disabled $1");

test/boss.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
<?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>
32
<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>
43
<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>

0 commit comments

Comments
 (0)