...

Source file src/github.com/emicklei/proto/oneof_test.go

Documentation: github.com/emicklei/proto

     1  // Copyright (c) 2017 Ernest Micklei
     2  //
     3  // MIT License
     4  //
     5  // Permission is hereby granted, free of charge, to any person obtaining
     6  // a copy of this software and associated documentation files (the
     7  // "Software"), to deal in the Software without restriction, including
     8  // without limitation the rights to use, copy, modify, merge, publish,
     9  // distribute, sublicense, and/or sell copies of the Software, and to
    10  // permit persons to whom the Software is furnished to do so, subject to
    11  // the following conditions:
    12  //
    13  // The above copyright notice and this permission notice shall be
    14  // included in all copies or substantial portions of the Software.
    15  //
    16  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    17  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    18  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    19  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    20  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    21  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    22  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    23  
    24  package proto
    25  
    26  import "testing"
    27  
    28  func TestOneof(t *testing.T) {
    29  	proto := `oneof foo {
    30  	// just a name
    31      string	 name = 4;
    32      SubMessage sub_message = 9 [options=none];
    33  }`
    34  	p := newParserOn(proto)
    35  	p.next() // consume first token
    36  	o := new(Oneof)
    37  	err := o.parse(p)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	if got, want := o.Name, "foo"; got != want {
    42  		t.Errorf("got [%v] want [%v]", got, want)
    43  	}
    44  	if got, want := len(o.Elements), 2; got != want {
    45  		t.Fatalf("got [%v] want [%v]", got, want)
    46  	}
    47  	first := o.Elements[0].(*OneOfField)
    48  	if got, want := first.Comment.Message(), " just a name"; got != want {
    49  		t.Errorf("got [%v] want [%v]", got, want)
    50  	}
    51  	if got, want := first.Position.Line, 3; got != want {
    52  		t.Errorf("got [%v] want [%v]", got, want)
    53  	}
    54  	second := o.Elements[1].(*OneOfField)
    55  	if got, want := second.Name, "sub_message"; got != want {
    56  		t.Errorf("got [%v] want [%v]", got, want)
    57  	}
    58  	if got, want := second.Type, "SubMessage"; got != want {
    59  		t.Errorf("got [%v] want [%v]", got, want)
    60  	}
    61  	if got, want := second.Sequence, 9; got != want {
    62  		t.Errorf("got [%v] want [%v]", got, want)
    63  	}
    64  	if got, want := second.Position.Line, 4; got != want {
    65  		t.Errorf("got [%v] want [%v]", got, want)
    66  	}
    67  	checkParent(second.Options[0], t)
    68  }
    69  
    70  func TestFieldOneofImported(t *testing.T) {
    71  	fieldType := "foo.bar"
    72  	proto := `message Value {
    73  		oneof value {
    74  			` + fieldType + ` value = 1;
    75  		}
    76  	}`
    77  	p := newParserOn(proto)
    78  	def := new(Proto)
    79  	err := def.parse(p)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  	m := def.Elements[0].(*Message)
    84  	if len(m.Elements) != 1 {
    85  		t.Errorf("expected one element but got %d", len(m.Elements))
    86  	}
    87  	o := m.Elements[0].(*Oneof)
    88  	if len(o.Elements) != 1 {
    89  		t.Errorf("expected one element but got %d", len(o.Elements))
    90  	}
    91  	f := o.Elements[0].(*OneOfField)
    92  	if got, want := f.Type, fieldType; got != want {
    93  		t.Errorf("got [%v] want [%v]", got, want)
    94  	}
    95  	if got, want := f.Name, "value"; got != want {
    96  		t.Errorf("got [%v] want [%v]", got, want)
    97  	}
    98  }
    99  
   100  func TestFieldOneofDotImported(t *testing.T) {
   101  	fieldType := ".foo.bar"
   102  	proto := `message Value {
   103  		oneof value {
   104  			` + fieldType + ` value = 1;
   105  		}
   106  	}`
   107  	p := newParserOn(proto)
   108  	def := new(Proto)
   109  	err := def.parse(p)
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  	m := def.Elements[0].(*Message)
   114  	if len(m.Elements) != 1 {
   115  		t.Errorf("expected one element but got %d", len(m.Elements))
   116  	}
   117  	o := m.Elements[0].(*Oneof)
   118  	if len(o.Elements) != 1 {
   119  		t.Errorf("expected one element but got %d", len(o.Elements))
   120  	}
   121  	f := o.Elements[0].(*OneOfField)
   122  	if got, want := f.Type, fieldType; got != want {
   123  		t.Errorf("got [%v] want [%v]", got, want)
   124  	}
   125  	if got, want := f.Name, "value"; got != want {
   126  		t.Errorf("got [%v] want [%v]", got, want)
   127  	}
   128  }
   129  
   130  func TestOneOfWithOption(t *testing.T) {
   131  	src := `oneof AnOneof {
   132  		option (oneof_opt1) = -99;
   133  		int32 oneof_field = 2;
   134  	  }`
   135  	p := newParserOn(src)
   136  	p.next()
   137  	oneof := new(Oneof)
   138  	err := oneof.parse(p)
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  	option := oneof.Elements[0].(*Option)
   143  	if got, want := option.Name, "(oneof_opt1)"; got != want {
   144  		t.Errorf("got [%v] want [%v]", got, want)
   145  	}
   146  	checkParent(option, t)
   147  }
   148  
   149  func TestOneofInlineCommentBeforeBody(t *testing.T) {
   150  	src := `oneof BarOption // BarOption
   151  	  // with another line
   152  	  {
   153  		  name string = 1;
   154  	  } 
   155  	`
   156  	p := newParserOn(src)
   157  	oneof := new(Oneof)
   158  	p.next()
   159  	if err := oneof.parse(p); err != nil {
   160  		t.Fatal(err)
   161  	}
   162  	nestedComment := oneof.Elements[0].(*Comment)
   163  	if nestedComment == nil {
   164  		t.Fatal("expected comment present")
   165  	}
   166  	if got, want := len(nestedComment.Lines), 2; got != want {
   167  		t.Errorf("got %d want %d lines", got, want)
   168  	}
   169  }
   170  
   171  func TestOneOfDocumented(t *testing.T) {
   172  	src := `message Value {
   173  	// documented
   174  	oneof Foo {
   175  		int32 oneof_field = 1;
   176  	}
   177  }`
   178  	p := newParserOn(src)
   179  	def := new(Proto)
   180  	err := def.parse(p)
   181  	if err != nil {
   182  		t.Fatal(err)
   183  	}
   184  	m := def.Elements[0].(*Message)
   185  	if len(m.Elements) != 1 {
   186  		t.Errorf("expected one element but got %d", len(m.Elements))
   187  	}
   188  	o := m.Elements[0].(*Oneof)
   189  	if len(o.Elements) != 1 {
   190  		t.Errorf("expected one element but got %d", len(o.Elements))
   191  	}
   192  	if Documented(o).Doc() == nil {
   193  		t.Fatal("doc expected")
   194  	}
   195  }
   196  

View as plain text