Home | History | Annotate | Download | only in parser
      1 // Copyright 2014 Google Inc. All rights reserved.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package parser
     16 
     17 import (
     18 	"bytes"
     19 	"testing"
     20 )
     21 
     22 var validPrinterTestCases = []struct {
     23 	input  string
     24 	output string
     25 }{
     26 	{
     27 		input: `
     28 foo {}
     29 `,
     30 		output: `
     31 foo {}
     32 `,
     33 	},
     34 	{
     35 		input: `
     36 foo{name= "abc",}
     37 `,
     38 		output: `
     39 foo {
     40     name: "abc",
     41 }
     42 `,
     43 	},
     44 	{
     45 		input: `
     46 			foo {
     47 				stuff: ["asdf", "jkl;", "qwert",
     48 					"uiop", "bnm,"]
     49 			}
     50 			`,
     51 		output: `
     52 foo {
     53     stuff: [
     54         "asdf",
     55         "bnm,",
     56         "jkl;",
     57         "qwert",
     58         "uiop",
     59     ],
     60 }
     61 `,
     62 	},
     63 	{
     64 		input: `
     65 		foo {
     66 			stuff: {
     67 				isGood: true,
     68 				name: "bar"
     69 			}
     70 		}
     71 		`,
     72 		output: `
     73 foo {
     74     stuff: {
     75         isGood: true,
     76         name: "bar",
     77     },
     78 }
     79 `,
     80 	},
     81 	{
     82 		input: `
     83 // comment1
     84 foo {
     85 	// comment2
     86 	isGood: true,  // comment3
     87 }
     88 `,
     89 		output: `
     90 // comment1
     91 foo {
     92     // comment2
     93     isGood: true, // comment3
     94 }
     95 `,
     96 	},
     97 	{
     98 		input: `
     99 foo {
    100 	name: "abc",
    101 }
    102 
    103 bar  {
    104 	name: "def",
    105 }
    106 		`,
    107 		output: `
    108 foo {
    109     name: "abc",
    110 }
    111 
    112 bar {
    113     name: "def",
    114 }
    115 `,
    116 	},
    117 	{
    118 		input: `
    119 foo = "stuff"
    120 bar = foo
    121 baz = foo + bar
    122 baz += foo
    123 `,
    124 		output: `
    125 foo = "stuff"
    126 bar = foo
    127 baz = foo + bar
    128 baz += foo
    129 `,
    130 	},
    131 	{
    132 		input: `
    133 //test
    134 test /* test */ {
    135     srcs: [
    136         /*"bootstrap/bootstrap.go",
    137     "bootstrap/cleanup.go",*/
    138         "bootstrap/command.go",
    139         "bootstrap/doc.go", //doc.go
    140         "bootstrap/config.go", //config.go
    141     ],
    142     deps: ["libabc"],
    143     incs: []
    144 } //test
    145 //test
    146 test2 {
    147 }
    148 
    149 
    150 //test3
    151 `,
    152 		output: `
    153 //test
    154 test /* test */ {
    155     srcs: [
    156         /*"bootstrap/bootstrap.go",
    157         "bootstrap/cleanup.go",*/
    158         "bootstrap/command.go",
    159         "bootstrap/config.go", //config.go
    160         "bootstrap/doc.go", //doc.go
    161     ],
    162     deps: ["libabc"],
    163     incs: [],
    164 } //test
    165 
    166 //test
    167 test2 {
    168 }
    169 
    170 //test3
    171 `,
    172 	},
    173 	{
    174 		input: `
    175 // test
    176 module // test
    177 
    178  {
    179     srcs
    180    : [
    181         "src1.c",
    182         "src2.c",
    183     ],
    184 //test
    185 }
    186 //test2
    187 `,
    188 		output: `
    189 // test
    190 module { // test
    191 
    192     srcs: [
    193         "src1.c",
    194         "src2.c",
    195     ],
    196     //test
    197 }
    198 
    199 //test2
    200 `,
    201 	},
    202 	{
    203 		input: `
    204 /*test {
    205     test: true,
    206 }*/
    207 
    208 test {
    209 /*test: true,*/
    210 }
    211 
    212 // This
    213 /* Is */
    214 // A
    215 
    216 // Multiline
    217 // Comment
    218 
    219 test {}
    220 
    221 // This
    222 /* Is */
    223 // A
    224 // Trailing
    225 
    226 // Multiline
    227 // Comment
    228 `,
    229 		output: `
    230 /*test {
    231     test: true,
    232 }*/
    233 
    234 test {
    235     /*test: true,*/
    236 }
    237 
    238 // This
    239 /* Is */
    240 // A
    241 
    242 // Multiline
    243 // Comment
    244 
    245 test {}
    246 
    247 // This
    248 /* Is */
    249 // A
    250 // Trailing
    251 
    252 // Multiline
    253 // Comment
    254 `,
    255 	},
    256 }
    257 
    258 func TestPrinter(t *testing.T) {
    259 	for _, testCase := range validPrinterTestCases {
    260 		in := testCase.input[1:]
    261 		expected := testCase.output[1:]
    262 
    263 		r := bytes.NewBufferString(in)
    264 		file, errs := Parse("", r, NewScope(nil))
    265 		if len(errs) != 0 {
    266 			t.Errorf("test case: %s", in)
    267 			t.Errorf("unexpected errors:")
    268 			for _, err := range errs {
    269 				t.Errorf("  %s", err)
    270 			}
    271 			t.FailNow()
    272 		}
    273 
    274 		SortLists(file)
    275 
    276 		got, err := Print(file)
    277 		if err != nil {
    278 			t.Errorf("test case: %s", in)
    279 			t.Errorf("unexpected error: %s", err)
    280 			t.FailNow()
    281 		}
    282 
    283 		if string(got) != expected {
    284 			t.Errorf("test case: %s", in)
    285 			t.Errorf("  expected: %s", expected)
    286 			t.Errorf("       got: %s", string(got))
    287 		}
    288 	}
    289 }
    290