github.com/googleapis/api-linter@v1.65.2/rules/internal/utils/comments_test.go (about)

     1  // Copyright 2023 Google LLC
     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  //     https://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 utils
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  )
    23  
    24  func TestSeparateInternalComments(t *testing.T) {
    25  	for _, tst := range []struct {
    26  		name         string
    27  		in           string
    28  		wantExternal string
    29  		wantInternal string
    30  	}{
    31  		{
    32  			"external only",
    33  			"Hello,\nWorld!",
    34  			"Hello,\nWorld!",
    35  			"",
    36  		},
    37  		{
    38  			"internal only",
    39  			"(-- Hello,\nInternal! --)",
    40  			"",
    41  			"Hello,\nInternal!",
    42  		},
    43  		{
    44  			"mixed",
    45  			"Hello,\nWorld!\n(-- We come\nin peace --)\nWhat planet is this?",
    46  			"Hello,\nWorld!\nWhat planet is this?",
    47  			"We come\nin peace",
    48  		},
    49  	} {
    50  		t.Run(tst.name, func(t *testing.T) {
    51  			got := SeparateInternalComments(tst.in)
    52  			// Join them for ease of diffing.
    53  			gotExternal := strings.Join(got.External, "\n")
    54  			gotInternal := strings.Join(got.Internal, "\n")
    55  
    56  			if diff := cmp.Diff(gotExternal, tst.wantExternal); diff != "" {
    57  				t.Errorf("External: got(-),want(+):\n%s", diff)
    58  			}
    59  			if diff := cmp.Diff(gotInternal, tst.wantInternal); diff != "" {
    60  				t.Errorf("Internal: got(-),want(+):\n%s", diff)
    61  			}
    62  		})
    63  	}
    64  }