golang.org/x/tools/gopls@v0.15.3/internal/golang/completion/package_test.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package completion
     6  
     7  import (
     8  	"testing"
     9  
    10  	"golang.org/x/tools/gopls/internal/golang"
    11  )
    12  
    13  func TestIsValidDirName(t *testing.T) {
    14  	tests := []struct {
    15  		dirName string
    16  		valid   bool
    17  	}{
    18  		{dirName: "", valid: false},
    19  		//
    20  		{dirName: "a", valid: true},
    21  		{dirName: "abcdef", valid: true},
    22  		{dirName: "AbCdEf", valid: true},
    23  		//
    24  		{dirName: "1a35", valid: true},
    25  		{dirName: "a16", valid: true},
    26  		//
    27  		{dirName: "_a", valid: true},
    28  		{dirName: "a_", valid: true},
    29  		//
    30  		{dirName: "~a", valid: false},
    31  		{dirName: "a~", valid: true},
    32  		//
    33  		{dirName: "-a", valid: false},
    34  		{dirName: "a-", valid: true},
    35  		//
    36  		{dirName: ".a", valid: false},
    37  		{dirName: "a.", valid: false},
    38  		//
    39  		{dirName: "a~_b--c.-e", valid: true},
    40  		{dirName: "~a~_b--c.-e", valid: false},
    41  		{dirName: "a~_b--c.-e--~", valid: true},
    42  		{dirName: "a~_b--2134dc42.-e6--~", valid: true},
    43  		{dirName: "abc`def", valid: false},
    44  		{dirName: "тест", valid: false},
    45  		{dirName: "你好", valid: false},
    46  	}
    47  	for _, tt := range tests {
    48  		valid := isValidDirName(tt.dirName)
    49  		if tt.valid != valid {
    50  			t.Errorf("%s: expected %v, got %v", tt.dirName, tt.valid, valid)
    51  		}
    52  	}
    53  }
    54  
    55  func TestConvertDirNameToPkgName(t *testing.T) {
    56  	tests := []struct {
    57  		dirName string
    58  		pkgName golang.PackageName
    59  	}{
    60  		{dirName: "a", pkgName: "a"},
    61  		{dirName: "abcdef", pkgName: "abcdef"},
    62  		{dirName: "AbCdEf", pkgName: "abcdef"},
    63  		{dirName: "1a35", pkgName: "a35"},
    64  		{dirName: "14a35", pkgName: "a35"},
    65  		{dirName: "a16", pkgName: "a16"},
    66  		{dirName: "_a", pkgName: "a"},
    67  		{dirName: "a_", pkgName: "a"},
    68  		{dirName: "a~", pkgName: "a"},
    69  		{dirName: "a-", pkgName: "a"},
    70  		{dirName: "a~_b--c.-e", pkgName: "abce"},
    71  		{dirName: "a~_b--c.-e--~", pkgName: "abce"},
    72  		{dirName: "a~_b--2134dc42.-e6--~", pkgName: "ab2134dc42e6"},
    73  	}
    74  	for _, tt := range tests {
    75  		pkgName := convertDirNameToPkgName(tt.dirName)
    76  		if tt.pkgName != pkgName {
    77  			t.Errorf("%s: expected %v, got %v", tt.dirName, tt.pkgName, pkgName)
    78  			continue
    79  		}
    80  	}
    81  }