github.com/wiselike/revel-cmd@v1.2.1/parser/reflect_test.go (about) 1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved. 2 // Revel Framework source code and usage is governed by a MIT style 3 // license that can be found in the LICENSE file. 4 5 package parser_test 6 7 import ( 8 "go/ast" 9 "go/parser" 10 "go/token" 11 "reflect" 12 "strings" 13 "testing" 14 15 "github.com/wiselike/revel-cmd/model" 16 revelParser "github.com/wiselike/revel-cmd/parser" 17 ) 18 19 const validationKeysSource = ` 20 package test 21 22 func (c *Application) testFunc(a, b int, user models.User) revel.Result { 23 // Line 5 24 c.Validation.Required(a) 25 c.Validation.Required(a).Message("Error message") 26 c.Validation.Required(a). 27 Message("Error message") 28 29 // Line 11 30 c.Validation.Required(user.Name) 31 c.Validation.Required(user.Name).Message("Error message") 32 33 // Line 15 34 c.Validation.MinSize(b, 12) 35 c.Validation.MinSize(b, 12).Message("Error message") 36 c.Validation.MinSize(b, 37 12) 38 39 // Line 21 40 c.Validation.Required(b == 5) 41 } 42 43 func (m Model) Validate(v *revel.Validation) { 44 // Line 26 45 v.Required(m.name) 46 v.Required(m.name == "something"). 47 Message("Error Message") 48 v.Required(!m.bool) 49 } 50 ` 51 52 var expectedValidationKeys = []map[int]string{ 53 { 54 6: "a", 55 7: "a", 56 8: "a", 57 12: "user.Name", 58 13: "user.Name", 59 16: "b", 60 17: "b", 61 19: "b", 62 22: "b", 63 }, { 64 27: "m.name", 65 28: "m.name", 66 30: "m.bool", 67 }, 68 } 69 70 // This tests the recording of line number to validation key of the preceding 71 // example source. 72 func TestGetValidationKeys(t *testing.T) { 73 fset := token.NewFileSet() 74 75 file, err := parser.ParseFile(fset, "validationKeysSource", validationKeysSource, 0) 76 if err != nil { 77 t.Fatal(err) 78 } 79 if len(file.Decls) != 2 { 80 t.Fatal("Expected 2 decl in the source, found", len(file.Decls)) 81 } 82 83 for i, decl := range file.Decls { 84 lineKeys := revelParser.GetValidationKeys("test", fset, decl.(*ast.FuncDecl), map[string]string{"revel": model.RevelImportPath}) 85 for k, v := range expectedValidationKeys[i] { 86 if lineKeys[k] != v { 87 t.Errorf("Not found - %d: %v - Actual Map: %v", k, v, lineKeys) 88 } 89 } 90 91 if len(lineKeys) != len(expectedValidationKeys[i]) { 92 t.Error("Validation key map not the same size as expected:", lineKeys) 93 } 94 } 95 } 96 97 var TypeExprs = map[string]model.TypeExpr{ 98 "int": model.NewTypeExprFromData("int", "", 0, true), 99 "*int": model.NewTypeExprFromData("*int", "", 1, true), 100 "[]int": model.NewTypeExprFromData("[]int", "", 2, true), 101 "...int": model.NewTypeExprFromData("[]int", "", 2, true), 102 "[]*int": model.NewTypeExprFromData("[]*int", "", 3, true), 103 "...*int": model.NewTypeExprFromData("[]*int", "", 3, true), 104 "MyType": model.NewTypeExprFromData("MyType", "pkg", 0, true), 105 "*MyType": model.NewTypeExprFromData("*MyType", "pkg", 1, true), 106 "[]MyType": model.NewTypeExprFromData("[]MyType", "pkg", 2, true), 107 "...MyType": model.NewTypeExprFromData("[]MyType", "pkg", 2, true), 108 "[]*MyType": model.NewTypeExprFromData("[]*MyType", "pkg", 3, true), 109 "...*MyType": model.NewTypeExprFromData("[]*MyType", "pkg", 3, true), 110 "map[int]MyType": model.NewTypeExprFromData("map[int]MyType", "pkg", 8, true), 111 "map[int]*MyType": model.NewTypeExprFromData("map[int]*MyType", "pkg", 9, true), 112 } 113 114 func TestTypeExpr(t *testing.T) { 115 for typeStr, expected := range TypeExprs { 116 // Handle arrays and ... myself, since ParseExpr() does not. 117 array := strings.HasPrefix(typeStr, "[]") 118 if array { 119 typeStr = typeStr[2:] 120 } 121 122 ellipsis := strings.HasPrefix(typeStr, "...") 123 if ellipsis { 124 typeStr = typeStr[3:] 125 } 126 127 expr, err := parser.ParseExpr(typeStr) 128 if err != nil { 129 t.Error("Failed to parse test expr:", typeStr) 130 continue 131 } 132 133 if array { 134 expr = &ast.ArrayType{Lbrack: expr.Pos(), Len: nil, Elt: expr} 135 } 136 if ellipsis { 137 expr = &ast.Ellipsis{Ellipsis: expr.Pos(), Elt: expr} 138 } 139 140 actual := model.NewTypeExprFromAst("pkg", expr) 141 if !reflect.DeepEqual(expected, actual) { 142 t.Error("Fail, expected", expected, ", was", actual) 143 } 144 } 145 }