Skip to content

Commit

Permalink
add tests & fix line ending issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Damianos Mylonakis committed Feb 17, 2012
1 parent 4f16ecd commit 6575414
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
13 changes: 13 additions & 0 deletions Makefile
@@ -0,0 +1,13 @@


TESTS = test/*.js
REPORTER = spec

test:
@./node_modules/.bin/mocha \
--require should \
--reporter $(REPORTER) \
--growl \
$(TESTS)

.PHONY: test
3 changes: 0 additions & 3 deletions ical.js

This file was deleted.

1 change: 1 addition & 0 deletions index.js
@@ -0,0 +1 @@
module.exports = require('./lib/ical');
3 changes: 3 additions & 0 deletions lib/ical.js
@@ -0,0 +1,3 @@
module.exports = {
Parser: require('./parser').Parser
}
20 changes: 15 additions & 5 deletions lib/parser.js
Expand Up @@ -8,6 +8,7 @@ function Parser(opts) {
this.writable = true;
this.calendars = [];
this._handles = [];
this._done = false;
}
util.inherits(Parser, Stream);
exports.Parser = Parser;
Expand All @@ -20,7 +21,7 @@ Parser.parseLine = function (line) {
var obj = { };
var valueSplitter = line.indexOf(':');
if (valueSplitter === -1) {
throw new Error('this line has no ":"!');
throw new Error('this line has no ":", "' + line + '"');
}
var paramSplitter = line.indexOf(';');
if (paramSplitter === -1) {
Expand Down Expand Up @@ -110,22 +111,31 @@ Parser.prototype._dispatch = function () {
* Need to take into
* account \r\n in quotes here
*/
var content = this.buffer.substr(0, this.buffer.lastIndexOf('\r\n'));
this.buffer = this.buffer.substr(2);
var limit = this.buffer.lastIndexOf('\r\n') + 2;
if (limit == -1) {
return;
}
debugger;
var content = this.buffer.substr(0, limit);
this.buffer = this.buffer.slice(limit + 2, limit + 4);
var contentLines = content.split('\r\n');
for (var i = 0; i < contentLines.length; i++) {
var line = contentLines[i];
if (line === '') {
this.emit('end')
break
}
line = Parser.parseLine(line);
var fn = this.handlers[line.name];
if (!fn) {
console.log('dont know how to handle ' + line.name + ' will use dummy handler');
//console.log('dont know how to handle ' + line.name + ' will use dummy handler');
this.handlers._onlyCopy.call(this, line);
}
else {
fn.call(this, line);
}
}
};
}

Parser.prototype.end = function () {
this.emit('end');
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -10,10 +10,12 @@
"keywords": [
"ical", "icalendar", "rfc2445"
],
"main": "./ical.js",
"main": "./",
"private": true,
"dependencies": {},
"devDependencies": {
"mocha": "0.12.1",
"should": "0.5.1"
},
"license": {
"type": "MIT",
Expand Down
23 changes: 23 additions & 0 deletions test/parse_full.js
@@ -0,0 +1,23 @@
var should = require('should'),
fs = require('fs'),
ical = require('../');

describe('Parser', function () {
var parser, full_ics_stream
var full_ics = fs.readFileSync(__dirname + '/resources/full.ics')
beforeEach(function (done) {
parser = new ical.Parser()
full_ics_stream = fs.createReadStream(__dirname + '/resources/full.ics')
done()
})
describe('parses correctly tests', function () {
it('must parse full.ics', function (done) {
parser.on('end', done)
parser.write(full_ics)
})
it('must parse full.ics streamed', function (done) {
parser.on('end', done);
full_ics_stream.pipe(parser)
})
})
})
30 changes: 30 additions & 0 deletions test/resources/full.ics
@@ -0,0 +1,30 @@
BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:dan Mylonakis
X-WR-TIMEZONE:Europe/Athens
BEGIN:VEVENT
DTSTART:20111206T150000Z
DTEND:20111206T180000Z
DTSTAMP:20120217T005926Z
UID:3v454h0dqpe7m0kbqds4rca32g@google.com
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;CN=danmyl
onakis@gmail.com;X-NUM-GUESTS=0:mailto:danmylonakis@gmail.com
CREATED:20111206T013929Z
DESCRIPTION:
LAST-MODIFIED:20111206T013929Z
LOCATION:
SEQUENCE:1
STATUS:TENTATIVE
SUMMARY:557 με δαμον
TRANSP:OPAQUE
CATEGORIES:http://schemas.google.com/g/2005#event
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:This is an event reminder
TRIGGER:-P0DT0H10M0S
END:VALARM
END:VEVENT
END:VCALENDAR

0 comments on commit 6575414

Please sign in to comment.