github.com/v2fly/tools@v0.100.0/internal/lsp/source/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 "testing" 8 9 func TestIsValidDirName(t *testing.T) { 10 tests := []struct { 11 dirName string 12 valid bool 13 }{ 14 {dirName: "", valid: false}, 15 // 16 {dirName: "a", valid: true}, 17 {dirName: "abcdef", valid: true}, 18 {dirName: "AbCdEf", valid: true}, 19 // 20 {dirName: "1a35", valid: true}, 21 {dirName: "a16", valid: true}, 22 // 23 {dirName: "_a", valid: true}, 24 {dirName: "a_", valid: true}, 25 // 26 {dirName: "~a", valid: false}, 27 {dirName: "a~", valid: true}, 28 // 29 {dirName: "-a", valid: false}, 30 {dirName: "a-", valid: true}, 31 // 32 {dirName: ".a", valid: false}, 33 {dirName: "a.", valid: false}, 34 // 35 {dirName: "a~_b--c.-e", valid: true}, 36 {dirName: "~a~_b--c.-e", valid: false}, 37 {dirName: "a~_b--c.-e--~", valid: true}, 38 {dirName: "a~_b--2134dc42.-e6--~", valid: true}, 39 {dirName: "abc`def", valid: false}, 40 {dirName: "тест", valid: false}, 41 {dirName: "你好", valid: false}, 42 } 43 for _, tt := range tests { 44 valid := isValidDirName(tt.dirName) 45 if tt.valid != valid { 46 t.Errorf("%s: expected %v, got %v", tt.dirName, tt.valid, valid) 47 } 48 } 49 } 50 51 func TestConvertDirNameToPkgName(t *testing.T) { 52 tests := []struct { 53 dirName string 54 pkgName string 55 }{ 56 {dirName: "a", pkgName: "a"}, 57 {dirName: "abcdef", pkgName: "abcdef"}, 58 {dirName: "AbCdEf", pkgName: "abcdef"}, 59 {dirName: "1a35", pkgName: "a35"}, 60 {dirName: "14a35", pkgName: "a35"}, 61 {dirName: "a16", pkgName: "a16"}, 62 {dirName: "_a", pkgName: "a"}, 63 {dirName: "a_", pkgName: "a"}, 64 {dirName: "a~", pkgName: "a"}, 65 {dirName: "a-", pkgName: "a"}, 66 {dirName: "a~_b--c.-e", pkgName: "abce"}, 67 {dirName: "a~_b--c.-e--~", pkgName: "abce"}, 68 {dirName: "a~_b--2134dc42.-e6--~", pkgName: "ab2134dc42e6"}, 69 } 70 for _, tt := range tests { 71 pkgName := convertDirNameToPkgName(tt.dirName) 72 if tt.pkgName != pkgName { 73 t.Errorf("%s: expected %v, got %v", tt.dirName, tt.pkgName, pkgName) 74 continue 75 } 76 } 77 }