shared_value_stores_test.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/base/shared_value_stores.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "testing/base/test_raw_ostream.h"
  8. #include "toolchain/testing/yaml_test_helpers.h"
  9. namespace Carbon::Testing {
  10. namespace {
  11. using ::testing::ElementsAre;
  12. using ::testing::IsEmpty;
  13. using ::testing::Pair;
  14. auto MatchSharedValues(testing::Matcher<Yaml::MappingValue> ints,
  15. testing::Matcher<Yaml::MappingValue> reals,
  16. testing::Matcher<Yaml::MappingValue> identifiers,
  17. testing::Matcher<Yaml::MappingValue> strings) -> auto {
  18. return Yaml::IsYaml(Yaml::Sequence(ElementsAre(Yaml::Mapping(ElementsAre(Pair(
  19. "shared_values",
  20. Yaml::Mapping(ElementsAre(Pair("ints", Yaml::Mapping(ints)),
  21. Pair("reals", Yaml::Mapping(reals)),
  22. Pair("identifiers", Yaml::Mapping(identifiers)),
  23. Pair("strings", Yaml::Mapping(strings))))))))));
  24. }
  25. TEST(SharedValueStores, PrintEmpty) {
  26. SharedValueStores value_stores;
  27. TestRawOstream out;
  28. value_stores.Print(out);
  29. EXPECT_THAT(Yaml::Value::FromText(out.TakeStr()),
  30. MatchSharedValues(IsEmpty(), IsEmpty(), IsEmpty(), IsEmpty()));
  31. }
  32. TEST(SharedValueStores, PrintVals) {
  33. SharedValueStores value_stores;
  34. llvm::APInt apint(64, 8, /*isSigned=*/true);
  35. value_stores.ints().Add(apint);
  36. value_stores.reals().Add(
  37. Real{.mantissa = apint, .exponent = apint, .is_decimal = true});
  38. value_stores.identifiers().Add("a");
  39. value_stores.string_literal_values().Add("foo'\"baz");
  40. TestRawOstream out;
  41. value_stores.Print(out);
  42. EXPECT_THAT(Yaml::Value::FromText(out.TakeStr()),
  43. MatchSharedValues(
  44. ElementsAre(Pair("int0", Yaml::Scalar("8"))),
  45. ElementsAre(Pair("real0", Yaml::Scalar("8*10^8"))),
  46. ElementsAre(Pair("identifier0", Yaml::Scalar("a"))),
  47. ElementsAre(Pair("string0", Yaml::Scalar("foo'\"baz")))));
  48. }
  49. } // namespace
  50. } // namespace Carbon::Testing