...

Source file src/github.com/emicklei/proto/message_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 (
    27  	"testing"
    28  )
    29  
    30  func TestMessage(t *testing.T) {
    31  	proto := `
    32  		message   Out   {
    33  		// identifier
    34  		string   id  = 1;
    35  		// size
    36  		int64   size = 2;
    37  
    38  		oneof foo {
    39  			string     name        = 4;
    40  			SubMessage sub_message = 9;
    41  		}
    42  		message  Inner {   // Level 2
    43     			int64  ival = 1;
    44    		}
    45  		map<string, testdata.SubDefaults> proto2_value  =  13;
    46  		option  (my_option).a  =  true;
    47  	}`
    48  	p := newParserOn(proto)
    49  	p.next() // consume first token
    50  	m := new(Message)
    51  	err := m.parse(p)
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	if got, want := m.Name, "Out"; got != want {
    56  		t.Errorf("got [%v] want [%v]", got, want)
    57  	}
    58  	if got, want := len(m.Elements), 6; got != want {
    59  		t.Errorf("got [%v] want [%v]", got, want)
    60  	}
    61  	if got, want := m.Elements[0].(*NormalField).Position.String(), "<input>:4:3"; got != want {
    62  		t.Errorf("got [%v] want [%v]", got, want)
    63  	}
    64  	if got, want := m.Elements[0].(*NormalField).Comment.Position.String(), "<input>:3:3"; got != want {
    65  		t.Errorf("got [%v] want [%v]", got, want)
    66  	}
    67  	if got, want := m.Elements[3].(*Message).Position.String(), "<input>:12:3"; got != want {
    68  		t.Errorf("got [%v] want [%v]", got, want)
    69  	}
    70  	if got, want := m.Elements[3].(*Message).Elements[0].(*NormalField).Position.Line, 13; got != want {
    71  		t.Errorf("got [%v] want [%v]", got, want)
    72  	}
    73  	checkParent(m.Elements[5].(*Option), t)
    74  }
    75  
    76  func TestRepeatedGroupInMessage(t *testing.T) {
    77  	src := `message SearchResponse {
    78  		repeated group Result = 1 {
    79  		  required string url = 2;
    80  		  optional string title = 3;
    81  		  repeated string snippets = 4;
    82  		}
    83  	  }`
    84  	p := newParserOn(src)
    85  	p.next() // consume first token
    86  	m := new(Message)
    87  	err := m.parse(p)
    88  	if err != nil {
    89  		t.Error(err)
    90  	}
    91  	if got, want := len(m.Elements), 1; got != want {
    92  		t.Logf("%#v", m.Elements)
    93  		t.Fatalf("got [%v] want [%v]", got, want)
    94  	}
    95  	g := m.Elements[0].(*Group)
    96  	if got, want := len(g.Elements), 3; got != want {
    97  		t.Fatalf("got [%v] want [%v]", got, want)
    98  	}
    99  	if got, want := g.Repeated, true; got != want {
   100  		t.Fatalf("got Repeated [%v] want [%v]", got, want)
   101  	}
   102  
   103  }
   104  
   105  func TestRequiredGroupInMessage(t *testing.T) {
   106  	src := `message SearchResponse {
   107  		required group Result = 1 {
   108  		  required string url = 2;
   109  		  optional string title = 3;
   110  		  repeated string snippets = 4;
   111  		}
   112  	  }`
   113  	p := newParserOn(src)
   114  	p.next() // consume first token
   115  	m := new(Message)
   116  	err := m.parse(p)
   117  	if err != nil {
   118  		t.Error(err)
   119  	}
   120  	if got, want := len(m.Elements), 1; got != want {
   121  		t.Logf("%#v", m.Elements)
   122  		t.Fatalf("got [%v] want [%v]", got, want)
   123  	}
   124  	g := m.Elements[0].(*Group)
   125  	if got, want := len(g.Elements), 3; got != want {
   126  		t.Fatalf("got [%v] want [%v]", got, want)
   127  	}
   128  	if got, want := g.Required, true; got != want {
   129  		t.Fatalf("got Required [%v] want [%v]", got, want)
   130  	}
   131  
   132  }
   133  
   134  func TestSingleQuotedReservedNames(t *testing.T) {
   135  	src := `message Channel {
   136  		reserved '', 'things', "";
   137  	  }`
   138  	p := newParserOn(src)
   139  	p.next() // consume first token
   140  	m := new(Message)
   141  	err := m.parse(p)
   142  	if err != nil {
   143  		t.Error(err)
   144  	}
   145  	r := m.Elements[0].(*Reserved)
   146  	if got, want := r.FieldNames[0], ""; got != want {
   147  		t.Fatalf("got [%v] want [%v]", got, want)
   148  	}
   149  	if got, want := r.FieldNames[1], "things"; got != want {
   150  		t.Fatalf("got [%v] want [%v]", got, want)
   151  	}
   152  	if got, want := r.FieldNames[2], ""; got != want {
   153  		t.Fatalf("got [%v] want [%v]", got, want)
   154  	}
   155  }
   156  
   157  func TestMessageInlineCommentBeforeBody(t *testing.T) {
   158  	src := `message BarMessage // BarMessage
   159  	  // with another line
   160  	  {
   161  		  name string = 1;
   162  	  } 
   163  	`
   164  	p := newParserOn(src)
   165  	msg := new(Message)
   166  	p.next()
   167  	if err := msg.parse(p); err != nil {
   168  		t.Fatal(err)
   169  	}
   170  	nestedComment := msg.Elements[0].(*Comment)
   171  	if nestedComment == nil {
   172  		t.Fatal("expected comment present")
   173  	}
   174  	if got, want := len(nestedComment.Lines), 2; got != want {
   175  		t.Errorf("got %d want %d lines", got, want)
   176  	}
   177  }
   178  
   179  func TestMessageWithMessage(t *testing.T) {
   180  	src := `message message {
   181  		string message = 1;
   182  	}
   183  	`
   184  	p := newParserOn(src)
   185  	msg := new(Message)
   186  	p.next()
   187  	if err := msg.parse(p); err != nil {
   188  		t.Fatal(err)
   189  	}
   190  	if got, want := msg.Name, "message"; got != want {
   191  		t.Errorf("got %s want %s", got, want)
   192  	}
   193  	if got, want := len(msg.Elements), 1; got != want {
   194  		t.Errorf("got %d want %d elements", got, want)
   195  	}
   196  	f := msg.Elements[0].(*NormalField)
   197  	if got, want := f.Name, "message"; got != want {
   198  		t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
   199  	}
   200  }
   201  

View as plain text