Test various extend scenario
authorAlejandro R Sedeño <asedeno@google.com>
Fri, 14 Dec 2012 19:05:29 +0000 (14:05 -0500)
committerAlejandro R Sedeño <asedeno@google.com>
Mon, 17 Dec 2012 19:32:43 +0000 (14:32 -0500)
Inspired by:
https://developers.google.com/protocol-buffers/docs/proto#extensions

Nested Extensions:

"A common pattern is to define extensions inside the scope of the
 extension's field type – for example, here's an extension to Foo of
 type Baz, where the extension is defined as part of Baz"

tests/cl-protobufs-tests.asd
tests/extend-test-base.proto [new file with mode: 0644]
tests/extend-test.proto [new file with mode: 0644]
tests/lisp-extend-test.lisp [new file with mode: 0644]

index 98ca6fe..15e3331 100644 (file)
                  (:protobuf-file "forward_reference")
                  (:file "lisp-reference-tests")))
 
+     (module "nested-extend-test"
+             :serial t
+             :pathname #p""
+             :components
+               ((:protobuf-file "extend-test")
+                (:file "lisp-extend-test")))
+
      ;; Google's own protocol buffers and protobuf definitions tests
      #+++notyet
      (:module "google-tests-proto"
diff --git a/tests/extend-test-base.proto b/tests/extend-test-base.proto
new file mode 100644 (file)
index 0000000..fa56b26
--- /dev/null
@@ -0,0 +1,20 @@
+// Free Software published under an MIT-like license. See LICENSE
+//
+// Copyright (c) 2012 Google, Inc.  All rights reserved.
+//
+// Original author: Alejandro Sedeño
+
+syntax = "proto2";
+
+package protobuf_extend_base_unittest;
+
+message Foo {
+  extensions 100 to 199;
+}
+
+message Bar {
+}
+
+message Baz {
+  extensions 300 to 399;
+}
diff --git a/tests/extend-test.proto b/tests/extend-test.proto
new file mode 100644 (file)
index 0000000..f127af6
--- /dev/null
@@ -0,0 +1,43 @@
+// Free Software published under an MIT-like license. See LICENSE
+//
+// Copyright (c) 2012 Google, Inc.  All rights reserved.
+//
+// Original author: Alejandro Sedeño
+
+syntax = "proto2";
+
+import "extend-test-base.proto";
+
+package protobuf_extend_unittest;
+
+message Foo {
+  extensions 200 to 299;
+}
+
+message Bar {
+  // Extend file-local Foo with this Bar.
+  extend Foo {
+    optional Bar foo_227 = 227;
+  }
+  // Extend file-local Foo with imported Bar.
+  extend Foo {
+    optional protobuf_extend_base_unittest.Bar foo_228 = 228;
+  }
+  // Extend imported Foo with this Bar.
+  extend protobuf_extend_base_unittest.Foo {
+    optional Bar foo_127 = 127;
+  }
+  // Extend imported Foo with imported Bar.
+  extend protobuf_extend_base_unittest.Foo {
+    optional protobuf_extend_base_unittest.Bar foo_128 = 128;
+  }
+}
+
+// NB: Unlike Foo and Bar, no Quux is defined in our import.
+//     Unlike Foo and Bar, no Baz is defined in this proto file.
+message Quux {
+  // Extend imported Baz with self.
+  extend protobuf_extend_base_unittest.Baz {
+    optional Quux ext = 327;
+  }
+}
diff --git a/tests/lisp-extend-test.lisp b/tests/lisp-extend-test.lisp
new file mode 100644 (file)
index 0000000..6ebda96
--- /dev/null
@@ -0,0 +1,52 @@
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;                                                                  ;;;
+;;; Free Software published under an MIT-like license. See LICENSE   ;;;
+;;;                                                                  ;;;
+;;; Copyright (c) 2012 Google, Inc.  All rights reserved.            ;;;
+;;;                                                                  ;;;
+;;; Original author: Alejandro Sedeño                                ;;;
+;;;                                                                  ;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(in-package "PROTO-TEST")
+
+(define-test extend-test ()
+  (let* ((schema (proto:find-schema "ExtendTest"))
+         (imported-schema (proto:find-schema "ExtendTestBase"))
+         (foo (proto:find-message schema "Foo"))
+         (bar (proto:find-message schema "Bar"))
+         (quux (proto:find-message schema "Quux"))
+         (ifoo (proto:find-message imported-schema "Foo"))
+         (ibar (proto:find-message imported-schema "Bar"))
+         (ibaz (proto:find-message imported-schema "Baz")))
+    (destructuring-bind (local-local local-import import-local import-import)
+        (proto-impl:proto-extenders bar)
+      ;; Are we extending the right message?
+      (assert-equal (proto-impl:proto-class local-local)
+                    (proto-impl:proto-class foo))
+      (assert-equal (proto-impl:proto-class local-import)
+                    (proto-impl:proto-class foo))
+      (assert-equal (proto-impl:proto-class import-local)
+                    (proto-impl:proto-class ifoo))
+      (assert-equal (proto-impl:proto-class import-import)
+                    (proto-impl:proto-class ifoo))
+      ;; Is the extended field of the right type?
+      (assert-equal (proto-impl:proto-class
+                     (first (proto-impl:proto-extended-fields local-local)))
+                    (proto-impl:proto-class bar))
+      (assert-equal (proto-impl:proto-class
+                     (first (proto-impl:proto-extended-fields local-import)))
+                    (proto-impl:proto-class ibar))
+      (assert-equal (proto-impl:proto-class
+                     (first (proto-impl:proto-extended-fields import-local)))
+                    (proto-impl:proto-class bar))
+      (assert-equal (proto-impl:proto-class
+                     (first (proto-impl:proto-extended-fields import-import)))
+                    (proto-impl:proto-class ibar)))
+    ;; Smaller stand-alone test
+    (let ((ebaz (first (proto-extenders quux))))
+      (assert-equal (proto-impl:proto-class ebaz) (proto-impl:proto-class ibaz))
+      (assert-equal (proto-impl:proto-class (first (proto-extended-fields ebaz)))
+                    (proto-impl:proto-class quux)))))
+
+(register-test 'extend-test)