| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package templates | |
| 6 | |
| 7 import ( | |
| 8 "bytes" | |
| 9 "fmt" | |
| 10 "strings" | |
| 11 "testing" | |
| 12 | |
| 13 "mojom/generators/go/gofmt" | |
| 14 "mojom/generators/go/translator" | |
| 15 ) | |
| 16 | |
| 17 func check(t *testing.T, expected string, template string, input interface{}) { | |
| 18 buffer := &bytes.Buffer{} | |
| 19 if err := goFileTmpl.ExecuteTemplate(buffer, template, input); err != ni
l { | |
| 20 panic(err) | |
| 21 } | |
| 22 | |
| 23 src := buffer.String() | |
| 24 actual, err := gofmt.FormatFragment(src) | |
| 25 if err != nil { | |
| 26 t.Fatalf("Formatting failed: %s\n%s\n", err, src) | |
| 27 } | |
| 28 | |
| 29 if expected != actual { | |
| 30 errorMsg := fmt.Sprintf("Failed check: Expected\n%s\nActual\n%s\
n", expected, actual) | |
| 31 if strings.TrimSpace(expected) == strings.TrimSpace(actual) { | |
| 32 errorMsg = fmt.Sprintf("%s\nTrailing or leading spaces d
iffer.\n", errorMsg) | |
| 33 } | |
| 34 t.Fatalf(errorMsg) | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 func TestStructDecl(t *testing.T) { | |
| 39 expected := `type Foo struct { | |
| 40 Alpha string | |
| 41 Beta uint32 | |
| 42 }` | |
| 43 | |
| 44 s := translator.StructTemplate{ | |
| 45 Name: "Foo", | |
| 46 Fields: []translator.StructFieldTemplate{ | |
| 47 {Name: "Alpha", Type: "string"}, | |
| 48 {Name: "Beta", Type: "uint32"}, | |
| 49 }, | |
| 50 } | |
| 51 | |
| 52 check(t, expected, "StructDecl", s) | |
| 53 } | |
| OLD | NEW |