UNPKG

5.64 kBJavaScriptView Raw
1'use strict';
2
3const assert = require('assert');
4const common = require('../common');
5const httpsnippetGenerator = require('../httpsnippetGenerator');
6const operation = require('./operationFixture');
7
8const sampleData = {
9 header: {
10 language_tabs: [
11 'csharp',
12 { php: 'PHP' }
13 ]
14 },
15 operation: clone(operation),
16 options: {}
17};
18
19const xCodeSamples = [
20 {
21 lang: 'C#',
22 source: `
23 PetStore.v1.Pet pet = new PetStore.v1.Pet();
24 pet.setApiKey("your api key");
25 pet.petType = PetStore.v1.Pet.TYPE_DOG;
26 pet.name = "Rex";
27 // set other fields
28 PetStoreResponse response = pet.create();
29 if (response.statusCode == HttpStatusCode.Created)
30 {
31 // Successfully created
32 }
33 else
34 {
35 // Something wrong -- check response for errors
36 Console.WriteLine(response.getRawResponse());
37 }
38 `
39 },
40 {
41 lang: 'PHP',
42 source: `
43 $form = new \\PetStore\\Entities\\Pet();
44 $form->setPetType("Dog");
45 $form->setName("Rex");
46 // set other fields
47 try {
48 $pet = $client->pets()->create($form);
49 } catch (UnprocessableEntityException $e) {
50 var_dump($e->getErrors());
51 }
52 `
53 },
54 {
55 lang: 'sample',
56 source: 'sample code'
57 }
58];
59
60describe('getCodeSamples tests', () => {
61 let result, testData;
62
63 describe('when x-code-samples are included', () => {
64 beforeEach(() => {
65 testData = clone(sampleData);
66 testData.operation['x-code-samples'] = clone(xCodeSamples);
67
68 result = common.getCodeSamples(testData);
69 });
70
71 it('should generate code samples from x-code-samples content', () => {
72 const expected = `\`\`\`csharp\n${xCodeSamples[0].source}\n\`\`\`\n\n` +
73 `\`\`\`php\n${xCodeSamples[1].source}\n\`\`\`\n\n` +
74 `\`\`\`sample\n${xCodeSamples[2].source}\n\`\`\`\n\n`;
75
76 assert.equal(result, expected);
77 });
78
79 it('should add unknown x-code-samples language to language_tabs', () => {
80 const expectedLanguageTabs = [].concat(sampleData.header.language_tabs, { sample: 'sample' });
81
82 console.log(expectedLanguageTabs);
83
84 assert.deepEqual(testData.header.language_tabs, expectedLanguageTabs);
85 });
86
87 it('should return empty string if no code samples can be generated', () => {
88 testData.operation['x-code-samples'] = [];
89
90 result = common.getCodeSamples(testData);
91
92 assert.equal(result, '');
93 });
94 });
95
96 describe('when x-code-samples are not available', () => {
97 let originalHttpsnippetGenerator;
98
99 beforeEach(() => {
100 testData = clone(sampleData);
101 testData.templates = {
102 code_csharp: () => `csharp-sample`,
103 code_php: () => `php-sample`,
104 code_nodejs: () => `nodejs-sample`,
105 code_unknown: () => ''
106 };
107
108 originalHttpsnippetGenerator = httpsnippetGenerator.generate;
109 httpsnippetGenerator.generate = (target, client, data) => `httpsnippet-${target}-${client}-sample`;
110 });
111
112 afterEach(() => {
113 httpsnippetGenerator.generate = originalHttpsnippetGenerator;
114 });
115
116 it('should generate code samples using template files by default', () => {
117 const expected = `\`\`\`csharp\ncsharp-sample\n\`\`\`\n\n` +
118 `\`\`\`php\nphp-sample\n\`\`\`\n\n`;
119
120 result = common.getCodeSamples(testData);
121
122 assert.deepEqual(result, expected);
123 });
124
125 it('should use httpsnippet to generate code samples if it is active', () => {
126 const testData = clone(sampleData);
127 testData.options = { httpsnippet: true };
128 const expected = `\`\`\`csharp\nhttpsnippet-csharp--sample\n\`\`\`\n\n` +
129 `\`\`\`php\nhttpsnippet-php--sample\n\`\`\`\n\n`;
130
131 result = common.getCodeSamples(testData);
132
133 assert.deepEqual(result, expected);
134 });
135
136 it('should support specifying language custom target name', () => {
137 testData.header.language_tabs[0] = 'javascript--nodejs';
138 const expected = `\`\`\`javascript--nodejs\nnodejs-sample\n\`\`\`\n\n` +
139 `\`\`\`php\nphp-sample\n\`\`\`\n\n`;
140
141 result = common.getCodeSamples(testData);
142
143 assert.deepEqual(result, expected);
144 });
145
146 it('should support specifying language custom client name', () => {
147 const testData = clone(sampleData);
148 testData.options = { httpsnippet: true };
149 testData.header.language_tabs[0] = 'javascript--nodejs';
150 testData.options.language_clients = [ { 'javascript--nodejs': 'request' } ];
151 const expected = `\`\`\`javascript--nodejs\nhttpsnippet-nodejs-request-sample\n\`\`\`\n\n` +
152 `\`\`\`php\nhttpsnippet-php--sample\n\`\`\`\n\n`;
153
154 result = common.getCodeSamples(testData);
155
156 assert.deepEqual(result, expected);
157 });
158
159 it('should return empty string if no code samples can be generated', () => {
160 testData.header.language_tabs = ['unknown'];
161
162 result = common.getCodeSamples(testData);
163
164 assert.deepEqual(result, '');
165 });
166 });
167});
168
169function clone(obj) {
170 return JSON.parse(JSON.stringify(obj));
171}