I'm generating some source code using the templates package( is there a better method? )and part of the testing I need to check if the output matches the expected source code.
- I tried a string comparison but it fails due the extra spaces / new lines generated by the templates package. I've also tried format.Source with not success. ( FAIL)
-
I tried to parse the ast of the both sources (see bellow) but the ast doesn't match either even if the code is basically same except the new lines / spaces. (FAIL)
package main
import ( "fmt" "go/parser" "go/token" "reflect" ) func main() { stub1 := `package main func myfunc(s string) error { return nil }` stub2 := `package main func myfunc(s string) error { return nil }` fset := token.NewFileSet() r1, err := parser.ParseFile(fset, "", stub1, parser.AllErrors) if err != nil { panic(err) } fset = token.NewFileSet() r2, err := parser.ParseFile(fset, "", stub2, parser.AllErrors) if err != nil { panic(err) } if !reflect.DeepEqual(r1, r2) { fmt.Printf("e %v, r %s, ", r1, r2) } }