controller_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. package beego
  15. import (
  16. "math"
  17. "strconv"
  18. "testing"
  19. "github.com/astaxie/beego/context"
  20. "os"
  21. "path/filepath"
  22. )
  23. func TestGetInt(t *testing.T) {
  24. i := context.NewInput()
  25. i.SetParam("age", "40")
  26. ctx := &context.Context{Input: i}
  27. ctrlr := Controller{Ctx: ctx}
  28. val, _ := ctrlr.GetInt("age")
  29. if val != 40 {
  30. t.Errorf("TestGetInt expect 40,get %T,%v", val, val)
  31. }
  32. }
  33. func TestGetInt8(t *testing.T) {
  34. i := context.NewInput()
  35. i.SetParam("age", "40")
  36. ctx := &context.Context{Input: i}
  37. ctrlr := Controller{Ctx: ctx}
  38. val, _ := ctrlr.GetInt8("age")
  39. if val != 40 {
  40. t.Errorf("TestGetInt8 expect 40,get %T,%v", val, val)
  41. }
  42. //Output: int8
  43. }
  44. func TestGetInt16(t *testing.T) {
  45. i := context.NewInput()
  46. i.SetParam("age", "40")
  47. ctx := &context.Context{Input: i}
  48. ctrlr := Controller{Ctx: ctx}
  49. val, _ := ctrlr.GetInt16("age")
  50. if val != 40 {
  51. t.Errorf("TestGetInt16 expect 40,get %T,%v", val, val)
  52. }
  53. }
  54. func TestGetInt32(t *testing.T) {
  55. i := context.NewInput()
  56. i.SetParam("age", "40")
  57. ctx := &context.Context{Input: i}
  58. ctrlr := Controller{Ctx: ctx}
  59. val, _ := ctrlr.GetInt32("age")
  60. if val != 40 {
  61. t.Errorf("TestGetInt32 expect 40,get %T,%v", val, val)
  62. }
  63. }
  64. func TestGetInt64(t *testing.T) {
  65. i := context.NewInput()
  66. i.SetParam("age", "40")
  67. ctx := &context.Context{Input: i}
  68. ctrlr := Controller{Ctx: ctx}
  69. val, _ := ctrlr.GetInt64("age")
  70. if val != 40 {
  71. t.Errorf("TestGeetInt64 expect 40,get %T,%v", val, val)
  72. }
  73. }
  74. func TestGetUint8(t *testing.T) {
  75. i := context.NewInput()
  76. i.SetParam("age", strconv.FormatUint(math.MaxUint8, 10))
  77. ctx := &context.Context{Input: i}
  78. ctrlr := Controller{Ctx: ctx}
  79. val, _ := ctrlr.GetUint8("age")
  80. if val != math.MaxUint8 {
  81. t.Errorf("TestGetUint8 expect %v,get %T,%v", math.MaxUint8, val, val)
  82. }
  83. }
  84. func TestGetUint16(t *testing.T) {
  85. i := context.NewInput()
  86. i.SetParam("age", strconv.FormatUint(math.MaxUint16, 10))
  87. ctx := &context.Context{Input: i}
  88. ctrlr := Controller{Ctx: ctx}
  89. val, _ := ctrlr.GetUint16("age")
  90. if val != math.MaxUint16 {
  91. t.Errorf("TestGetUint16 expect %v,get %T,%v", math.MaxUint16, val, val)
  92. }
  93. }
  94. func TestGetUint32(t *testing.T) {
  95. i := context.NewInput()
  96. i.SetParam("age", strconv.FormatUint(math.MaxUint32, 10))
  97. ctx := &context.Context{Input: i}
  98. ctrlr := Controller{Ctx: ctx}
  99. val, _ := ctrlr.GetUint32("age")
  100. if val != math.MaxUint32 {
  101. t.Errorf("TestGetUint32 expect %v,get %T,%v", math.MaxUint32, val, val)
  102. }
  103. }
  104. func TestGetUint64(t *testing.T) {
  105. i := context.NewInput()
  106. i.SetParam("age", strconv.FormatUint(math.MaxUint64, 10))
  107. ctx := &context.Context{Input: i}
  108. ctrlr := Controller{Ctx: ctx}
  109. val, _ := ctrlr.GetUint64("age")
  110. if val != math.MaxUint64 {
  111. t.Errorf("TestGetUint64 expect %v,get %T,%v", uint64(math.MaxUint64), val, val)
  112. }
  113. }
  114. func TestAdditionalViewPaths(t *testing.T) {
  115. dir1 := "_beeTmp"
  116. dir2 := "_beeTmp2"
  117. defer os.RemoveAll(dir1)
  118. defer os.RemoveAll(dir2)
  119. dir1file := "file1.tpl"
  120. dir2file := "file2.tpl"
  121. genFile := func(dir string, name string, content string) {
  122. os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0777)
  123. if f, err := os.Create(filepath.Join(dir, name)); err != nil {
  124. t.Fatal(err)
  125. } else {
  126. defer f.Close()
  127. f.WriteString(content)
  128. f.Close()
  129. }
  130. }
  131. genFile(dir1, dir1file, `<div>{{.Content}}</div>`)
  132. genFile(dir2, dir2file, `<html>{{.Content}}</html>`)
  133. AddViewPath(dir1)
  134. AddViewPath(dir2)
  135. ctrl := Controller{
  136. TplName: "file1.tpl",
  137. ViewPath: dir1,
  138. }
  139. ctrl.Data = map[interface{}]interface{}{
  140. "Content": "value2",
  141. }
  142. if result, err := ctrl.RenderString(); err != nil {
  143. t.Fatal(err)
  144. } else {
  145. if result != "<div>value2</div>" {
  146. t.Fatalf("TestAdditionalViewPaths expect %s got %s", "<div>value2</div>", result)
  147. }
  148. }
  149. func() {
  150. ctrl.TplName = "file2.tpl"
  151. defer func() {
  152. if r := recover(); r == nil {
  153. t.Fatal("TestAdditionalViewPaths expected error")
  154. }
  155. }()
  156. ctrl.RenderString();
  157. }()
  158. ctrl.TplName = "file2.tpl"
  159. ctrl.ViewPath = dir2
  160. ctrl.RenderString();
  161. }