Created
October 24, 2022 10:40
-
-
Save zdunecki/47dedf369c973e6f23e79624f47e3819 to your computer and use it in GitHub Desktop.
Check composition variables in go template engine
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
func TestTemplateCompositionStruct(t *testing.T) { | |
type Deep struct { | |
Name string | |
} | |
type M struct { | |
Deep | |
} | |
var b bytes.Buffer | |
te, _ := template.New("").Parse("You have new alert {{.Name}}") | |
data := &M{ | |
Deep{Name: "Something went wrong"}, | |
} | |
te.Execute(&b, &data) | |
current := b.String() | |
should := "You have new alert Something went wrong" | |
if current != should { | |
t.Fatal(fmt.Sprintf("'%s'", current), "is not equal", fmt.Sprintf("'%s'", should)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment