github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/docs_test.go (about)

     1  // Copyright 2016-2020, Pulumi Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package codegen
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  const codeFence = "```"
    24  
    25  func TestFilterExamples(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	tsCodeSnippet := `### Example 1
    29  ` + codeFence + `typescript
    30  import * as path from path;
    31  
    32  console.log("I am a console log statement in ts.");
    33  ` + codeFence
    34  
    35  	goCodeSnippet := `\n` + codeFence + `go
    36  import (
    37  	"fmt"
    38  	"strings"
    39  )
    40  
    41  func fakeFunc() {
    42  	fmt.Print("Hi, I am a fake func!")
    43  }
    44  ` + codeFence
    45  
    46  	leadingDescription := "This is a leading description for this resource."
    47  	exampleShortCode := `{{% example %}}` + tsCodeSnippet + "\n" + goCodeSnippet + `{{% /example %}}`
    48  	description := leadingDescription + `
    49  {{% examples %}}` + exampleShortCode + `
    50  {{% /examples %}}`
    51  
    52  	t.Run("ContainsRelevantCodeSnippet", func(t *testing.T) {
    53  		t.Parallel()
    54  
    55  		strippedDescription := FilterExamples(description, "typescript")
    56  		assert.NotEmpty(t, strippedDescription, "content could not be extracted")
    57  		assert.Contains(t, strippedDescription, leadingDescription, "expected to at least find the leading description")
    58  	})
    59  
    60  	// The above description does not contain a Python code snippet and because
    61  	// the description contains only one Example without any Python code snippet,
    62  	// we should expect an empty string in this test.
    63  	t.Run("DoesNotContainRelevantSnippet", func(t *testing.T) {
    64  		t.Parallel()
    65  
    66  		strippedDescription := FilterExamples(description, "python")
    67  		assert.Contains(t, strippedDescription, leadingDescription, "expected to at least find the leading description")
    68  		// Should not contain any examples sections.
    69  		assert.NotContains(t, strippedDescription, "### ", "expected to not have any examples but found at least one")
    70  	})
    71  }
    72  
    73  func TestTestFilterExamplesFromMultipleExampleSections(t *testing.T) {
    74  	t.Parallel()
    75  
    76  	tsCodeSnippet := codeFence + `typescript
    77  import * as path from path;
    78  
    79  console.log("I am a console log statement in ts.");
    80  ` + codeFence
    81  
    82  	goCodeSnippet := codeFence + `go
    83  import (
    84  	"fmt"
    85  	"strings"
    86  )
    87  
    88  func fakeFunc() {
    89  	fmt.Print("Hi, I am a fake func!")
    90  }
    91  ` + codeFence
    92  
    93  	example1 := `### Example 1
    94  ` + tsCodeSnippet + "\n" + goCodeSnippet
    95  
    96  	example2 := `### Example 2
    97  ` + tsCodeSnippet
    98  
    99  	example1ShortCode := `{{% example %}}` + "\n" + example1 + "\n" + `{{% /example %}}`
   100  	example2ShortCode := `{{% example %}}` + "\n" + example2 + "\n" + `{{% /example %}}`
   101  	description := `{{% examples %}}` + "\n" + example1ShortCode + "\n" + example2ShortCode + "\n" + `{{% /examples %}}`
   102  
   103  	t.Run("EveryExampleHasRelevantCodeSnippet", func(t *testing.T) {
   104  		t.Parallel()
   105  
   106  		strippedDescription := FilterExamples(description, "typescript")
   107  		assert.NotEmpty(t, strippedDescription, "content could not be extracted")
   108  		assert.Contains(t, strippedDescription, "Example 1", "expected Example 1 section")
   109  		assert.Contains(t, strippedDescription, "Example 2", "expected Example 2 section")
   110  	})
   111  
   112  	t.Run("SomeExamplesHaveRelevantCodeSnippet", func(t *testing.T) {
   113  		t.Parallel()
   114  
   115  		strippedDescription := FilterExamples(description, "go")
   116  		assert.NotEmpty(t, strippedDescription, "content could not be extracted")
   117  		assert.Contains(t, strippedDescription, "Example 1", "expected Example 1 section")
   118  		assert.NotContains(t, strippedDescription, "Example 2",
   119  			"unexpected Example 2 section. section should have been excluded")
   120  	})
   121  }