-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests to check config properties and tags
- Loading branch information
Showing
3 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package config | ||
|
||
import ( | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func Test_Constants_Properties(t *testing.T) { | ||
file, err := parser.ParseFile(&token.FileSet{}, "./constants.go", nil, 0) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
ast.Walk(&visitor{t: t}, file) | ||
} | ||
|
||
type visitor struct { | ||
config Config | ||
t *testing.T | ||
} | ||
|
||
func (v *visitor) Visit(node ast.Node) (w ast.Visitor) { | ||
if valueSpec, ok := node.(*ast.ValueSpec); ok { | ||
name := valueSpec.Names[0].Name | ||
literal := valueSpec.Values[0].(*ast.BasicLit) | ||
|
||
element := reflect.ValueOf(v.config) | ||
value := literal.Value[1 : len(literal.Value)-1] | ||
|
||
properties := strings.Split(value, ".") | ||
for _, property := range properties { | ||
if e, ok := findProperty(element, property); ok { | ||
element = e | ||
} else { | ||
v.t.Errorf("Property %s isn't defined in Config", name) | ||
break | ||
} | ||
} | ||
} | ||
return v | ||
} |