code.gitea.io/gitea@v1.19.3/modules/util/string_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package util
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestToSnakeCase(t *testing.T) {
    13  	cases := map[string]string{
    14  		// all old cases from the legacy package
    15  		"HTTPServer":         "http_server",
    16  		"_camelCase":         "_camel_case",
    17  		"NoHTTPS":            "no_https",
    18  		"Wi_thF":             "wi_th_f",
    19  		"_AnotherTES_TCaseP": "_another_tes_t_case_p",
    20  		"ALL":                "all",
    21  		"_HELLO_WORLD_":      "_hello_world_",
    22  		"HELLO_WORLD":        "hello_world",
    23  		"HELLO____WORLD":     "hello____world",
    24  		"TW":                 "tw",
    25  		"_C":                 "_c",
    26  
    27  		"  sentence case  ": "__sentence_case__",
    28  		" Mixed-hyphen case _and SENTENCE_case and UPPER-case": "_mixed_hyphen_case__and_sentence_case_and_upper_case",
    29  
    30  		// new cases
    31  		" ":        "_",
    32  		"A":        "a",
    33  		"A0":       "a0",
    34  		"a0":       "a0",
    35  		"Aa0":      "aa0",
    36  		"啊":        "啊",
    37  		"A啊":       "a啊",
    38  		"Aa啊b":     "aa啊b",
    39  		"A啊B":      "a啊_b",
    40  		"Aa啊B":     "aa啊_b",
    41  		"TheCase2": "the_case2",
    42  		"ObjIDs":   "obj_i_ds", // the strange database column name which already exists
    43  	}
    44  	for input, expected := range cases {
    45  		assert.Equal(t, expected, ToSnakeCase(input))
    46  	}
    47  }