github.com/caseydavenport/controller-tools@v0.2.6-0.20200519183242-e8a18b1a6750/pkg/crd/desc_visitor_test.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package crd_test
    18  
    19  import (
    20  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  
    23  	apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    24  
    25  	"sigs.k8s.io/controller-tools/pkg/crd"
    26  )
    27  
    28  var _ = Describe("TruncateDescription", func() {
    29  
    30  	It("should drop the description for all fields for zero value of maxLen", func() {
    31  		schema := &apiext.JSONSchemaProps{
    32  			Description: "top level description",
    33  			Properties: map[string]apiext.JSONSchemaProps{
    34  				"Spec": apiext.JSONSchemaProps{
    35  					Description: "specification for the API type",
    36  				},
    37  			},
    38  		}
    39  		crd.TruncateDescription(schema, 0)
    40  		Expect(schema).To(Equal(&apiext.JSONSchemaProps{
    41  			Properties: map[string]apiext.JSONSchemaProps{
    42  				"Spec": apiext.JSONSchemaProps{},
    43  			},
    44  		}))
    45  
    46  	})
    47  
    48  	It("should truncate the description only in cases where description exceeds maxLen", func() {
    49  		schema := &apiext.JSONSchemaProps{
    50  			Description: "top level description of the root object.",
    51  			Properties: map[string]apiext.JSONSchemaProps{
    52  				"Spec": apiext.JSONSchemaProps{
    53  					Description: "specification of a field",
    54  				},
    55  			},
    56  		}
    57  		original := schema.DeepCopy()
    58  		crd.TruncateDescription(schema, len(schema.Description))
    59  		Expect(schema).To(Equal(original))
    60  	})
    61  
    62  	It("should truncate the description at closest sentence boundary", func() {
    63  		schema := &apiext.JSONSchemaProps{
    64  			Description: `This is top level description. There is an empty schema. More to come`,
    65  		}
    66  		crd.TruncateDescription(schema, len(schema.Description)-5)
    67  		Expect(schema).To(Equal(&apiext.JSONSchemaProps{
    68  			Description: `This is top level description. There is an empty schema.`,
    69  		}))
    70  	})
    71  
    72  	It("should truncate the description at maxLen in absence of sentence boundary", func() {
    73  		schema := &apiext.JSONSchemaProps{
    74  			Description: `This is top level description of the root object`,
    75  		}
    76  		crd.TruncateDescription(schema, len(schema.Description)-2)
    77  		Expect(schema).To(Equal(&apiext.JSONSchemaProps{
    78  			Description: `This is top level description of the root obje`,
    79  		}))
    80  	})
    81  })