serializable_test.cc 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <cstdlib>
  5. #include <string>
  6. #include "serializable.h"
  7. #include "test_platform.h"
  8. namespace crdtp {
  9. // =============================================================================
  10. // Serializable - An object to be emitted as a sequence of bytes.
  11. // =============================================================================
  12. namespace {
  13. // Tests ::Serialize (which invokes ::AppendSerialized).
  14. class SimpleExample : public Serializable {
  15. public:
  16. explicit SimpleExample(const std::vector<uint8_t>& contents)
  17. : contents_(contents) {}
  18. void AppendSerialized(std::vector<uint8_t>* out) const override {
  19. out->insert(out->end(), contents_.begin(), contents_.end());
  20. }
  21. private:
  22. std::vector<uint8_t> contents_;
  23. };
  24. } // namespace
  25. TEST(SerializableTest, YieldsContents) {
  26. std::vector<uint8_t> contents = {1, 2, 3};
  27. SimpleExample foo(contents);
  28. foo.AppendSerialized(&contents); // Yields contents by appending.
  29. EXPECT_THAT(contents, testing::ElementsAre(1, 2, 3, 1, 2, 3));
  30. // Yields contents by returning.
  31. EXPECT_THAT(foo.Serialize(), testing::ElementsAre(1, 2, 3));
  32. }
  33. } // namespace crdtp