github.com/astaxie/beego@v1.12.3/controller_test.go (about)

     1  // Copyright 2014 beego Author. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package beego
    16  
    17  import (
    18  	"math"
    19  	"strconv"
    20  	"testing"
    21  
    22  	"os"
    23  	"path/filepath"
    24  
    25  	"github.com/astaxie/beego/context"
    26  )
    27  
    28  func TestGetInt(t *testing.T) {
    29  	i := context.NewInput()
    30  	i.SetParam("age", "40")
    31  	ctx := &context.Context{Input: i}
    32  	ctrlr := Controller{Ctx: ctx}
    33  	val, _ := ctrlr.GetInt("age")
    34  	if val != 40 {
    35  		t.Errorf("TestGetInt expect 40,get %T,%v", val, val)
    36  	}
    37  }
    38  
    39  func TestGetInt8(t *testing.T) {
    40  	i := context.NewInput()
    41  	i.SetParam("age", "40")
    42  	ctx := &context.Context{Input: i}
    43  	ctrlr := Controller{Ctx: ctx}
    44  	val, _ := ctrlr.GetInt8("age")
    45  	if val != 40 {
    46  		t.Errorf("TestGetInt8 expect 40,get %T,%v", val, val)
    47  	}
    48  	//Output: int8
    49  }
    50  
    51  func TestGetInt16(t *testing.T) {
    52  	i := context.NewInput()
    53  	i.SetParam("age", "40")
    54  	ctx := &context.Context{Input: i}
    55  	ctrlr := Controller{Ctx: ctx}
    56  	val, _ := ctrlr.GetInt16("age")
    57  	if val != 40 {
    58  		t.Errorf("TestGetInt16 expect 40,get %T,%v", val, val)
    59  	}
    60  }
    61  
    62  func TestGetInt32(t *testing.T) {
    63  	i := context.NewInput()
    64  	i.SetParam("age", "40")
    65  	ctx := &context.Context{Input: i}
    66  	ctrlr := Controller{Ctx: ctx}
    67  	val, _ := ctrlr.GetInt32("age")
    68  	if val != 40 {
    69  		t.Errorf("TestGetInt32 expect 40,get %T,%v", val, val)
    70  	}
    71  }
    72  
    73  func TestGetInt64(t *testing.T) {
    74  	i := context.NewInput()
    75  	i.SetParam("age", "40")
    76  	ctx := &context.Context{Input: i}
    77  	ctrlr := Controller{Ctx: ctx}
    78  	val, _ := ctrlr.GetInt64("age")
    79  	if val != 40 {
    80  		t.Errorf("TestGeetInt64 expect 40,get %T,%v", val, val)
    81  	}
    82  }
    83  
    84  func TestGetUint8(t *testing.T) {
    85  	i := context.NewInput()
    86  	i.SetParam("age", strconv.FormatUint(math.MaxUint8, 10))
    87  	ctx := &context.Context{Input: i}
    88  	ctrlr := Controller{Ctx: ctx}
    89  	val, _ := ctrlr.GetUint8("age")
    90  	if val != math.MaxUint8 {
    91  		t.Errorf("TestGetUint8 expect %v,get %T,%v", math.MaxUint8, val, val)
    92  	}
    93  }
    94  
    95  func TestGetUint16(t *testing.T) {
    96  	i := context.NewInput()
    97  	i.SetParam("age", strconv.FormatUint(math.MaxUint16, 10))
    98  	ctx := &context.Context{Input: i}
    99  	ctrlr := Controller{Ctx: ctx}
   100  	val, _ := ctrlr.GetUint16("age")
   101  	if val != math.MaxUint16 {
   102  		t.Errorf("TestGetUint16 expect %v,get %T,%v", math.MaxUint16, val, val)
   103  	}
   104  }
   105  
   106  func TestGetUint32(t *testing.T) {
   107  	i := context.NewInput()
   108  	i.SetParam("age", strconv.FormatUint(math.MaxUint32, 10))
   109  	ctx := &context.Context{Input: i}
   110  	ctrlr := Controller{Ctx: ctx}
   111  	val, _ := ctrlr.GetUint32("age")
   112  	if val != math.MaxUint32 {
   113  		t.Errorf("TestGetUint32 expect %v,get %T,%v", math.MaxUint32, val, val)
   114  	}
   115  }
   116  
   117  func TestGetUint64(t *testing.T) {
   118  	i := context.NewInput()
   119  	i.SetParam("age", strconv.FormatUint(math.MaxUint64, 10))
   120  	ctx := &context.Context{Input: i}
   121  	ctrlr := Controller{Ctx: ctx}
   122  	val, _ := ctrlr.GetUint64("age")
   123  	if val != math.MaxUint64 {
   124  		t.Errorf("TestGetUint64 expect %v,get %T,%v", uint64(math.MaxUint64), val, val)
   125  	}
   126  }
   127  
   128  func TestAdditionalViewPaths(t *testing.T) {
   129  	dir1 := tmpDir("TestAdditionalViewPaths1")
   130  	dir2 := tmpDir("TestAdditionalViewPaths2")
   131  	defer os.RemoveAll(dir1)
   132  	defer os.RemoveAll(dir2)
   133  
   134  	dir1file := "file1.tpl"
   135  	dir2file := "file2.tpl"
   136  
   137  	genFile := func(dir string, name string, content string) {
   138  		os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0777)
   139  		if f, err := os.Create(filepath.Join(dir, name)); err != nil {
   140  			t.Fatal(err)
   141  		} else {
   142  			defer f.Close()
   143  			f.WriteString(content)
   144  			f.Close()
   145  		}
   146  
   147  	}
   148  	genFile(dir1, dir1file, `<div>{{.Content}}</div>`)
   149  	genFile(dir2, dir2file, `<html>{{.Content}}</html>`)
   150  
   151  	AddViewPath(dir1)
   152  	AddViewPath(dir2)
   153  
   154  	ctrl := Controller{
   155  		TplName:  "file1.tpl",
   156  		ViewPath: dir1,
   157  	}
   158  	ctrl.Data = map[interface{}]interface{}{
   159  		"Content": "value2",
   160  	}
   161  	if result, err := ctrl.RenderString(); err != nil {
   162  		t.Fatal(err)
   163  	} else {
   164  		if result != "<div>value2</div>" {
   165  			t.Fatalf("TestAdditionalViewPaths expect %s got %s", "<div>value2</div>", result)
   166  		}
   167  	}
   168  
   169  	func() {
   170  		ctrl.TplName = "file2.tpl"
   171  		defer func() {
   172  			if r := recover(); r == nil {
   173  				t.Fatal("TestAdditionalViewPaths expected error")
   174  			}
   175  		}()
   176  		ctrl.RenderString()
   177  	}()
   178  
   179  	ctrl.TplName = "file2.tpl"
   180  	ctrl.ViewPath = dir2
   181  	ctrl.RenderString()
   182  }