Shaka Player Embedded
test_type.h
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_JS_TEST_TYPE_H_
16 #define SHAKA_EMBEDDED_JS_TEST_TYPE_H_
17 
18 #include <string>
19 #include <unordered_map>
20 #include <vector>
21 
22 #include "shaka/optional.h"
23 #include "shaka/variant.h"
24 #include "src/core/member.h"
25 #include "src/mapping/any.h"
29 #include "src/mapping/callback.h"
30 #include "src/mapping/enum.h"
31 #include "src/mapping/promise.h"
32 #include "src/mapping/struct.h"
33 
34 namespace shaka {
35 namespace js {
36 
37 // A Struct type that is used by TestType.
38 struct TestTypeOptions : public Struct {
40 
41  ADD_DICT_FIELD(string, std::string);
42  ADD_DICT_FIELD(boolean, bool);
43  ADD_DICT_FIELD(any, Any);
44 };
45 
46 enum class TestNumberEnum {
47  FIRST = 1,
48  SECOND,
49 };
50 
51 enum class TestStringEnum {
52  EMPTY,
53  AUTO,
54  OTHER,
55 };
56 
57 constexpr const int EXPECTED_INT = 123;
60 // Used to verify that Unicode characters and embedded nulls are converted
61 // correctly.
62 constexpr const char EXPECTED_STRING[] = "ab\xe2\x8d\x85_\0_\xf0\x90\x90\xb7!";
63 
64 
69 class TestType : public BackingObject {
71 
72  public:
73  TestType();
74 
75  static TestType* Create() {
76  return new TestType();
77  }
78 
79  void Trace(memory::HeapTracer* tracer) const override;
80 
81  void AcceptNumber(double) const {}
82  void AcceptBoolean(bool) const {}
83  void AcceptString(const std::string&) const {}
91  void AcceptArrayOfStrings(std::vector<std::string>) const {}
92  void AcceptCallback(Callback) const {}
93  void AcceptAnything(Any) const {}
95 
96  // These return true if the given value is an expected value (the constants
97  // above).
98  bool IsExpectedString(const std::string& arg) const;
99  bool IsOptionalPresent(optional<std::string> arg) const;
100  bool IsExpectedIntWithOr(variant<int, Any> arg) const;
101  bool IsExpectedStructWithOr(variant<int, TestTypeOptions> arg) const;
102  bool IsExpectedConvertedStruct(TestTypeOptions opts) const;
103  bool IsConvertedStructEmpty(TestTypeOptions opts) const;
104  bool IsExpectedNumberEnum(TestNumberEnum e) const;
105  bool IsExpectedStringEnum(TestStringEnum e) const;
106  bool IsExpectedArrayOfStrings(const std::vector<std::string>& data) const;
107  bool IsExpectedStringWithAny(Any anything) const;
108  bool IsTruthy(Any anything) const;
109 
110  void InvokeCallbackWithString(Callback callback) const;
111  void StoreByteBuffer(ByteBuffer buffer);
112  TestTypeOptions ChangeStringField(TestTypeOptions opts);
113 
114  ExceptionOr<void> ThrowException(const std::string& message) const;
115 
116  Promise PromiseAcceptString(const std::string& value) const;
117  Promise PromiseResolveWith(Any value) const;
118  Promise PromiseResolveAfter(uint64_t delay) const;
119 
120  std::string GetString() const;
121  optional<std::string> GetOptionalString(bool has_value) const;
122  variant<int, std::string> GetIntOrString(bool get_int) const;
123  TestTypeOptions GetStruct() const;
124  TestNumberEnum GetNumberEnum() const;
125  TestStringEnum GetStringEnum() const;
126  std::vector<std::string> GetArrayOfStrings() const;
127  std::unordered_map<std::string, std::string> GetMapOfStrings() const;
128  const ByteBuffer& GetByteBuffer() const;
129 
130  std::string ToPrettyString(Any anything) const;
131 
135  std::vector<Any> array;
139 };
140 
141 class TestTypeFactory : public BackingObjectFactory<TestType> {
142  public:
143  TestTypeFactory();
144 };
145 
146 } // namespace js
147 } // namespace shaka
148 
150 
152  AddMapping(Enum::EMPTY, "");
153  AddMapping(Enum::AUTO, "auto");
154  AddMapping(Enum::OTHER, "other");
155 }
156 
157 #endif // SHAKA_EMBEDDED_JS_TEST_TYPE_H_
void AcceptIntOrStruct(variant< int, TestTypeOptions >) const
Definition: test_type.h:86
void AcceptStringEnumOrAnyNumber(variant< TestStringEnum, double >) const
Definition: test_type.h:87
TestTypeOptions struct_
Definition: test_type.h:134
Definition: any.h:31
TestNumberEnum
Definition: test_type.h:46
void AcceptByteBuffer(ByteBuffer) const
Definition: test_type.h:94
constexpr const TestNumberEnum EXPECTED_NUMBER_ENUM
Definition: test_type.h:58
TestStringEnum
Definition: test_type.h:51
DEFINE_ENUM_MAPPING(shaka::js, TestStringEnum)
Definition: test_type.h:151
optional< Any > optional_object
Definition: test_type.h:132
void AcceptOptionalString(optional< std::string >) const
Definition: test_type.h:84
std::vector< Any > array
Definition: test_type.h:135
constexpr const TestStringEnum EXPECTED_STRING_ENUM
Definition: test_type.h:59
void AcceptOptionalStruct(optional< TestTypeOptions >) const
Definition: test_type.h:85
void AcceptArrayOfStrings(std::vector< std::string >) const
Definition: test_type.h:91
void AcceptCallback(Callback) const
Definition: test_type.h:92
void AcceptAnything(Any) const
Definition: test_type.h:93
ADD_DICT_FIELD(string, std::string)
ByteBuffer buffer
Definition: test_type.h:138
constexpr const int EXPECTED_INT
Definition: test_type.h:57
DECLARE_STRUCT_SPECIAL_METHODS_COPYABLE(TestTypeOptions)
void AcceptStringEnum(TestStringEnum) const
Definition: test_type.h:90
void AcceptString(const std::string &) const
Definition: test_type.h:83
Definition: data.h:29
void AcceptBoolean(bool) const
Definition: test_type.h:82
variant< int, Any > int_or_object
Definition: test_type.h:133
void AcceptStruct(TestTypeOptions) const
Definition: test_type.h:88
const char * message
void Trace(memory::HeapTracer *tracer) const override
Definition: struct.cc:49
#define DECLARE_TYPE_INFO(type)
void AcceptNumber(double) const
Definition: test_type.h:81
constexpr const char EXPECTED_STRING[]
Definition: test_type.h:62
static TestType * Create()
Definition: test_type.h:75
void AcceptNumberEnum(TestNumberEnum) const
Definition: test_type.h:89
CONVERT_ENUM_AS_NUMBER(shaka::js, TestNumberEnum)