github.com/apipluspower/gqlgen@v0.15.2/codegen/templates/templates_test.go (about)

     1  package templates
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/apipluspower/gqlgen/internal/code"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestToGo(t *testing.T) {
    14  	require.Equal(t, "ToCamel", ToGo("TO_CAMEL"))
    15  	require.Equal(t, "ToCamel", ToGo("to_camel"))
    16  	require.Equal(t, "ToCamel", ToGo("toCamel"))
    17  	require.Equal(t, "ToCamel", ToGo("ToCamel"))
    18  	require.Equal(t, "ToCamel", ToGo("to-camel"))
    19  	require.Equal(t, "ToCamel", ToGo("-to-camel"))
    20  	require.Equal(t, "ToCamel", ToGo("_to-camel"))
    21  	require.Equal(t, "_", ToGo("_"))
    22  
    23  	require.Equal(t, "RelatedURLs", ToGo("RelatedURLs"))
    24  	require.Equal(t, "ImageIDs", ToGo("ImageIDs"))
    25  	require.Equal(t, "FooID", ToGo("FooID"))
    26  	require.Equal(t, "IDFoo", ToGo("IDFoo"))
    27  	require.Equal(t, "FooASCII", ToGo("FooASCII"))
    28  	require.Equal(t, "ASCIIFoo", ToGo("ASCIIFoo"))
    29  	require.Equal(t, "FooUTF8", ToGo("FooUTF8"))
    30  	require.Equal(t, "UTF8Foo", ToGo("UTF8Foo"))
    31  	require.Equal(t, "JSONEncoding", ToGo("JSONEncoding"))
    32  
    33  	require.Equal(t, "A", ToGo("A"))
    34  	require.Equal(t, "ID", ToGo("ID"))
    35  	require.Equal(t, "ID", ToGo("id"))
    36  	require.Equal(t, "", ToGo(""))
    37  
    38  	require.Equal(t, "RelatedUrls", ToGo("RelatedUrls"))
    39  	require.Equal(t, "ITicket", ToGo("ITicket"))
    40  	require.Equal(t, "FooTicket", ToGo("fooTicket"))
    41  }
    42  
    43  func TestToGoPrivate(t *testing.T) {
    44  	require.Equal(t, "toCamel", ToGoPrivate("TO_CAMEL"))
    45  	require.Equal(t, "toCamel", ToGoPrivate("to_camel"))
    46  	require.Equal(t, "toCamel", ToGoPrivate("toCamel"))
    47  	require.Equal(t, "toCamel", ToGoPrivate("ToCamel"))
    48  	require.Equal(t, "toCamel", ToGoPrivate("to-camel"))
    49  
    50  	require.Equal(t, "relatedURLs", ToGoPrivate("RelatedURLs"))
    51  	require.Equal(t, "imageIDs", ToGoPrivate("ImageIDs"))
    52  	require.Equal(t, "fooID", ToGoPrivate("FooID"))
    53  	require.Equal(t, "idFoo", ToGoPrivate("IDFoo"))
    54  	require.Equal(t, "fooASCII", ToGoPrivate("FooASCII"))
    55  	require.Equal(t, "asciiFoo", ToGoPrivate("ASCIIFoo"))
    56  	require.Equal(t, "fooUTF8", ToGoPrivate("FooUTF8"))
    57  	require.Equal(t, "utf8Foo", ToGoPrivate("UTF8Foo"))
    58  	require.Equal(t, "jsonEncoding", ToGoPrivate("JSONEncoding"))
    59  
    60  	require.Equal(t, "relatedUrls", ToGoPrivate("RelatedUrls"))
    61  	require.Equal(t, "iTicket", ToGoPrivate("ITicket"))
    62  
    63  	require.Equal(t, "rangeArg", ToGoPrivate("Range"))
    64  
    65  	require.Equal(t, "a", ToGoPrivate("A"))
    66  	require.Equal(t, "id", ToGoPrivate("ID"))
    67  	require.Equal(t, "id", ToGoPrivate("id"))
    68  	require.Equal(t, "", ToGoPrivate(""))
    69  	require.Equal(t, "_", ToGoPrivate("_"))
    70  }
    71  
    72  func Test_wordWalker(t *testing.T) {
    73  	helper := func(str string) []*wordInfo {
    74  		resultList := []*wordInfo{}
    75  		wordWalker(str, func(info *wordInfo) {
    76  			resultList = append(resultList, info)
    77  		})
    78  		return resultList
    79  	}
    80  
    81  	require.Equal(t, []*wordInfo{{Word: "TO"}, {Word: "CAMEL"}}, helper("TO_CAMEL"))
    82  	require.Equal(t, []*wordInfo{{Word: "to"}, {Word: "camel"}}, helper("to_camel"))
    83  	require.Equal(t, []*wordInfo{{Word: "to"}, {Word: "Camel"}}, helper("toCamel"))
    84  	require.Equal(t, []*wordInfo{{Word: "To"}, {Word: "Camel"}}, helper("ToCamel"))
    85  	require.Equal(t, []*wordInfo{{Word: "to"}, {Word: "camel"}}, helper("to-camel"))
    86  
    87  	require.Equal(t, []*wordInfo{{Word: "Related"}, {Word: "URLs", HasCommonInitial: true}}, helper("RelatedURLs"))
    88  	require.Equal(t, []*wordInfo{{Word: "Image"}, {Word: "IDs", HasCommonInitial: true}}, helper("ImageIDs"))
    89  	require.Equal(t, []*wordInfo{{Word: "Foo"}, {Word: "ID", HasCommonInitial: true, MatchCommonInitial: true}}, helper("FooID"))
    90  	require.Equal(t, []*wordInfo{{Word: "ID", HasCommonInitial: true, MatchCommonInitial: true}, {Word: "Foo"}}, helper("IDFoo"))
    91  	require.Equal(t, []*wordInfo{{Word: "Foo"}, {Word: "ASCII", HasCommonInitial: true, MatchCommonInitial: true}}, helper("FooASCII"))
    92  	require.Equal(t, []*wordInfo{{Word: "ASCII", HasCommonInitial: true, MatchCommonInitial: true}, {Word: "Foo"}}, helper("ASCIIFoo"))
    93  	require.Equal(t, []*wordInfo{{Word: "Foo"}, {Word: "UTF8", HasCommonInitial: true, MatchCommonInitial: true}}, helper("FooUTF8"))
    94  	require.Equal(t, []*wordInfo{{Word: "UTF8", HasCommonInitial: true, MatchCommonInitial: true}, {Word: "Foo"}}, helper("UTF8Foo"))
    95  
    96  	require.Equal(t, []*wordInfo{{Word: "A"}}, helper("A"))
    97  	require.Equal(t, []*wordInfo{{Word: "ID", HasCommonInitial: true, MatchCommonInitial: true}}, helper("ID"))
    98  	require.Equal(t, []*wordInfo{{Word: "id", HasCommonInitial: true, MatchCommonInitial: true}}, helper("id"))
    99  	require.Equal(t, []*wordInfo{}, helper(""))
   100  
   101  	require.Equal(t, []*wordInfo{{Word: "Related"}, {Word: "Urls"}}, helper("RelatedUrls"))
   102  	require.Equal(t, []*wordInfo{{Word: "ITicket"}}, helper("ITicket"))
   103  }
   104  
   105  func TestCenter(t *testing.T) {
   106  	require.Equal(t, "fffff", center(3, "#", "fffff"))
   107  	require.Equal(t, "##fffff###", center(10, "#", "fffff"))
   108  	require.Equal(t, "###fffff###", center(11, "#", "fffff"))
   109  }
   110  
   111  func TestTemplateOverride(t *testing.T) {
   112  	f, err := ioutil.TempFile("", "gqlgen")
   113  	if err != nil {
   114  		t.Fatal(err)
   115  	}
   116  	defer f.Close()
   117  	defer os.RemoveAll(f.Name())
   118  	err = Render(Options{Template: "hello", Filename: f.Name(), Packages: &code.Packages{}})
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  }