Skip to content

Commit

Permalink
Handle BOM. Also fix regexp for hyphenated languages. Fixes #144.
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Jan 2, 2015
1 parent 0615b9e commit bb60dbb
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 23 deletions.
53 changes: 53 additions & 0 deletions features/with_bom.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Feature: User logs into the system
In order to be able to use eraNET components
As a user
I want to log in to the system


Scenario: Unlogged user sees welcome page with login
Given I have not logged or have logged out before
When I visit initial page
Then Default app should be loaded
And I should see login request
And I should not see any username


Scenario: Minimal user sees welcome page with its username and logout
Given I have logged as guest named "Guest"
When I visit initial page
Then Default app should be loaded
And I should see logout request
And I should see "Guest" as username


Scenario: Unlogged user logs in
Given I have not logged or have logged out before
And I have visited initial page
And I have seen login request
When I ask to log in
Then I should be taken to login page


Scenario: Logged user logs out
Given I have logged as guest named "Guest"
And I have visited initial page
And I have seen logout request
When I ask to log out
Then I should be taken to logout page


Scenario: Disconnected user sees welcome page and reconnect option
Given I have had a broken connection with api site
When I visit initial page
Then Default app should be loaded
And I should see reconnect request
And I should not see any username


Scenario: User sees 'connecting' while connecting
Given I have had lagging api site
When I visit initial page
Then I should see connecting message
And I should not see any username


19 changes: 19 additions & 0 deletions lib/cucumber/gherkin_lexer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var gherkin = require('gherkin');

/**
* Small wrapper around Gherkin that strips BOM and loads the correct lexer based
* on the language header.
*/
module.exports = function GherkinLexer(content, gherkinListener) {
// Strip BOM
content = content.replace(/^\ufeff/g, '');

var languageMatch = /^\s*#\s*language:\s*([a-zA-Z-]+)\s*$/m.exec(content);
var language = languageMatch == null ? 'en' : languageMatch[1].toLowerCase();
var Lexer = gherkin.Lexer(language);
var lexer = new Lexer(gherkinListener);

this.scan = function() {
lexer.scan(content);
}
};
16 changes: 3 additions & 13 deletions lib/cucumber/parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Parser = function(featureSources, astFilter) {
var Gherkin = require('gherkin');
var GherkinLexer = require('./gherkin_lexer');
var Cucumber = require('../cucumber');

var features = Cucumber.Ast.Features();
Expand All @@ -8,22 +8,12 @@ var Parser = function(featureSources, astFilter) {

var self = {
parse: function parse() {
var lexers = {};
var lexer = function (lang) {
if (!(lang in lexers)) {
lexers[lang] = new (Gherkin.Lexer(lang))(self.getEventHandlers());
}

return lexers[lang];
};

for (i in featureSources) {
var currentSourceUri = featureSources[i][Parser.FEATURE_NAME_SOURCE_PAIR_URI_INDEX];
var featureSource = featureSources[i][Parser.FEATURE_NAME_SOURCE_PAIR_SOURCE_INDEX];
self.setCurrentSourceUri(currentSourceUri);
var languageMatch = /^\s*#\s*language:\s*([a-z_]*)/.exec(featureSource.toString());
var language = languageMatch == null ? 'en' : languageMatch[1];
lexer(language).scan(featureSource);
var lexer = new GherkinLexer(featureSource.toString(), self.getEventHandlers());
lexer.scan();
}
return features;
},
Expand Down
10 changes: 0 additions & 10 deletions spec/cucumber/parser_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,6 @@ describe("Cucumber.Parser", function () {
expect(parser.setCurrentSourceUri).toHaveBeenCalledWith(featureSources[1][0]);
});

it("asks the English lexer to scan the first feature source", function () {
parser.parse();
expect(gherkinENLexer.scan).toHaveBeenCalledWith(featureSources[0][1]);
});

it("asks the French lexer to scan the second feature source", function () {
parser.parse();
expect(gherkinFRLexer.scan).toHaveBeenCalledWith(featureSources[1][1]);
});

it("returns the features root element", function () {
expect(parser.parse()).toBe(features);
});
Expand Down

4 comments on commit bb60dbb

@jbpros
Copy link
Member

@jbpros jbpros commented on bb60dbb Jan 2, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Aslak. Unfortunately, this commit broke the build.

@aslakhellesoy
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that. I thought all tests were passing locally. Investigating...

@aslakhellesoy
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by 4ac467e

@jbpros
Copy link
Member

@jbpros jbpros commented on bb60dbb Jan 4, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ace! thanks

Please sign in to comment.