The problem: Many tests (unit-tests and browser tests) or test-only classes define string literals that represent Gaia IDs in a way that the literal is initialized statically (constexpr or otherwise). There are 500+ explicit casts to GaiaId in tests, excluding iOS code (which hasn't been updated yet). One illustrative example is FakeGaiaMixin::kFakeUserGaiaId, with 24 references, all of which convert to GaiaId. It would be nice (more readable, less error-prone) if those literals could be defined using class GaiaId directly.
The solution being considered: one idea that I thought of was to defined a test-only class, say class GaiaId::LiteralForTest,
that has a trivial destructor (e.g. encapsulates a std::string_view) and converts implicitly to GaiaId. This works (see patch) but goes against the style guide and also generally it isn't clear if it improves or harms readability.
On Tue, Jan 14, 2025 at 7:41 AM Mikel Astiz <mas...@chromium.org> wrote:The problem: Many tests (unit-tests and browser tests) or test-only classes define string literals that represent Gaia IDs in a way that the literal is initialized statically (constexpr or otherwise). There are 500+ explicit casts to GaiaId in tests, excluding iOS code (which hasn't been updated yet). One illustrative example is FakeGaiaMixin::kFakeUserGaiaId, with 24 references, all of which convert to GaiaId. It would be nice (more readable, less error-prone) if those literals could be defined using class GaiaId directly.To push on this a little: Would it be less error-prone? Have you had problems with tests accidentally using the wrong string as a GAIA ID?
For readability, it seems like we have:constexpr char kId[] = "...";foo(GaiaId(kId));vs.constexpr GaiaIdLiteral kId = "...";foo(kId);
I do think the latter is better. I don't know that it's a very large difference.The solution being considered: one idea that I thought of was to defined a test-only class, say class GaiaId::LiteralForTest,Trivial: I'd omit things like "ForTest" from the class name because there's no reason the type cares where it's used, and it makes the code less noisy.
that has a trivial destructor (e.g. encapsulates a std::string_view) and converts implicitly to GaiaId. This works (see patch) but goes against the style guide and also generally it isn't clear if it improves or harms readability.I'm not too worried about the implicit conversion, because these two objects are meant to represent the same conceptual thing, so an implicit conversion is appropriate.
I think the big question is whether this buys much readability and is worth the churn of trying to do this.
A question: how would you feel if there were several hundred instances each of (using this, not using it) in tests? I think whether you consider that world a win over the current one informs how to proceed here.
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAAy826cZFXgiHpWwro_cMGMujh1zSAW94TrPnqYikSfJVwNpBA%40mail.gmail.com.
To view this discussion visit https://groups.google.com/a/chromium.org/d/msgid/cxx/17dde71f-0427-4f1d-b6f4-78a0f5ab8aean%40chromium.org.
How many of the tests in question can declare GaiaId's as fields of their fixture?
It's not the greatest workaround, but it's one way I've used to work around having test constants with non-trivial destructors.
DanielP.S. you didn't hear it from me but we don't actually warn about exit-time destructors in tests. The style guide does not provide an exception though :)
On Wed, 15 Jan 2025 at 08:52, John Admanski <jadm...@chromium.org> wrote:Note that std::string (or any class) having a constexpr constructor and destructor is not sufficient for it to be usable for making static constexpr objects. If your class dynamically allocates memory then it can't be used as a constexpr variable regardless of the constexpr-ness of its construction and destruction.On Wednesday, January 15, 2025 at 8:30:38 AM UTC-8 dani...@webrtc.org wrote:Note that std::string has constexpr constructor and destructor since c++20Have you tried to declare GaiaId constructor (and destructor) constexpr?