Skip to content

[Bug]: Status matchers: no support for printing std::wstring #1951

@phst

Description

@phst

Describe the issue

When matching a StatusOr<wstring> with IsOkAndHolds, Googletest won't print the actual string contents.

Steps to reproduce the problem

A Bazel workspace with:

MODULE.bazel:

bazel_dep(name = "rules_cc", version = "0.2.9")
bazel_dep(name = "googletest", version = "1.17.0.bcr.1")
bazel_dep(name = "abseil-cpp", version = "20250814.1")

BUILD.bazel:

load("@rules_cc//cc:cc_test.bzl", "cc_test")
cc_test(
    name = "test",
    srcs = ["test.cc"],
    deps = [
        "@abseil-cpp//absl/status:status_matchers",
        "@abseil-cpp//absl/status:statusor",
        "@googletest//:gtest",
        "@googletest//:gtest_main",
    ],
)

test.cc:

#include <string>

#include "absl/status/statusor.h"
#include "absl/status/status_matchers.h"
#include "gtest/gtest.h"

TEST(Test, WString) {
  EXPECT_THAT(std::wstring(L"foo"), testing::Eq(L"bar"));
  EXPECT_THAT(std::wstring(L"foo"), testing::Eq(std::wstring(L"bar")));
  EXPECT_THAT(absl::StatusOr<std::wstring>(std::wstring(L"foo")),
              absl_testing::IsOkAndHolds(L"bar"));
  EXPECT_THAT(absl::StatusOr<std::wstring>(std::wstring(L"foo")),
              absl_testing::IsOkAndHolds(std::wstring(L"bar")));
}

Running the test results in the following output:

Executing tests from //:test
-----------------------------------------------------------------------------
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from Test
[ RUN      ] Test.WString
test.cc:8: Failure
Value of: std::wstring(L"foo")
Expected: is equal to 0x100d23a10 pointing to L"bar"
  Actual: L"foo"

test.cc:9: Failure
Value of: std::wstring(L"foo")
Expected: is equal to L"bar"
  Actual: L"foo"

test.cc:11: Failure
Value of: absl::StatusOr<std::wstring>(std::wstring(L"foo"))
Expected: is OK and has a value that is equal to L"bar"
  Actual: 32-byte object <01-00 00-00 00-00 00-00 66-00 00-00 6F-00 00-00 6F-00 00-00 00-00 00-00 68-ED F2-00 01-00 00-03>

test.cc:13: Failure
Value of: absl::StatusOr<std::wstring>(std::wstring(L"foo"))
Expected: is OK and has a value that is equal to L"bar"
  Actual: 32-byte object <01-00 00-00 00-00 00-00 66-00 00-00 6F-00 00-00 6F-00 00-00 00-00 00-00 00-E9 94-80 01-00 00-03>

[  FAILED  ] Test.WString (0 ms)
[----------] 1 test from Test (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] Test.WString

 1 FAILED TEST

Note that the normal string comparison (line 8 and 9) prints as expected, while the StatusOr check results in an unhelpful "Actual" message since apparently Googletest doesn't know how to print StatusOr<wstring>.

What version of Abseil are you using?

20250814.1

What operating system and version are you using?

macOS Tahoe

What compiler and version are you using?

Apple clang version 17.0.0 (clang-1700.3.19.1)
Target: arm64-apple-darwin25.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

What build system are you using?

bazel 8.4.2

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions