Skip to content

test against reference #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pull requests are welcome, but please update the documentation and tests. The tests are executed by running the default `grunt` task. The tests will probably fail if your changes affect the generated output - please review the differences and when you're happy _temporarily_ edit the `if (false)` in [tests/generation.js#verify()](blob/master/tests/generation.js#L15) and re-run the tests.
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,7 @@ methods:
description: true if method === 'GET'
summary:
type: string
description: Provided by the 'description' or 'summary' field in the schema
externalDocs:
type: object
properties:
url:
type: string
description: The URL for the target documentation. Value MUST be in the format of a URL.
required: true
description:
type: string
description: A short description of the target documentation. GitHub-Markdown syntax can be used for rich text representation.
description: Provided by the 'description' field in the schema
isSecure:
type: boolean
description: true if the 'security' is defined for the method in the schema
Expand Down Expand Up @@ -220,3 +210,6 @@ And example of gulp task is available [here](https://github.com/wcandillon/cells
The [CellStore](https://github.com/wcandillon/cellstore) project.

[28.io](http://28.io) is using this project to generate their [nodejs](https://github.com/28msec/28.io-nodejs) and [angularjs language bindings](https://github.com/28msec/28.io-angularjs).

##Contributing
Pull requests welcome, refer to the [contribution guidelines](CONTRIBUTING.md)
5 changes: 2 additions & 3 deletions lib/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ var getViewForSwagger2 = function(opts, type){
methodName: op.operationId ? normalizeName(op.operationId) : getPathToMethodName(opts, m, path),
method: m.toUpperCase(),
isGET: m.toUpperCase() === 'GET',
summary: op.description || op.summary,
externalDocs: op.externalDocs,
summary: op.description,
isSecure: swagger.security !== undefined || op.security !== undefined,
parameters: [],
headers: []
Expand Down Expand Up @@ -190,7 +189,7 @@ var getViewForSwagger1 = function(opts, type){

headers.name = 'Accept';
headers.value.push(op.produces.map(function(value) { return '\'' + value + '\''; }).join(', '));

method.headers.push(headers);
}

Expand Down
4 changes: 1 addition & 3 deletions templates/typescript-method.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ $queryParameters?: {}
/**
* {{&summary}}
* @method
{{#externalDocs}}
* @see {@link {{&url}}|{{#description}}{{&description}}{{/description}}{{^description}}External docs{{/description}}}
{{/externalDocs}}
* @name {{&className}}#{{&methodName}}
{{#parameters}}
{{^isSingleton}} * @param {{=<% %>=}}{<%&type%>}<%={{ }}=%> {{&camelCaseName}} - {{&description}}{{/isSingleton}}
{{/parameters}}
*
*/
{{&methodName}}(parameters: {
{{#parameters}}{{^isSingleton}}'{{&camelCaseName}}'{{&cardinality}}: {{> type}},
Expand Down
23 changes: 16 additions & 7 deletions tests/generation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ var CodeGen = require('../lib/codegen').CodeGen;

var batch = {};
var list = ffs.readdirSync('tests/apis');

function verify(result, file, type) {
var reference = file.replace(/^tests\/apis\//, 'tests/reference/' + type + '_');
if (false) {
fs.writeFileSync(reference, result, 'UTF-8');
} else {
assert.equal(result, fs.readFileSync(reference, 'UTF-8'), file + ', ', type);
}
}

list.forEach(function(file){
file = 'tests/apis/' + file;
batch[file] = function(){
Expand All @@ -17,30 +27,29 @@ list.forEach(function(file){
className: 'Test',
swagger: swagger
});
assert(typeof(result), 'string');
verify(result, file, 'node');
result = CodeGen.getAngularCode({
moduleName: 'Test',
className: 'Test',
swagger: swagger
});
assert(typeof(result), 'string');
verify(result, file, 'angular');
result = CodeGen.getAngularCode({
moduleName: 'Test',
className: 'Test',
swagger: swagger,
lint: false,
beautify: false
});
assert(typeof(result), 'string');
assert(typeof(result), 'string');
verify(result, file, 'angular-ugly');
if(swagger.swagger === '2.0') {
result = CodeGen.getTypescriptCode({
moduleName: 'Test',
className: 'Test',
swagger: swagger,
lint: false
});
assert(typeof(result), 'string');
verify(result, file, 'typescript');
}
result = CodeGen.getCustomCode({
moduleName: 'Test',
Expand All @@ -51,7 +60,7 @@ list.forEach(function(file){
method: fs.readFileSync(__dirname + '/../templates/method.mustache', 'utf-8')
}
});
assert(typeof(result), 'string');
verify(result, file, 'custom');
};
});
vows.describe('Test Generation').addBatch(batch).export(module);
vows.describe('Test Generation').addBatch(batch).export(module);
218 changes: 218 additions & 0 deletions tests/reference/angular-ugly__batch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/*jshint -W069 */
/*global angular:false */
angular.module('Test', [])
.factory('Test', ['$q', '$http', '$rootScope', function($q, $http, $rootScope){
'use strict';

/**
* <p>These resources can be used to perform batch operations. The endpoint of these resources is based on your project name. For instance, if your 28.io project is named <code>myproject</code>, your endpoint for this API will be will be: <code>http://myproject.28.io/v1/_batch</code>.</p>
* @class Test
* @param {(string|object)} [domainOrOptions] - The project domain or options object. If object, see the object's optional properties.
* @param {string} [domainOrOptions.domain] - The project domain
* @param {string} [domainOrOptions.cache] - An angularjs cache implementation
* @param {object} [domainOrOptions.token] - auth token - object with value property and optional headerOrQueryName and isQuery properties
* @param {string} [cache] - An angularjs cache implementation
*/
var Test = (function(){
function Test(options, cache){
var domain = (typeof options === 'object') ? options.domain : options;
this.domain = typeof(domain) === 'string' ? domain : 'http://portal.28.io/v1';
if(this.domain.length === 0) {
throw new Error('Domain parameter must be specified as a string.');
}
cache = cache || ((typeof options === 'object') ? options.cache : cache);
this.cache = cache;
}

Test.prototype.$on = function($scope, path, handler){
var url = this.domain + path;
$scope.$on(url, function(){
handler();
});
return this;
};

Test.prototype.$broadcast = function(path){
var url = this.domain + path;
//cache.remove(url);
$rootScope.$broadcast(url);
return this;
};

Test.transformRequest = function(obj) {
var str = [];
for(var p in obj) {
var val = obj[p];
if(angular.isArray(val)) {
val.forEach(function(val){
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
});
} else {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
}
}
return str.join("&");
};


/**
* Import project contents from an archive
* @method
* @name Test#importProject
* @param {string} url - The archive url.
* @param {string} archive - The archive contents.
* @param {string} root - The path inside the archive that contains the modules and queries folders. Use '/' as folder separator.
* @param {string} overwrite - Whether to overwrite current project queries and modules. Default is true.
* @param {boolean} deleteOrphaned - Whether to delete orphaned file or not. Default is false.
* @param {boolean} simulate - Whether to simulate the operation or not. Default is false.
* @param {string} token - A project token.
* @param {string} contentType - <p>These resources can be used to perform batch operations. The endpoint of these resources is based on your project name. For instance, if your 28.io project is named <code>myproject</code>, your endpoint for this API will be will be: <code>http://myproject.28.io/v1/_batch</code>.</p>
*
*/
Test.prototype.importProject = function(parameters){
if(parameters === undefined) {
parameters = {};
}
var deferred = $q.defer();

var domain = this.domain;
var path = '/_batch/project';

var body;
var queryParameters = {};
var headers = {};
var form = {};




if(parameters['url'] !== undefined){
queryParameters['url'] = parameters['url'];
}










if(parameters['archive'] !== undefined){
body = parameters['archive'];
}




if(parameters['root'] !== undefined){
queryParameters['root'] = parameters['root'];
}







if(parameters['overwrite'] !== undefined){
queryParameters['overwrite'] = parameters['overwrite'];
}







if(parameters['deleteOrphaned'] !== undefined){
queryParameters['delete-orphaned'] = parameters['deleteOrphaned'];
}







if(parameters['simulate'] !== undefined){
queryParameters['simulate'] = parameters['simulate'];
}







if(parameters['token'] !== undefined){
queryParameters['token'] = parameters['token'];
}





if(parameters['token'] === undefined){
deferred.reject(new Error('Missing required query parameter: token'));
return deferred.promise;
}




if(parameters['contentType'] !== undefined){
headers['Content-Type'] = parameters['contentType'];
}





if(parameters.$queryParameters) {
Object.keys(parameters.$queryParameters)
.forEach(function(parameterName){
var parameter = parameters.$queryParameters[parameterName];
queryParameters[parameterName] = parameter;
});
}

var url = domain + path;
var options = {
timeout: parameters.$timeout,
method: 'PUT',
url: url,
params: queryParameters,
data: body,
headers: headers
};
if(Object.keys(form).length > 0) {
options.data = form;
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
options.transformRequest = Test.transformRequest;
}
$http(options)
.success(function(data, status, headers, config){
deferred.resolve(data);
if(parameters.$cache !== undefined) {
parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
}
})
.error(function(data, status, headers, config){
deferred.reject({
status: status,
headers: headers,
config: config,
body: data
});
});

return deferred.promise;
};

return Test;
})();

return Test;
}]);
Loading