github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/starlib/encoding/starlarkproto/testdata/proto.star (about)

     1  # Tests of Starlark 'proto' extension.
     2  load("encoding/proto.star", "proto")
     3  
     4  def test_protos(t):
     5      s = struct(body = "hello")
     6      t.eq(s, s)
     7      print(s)
     8  
     9      # Prefer load by import path for dynamic protobuf support
    10      #m = proto("starlarkproto.test.Message", body="Hello, world!")
    11      #test = proto.package("starlarkproto.test")
    12      #test = 1
    13      test = proto.file("testpb/star.proto")
    14      m = test.Message(body = "Hello, world!")
    15      t.eq(m, m)
    16      t.eq(dir(m), ["body", "maps", "nested", "one_number", "one_string", "oneofs", "strings", "type"])
    17      print(m)
    18      t.eq(m.body, "Hello, world!")
    19  
    20      # Setting value asserts types
    21      def set_field_invalid():
    22          m.body = 2
    23  
    24      t.fails(set_field_invalid, "proto: *")
    25  
    26      # Enums
    27      enum = proto.new("starlarkproto.test.Enum")
    28      enum_a = enum(0)
    29      enum_a_alt = enum("ENUM_A")
    30      t.eq(enum_a, enum_a_alt)
    31  
    32      enum_file = test.Enum
    33      enum_b = enum_file(1)
    34      enum_b_alt = enum_file("ENUM_B")
    35      t.eq(enum_b, enum_b_alt)
    36      t.ne(enum_a, enum_b)
    37      #print("ENUMS", enum_a, enum_b)
    38  
    39      # Nested Enums
    40      message_unknown = test.Message.Type.UNKNOWN
    41      message_greeting = test.Message.Type.GREETING
    42      t.ne(message_unknown, message_greeting)
    43  
    44      # Enums can be assigned by String or Ints
    45      t.eq(m.type, message_unknown)
    46      m.type = "GREETING"
    47      t.eq(m.type, message_greeting)
    48      m.type = 0
    49      t.eq(m.type, message_unknown)
    50      m.type = message_greeting
    51      t.eq(m.type, message_greeting)
    52  
    53      # Lists are references
    54      b = m.strings
    55      b.append("hello")
    56      t.eq(m.strings[0], "hello")
    57  
    58      #print(m)
    59      b.extend(["world", "it", "is", "me"])
    60      t.eq(len(m.strings), 5)
    61      slice = m.strings[0:5:2]
    62      t.eq(slice, ["hello", "it", "me"])
    63      t.eq(len(m.strings), 5)
    64      m.strings = slice
    65      t.eq(len(m.strings), 3)
    66  
    67      # Message can be created from structs
    68      m.nested = struct(body = "struct", type = "GREETING")
    69      t.eq(m.nested.body, "struct")
    70      t.eq(m.nested.type, message_greeting)
    71      print(m)
    72  
    73      # Messages can be assigned None to delete
    74      m.nested = None
    75  
    76      #t.eq(m.nested, test.Message(None))  # None creates typed nil
    77      t.true(not m.nested, msg = "Nil RO type is falsy")  #
    78      t.eq(m.nested.nested.body, "")  # Recursive nil returns default types
    79  
    80      # Messages can be created from maps
    81      m.nested = {"body": "map"}
    82      t.eq(m.nested.body, "map")
    83      mmap = test.Message({"body": "new map"})
    84      t.eq(mmap.body, "new map")
    85  
    86      # Messages can be assigned Messages
    87      nested = test.Message(body = "nested")
    88      m.nested = nested
    89      print("here")
    90  
    91      # Maps shallow copy Dicts on set
    92      m.maps = {
    93          "hello": struct(body = "world!", type = "GREETING"),
    94      }
    95      print("here")
    96      print(m)
    97  
    98      # Oneofs
    99      m.one_string = "one dream"
   100      t.eq(m.one_string, "one dream")
   101      t.eq(m.one_number, 0)
   102      t.eq(m.oneofs, "one dream")
   103      m.one_number = 1
   104      t.eq(m.one_string, "")
   105      t.eq(m.one_number, 1)
   106      t.eq(m.oneofs, 1)
   107      #print(m)
   108  
   109      # Marshal/Unmarshal
   110      data = proto.marshal(m)
   111      m2 = test.Message()
   112      proto.unmarshal(data, m2)
   113      t.eq(m, m2)
   114  
   115      # print(proto.marshal_json(m))
   116      # print(proto.marshal_text(m))
   117  
   118  ##def test_load(t):
   119  ##    proto.load(library_bin)
   120  ##
   121  ##    apipb = proto.file("larking.examples.apipb")
   122  ##    book = apipb.Book(
   123  ##        name = req.name,
   124  ##        title = "A book appears!",
   125  ##        author = "starlark",
   126  ##    )
   127  ##    print("created book: %s" % book)