Skip to content

Commit

Permalink
Load Gherkin module without any hack in browsers
Browse files Browse the repository at this point in the history
All Gherkin languages are now potentially supported by Cucumber.js in browsers.
  • Loading branch information
jbpros committed Jul 19, 2011
1 parent b96e613 commit 4090a2f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
1 change: 0 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<title>Cucumber.js</title>
<link href="example.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="vendor/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="vendor/gherkin.en.js"></script>
<script type="text/javascript" src="/browserify.js"></script>
<script type="text/javascript" src="/example.js"></script>
</head>
Expand Down
6 changes: 3 additions & 3 deletions example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ var server = connect.createServer();

server.use(connect.static(__dirname));
server.use(require('browserify')({
require : __dirname + '/../lib/cucumber.js'
require: {'cucumber': './lib/cucumber.js',
'./gherkin/lexer/en': 'gherkin/lib/gherkin/lexer/en'}
}));

server.listen(9797);
console.log('Listening on 9797...');
console.log('Listening on port 9797...');
2 changes: 1 addition & 1 deletion lib/cucumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ Cucumber.Listener = require('./cucumber/listener');
Cucumber.Type = require('./cucumber/type');
Cucumber.Util = require('./cucumber/util');
Cucumber.Debug = require('./cucumber/debug'); // Untested namespace
module.exports = Cucumber;
module.exports = Cucumber;
13 changes: 5 additions & 8 deletions lib/cucumber/parser.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
var Parser = function(featuresSource) {
// We currently only support the English lexer because we don't know yet
// how to load different modules based on a string in browserify (i.e.
// Gherkin does not load properly under browserify).
var GherkinEnglishLexer = require('gherkin/lib/gherkin/lexer/en');
var Cucumber = require('../cucumber');
var Gherkin = require('gherkin');
var Cucumber = require('../cucumber');

var features = Cucumber.Ast.Features();

var self = {
parse: function parse() {
var eventHandlers = self.getEventHandlers();
var lexer = new GherkinEnglishLexer.Lexer(eventHandlers);
var Lexer = Gherkin.Lexer('en');
var lexer = new Lexer(self.getEventHandlers());
lexer.scan(featuresSource);
return features;
},
Expand Down Expand Up @@ -66,4 +63,4 @@ var Parser = function(featuresSource) {
};
return self;
};
module.exports = Parser;
module.exports = Parser;
16 changes: 10 additions & 6 deletions spec/cucumber/parser_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ describe("Cucumber.Parser", function() {
});

describe("parse()", function() {
var GherkinEnglishLexer;
var Gherkin = require('gherkin');
var gherkinLexerConstructor, gherkinLexer;
var eventHandlers;

beforeEach(function() {
gherkinLexer = createSpyWithStubs("Gherkin lexer instance", {scan: null});
gherkinLexerConstructor = createSpy("Gherkin lexer constructor").andReturn(gherkinLexer);
GherkinEnglishLexer = require('gherkin/lib/gherkin/lexer/en');
eventHandlers = createSpy("Parser event handlers");
GherkinEnglishLexer.Lexer = gherkinLexerConstructor;
gherkinLexer = createSpyWithStubs("Gherkin lexer instance", {scan: null});
gherkinLexerConstructor = createSpy("Gherkin lexer module").andReturn(gherkinLexer);
eventHandlers = createSpy("Parser event handlers");
spyOn(Gherkin, 'Lexer').andReturn(gherkinLexerConstructor);
spyOn(parser, 'getEventHandlers').andReturn(eventHandlers);
});

it("loads the gherkin lexer module for English", function() {
parser.parse();
expect(Gherkin.Lexer).toHaveBeenCalledWith('en');
});

it("gets the parse event handlers", function() {
parser.parse();
expect(parser.getEventHandlers).toHaveBeenCalled();
Expand Down

0 comments on commit 4090a2f

Please sign in to comment.