Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(775)

Side by Side Diff: mojom/generators/go/templates/test_utils.go

Issue 2045063002: [New go generator] Implement declaring unions. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojom/generators/go/templates/templates.go ('k') | mojom/generators/go/templates/unions.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package templates 5 package templates
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "fmt" 9 "fmt"
10 "strings" 10 "strings"
11 "testing" 11 "testing"
12 12
13 "mojom/generators/go/gofmt" 13 "mojom/generators/go/gofmt"
14 "mojom/generators/go/translator"
15 ) 14 )
16 15
17 func check(t *testing.T, expected string, template string, input interface{}) { 16 func check(t *testing.T, expected string, template string, input interface{}) {
18 buffer := &bytes.Buffer{} 17 buffer := &bytes.Buffer{}
19 if err := goFileTmpl.ExecuteTemplate(buffer, template, input); err != ni l { 18 if err := goFileTmpl.ExecuteTemplate(buffer, template, input); err != ni l {
20 panic(err) 19 panic(err)
21 } 20 }
22 21
22 expected, err := gofmt.FormatFragment(expected)
23 if err != nil {
24 panic(err)
25 }
26
23 src := buffer.String() 27 src := buffer.String()
24 actual, err := gofmt.FormatFragment(src) 28 actual, err := gofmt.FormatFragment(src)
25 if err != nil { 29 if err != nil {
26 t.Fatalf("Formatting failed: %s\n%s\n", err, src) 30 t.Fatalf("Formatting failed: %s\n%s\n", err, src)
27 } 31 }
28 32
29 if expected != actual { 33 if expected != actual {
30 errorMsg := fmt.Sprintf("Failed check: Expected\n%s\nActual\n%s\ n", expected, actual) 34 errorMsg := fmt.Sprintf("Failed check: Expected\n%s\nActual\n%s\ n", expected, actual)
31 if strings.TrimSpace(expected) == strings.TrimSpace(actual) { 35 if strings.TrimSpace(expected) == strings.TrimSpace(actual) {
32 errorMsg = fmt.Sprintf("%s\nTrailing or leading spaces d iffer.\n", errorMsg) 36 errorMsg = fmt.Sprintf("%s\nTrailing or leading spaces d iffer.\n", errorMsg)
33 } 37 }
34 t.Fatalf(errorMsg) 38 t.Fatalf(errorMsg)
35 } 39 }
36 } 40 }
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 }
OLDNEW
« no previous file with comments | « mojom/generators/go/templates/templates.go ('k') | mojom/generators/go/templates/unions.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698