forked from apache/cordova-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d4f9fe
commit d78b190
Showing
8 changed files
with
405 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, | ||
software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations | ||
under the License. | ||
*/ | ||
'use strict'; | ||
|
||
var fs = require('fs'); | ||
var CordovaError = require('cordova-common').CordovaError; | ||
|
||
function BridgingHeader (bridgingHeaderPath) { | ||
this.path = bridgingHeaderPath; | ||
this.bridgingHeaders = null; | ||
if (!fs.existsSync(this.path)) { | ||
throw new CordovaError('BridgingHeader.js is not found.'); | ||
} | ||
this.bridgingHeaders = this.__parseForBridgingHeader(fs.readFileSync(this.path, 'utf8')); | ||
} | ||
|
||
BridgingHeader.prototype.addHeader = function (plugin_id, header_path) { | ||
this.bridgingHeaders.push({type: 'code', code: '#import "' + header_path + '"\n'}); | ||
}; | ||
|
||
BridgingHeader.prototype.removeHeader = function (plugin_id, header_path) { | ||
this.bridgingHeaders = this.bridgingHeaders.filter(function (line) { | ||
if (this.found) { | ||
return true; | ||
} | ||
if (line.type === 'code') { | ||
var re = new RegExp('#import\\s+"' + preg_quote(header_path) + '"(\\s*|\\s.+)(\\n|$)'); | ||
if (re.test(line.code)) { | ||
this.found = true; | ||
return false; | ||
} | ||
} | ||
return true; | ||
}, {found: false}); | ||
}; | ||
|
||
BridgingHeader.prototype.write = function () { | ||
var text = this.__stringifyForBridgingHeader(this.bridgingHeaders); | ||
fs.writeFileSync(this.path, text, 'utf8'); | ||
}; | ||
|
||
BridgingHeader.prototype.__stringifyForBridgingHeader = function (bridgingHeaders) { | ||
return bridgingHeaders.map(function (obj) { | ||
return obj.code; | ||
}).join(''); | ||
}; | ||
|
||
BridgingHeader.prototype.__parseForBridgingHeader = function (text) { | ||
var i = 0; | ||
var list = []; | ||
var type = 'code'; | ||
var start = 0; | ||
while (i < text.length) { | ||
switch (type) { | ||
case 'comment': | ||
if (i + 1 < text.length && text[i] === '*' && text[i + 1] === '/') { | ||
i += 2; | ||
list.push({type: type, code: text.slice(start, i)}); | ||
type = 'code'; | ||
start = i; | ||
} else { | ||
i += 1; | ||
} | ||
break; | ||
case 'line-comment': | ||
if (i < text.length && text[i] === '\n') { | ||
i += 1; | ||
list.push({type: type, code: text.slice(start, i)}); | ||
type = 'code'; | ||
start = i; | ||
} else { | ||
i += 1; | ||
} | ||
break; | ||
case 'code': | ||
default: | ||
if (i + 1 < text.length && text[i] === '/' && text[i + 1] === '*') { // comment | ||
if (start < i) { | ||
list.push({type: type, code: text.slice(start, i)}); | ||
} | ||
type = 'comment'; | ||
start = i; | ||
} else if (i + 1 < text.length && text[i] === '/' && text[i + 1] === '/') { // line comment | ||
if (start < i) { | ||
list.push({type: type, code: text.slice(start, i)}); | ||
} | ||
type = 'line-comment'; | ||
start = i; | ||
} else if (i < text.length && text[i] === '\n') { | ||
i += 1; | ||
list.push({type: type, code: text.slice(start, i)}); | ||
start = i; | ||
} else { | ||
i += 1; | ||
} | ||
break; | ||
} | ||
} | ||
if (start < i) { | ||
list.push({type: type, code: text.slice(start, i)}); | ||
} | ||
return list; | ||
}; | ||
|
||
function preg_quote (str, delimiter) { | ||
return (str + '').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&'); | ||
} | ||
|
||
module.exports.BridgingHeader = BridgingHeader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
|
||
var BridgingHeader = require(path.resolve(path.join(__dirname, '..', '..', '..', 'bin', 'templates', 'scripts', 'cordova', 'lib', 'BridgingHeader.js'))).BridgingHeader; | ||
var fixtureBridgingHeader = fs.readFileSync(path.resolve(__dirname, 'fixtures', 'test-Bridging-Header.h'), 'utf-8'); | ||
|
||
describe('unit tests for BridgingHeader module', function () { | ||
var existsSyncSpy; | ||
var readFileSyncSpy; | ||
var writeFileSyncSpy; | ||
var dummy_path = 'dummy_path'; | ||
var dummy_plugin = { id: 'dummy_plugin', header_path: 'dummy_header_path' }; | ||
var dummy_plugin2 = { id: 'dummy_plugin2', header_path: 'dummy_header_path2' }; | ||
var headerImportText = function (header_path) { return '#import "' + header_path + '"'; }; | ||
|
||
beforeEach(function () { | ||
existsSyncSpy = spyOn(fs, 'existsSync'); | ||
readFileSyncSpy = spyOn(fs, 'readFileSync'); | ||
writeFileSyncSpy = spyOn(fs, 'writeFileSync'); | ||
}); | ||
it('Test#001 : should error if BridgingHeader file does not exist', function () { | ||
existsSyncSpy.and.returnValue(false); | ||
expect(function () { | ||
var _ = new BridgingHeader(fixtureBridgingHeader); | ||
expect(_).not.toEqual(null); // To avoid ESLINT error "Do not use 'new' for side effects" | ||
}).toThrow(); | ||
}); | ||
it('Test#002 : load BridgingHeader file', function () { | ||
existsSyncSpy.and.returnValue(true); | ||
readFileSyncSpy.and.returnValue(fixtureBridgingHeader); | ||
|
||
var bridgingHeader = new BridgingHeader(dummy_path); | ||
expect(bridgingHeader.path).toEqual(dummy_path); | ||
expect(bridgingHeader).not.toEqual(null); | ||
}); | ||
it('Test#003 : add and remove a BridgingHeader', function () { | ||
var result_json = null; | ||
var text_list = null; | ||
var bridgingHeaderFileContent = fixtureBridgingHeader; | ||
existsSyncSpy.and.returnValue(true); | ||
readFileSyncSpy.and.callFake(function (read_path, charset) { | ||
return bridgingHeaderFileContent; | ||
}); | ||
writeFileSyncSpy.and.callFake(function (write_path, text, charset) { | ||
result_json = {write_path: write_path, text: text, charset: charset}; | ||
}); | ||
|
||
var bridgingHeader = new BridgingHeader(dummy_path); | ||
bridgingHeader.addHeader(dummy_plugin.id, dummy_plugin.header_path); | ||
bridgingHeader.write(); | ||
expect(result_json).not.toEqual(null); | ||
expect(result_json.write_path).toEqual(dummy_path); | ||
expect(result_json.text).not.toEqual(null); | ||
expect(result_json.charset).toEqual('utf8'); | ||
text_list = result_json.text.split('\n'); | ||
expect(text_list.filter(function (line) { return line === headerImportText(dummy_plugin.header_path); }).length).toEqual(1); | ||
|
||
bridgingHeader = new BridgingHeader(dummy_path); | ||
bridgingHeader.removeHeader(dummy_plugin.id, dummy_plugin.header_path); | ||
bridgingHeader.write(); | ||
expect(result_json).not.toEqual(null); | ||
expect(result_json.write_path).toEqual(dummy_path); | ||
expect(result_json.text).not.toEqual(null); | ||
expect(result_json.charset).toEqual('utf8'); | ||
text_list = result_json.text.split('\n'); | ||
expect(text_list.filter(function (line) { return line === headerImportText(dummy_plugin.header_path); }).length).toEqual(0); | ||
}); | ||
it('Test#004 : add and remove two BridgingHeaders', function () { | ||
var result_json = null; | ||
var text_list = null; | ||
var bridgingHeaderFileContent = fixtureBridgingHeader; | ||
existsSyncSpy.and.returnValue(true); | ||
readFileSyncSpy.and.callFake(function (read_path, charset) { | ||
return bridgingHeaderFileContent; | ||
}); | ||
writeFileSyncSpy.and.callFake(function (write_path, text, charset) { | ||
bridgingHeaderFileContent = text; | ||
result_json = {write_path: write_path, text: text, charset: charset}; | ||
}); | ||
|
||
var bridgingHeader = new BridgingHeader(dummy_path); | ||
bridgingHeader.addHeader(dummy_plugin.id, dummy_plugin.header_path); | ||
bridgingHeader.write(); | ||
|
||
bridgingHeader = new BridgingHeader(dummy_path); | ||
bridgingHeader.addHeader(dummy_plugin2.id, dummy_plugin2.header_path); | ||
bridgingHeader.write(); | ||
|
||
text_list = result_json.text.split('\n'); | ||
expect(text_list.filter(function (line) { return line === headerImportText(dummy_plugin.header_path); }).length).toEqual(1); | ||
expect(text_list.filter(function (line) { return line === headerImportText(dummy_plugin2.header_path); }).length).toEqual(1); | ||
|
||
bridgingHeader = new BridgingHeader(dummy_path); | ||
bridgingHeader.removeHeader(dummy_plugin.id, dummy_plugin.header_path); | ||
bridgingHeader.write(); | ||
|
||
text_list = result_json.text.split('\n'); | ||
expect(text_list.filter(function (line) { return line === headerImportText(dummy_plugin.header_path); }).length).toEqual(0); | ||
expect(text_list.filter(function (line) { return line === headerImportText(dummy_plugin2.header_path); }).length).toEqual(1); | ||
|
||
bridgingHeader = new BridgingHeader(dummy_path); | ||
bridgingHeader.removeHeader(dummy_plugin2.id, dummy_plugin2.header_path); | ||
bridgingHeader.write(); | ||
|
||
text_list = result_json.text.split('\n'); | ||
expect(text_list.filter(function (line) { return line === headerImportText(dummy_plugin.header_path); }).length).toEqual(0); | ||
expect(text_list.filter(function (line) { return line === headerImportText(dummy_plugin2.header_path); }).length).toEqual(0); | ||
|
||
}); | ||
|
||
}); |
Oops, something went wrong.