Home | History | Annotate | Download | only in examples
      1 package main
      2 
      3 import (
      4 	"strings"
      5 	"testing"
      6 
      7 	"github.com/golang/protobuf/proto"
      8 	pb "github.com/google/protobuf/examples/tutorial"
      9 )
     10 
     11 func TestPromptForAddressReturnsAddress(t *testing.T) {
     12 	in := `12345
     13 Example Name
     14 name (a] example.com
     15 123-456-7890
     16 home
     17 222-222-2222
     18 mobile
     19 111-111-1111
     20 work
     21 777-777-7777
     22 unknown
     23 
     24 `
     25 	got, err := promptForAddress(strings.NewReader(in))
     26 	if err != nil {
     27 		t.Fatalf("promptForAddress(%q) had unexpected error: %s", in, err.Error())
     28 	}
     29 	if got.Id != 12345 {
     30 		t.Errorf("promptForAddress(%q) got %d, want ID %d", in, got.Id, 12345)
     31 	}
     32 	if got.Name != "Example Name" {
     33 		t.Errorf("promptForAddress(%q) => want name %q, got %q", "Example Name", got.Name)
     34 	}
     35 	if got.Email != "name (a] example.com" {
     36 		t.Errorf("promptForAddress(%q) => want email %q, got %q", "name (a] example.com", got.Email)
     37 	}
     38 
     39 	want := []*pb.Person_PhoneNumber{
     40 		{Number: "123-456-7890", Type: pb.Person_HOME},
     41 		{Number: "222-222-2222", Type: pb.Person_MOBILE},
     42 		{Number: "111-111-1111", Type: pb.Person_WORK},
     43 		{Number: "777-777-7777", Type: pb.Person_MOBILE},
     44 	}
     45 	if len(got.Phones) != len(want) {
     46 		t.Errorf("want %d phone numbers, got %d", len(want), len(got.Phones))
     47 	}
     48 	phones := len(got.Phones)
     49 	if phones > len(want) {
     50 		phones = len(want)
     51 	}
     52 	for i := 0; i < phones; i++ {
     53 		if !proto.Equal(got.Phones[i], want[i]) {
     54 			t.Errorf("want phone %q, got %q", *want[i], *got.Phones[i])
     55 		}
     56 
     57 	}
     58 }
     59