Commit | Line | Data |
---|---|---|
08b4db41 SM |
1 | /* |
2 | * SPDX-License-Identifier: GPL-2.0-only | |
3 | * | |
4 | * Copyright (C) 2023 EfficiOS Inc. | |
5 | */ | |
6 | ||
c802cacb SM |
7 | #include "common/assert.h" |
8 | ||
08b4db41 | 9 | #include "utils/run-in.hpp" |
c802cacb | 10 | |
08b4db41 | 11 | #include "tap/tap.h" |
08b4db41 | 12 | |
2dac3508 SM |
13 | namespace { |
14 | ||
15 | constexpr int NR_TESTS = 2; | |
08b4db41 | 16 | |
5d7e57e8 | 17 | class TestStringClear final : public RunIn |
08b4db41 | 18 | { |
5d7e57e8 SM |
19 | public: |
20 | void onMsgIterInit(const bt2::SelfMessageIterator self) override | |
21 | { | |
08b4db41 | 22 | /* Boilerplate to get a string field */ |
2dac3508 SM |
23 | const auto traceCls = self.component().createTraceClass(); |
24 | const auto streamCls = traceCls->createStreamClass(); | |
25 | const auto eventCls = streamCls->createEventClass(); | |
26 | const auto payloadCls = traceCls->createStructureFieldClass(); | |
08b4db41 | 27 | |
2dac3508 SM |
28 | payloadCls->appendMember("str", *traceCls->createStringFieldClass()); |
29 | eventCls->payloadFieldClass(*payloadCls); | |
08b4db41 | 30 | |
2dac3508 SM |
31 | const auto trace = traceCls->instantiate(); |
32 | const auto stream = streamCls->instantiate(*trace); | |
33 | const auto msg = self.createEventMessage(*eventCls, *stream); | |
34 | const auto field = (*msg->event().payloadField())["str"]->asString(); | |
08b4db41 SM |
35 | |
36 | /* Set the field to a known non-empty value */ | |
2dac3508 SM |
37 | *field = "pomme"; |
38 | BT_ASSERT(field.value() == "pomme"); | |
08b4db41 SM |
39 | |
40 | /* Clear the field, verify its value and length */ | |
2dac3508 SM |
41 | field.clear(); |
42 | ok(field.value() == "", "string field is empty"); | |
43 | ok(field.length() == 0, "string field length is 0"); | |
5d7e57e8 SM |
44 | } |
45 | }; | |
08b4db41 | 46 | |
2dac3508 SM |
47 | } /* namespace */ |
48 | ||
08b4db41 SM |
49 | int main() |
50 | { | |
51 | plan_tests(NR_TESTS); | |
52 | ||
5d7e57e8 SM |
53 | TestStringClear testStringClear; |
54 | runIn(testStringClear); | |
08b4db41 SM |
55 | |
56 | return exit_status(); | |
57 | } |