Skip to content

Commit 6874e37

Browse files
committed
Cleanup and more tests
1 parent 6a2bd3a commit 6874e37

8 files changed

Lines changed: 239 additions & 138 deletions

File tree

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ gem 'rails-assets-comment-core-library'
4444

4545
## Examples and Documentation
4646
- [Documentation](docs/) can be found inside the `docs/` folder.
47-
- Some sample extension modules may be found in `src/extend/`.
4847
- Experimental modules are in `experimental/`.
4948
- You may test using test data found in `test/`.
5049

@@ -77,7 +76,6 @@ implementation of a video player with CommentCoreLibrary.
7776

7877
## 使用
7978
- 有关本项目的[文档](docs/) 可以在 `docs/` 文件夹里面找到。
80-
- 一些功能性扩展模块会出现在 `src/extend/` 中。
8179
- 一些实验性模块在 `experimental/` 里。
8280
- 测试数据在 `test/` 里。
8381

dist/CommentCoreLibrary.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ var CommentProvider = (function () {
10581058

10591059
xhr.onerror = function () {
10601060
reject(new Error(this.status + " " + this.statusText));
1061-
}
1061+
};
10621062

10631063
xhr.open(method, uri);
10641064
if (typeof body !== 'undefined') {
@@ -1119,7 +1119,7 @@ var CommentProvider = (function () {
11191119
CommentProvider.TextProvider = function (method, url, args, body) {
11201120
return CommentProvider.BaseHttpProvider(
11211121
method, url, "text", args, body).then(function (response) {
1122-
return response.text;
1122+
return response;
11231123
});
11241124
};
11251125

@@ -1211,7 +1211,7 @@ var CommentProvider = (function () {
12111211
return new Promise(function (resolve, reject) {
12121212
if (!(type in this._parsers)) {
12131213
reject(new Error('No parsers defined for "' + type + '"'));
1214-
return;s
1214+
return;
12151215
}
12161216
for (var i = 0; i < this._parsers[type].length; i++) {
12171217
var output = null;

dist/CommentCoreLibrary.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/CommentProvider_spec.coffee

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,62 @@ describe 'CommentProvider', ->
110110
provider.destroy()
111111
expect( => provider.addTarget(commentManager)).toThrow()
112112

113+
describe '.applyParsersOne', ->
114+
rejectingParser =
115+
parseOne: () -> null
116+
parseMany: () -> null
117+
resolvingParser =
118+
parseOne: (t) -> t
119+
parseMany: (t) -> t
120+
121+
it 'rejects if no parsers for type', (done) ->
122+
promise = provider.applyParsersOne {}, CommentProvider.SOURCE_JSON
123+
promise.catch (e) ->
124+
expect(e instanceof Error).toBe true
125+
done()
126+
127+
it 'rejects if no parser accepts', (done) ->
128+
provider.addParser rejectingParser, CommentProvider.SOURCE_JSON
129+
promise = provider.applyParsersOne {}, CommentProvider.SOURCE_JSON
130+
promise.catch (e) ->
131+
expect(e instanceof Error).toBe true
132+
done()
133+
134+
it 'accepts if parser accepts', (done) ->
135+
provider.addParser resolvingParser, CommentProvider.SOURCE_JSON
136+
promise = provider.applyParsersOne {}, CommentProvider.SOURCE_JSON
137+
promise.then (item) ->
138+
expect(item).toEqual {}
139+
done()
140+
141+
describe '.applyParsersList', ->
142+
rejectingParser =
143+
parseOne: () -> null
144+
parseMany: () -> null
145+
resolvingParser =
146+
parseOne: (t) -> t
147+
parseMany: (t) -> t
148+
149+
it 'rejects if no parsers for type', (done) ->
150+
promise = provider.applyParsersList [], CommentProvider.SOURCE_JSON
151+
promise.catch (e) ->
152+
expect(e instanceof Error).toBe true
153+
done()
154+
155+
it 'rejects if no parser accepts', (done) ->
156+
provider.addParser rejectingParser, CommentProvider.SOURCE_JSON
157+
promise = provider.applyParsersList [], CommentProvider.SOURCE_JSON
158+
promise.catch (e) ->
159+
expect(e instanceof Error).toBe true
160+
done()
161+
162+
it 'accepts if parser accepts', (done) ->
163+
provider.addParser resolvingParser, CommentProvider.SOURCE_JSON
164+
promise = provider.applyParsersList [], CommentProvider.SOURCE_JSON
165+
promise.then (items) ->
166+
expect(items).toEqual []
167+
done()
168+
113169
describe '.load', ->
114170
it 'requests static sources', (done) ->
115171
provider.addStaticSource (Promise.resolve 'Foo'), CommentProvider.SOURCE_TEXT
@@ -139,6 +195,11 @@ describe 'CommentProvider', ->
139195
addEventListener: () ->
140196
spy = sinon.spy dynamicSource, "addEventListener"
141197

198+
it 'fails if called on destroyed object', ->
199+
p = new CommentProvider()
200+
p.destroy()
201+
expect( => p.start()).toThrow()
202+
142203
it 'calls load', ->
143204
loadspy = sinon.spy provider, 'load'
144205
provider.start()
@@ -161,3 +222,8 @@ describe 'CommentProvider', ->
161222
it 'sets destroyed flag', ->
162223
provider.destroy()
163224
expect(provider._destroyed).toBe true
225+
226+
it 'destroy can be called multiple times', ->
227+
provider.destroy()
228+
provider.destroy()
229+
expect(provider._destroyed).toBe true

src/CommentProvider.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var CommentProvider = (function () {
6060

6161
xhr.onerror = function () {
6262
reject(new Error(this.status + " " + this.statusText));
63-
}
63+
};
6464

6565
xhr.open(method, uri);
6666
if (typeof body !== 'undefined') {
@@ -121,7 +121,7 @@ var CommentProvider = (function () {
121121
CommentProvider.TextProvider = function (method, url, args, body) {
122122
return CommentProvider.BaseHttpProvider(
123123
method, url, "text", args, body).then(function (response) {
124-
return response.text;
124+
return response;
125125
});
126126
};
127127

@@ -213,7 +213,7 @@ var CommentProvider = (function () {
213213
return new Promise(function (resolve, reject) {
214214
if (!(type in this._parsers)) {
215215
reject(new Error('No parsers defined for "' + type + '"'));
216-
return;s
216+
return;
217217
}
218218
for (var i = 0; i < this._parsers[type].length; i++) {
219219
var output = null;

src/core/CommentFactory.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Comment Factory Abstraction
3+
*
4+
* @author Jim Chen
5+
* @license MIT License
6+
* @description Factory to allow creation of different kinds of comments with
7+
* different underlying abstractions.
8+
*/
9+
/// <reference path="Core.d.ts" />
10+
interface ICommentFactory {
11+
(comment:Object):IComment;
12+
}
13+
class CommentFactory {
14+
private _bindings:{[key:number]:ICommentFactory;} = {};
15+
16+
public bind (mode:number, factory:ICommentFactory):void {
17+
this._bindings[mode] = factory;
18+
}
19+
20+
public create (comment:Object):IComment {
21+
if (comment === null || !comment.hasOwnProperty('mode')) {
22+
throw new Error('Comment format incorrect');
23+
}
24+
if (!this._bindings.hasOwnProperty(comment.mode)) {
25+
throw new Error('No binding for comment type ' + comment.mode);
26+
}
27+
return this._bindings[comment.mode](comment);
28+
}
29+
}

0 commit comments

Comments
 (0)