| OLD | NEW |
| 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 formatter | 5 package formatter |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "strings" | 8 "strings" |
| 9 "testing" | 9 "testing" |
| 10 ) | 10 ) |
| 11 | 11 |
| 12 func TestFormatMojom(t *testing.T) { | 12 func TestFormatMojom(t *testing.T) { |
| 13 original := `// Top line comment. | 13 original := `// Top line comment. |
| 14 [Attr1=1, | 14 [Attr1=1, |
| 15 Attr2=2] | 15 Attr2=2] |
| 16 /* left comment */ module hello.world; // Right comment | 16 /* left comment */ module hello.world; // Right comment |
| 17 | 17 |
| 18 // First block line 1 | 18 // First block line 1 |
| 19 // First block line 2 | 19 // First block line 2 |
| 20 | 20 |
| 21 // Second block line 1 | 21 // Second block line 1 |
| 22 // Second block line 2 | 22 // Second block line 2 |
| 23 import "foo1.mojom"; | 23 import "foo1.mojom"; |
| 24 import "foo2.mojom"; | 24 import "foo2.mojom"; |
| 25 | 25 |
| 26 import "afoo1.mojom"; | 26 import "afoo1.mojom"; |
| 27 import "afoo2.mojom"; | 27 import "afoo2.mojom"; |
| 28 | 28 |
| 29 [Attr1=1, | 29 [Attr1=1, |
| 30 Attr2=2] | 30 Attr2=2] |
| 31 struct FooStruct { // FooStruct comment. | 31 struct FooStruct { // FooStruct comment. |
| 32 // Field 1 comment. | 32 // Field 1 comment. |
| 33 int8 field1; // Field 1 comment. | 33 int8 field1; // Field 1 comment. |
| 34 int16 field2 = 10; | 34 int16 field2 = 10; |
| 35 // Field3 comment. | 35 // Field3 comment. |
| 36 // Field3 other comment. | 36 // Field3 other comment. |
| 37 int32 field3@10 = 10; | 37 int32 field3@10 = 10; |
| 38 }; | 38 }; |
| 39 | 39 |
| 40 struct FooUnion { | 40 struct FooUnion { |
| 41 int8 field1; | 41 int8 field1; |
| 42 int16 field2@5; | 42 int16 field2@5; |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 enum FooEnum { | 45 enum FooEnum { |
| 46 » VALUE1, // VALUE1 comment. | 46 » VALUE1, // VALUE1 comment. |
| 47 » VALUE2 = 10, // VALUE2 comment. | 47 » VALUE2 = 10, // VALUE2 comment. |
| 48 | 48 |
| 49 » // FooEnum Final Comment. | 49 // FooEnum Final Comment. |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 // constant comment. | 52 // constant comment. |
| 53 const int8 foo_constant1 = 10; // constant comment. | 53 const int8 foo_constant1 = 10; // constant comment. |
| 54 const int8 foo_constant2 = -10; // constant comment. | 54 const int8 foo_constant2 = -10; // constant comment. |
| 55 const float foo_constant3 = -10e10; // constant comment. | 55 const float foo_constant3 = -10e10; // constant comment. |
| 56 const int64 foo_constant4 = 0xAD10; // constant comment. | 56 const int64 foo_constant4 = 0xAD10; // constant comment. |
| 57 const int64 foo_constant5 = -0xAD10; // constant comment. | 57 const int64 foo_constant5 = -0xAD10; // constant comment. |
| 58 | 58 |
| 59 // Interface Comment. | 59 // Interface Comment. |
| 60 interface InterfaceFoo { // Interface comment. | 60 interface InterfaceFoo { // Interface comment. |
| 61 const int8 const_in_interface = 20; | 61 const int8 const_in_interface = 20; |
| 62 | 62 |
| 63 » // Method 1 comment. | 63 // Method 1 comment. |
| 64 » method1@5(int8 hello@10); | 64 method1@5(int8 hello@10); |
| 65 » // Method 2 comment. | 65 // Method 2 comment. |
| 66 » method2([MinVersion=5]int8 hello) => (Foo bar@20); | 66 » method2([MinVersion=5] int8 hello) => (Foo bar@20); |
| 67 method3(); | 67 method3(); |
| 68 method4() => (Foo bar); | 68 method4() => (Foo bar); |
| 69 » method5(int8 p1 /* p1 comment */, int16 p2); // method comment | 69 » method5(int8 p1 /* p1 comment */, int16 p2); // method comment |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 // Final Comments. | 72 // Final Comments. |
| 73 ` | 73 ` |
| 74 | 74 |
| 75 // TODO(azani): Remove this and just fix the tabs. | 75 // TODO(azani): Remove this and just fix the tabs. |
| 76 original = strings.Replace(original, "\t", " ", -1) | 76 original = strings.Replace(original, "\t", " ", -1) |
| 77 original = strings.Replace(original, "\t//", " //", -1) |
| 77 | 78 |
| 78 actual, err := FormatMojom("test.mojom", original) | 79 actual, err := FormatMojom("test.mojom", original) |
| 79 if err != nil { | 80 if err != nil { |
| 80 t.Fatalf("Parser was not supposed to fail: %s", err.Error()) | 81 t.Fatalf("Parser was not supposed to fail: %s", err.Error()) |
| 81 } | 82 } |
| 82 | 83 |
| 83 if original != actual { | 84 if original != actual { |
| 84 t.Fatalf("\nExpected:\n%v\n\n*****\n\nActual:\n%v eof", original
, actual) | 85 t.Fatalf("\nExpected:\n%v\n\n*****\n\nActual:\n%v eof", original
, actual) |
| 85 } | 86 } |
| 86 } | 87 } |
| OLD | NEW |