Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
107 changes: 49 additions & 58 deletions scripts/specs/__tests__/snippets.test.ts
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test was broken and not testing anything

Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import { generateSnippetsJSON } from '../snippets.ts';
import type { CodeSamples } from '../types.ts';

describe('init', () => {
it('parses a multi line import', () => {
it('parses a multi line init', () => {
expect(
JSON.stringify(
generateSnippetsJSON({
foo: {
default: `
csharp: {
ope: {
default: `
// Initialize the client
var client = new QuerySuggestionsClient(
new QuerySuggestionsConfig("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION")
new QuerySuggestionsConfig(
"ALGOLIA_APPLICATION_ID",
"ALGOLIA_API_KEY",
"ALGOLIA_APPLICATION_REGION"
)
);

// Call the API
Expand All @@ -38,63 +43,60 @@ var response = await client.CreateConfigAsync(
}
);
// >LOG
// print the response
Console.WriteLine(response);
`,
},
},
} as unknown as CodeSamples),
null,
2,
),
).toMatchInlineSnapshot(`
"{
"foo": {
"default": "\\n // Initialize the client\\nvar client = new QuerySuggestionsClient(\\n new QuerySuggestionsConfig(\\"YOUR_APP_ID\\", \\"YOUR_API_KEY\\", \\"YOUR_APP_ID_REGION\\")\\n);\\n\\n// Call the API\\nvar response = await client.CreateConfigAsync(\\n new ConfigurationWithIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n SourceIndices = new List<SourceIndex>\\n {\\n new SourceIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n Facets = new List<Facet> { new Facet { Attribute = \\"test\\" } },\\n Generate = new List<List<string>>\\n {\\n new List<string> { \\"facetA\\", \\"facetB\\" },\\n new List<string> { \\"facetC\\" },\\n },\\n },\\n },\\n Languages = new Languages(new List<string> { \\"french\\" }),\\n Exclude = new List<string> { \\"test\\" },\\n }\\n);\\n// >LOG\\n "
"csharp": {
"ope": {
"default": "var response = await client.CreateConfigAsync(\\n new ConfigurationWithIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n SourceIndices = new List<SourceIndex>\\n {\\n new SourceIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n Facets = new List<Facet> { new Facet { Attribute = \\"test\\" } },\\n Generate = new List<List<string>>\\n {\\n new List<string> { \\"facetA\\", \\"facetB\\" },\\n new List<string> { \\"facetC\\" },\\n },\\n },\\n },\\n Languages = new Languages(new List<string> { \\"french\\" }),\\n Exclude = new List<string> { \\"test\\" },\\n }\\n);"
},
"init": {
"default": "var client = new QuerySuggestionsClient(\\n new QuerySuggestionsConfig(\\n \\"ALGOLIA_APPLICATION_ID\\",\\n \\"ALGOLIA_API_KEY\\",\\n \\"ALGOLIA_APPLICATION_REGION\\"\\n )\\n);"
}
}
}"
`);
});

it('parses a single line import', () => {
it('parses a single line init', () => {
expect(
JSON.stringify(
generateSnippetsJSON({
foo: {
default: `
csharp: {
ope: {
default: `
// Initialize the client
var client = new QuerySuggestionsClient(new Client("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION"));
var client = new QuerySuggestionsClient(new QuerySuggestionsConfig("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY", "ALGOLIA_APPLICATION_REGION"));

// Call the API
var response = await client.CreateConfigAsync(
new ConfigurationWithIndex
{
IndexName = "<YOUR_INDEX_NAME>",
SourceIndices = new List<SourceIndex>
{
new SourceIndex
{
IndexName = "<YOUR_INDEX_NAME>",
Facets = new List<Facet> { new Facet { Attribute = "test" } },
Generate = new List<List<string>>
{
new List<string> { "facetA", "facetB" },
new List<string> { "facetC" },
},
},
},
Languages = new Languages(new List<string> { "french" }),
Exclude = new List<string> { "test" },
}
);
var response = await client.CreateConfigAsync();
// >LOG
// print the response
Console.WriteLine(response);
`,
},
},
} as unknown as CodeSamples),
null,
2,
),
).toMatchInlineSnapshot(`
"{
"foo": {
"default": "\\n // Initialize the client\\nvar client = new QuerySuggestionsClient(new Client(\\"YOUR_APP_ID\\", \\"YOUR_API_KEY\\", \\"YOUR_APP_ID_REGION\\"));\\n\\n// Call the API\\nvar response = await client.CreateConfigAsync(\\n new ConfigurationWithIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n SourceIndices = new List<SourceIndex>\\n {\\n new SourceIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n Facets = new List<Facet> { new Facet { Attribute = \\"test\\" } },\\n Generate = new List<List<string>>\\n {\\n new List<string> { \\"facetA\\", \\"facetB\\" },\\n new List<string> { \\"facetC\\" },\\n },\\n },\\n },\\n Languages = new Languages(new List<string> { \\"french\\" }),\\n Exclude = new List<string> { \\"test\\" },\\n }\\n);\\n// >LOG\\n "
"csharp": {
"ope": {
"default": "var response = await client.CreateConfigAsync();"
},
"init": {
"default": "var client = new QuerySuggestionsClient(new QuerySuggestionsConfig(\\"ALGOLIA_APPLICATION_ID\\", \\"ALGOLIA_API_KEY\\", \\"ALGOLIA_APPLICATION_REGION\\"));"
}
}
}"
`);
Expand All @@ -106,46 +108,35 @@ describe('initialize', () => {
expect(
JSON.stringify(
generateSnippetsJSON({
foo: {
default: `
csharp: {
ope: {
default: `
// Initialize the client foo bar BAAAAAAAAAAAAAAAAAAAAAZ
var client = new QuerySuggestionsClient(
new QuerySuggestionsConfig("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION")
);

// Call the API
var response = await client.CreateConfigAsync(
new ConfigurationWithIndex
{
IndexName = "<YOUR_INDEX_NAME>",
SourceIndices = new List<SourceIndex>
{
new SourceIndex
{
IndexName = "<YOUR_INDEX_NAME>",
Facets = new List<Facet> { new Facet { Attribute = "test" } },
Generate = new List<List<string>>
{
new List<string> { "facetA", "facetB" },
new List<string> { "facetC" },
},
},
},
Languages = new Languages(new List<string> { "french" }),
Exclude = new List<string> { "test" },
}
);
var response = await client.CreateConfigAsync();
// >LOG
// print the response
Console.WriteLine(response);
`,
},
},
} as unknown as CodeSamples),
null,
2,
),
).toMatchInlineSnapshot(`
"{
"foo": {
"default": "\\n // Initialize the client foo bar BAAAAAAAAAAAAAAAAAAAAAZ\\nvar client = new QuerySuggestionsClient(\\n new QuerySuggestionsConfig(\\"YOUR_APP_ID\\", \\"YOUR_API_KEY\\", \\"YOUR_APP_ID_REGION\\")\\n);\\n\\n// Call the API\\nvar response = await client.CreateConfigAsync(\\n new ConfigurationWithIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n SourceIndices = new List<SourceIndex>\\n {\\n new SourceIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n Facets = new List<Facet> { new Facet { Attribute = \\"test\\" } },\\n Generate = new List<List<string>>\\n {\\n new List<string> { \\"facetA\\", \\"facetB\\" },\\n new List<string> { \\"facetC\\" },\\n },\\n },\\n },\\n Languages = new Languages(new List<string> { \\"french\\" }),\\n Exclude = new List<string> { \\"test\\" },\\n }\\n);\\n// >LOG\\n "
"csharp": {
"ope": {
"default": "var response = await client.CreateConfigAsync();"
},
"init": {
"default": "var client = new QuerySuggestionsClient(\\n new QuerySuggestionsConfig(\\"YOUR_APP_ID\\", \\"YOUR_API_KEY\\", \\"YOUR_APP_ID_REGION\\")\\n);"
}
}
}"
`);
Expand Down
11 changes: 11 additions & 0 deletions scripts/specs/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,16 @@ export async function transformGeneratedSnippetsToCodeSamples(clientName: string
JSON.stringify(jsonSnippets, null, 2),
);

// remove all the `>LOG` comments from the snippets
for (const lang of Object.keys(codeSamples) as Language[]) {
for (const operationId of Object.keys(codeSamples[lang])) {
for (const sampleName of Object.keys(codeSamples[lang][operationId])) {
codeSamples[lang][operationId][sampleName] = codeSamples[lang][operationId][sampleName]
.replace(/(#|\/\/) >LOG/g, '')
.trim();
}
}
}

return codeSamples;
}
4 changes: 4 additions & 0 deletions templates/csharp/snippets/method.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class Snippet{{client}}
// Call the API
{{#hasResponse}}var response = {{/hasResponse}}{{> tests/method}};
// >LOG
{{#hasResponse}}
// print the response
Console.WriteLine(response);
{{/hasResponse}}
// SEPARATOR<
}

Expand Down
4 changes: 4 additions & 0 deletions templates/dart/snippets/method.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ void snippetFor{{method}}{{testIndex}}() async {
{{/hasRequestOptions}}
);
// >LOG
{{#hasResponse}}
// print the response
print(response);
{{/hasResponse}}
// SEPARATOR<
}

Expand Down
2 changes: 1 addition & 1 deletion templates/go/snippets/method.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func SnippetFor{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}Of{{#lambda.p

// >LOG
{{#hasResponse}}
// use the model directly
// print the response
print(response)
{{/hasResponse}}
// SEPARATOR<
Expand Down
6 changes: 5 additions & 1 deletion templates/java/snippets/method.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ class Snippet{{client}} {
{{> snippets/init}}

// Call the API
{{> tests/method}};
{{#hasResponse}}{{{returnType}}} response ={{/hasResponse}}{{> tests/method}};
// >LOG
{{#hasResponse}}
// print the response
System.out.println(response);
{{/hasResponse}}
// SEPARATOR<
}

Expand Down
2 changes: 1 addition & 1 deletion templates/javascript/snippets/method.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export {{#isAsyncMethod}}async{{/isAsyncMethod}} function snippetFor{{#lambda.pa

// >LOG
{{#hasResponse}}
// use typed response
// print the response
console.log(response);
{{/hasResponse}}
// SEPARATOR<
Expand Down
2 changes: 1 addition & 1 deletion templates/kotlin/snippets/method.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Snippet{{client}} {

// >LOG
{{#hasResponse}}
// Use the response
// print the response
println(response)
{{/hasResponse}}
// SEPARATOR<
Expand Down
2 changes: 1 addition & 1 deletion templates/php/snippets/method.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Snippet{{client}}

// >LOG
{{#hasResponse}}
// play with the response
// print the response
var_dump($response);
{{/hasResponse}}
// SEPARATOR<
Expand Down
2 changes: 1 addition & 1 deletion templates/python/snippets/method.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def snippet_for_{{#lambda.snakecase}}{{method}}{{/lambda.snakecase}}{{testIndex}

# >LOG
{{#hasResponse}}
# use the class directly
# print the response
print(response)
{{/hasResponse}}
# SEPARATOR<
Expand Down
5 changes: 1 addition & 4 deletions templates/ruby/snippets/method.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ def snippet_for_{{#lambda.snakecase}}{{method}}{{/lambda.snakecase}}{{testIndex}

# >LOG
{{#hasResponse}}
# use the class directly
# print the response
puts response

# print the JSON response
puts response.to_json
{{/hasResponse}}
# SEPARATOR<
end
Expand Down
6 changes: 5 additions & 1 deletion templates/scala/snippets/method.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ class Snippet{{client}} {
Duration(100, "sec")
)
{{/isAsyncMethod}}
// >LOG
//
{{#hasResponse}}
// print the response
println(response)
{{/hasResponse}}
// SEPARATOR<
}

Expand Down
4 changes: 4 additions & 0 deletions templates/swift/snippets/method.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ final class {{client}}Snippet {
queryParameters: [{{#parametersWithDataType}}"{{key}}": {{> tests/paramValue }}{{^-last}}, {{/-last}}{{/parametersWithDataType}}]{{/requestOptions.queryParameters}}
){{/hasRequestOptions}})
// >LOG
{{#hasResponse}}
// print the response
print(response)
{{/hasResponse}}
// SEPARATOR<
}
{{/snippets}}
Expand Down