1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
4 #include "opentelemetry/trace/scope.h"
5 #include "opentelemetry/context/context.h"
6 #include "opentelemetry/nostd/shared_ptr.h"
7 #include "opentelemetry/trace/noop.h"
9 #include <gtest/gtest.h>
11 using opentelemetry::trace::kSpanKey
;
12 using opentelemetry::trace::NoopSpan
;
13 using opentelemetry::trace::Scope
;
14 using opentelemetry::trace::Span
;
15 namespace nostd
= opentelemetry::nostd
;
16 namespace context
= opentelemetry::context
;
18 TEST(ScopeTest
, Construct
)
20 nostd::shared_ptr
<Span
> span(new NoopSpan(nullptr));
23 context::ContextValue active_span_value
= context::RuntimeContext::GetValue(kSpanKey
);
24 ASSERT_TRUE(nostd::holds_alternative
<nostd::shared_ptr
<Span
>>(active_span_value
));
26 auto active_span
= nostd::get
<nostd::shared_ptr
<Span
>>(active_span_value
);
27 ASSERT_EQ(active_span
, span
);
30 TEST(ScopeTest
, Destruct
)
32 nostd::shared_ptr
<Span
> span(new NoopSpan(nullptr));
36 nostd::shared_ptr
<Span
> span_nested(new NoopSpan(nullptr));
37 Scope
scope_nested(span_nested
);
39 context::ContextValue active_span_value
= context::RuntimeContext::GetValue(kSpanKey
);
40 ASSERT_TRUE(nostd::holds_alternative
<nostd::shared_ptr
<Span
>>(active_span_value
));
42 auto active_span
= nostd::get
<nostd::shared_ptr
<Span
>>(active_span_value
);
43 ASSERT_EQ(active_span
, span_nested
);
46 context::ContextValue active_span_value
= context::RuntimeContext::GetValue(kSpanKey
);
47 ASSERT_TRUE(nostd::holds_alternative
<nostd::shared_ptr
<Span
>>(active_span_value
));
49 auto active_span
= nostd::get
<nostd::shared_ptr
<Span
>>(active_span_value
);
50 ASSERT_EQ(active_span
, span
);