* Add a new template for examples section. Extract the examples section into a structured format for custom template processing. * Update the description IFF we were able to extract examples sections from it. * Update doc comments and add missing file header for the newly added file. * Make the example description readable. Add a check for empty example sections. * Add a chooser right below the Example Usage header. Remove javascript as a language. * Allow an empty new-line between short-codes boundaries.
136 lines
4.4 KiB
Go
136 lines
4.4 KiB
Go
// Copyright 2016-2020, Pulumi Corporation.
|
|
//
|
|
// Licensed 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.
|
|
|
|
package codegen
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestExtractExamplesSection(t *testing.T) {
|
|
t.Run("NonEmptyContent", func(t *testing.T) {
|
|
expectedContent := `
|
|
something here
|
|
|
|
and here
|
|
|
|
.... and there
|
|
|
|
..some..more here..
|
|
`
|
|
description := `{{% examples %}}` + expectedContent + `{{% /examples %}}`
|
|
|
|
actualContent := ExtractExamplesSection(description)
|
|
assert.NotNil(t, actualContent, "content could not be extracted")
|
|
assert.Equal(t, expectedContent, *actualContent, "strings don't match")
|
|
})
|
|
|
|
t.Run("EmptyContent", func(t *testing.T) {
|
|
description := `{{% examples %}}
|
|
{{% /examples %}}`
|
|
|
|
actualContent := ExtractExamplesSection(description)
|
|
assert.Nil(t, actualContent, "expected content to be nil")
|
|
})
|
|
}
|
|
|
|
var codeFence = "```"
|
|
|
|
func TestStripNonRelevantExamples(t *testing.T) {
|
|
tsCodeSnippet := `### Example 1
|
|
` + codeFence + `typescript
|
|
import * as path from path;
|
|
|
|
console.log("I am a console log statement in ts.");
|
|
` + codeFence
|
|
|
|
goCodeSnippet := `\n` + codeFence + `go
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func fakeFunc() {
|
|
fmt.Print("Hi, I am a fake func!")
|
|
}
|
|
` + codeFence
|
|
|
|
leadingDescription := "This is a leading description for this resource."
|
|
exampleShortCode := `{{% example %}}` + tsCodeSnippet + "\n" + goCodeSnippet + `{{% /example %}}`
|
|
description := leadingDescription + `
|
|
{{% examples %}}` + exampleShortCode + `
|
|
{{% /examples %}}`
|
|
|
|
t.Run("ContainsRelevantCodeSnippet", func(t *testing.T) {
|
|
strippedDescription := StripNonRelevantExamples(description, "typescript")
|
|
assert.NotEmpty(t, strippedDescription, "content could not be extracted")
|
|
assert.Contains(t, strippedDescription, leadingDescription, "expected to at least find the leading description")
|
|
})
|
|
|
|
// The above description does not contain a Python code snippet and because
|
|
// the description contains only one Example without any Python code snippet,
|
|
// we should expect an empty string in this test.
|
|
t.Run("DoesNotContainRelevantSnippet", func(t *testing.T) {
|
|
strippedDescription := StripNonRelevantExamples(description, "python")
|
|
assert.Contains(t, strippedDescription, leadingDescription, "expected to at least find the leading description")
|
|
// Should not contain any examples sections.
|
|
assert.NotContains(t, strippedDescription, "### ", "expected to not have any examples but found at least one")
|
|
})
|
|
}
|
|
|
|
func TestTestStripNonRelevantExamplesFromMultipleExampleSections(t *testing.T) {
|
|
tsCodeSnippet := codeFence + `typescript
|
|
import * as path from path;
|
|
|
|
console.log("I am a console log statement in ts.");
|
|
` + codeFence
|
|
|
|
goCodeSnippet := codeFence + `go
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func fakeFunc() {
|
|
fmt.Print("Hi, I am a fake func!")
|
|
}
|
|
` + codeFence
|
|
|
|
example1 := `### Example 1
|
|
` + tsCodeSnippet + "\n" + goCodeSnippet
|
|
|
|
example2 := `### Example 2
|
|
` + tsCodeSnippet
|
|
|
|
example1ShortCode := `{{% example %}}` + "\n" + example1 + "\n" + `{{% /example %}}`
|
|
example2ShortCode := `{{% example %}}` + "\n" + example2 + "\n" + `{{% /example %}}`
|
|
description := `{{% examples %}}` + "\n" + example1ShortCode + "\n" + example2ShortCode + "\n" + `{{% /examples %}}`
|
|
|
|
t.Run("EveryExampleHasRelevantCodeSnippet", func(t *testing.T) {
|
|
strippedDescription := StripNonRelevantExamples(description, "typescript")
|
|
assert.NotEmpty(t, strippedDescription, "content could not be extracted")
|
|
assert.Contains(t, strippedDescription, "Example 1", "expected Example 1 section")
|
|
assert.Contains(t, strippedDescription, "Example 2", "expected Example 2 section")
|
|
})
|
|
|
|
t.Run("SomeExamplesHaveRelevantCodeSnippet", func(t *testing.T) {
|
|
strippedDescription := StripNonRelevantExamples(description, "go")
|
|
assert.NotEmpty(t, strippedDescription, "content could not be extracted")
|
|
assert.Contains(t, strippedDescription, "Example 1", "expected Example 1 section")
|
|
assert.NotContains(t, strippedDescription, "Example 2",
|
|
"unexpected Example 2 section. section should have been excluded")
|
|
})
|
|
}
|