michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 371932; michael@0: var summary = 'ES4 Regular Expression /y flag'; michael@0: var actual = ''; michael@0: var expect = ''; michael@0: michael@0: print('See http://developer.mozilla.org/es4/proposals/extend_regexps.html#y_flag'); michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: function test() michael@0: { michael@0: enterFunc ('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: michael@0: var c; michael@0: var s = '123456'; michael@0: michael@0: print('Test global flag.'); michael@0: michael@0: var g = /(1)/g; michael@0: expect = 'captures: 1,1; RegExp.leftContext: ""; RegExp.rightContext: "234561"'; michael@0: actual = 'captures: ' + g.exec('1234561') + michael@0: '; RegExp.leftContext: "' + RegExp.leftContext + michael@0: '"; RegExp.rightContext: "' + RegExp.rightContext + '"'; michael@0: reportCompare(expect, actual, summary + ' - /(1)/g.exec("1234561") first call'); michael@0: michael@0: expect = 'captures: 1,1; RegExp.leftContext: "123456"; RegExp.rightContext: ""'; michael@0: actual = 'captures: ' + g.exec('1234561') + michael@0: '; RegExp.leftContext: "' + RegExp.leftContext + michael@0: '"; RegExp.rightContext: "' + RegExp.rightContext + '"'; michael@0: reportCompare(expect, actual, summary + ' - /(1)/g.exec("1234561") second call'); michael@0: var y = /(1)/y; michael@0: michael@0: print('Test sticky flag.'); michael@0: michael@0: /* michael@0: * calls to reportCompare invoke regular expression matches which interfere michael@0: * with the test of the sticky flag. Collect expect and actual values prior michael@0: * to calling reportCompare. Note setting y = /(1)/y resets the lastIndex etc. michael@0: */ michael@0: michael@0: var y = /(1)/y; michael@0: var expect4 = 'captures: 1,1; RegExp.leftContext: ""; RegExp.rightContext: "234561"'; michael@0: var actual4 = 'captures: ' + y.exec('1234561') + michael@0: '; RegExp.leftContext: "' + RegExp.leftContext + michael@0: '"; RegExp.rightContext: "' + RegExp.rightContext + '"'; michael@0: michael@0: var expect5 = 'captures: null; RegExp.leftContext: ""; RegExp.rightContext: "234561"'; michael@0: var actual5 = 'captures: ' + y.exec('1234561') + michael@0: '; RegExp.leftContext: "' + RegExp.leftContext + michael@0: '"; RegExp.rightContext: "' + RegExp.rightContext + '"'; michael@0: michael@0: reportCompare(expect4, actual4, summary + ' - /(1)/y.exec("1234561") first call'); michael@0: reportCompare(expect5, actual5, summary + ' - /(1)/y.exec("1234561") second call'); michael@0: michael@0: var y = /(1)/y; michael@0: michael@0: reportCompare(expect5, actual5, summary); michael@0: michael@0: y = /(1)/y; michael@0: var expect6 = 'captures: 1,1; RegExp.leftContext: ""; RegExp.rightContext: "123456"'; michael@0: var actual6 = 'captures: ' + y.exec('1123456') + michael@0: '; RegExp.leftContext: "' + RegExp.leftContext + michael@0: '"; RegExp.rightContext: "' + RegExp.rightContext + '"'; michael@0: michael@0: var expect7 = 'captures: 1,1; RegExp.leftContext: "1"; RegExp.rightContext: "23456"'; michael@0: var actual7 = 'captures: ' + y.exec('1123456') + michael@0: '; RegExp.leftContext: "' + RegExp.leftContext + michael@0: '"; RegExp.rightContext: "' + RegExp.rightContext + '"'; michael@0: michael@0: reportCompare(expect6, actual6, summary + ' - /(1)/y.exec("1123456") first call'); michael@0: reportCompare(expect7, actual7, summary + ' - /(1)/y.exec("1123456") second call'); michael@0: michael@0: var y = /(1)/y; michael@0: michael@0: reportCompare(expect, actual, summary); michael@0: michael@0: exitFunc ('test'); michael@0: }