github.com/naoina/kocha@v0.7.1-0.20171129072645-78c7a531f799/util/util_test.go (about)

     1  package util
     2  
     3  import (
     4  	"encoding/base64"
     5  	"fmt"
     6  	"go/build"
     7  	"go/format"
     8  	"html/template"
     9  	"io/ioutil"
    10  	"os"
    11  	"path/filepath"
    12  	"reflect"
    13  	"regexp"
    14  	"sort"
    15  	"strings"
    16  	"testing"
    17  	"testing/quick"
    18  )
    19  
    20  type orderedOutputMap map[string]interface{}
    21  
    22  func (m orderedOutputMap) String() string {
    23  	keys := make([]string, 0, len(m))
    24  	for key, _ := range m {
    25  		keys = append(keys, key)
    26  	}
    27  	sort.Strings(keys)
    28  	outputs := make([]string, 0, len(keys))
    29  	for _, key := range keys {
    30  		outputs = append(outputs, fmt.Sprintf("%s:%s", key, m[key]))
    31  	}
    32  	return fmt.Sprintf("map[%v]", strings.Join(outputs, " "))
    33  }
    34  
    35  func (m orderedOutputMap) GoString() string {
    36  	keys := make([]string, 0, len(m))
    37  	for key, _ := range m {
    38  		keys = append(keys, key)
    39  	}
    40  	sort.Strings(keys)
    41  	for i, key := range keys {
    42  		keys[i] = fmt.Sprintf("%#v:%#v", key, m[key])
    43  	}
    44  	return fmt.Sprintf("map[string]interface{}{%v}", strings.Join(keys, ", "))
    45  }
    46  
    47  func Test_NormPath(t *testing.T) {
    48  	for v, expected := range map[string]string{
    49  		"/":           "/",
    50  		"/path":       "/path",
    51  		"/path/":      "/path/",
    52  		"//path//":    "/path/",
    53  		"/path/to":    "/path/to",
    54  		"/path/to///": "/path/to/",
    55  	} {
    56  		actual := NormPath(v)
    57  		if !reflect.DeepEqual(actual, expected) {
    58  			t.Errorf("%v: Expect %v, but %v", v, expected, actual)
    59  		}
    60  	}
    61  }
    62  
    63  func TestGoString(t *testing.T) {
    64  	re := regexp.MustCompile(`^/path/to/([^/]+)(?:\.html)?$`)
    65  	actual := GoString(re)
    66  	expected := `regexp.MustCompile("^/path/to/([^/]+)(?:\\.html)?$")`
    67  	if !reflect.DeepEqual(actual, expected) {
    68  		t.Errorf("Expect %v, but %v", expected, actual)
    69  	}
    70  
    71  	tmpl := template.Must(template.New("test").Parse(`foo{{.name}}bar`))
    72  	actual = GoString(tmpl)
    73  	expected = `template.Must(template.New("test").Funcs(kocha.TemplateFuncs).Parse(util.Gunzip("\x1f\x8b\b\x00\x00\x00\x00\x00\x02\xffJ\xcbϯ\xae\xd6\xcbK\xccM\xad\xadMJ,\x02\x04\x00\x00\xff\xff4%\x83\xb6\x0f\x00\x00\x00")))`
    74  	if !reflect.DeepEqual(actual, expected) {
    75  		t.Errorf("Expect %v, but %v", expected, actual)
    76  	}
    77  
    78  	actual = GoString(testGoString{})
    79  	expected = "gostring"
    80  	if !reflect.DeepEqual(actual, expected) {
    81  		t.Errorf("Expect %v, but %v", expected, actual)
    82  	}
    83  
    84  	actual = GoString(nil)
    85  	expected = "nil"
    86  	if !reflect.DeepEqual(actual, expected) {
    87  		t.Errorf("Expect %v, but %v", expected, actual)
    88  	}
    89  
    90  	var ptr *int
    91  	actual = GoString(ptr)
    92  	expected = "nil"
    93  	if !reflect.DeepEqual(actual, expected) {
    94  		t.Errorf("Expect %v, but %v", expected, actual)
    95  	}
    96  
    97  	n := 10
    98  	nptr := &n
    99  	actual = GoString(nptr)
   100  	expected = "10"
   101  	if !reflect.DeepEqual(actual, expected) {
   102  		t.Errorf("Expect %v, but %v", expected, actual)
   103  	}
   104  
   105  	aBuf, err := format.Source([]byte(GoString(struct {
   106  		Name, path string
   107  		Route      orderedOutputMap
   108  		G          *testGoString
   109  	}{
   110  		Name: "foo",
   111  		path: "path",
   112  		Route: orderedOutputMap{
   113  			"first":  "Tokyo",
   114  			"second": "Kyoto",
   115  			"third":  []int{10, 11, 20},
   116  		},
   117  		G: &testGoString{},
   118  	})))
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  	eBuf, err := format.Source([]byte(`
   123  struct {
   124  	Name string
   125  	path string
   126  	Route util.orderedOutputMap
   127  	G *util.testGoString
   128  }{
   129  
   130  	G: gostring,
   131  
   132  	Name: "foo",
   133  
   134  	Route: map[string]interface{}{"first": "Tokyo", "second": "Kyoto", "third": []int{10, 11, 20}},
   135  }`))
   136  	if err != nil {
   137  		t.Fatal(err)
   138  	}
   139  	actual = string(aBuf)
   140  	expected = string(eBuf)
   141  	if !reflect.DeepEqual(actual, expected) {
   142  		t.Errorf("Expect %q, but %q", expected, actual)
   143  	}
   144  }
   145  
   146  type testGoString struct{}
   147  
   148  func (g testGoString) GoString() string { return "gostring" }
   149  
   150  func Test_Gzip(t *testing.T) {
   151  	actual := base64.StdEncoding.EncodeToString([]byte(Gzip("kocha")))
   152  	expected := "H4sIAAAAAAAC/8rOT85IBAQAAP//DJOFlgUAAAA="
   153  	if !reflect.DeepEqual(actual, expected) {
   154  		t.Errorf("Expect %v, but %v", expected, actual)
   155  	}
   156  
   157  	// reversibility test
   158  	gz := Gzip("kocha")
   159  	actual = Gunzip(gz)
   160  	expected = "kocha"
   161  	if !reflect.DeepEqual(actual, expected) {
   162  		t.Errorf("Expect %v, but %v", expected, actual)
   163  	}
   164  }
   165  
   166  func TestGunzip(t *testing.T) {
   167  	actual := Gunzip("\x1f\x8b\b\x00\x00\tn\x88\x02\xff\xca\xceO\xceH\x04\x04\x00\x00\xff\xff\f\x93\x85\x96\x05\x00\x00\x00")
   168  	expected := "kocha"
   169  	if !reflect.DeepEqual(actual, expected) {
   170  		t.Errorf("Expect %v, but %v", expected, actual)
   171  	}
   172  
   173  	// reversibility test
   174  	raw := Gunzip("\x1f\x8b\b\x00\x00\tn\x88\x02\xff\xca\xceO\xceH\x04\x04\x00\x00\xff\xff\f\x93\x85\x96\x05\x00\x00\x00")
   175  	actual = base64.StdEncoding.EncodeToString([]byte(Gzip(raw)))
   176  	expected = "H4sIAAAAAAAC/8rOT85IBAQAAP//DJOFlgUAAAA="
   177  	if !reflect.DeepEqual(actual, expected) {
   178  		t.Errorf("Expect %v, but %v", expected, actual)
   179  	}
   180  }
   181  
   182  func TestFindAppDir(t *testing.T) {
   183  	tempDir, err := ioutil.TempDir("", "TestFindAppDir")
   184  	if err != nil {
   185  		t.Fatal(err)
   186  	}
   187  	defer os.RemoveAll(tempDir)
   188  	origGOPATH := build.Default.GOPATH
   189  	defer func() {
   190  		build.Default.GOPATH = origGOPATH
   191  		os.Setenv("GOPATH", origGOPATH)
   192  	}()
   193  	build.Default.GOPATH = tempDir + string(filepath.ListSeparator) + build.Default.GOPATH
   194  	os.Setenv("GOPATH", build.Default.GOPATH)
   195  	myappPath := filepath.Join(tempDir, "src", "path", "to", "myapp")
   196  	if err := os.MkdirAll(myappPath, 0755); err != nil {
   197  		t.Fatal(err)
   198  	}
   199  	if err := os.Chdir(myappPath); err != nil {
   200  		t.Fatal(err)
   201  	}
   202  	actual, err := FindAppDir()
   203  	if err != nil {
   204  		t.Fatal(err)
   205  	}
   206  	expected := "path/to/myapp"
   207  	if !reflect.DeepEqual(actual, expected) {
   208  		t.Errorf("FindAppDir() => %q, want %q", actual, expected)
   209  	}
   210  }
   211  
   212  func TestIsUnexportedField(t *testing.T) {
   213  	// test for bug case older than Go1.3.
   214  	func() {
   215  		type b struct{}
   216  		type C struct {
   217  			b
   218  		}
   219  		v := reflect.TypeOf(C{}).Field(0)
   220  		actual := IsUnexportedField(v)
   221  		expected := true
   222  		if !reflect.DeepEqual(actual, expected) {
   223  			t.Errorf("IsUnexportedField(%#v) => %v, want %v", v, actual, expected)
   224  		}
   225  	}()
   226  
   227  	// test for correct case.
   228  	func() {
   229  		type B struct{}
   230  		type C struct {
   231  			B
   232  		}
   233  		v := reflect.TypeOf(C{}).Field(0)
   234  		actual := IsUnexportedField(v)
   235  		expected := false
   236  		if !reflect.DeepEqual(actual, expected) {
   237  			t.Errorf("IsUnexportedField(%#v) => %v, want %v", v, actual, expected)
   238  		}
   239  	}()
   240  }
   241  
   242  func TestGenerateRandomKey(t *testing.T) {
   243  	if err := quick.Check(func(length uint16) bool {
   244  		already := make([][]byte, 0, 100)
   245  		for i := 0; i < 100; i++ {
   246  			buf := GenerateRandomKey(int(length))
   247  			for _, v := range already {
   248  				if !reflect.DeepEqual(buf, v) {
   249  					return false
   250  				}
   251  			}
   252  		}
   253  		return true
   254  	}, &quick.Config{MaxCount: 10}); err != nil {
   255  		t.Error(err)
   256  	}
   257  }