github.com/calavera/json_generate@v0.0.0-20150116195211-6949016f3624/json_generate_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestAliases(t *testing.T) {
    14  	h := aliases("")
    15  	assert.Len(t, h, 0)
    16  
    17  	h = aliases("Foo:")
    18  	assert.Len(t, h, 0)
    19  
    20  	h = aliases("Foo:Bar")
    21  	assert.Len(t, h, 1)
    22  	assert.Equal(t, h["Bar"], "Foo")
    23  
    24  	h = aliases("Foo:Bar,Qux:Quux")
    25  	assert.Len(t, h, 2)
    26  	assert.Equal(t, h["Bar"], "Foo")
    27  	assert.Equal(t, h["Quux"], "Qux")
    28  
    29  	h = aliases("Foo:Bar:Baz,Qux:Quux")
    30  	assert.Len(t, h, 3)
    31  	assert.Equal(t, h["Bar"], "Foo")
    32  	assert.Equal(t, h["Baz"], "Foo")
    33  	assert.Equal(t, h["Quux"], "Qux")
    34  }
    35  
    36  func TestFilterTypes(t *testing.T) {
    37  	h := filterTypes("")
    38  	assert.Len(t, h, 0)
    39  
    40  	h = filterTypes("Foo:")
    41  	assert.Len(t, h, 0)
    42  
    43  	h = filterTypes("Foo:*time.Time")
    44  	assert.Len(t, h, 1)
    45  	assert.Equal(t, h["Foo"], "*time.Time")
    46  
    47  	h = filterTypes("Foo:*time.Time,count:int64")
    48  	assert.Len(t, h, 2)
    49  	assert.Equal(t, h["Foo"], "*time.Time")
    50  	assert.Equal(t, h["count"], "int64")
    51  }
    52  
    53  func TestCapitalize(t *testing.T) {
    54  	c := capitalize("foo")
    55  	assert.Equal(t, c, "Foo")
    56  
    57  	c = capitalize("foo_bar")
    58  	assert.Equal(t, c, "FooBar")
    59  
    60  	c = capitalize("foo_url")
    61  	assert.Equal(t, c, "FooURL")
    62  
    63  	c = capitalize("http_url")
    64  	assert.Equal(t, c, "HTTPURL")
    65  }
    66  
    67  func TestNormalizeImport(t *testing.T) {
    68  	i, ok := normalizeImport("*url.URL")
    69  	assert.True(t, ok)
    70  	assert.Equal(t, "url", i)
    71  
    72  	i, ok = normalizeImport("string")
    73  	assert.False(t, ok)
    74  	assert.Equal(t, "string", i)
    75  }
    76  
    77  var dumpTestCases = []struct {
    78  	source   *Source
    79  	name     string
    80  	output   string
    81  	location string
    82  	content  string
    83  }{
    84  	{
    85  		source: &Source{
    86  			name:    "event",
    87  			pkg:     "main",
    88  			structs: map[string]string{"Event": "type Event struct{}"},
    89  			imports: make([]string, 0),
    90  		},
    91  		name:     "Event",
    92  		output:   "",
    93  		location: "event_json.go",
    94  		content: `// generated by json_generate; DO NOT EDIT
    95  package main
    96  
    97  type Event struct{}`,
    98  	},
    99  	{
   100  		source: &Source{
   101  			name:    "event",
   102  			pkg:     "main",
   103  			structs: map[string]string{"Event": "type Event struct{}"},
   104  			imports: []string{"url"},
   105  		},
   106  		name:     "Event",
   107  		output:   "",
   108  		location: "event_json.go",
   109  		content: `// generated by json_generate; DO NOT EDIT
   110  package main
   111  
   112  import "url"
   113  
   114  type Event struct{}`,
   115  	},
   116  }
   117  
   118  func TestDump(t *testing.T) {
   119  	temp, err := ioutil.TempDir("", "json-")
   120  	if !assert.NoError(t, err) {
   121  		return
   122  	}
   123  	defer os.RemoveAll(temp)
   124  
   125  	for _, c := range dumpTestCases {
   126  		w := newWalker([]string{}, nil, nil)
   127  
   128  		w.sources = map[string]*Source{c.name: c.source}
   129  		w.dump(c.output, temp)
   130  
   131  		file := fmt.Sprintf("%s/%s", temp, c.location)
   132  		fi, err := os.Stat(file)
   133  		if !assert.NoError(t, err) {
   134  			return
   135  		}
   136  		assert.False(t, fi.IsDir())
   137  
   138  		content, err := ioutil.ReadFile(file)
   139  		if !assert.NoError(t, err) {
   140  			return
   141  		}
   142  		assert.Equal(t, string(content), c.content)
   143  	}
   144  }
   145  
   146  var parseTestCases = []struct {
   147  	aliases  map[string]string
   148  	types    map[string]string
   149  	src      string
   150  	eLen     int
   151  	ePkg     string
   152  	eStructs map[string]string
   153  }{
   154  	{
   155  		src:      "package main\nconst JSONExample_Event = `{ \"test_url\": \"http://foobar\", \"hits\": 8 }`",
   156  		eLen:     1,
   157  		ePkg:     "main",
   158  		eStructs: map[string]string{"Event": "type Event struct {\n\tHits float64 `json:\"hits,omitempty\"`\n\tTestURL *url.URL `json:\"test_url,omitempty\"`\n}"},
   159  	},
   160  	{
   161  		src:      "package main\nconst JSONExample_Event = `{ \"empty_array\": [] }`",
   162  		eLen:     1,
   163  		ePkg:     "main",
   164  		eStructs: map[string]string{"Event": "type Event struct {\n\tEmptyArray []interface{} `json:\"empty_array,omitempty\"`\n}"},
   165  	},
   166  	{
   167  		src:      "package main\nconst JSONExample_Event = `{ \"struct_array\": [{\"hits\": 9}] }`",
   168  		eLen:     1,
   169  		ePkg:     "main",
   170  		eStructs: map[string]string{"Event": "type Event struct {\n\tStructArray []interface{} `json:\"struct_array,omitempty\"`\n}"},
   171  	},
   172  	{
   173  		aliases: map[string]string{"Owner": "User"},
   174  		src:     "package main\nconst JSONExample_User = `{\"login\": \"calavera\"}`\nconst JSONExample_Event = `{ \"owner\": {\"login\": \"calavera\"} }`",
   175  		eLen:    2,
   176  		ePkg:    "main",
   177  		eStructs: map[string]string{
   178  			"User":  "type User struct {\n\tLogin string `json:\"login,omitempty\"`\n}",
   179  			"Event": "type Event struct {\n\tOwner *User `json:\"owner,omitempty\"`\n}",
   180  		},
   181  	},
   182  	{
   183  		src:  "package main\nconst JSONExample_Event = `{ \"user\": {\"login\": \"calavera\" }}`",
   184  		eLen: 2,
   185  		ePkg: "main",
   186  		eStructs: map[string]string{
   187  			"User":  "type User struct {\n\tLogin string `json:\"login,omitempty\"`\n}",
   188  			"Event": "type Event struct {\n\tUser *User `json:\"user,omitempty\"`\n}",
   189  		},
   190  	},
   191  	{
   192  		src:  "package main\nconst JSONExample_Event = `{ \"user\": null }`",
   193  		eLen: 1,
   194  		ePkg: "main",
   195  		eStructs: map[string]string{
   196  			"Event": "type Event struct {\n\tUser string `json:\"user,omitempty\"`\n}",
   197  		},
   198  	},
   199  	{
   200  		types: map[string]string{"timestamp": "*time.Time"},
   201  		src:   "package main\nconst JSONExample_Event = `{ \"timestamp\": 1420941101 }`",
   202  		eLen:  1,
   203  		ePkg:  "main",
   204  		eStructs: map[string]string{
   205  			"Event": "type Event struct {\n\tTimestamp *time.Time `json:\"timestamp,omitempty\"`\n}",
   206  		},
   207  	},
   208  	{
   209  		types: map[string]string{"Timestamp": "*time.Time"},
   210  		src:   "package main\nconst JSONExample_Event = `{ \"timestamp\": 1420941101 }`",
   211  		eLen:  1,
   212  		ePkg:  "main",
   213  		eStructs: map[string]string{
   214  			"Event": "type Event struct {\n\tTimestamp *time.Time `json:\"timestamp,omitempty\"`\n}",
   215  		},
   216  	},
   217  	{
   218  		src:  "package main\nconst JSONExample_Event = `{ \"created_at\": 1420941101 }`",
   219  		eLen: 1,
   220  		ePkg: "main",
   221  		eStructs: map[string]string{
   222  			"Event": "type Event struct {\n\tCreatedAt *time.Time `json:\"created_at,omitempty\"`\n}",
   223  		},
   224  	},
   225  }
   226  
   227  func TestWalkerParseWithUnknownExample(t *testing.T) {
   228  	w := newWalker([]string{"EventExample"}, nil, nil)
   229  
   230  	input := strings.NewReader("package main\nconst JSONExample_Event = `{ \"test_url\": \"http://foobar\", \"hits\": 8 }`")
   231  
   232  	s := w.parse("test.go", input)
   233  	assert.Len(t, s.structs, 0)
   234  }
   235  
   236  func TestWalkerParse(t *testing.T) {
   237  	parseTestHelper(t, []string{})
   238  }
   239  
   240  func TestWalkerParseWithExamples(t *testing.T) {
   241  	parseTestHelper(t, []string{"User", "Event"})
   242  }
   243  
   244  func parseTestHelper(t *testing.T, examples []string) {
   245  	for _, c := range parseTestCases {
   246  		w := newWalker(examples, c.aliases, c.types)
   247  
   248  		input := strings.NewReader(c.src)
   249  
   250  		s := w.parse("test.go", input)
   251  		assert.Equal(t, s.PathName(), "test")
   252  
   253  		assert.Len(t, s.structs, c.eLen)
   254  		assert.Equal(t, s.pkg, c.ePkg)
   255  
   256  		for n, e := range c.eStructs {
   257  			assert.Equal(t, s.structs[n], e)
   258  		}
   259  	}
   260  }