github.com/sanposhiho/openapi2proto@v0.0.0-20230521044535-d1080a134e37/compiler/strings_test.go (about)

     1  package compiler
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/sanposhiho/openapi2proto/openapi"
     7  )
     8  
     9  type endpointNamingConversionTestCase struct {
    10  	Endpoint openapi.Endpoint
    11  	Expected string
    12  }
    13  
    14  func TestEndpointNames(t *testing.T) {
    15  	var tests = []endpointNamingConversionTestCase{
    16  		{
    17  			Endpoint: openapi.Endpoint{
    18  				Path: "/queue/{id}/enqueue_player",
    19  				Verb: "get",
    20  			},
    21  			Expected: "GetQueueIdEnqueuePlayer",
    22  		},
    23  	}
    24  	for _, test := range tests {
    25  		t.Run(test.Endpoint.Path, func(t *testing.T) {
    26  			if v := normalizeEndpointName(&test.Endpoint); v != test.Expected {
    27  				t.Errorf("PathMethodToName conversion failed: expected %s, got %s", test.Expected, v)
    28  			}
    29  		})
    30  	}
    31  }
    32  
    33  type enumNamingConversionTestCase struct {
    34  	Source   string
    35  	Expected string
    36  }
    37  
    38  func TestEnumNames(t *testing.T) {
    39  	var tests = []enumNamingConversionTestCase{
    40  		{
    41  			Source:   "foo & bar",
    42  			Expected: "FOO_AND_BAR",
    43  		},
    44  		{
    45  			Source:   "foo&bar",
    46  			Expected: "FOO_AND_BAR",
    47  		},
    48  		{
    49  			Source: "bad chars % { } [ ] ( ) / . ' ’ -",
    50  			Expected: "BAD_CHARS",
    51  		},
    52  	}
    53  
    54  	for _, test := range tests {
    55  		t.Run(test.Source, func(t *testing.T) {
    56  			if v := normalizeEnumName(test.Source); v != test.Expected {
    57  				t.Errorf("toEnum conversion failed: expected %s, got %s", test.Expected, v)
    58  			}
    59  		})
    60  	}
    61  }