Add unit tests, fix last_column reporting.

This commit is contained in:
Jason Walton 2013-01-14 17:11:07 -05:00
parent 923739ebb4
commit f67da27d2f
3 changed files with 28 additions and 5 deletions

View File

@ -731,14 +731,15 @@
};
Lexer.prototype.makeToken = function(tag, value, offsetInChunk, length) {
var locationData, token, _ref2, _ref3;
var lastCharacter, locationData, token, _ref2, _ref3;
offsetInChunk = offsetInChunk || 0;
if (length === void 0) {
length = value.length;
}
locationData = {};
_ref2 = this.getLineAndColumnFromChunk(offsetInChunk), locationData.first_line = _ref2[0], locationData.first_column = _ref2[1];
_ref3 = this.getLineAndColumnFromChunk(offsetInChunk + length), locationData.last_line = _ref3[0], locationData.last_column = _ref3[1];
lastCharacter = length > 0 ? length - 1 : 0;
_ref3 = this.getLineAndColumnFromChunk(offsetInChunk + (length - 1)), locationData.last_line = _ref3[0], locationData.last_column = _ref3[1];
token = [tag, value, locationData];
return token;
};

View File

@ -645,8 +645,12 @@ exports.Lexer = class Lexer
locationData = {}
[locationData.first_line, locationData.first_column] =
@getLineAndColumnFromChunk offsetInChunk
# Use length - 1 for the final offset - we're supplying the last_line and the last_column,
# so if last_column == first_column, then we're looking at a character of length 1.
lastCharacter = if length > 0 then (length - 1) else 0
[locationData.last_line, locationData.last_column] =
@getLineAndColumnFromChunk offsetInChunk + length
@getLineAndColumnFromChunk offsetInChunk + (length - 1)
token = [tag, value, locationData]

View File

@ -16,10 +16,28 @@ x = () ->
'''
test "Verify location of generated tokens", ->
tokens = CoffeeScript.tokens "a = 7"
tokens = CoffeeScript.tokens "a = 79"
eq tokens.length, 4
a = tokens[0]
aToken = tokens[0]
eq aToken[2].first_line, 0
eq aToken[2].first_column, 0
eq aToken[2].last_line, 0
eq aToken[2].last_column, 0
equalsToken = tokens[1]
eq equalsToken[2].first_line, 0
eq equalsToken[2].first_column, 2
eq equalsToken[2].last_line, 0
eq equalsToken[2].last_column, 2
numberToken = tokens[2]
eq numberToken[2].first_line, 0
eq numberToken[2].first_column, 4
eq numberToken[2].last_line, 0
eq numberToken[2].last_column, 5
test "Verify all tokens get a location", ->
doesNotThrow ->