Alternate e697dbe9c5997e35395fe158628dd8c5209481da
for Visual Studio 2022 and Windows 11.
読み取り中…
検索中…
一致する文字列を見つけられません
BinderTest.cpp
[詳解]
1
5
6#include "pch.h"
7#include "Binder.hpp"
8#include "Freight.hpp"
9#include "Exception.h"
10
11using namespace Microsoft::VisualStudio::CppUnitTestFramework;
12
13namespace WindowsLibraryTest
14{
15 TEST_CLASS (BinderTest)
16 {
17 public:
18 TEST_CLASS_INITIALIZE (ClassInitialize)
19 {
20 Logger::WriteMessage ("BinderTest class initialize.\n");
21 }
22
23 TEST_CLASS_CLEANUP (ClassCleanup)
24 {
25 Logger::WriteMessage ("BinderTest class cleanup.\n");
26 }
27
28 TEST_METHOD_INITIALIZE (MethodInitialize)
29 {
30 Logger::WriteMessage ("BinderTest method initialize.\n");
31 }
32
33 TEST_METHOD_CLEANUP (MethodCleanup)
34 {
35 Logger::WriteMessage ("BinderTest method cleanup.\n");
36 }
37
38 class Rectangle
39 {
40 public:
41 Rectangle ()
42 {
43 SetRect (-1, -1, -1, -1);
44 };
45
46 Rectangle (int x, int y, int width, int height)
47 {
48 SetRect (x, y, width, height);
49 };
50
51 ~Rectangle ()
52 {
53 SetRect (-1, -1, -1, -1);
54 };
55
56 int GetArea ()
57 {
58 return _width * _height;
59 };
60
61 void GetRect (int& x, int& y, int& width, int& height)
62 {
63 x = _x;
64 y = _y;
65 width = _width;
66 height = _height;
67 }
68
69 void SetRect (int x, int y, int width, int height)
70 {
71 _x = x;
72 _y = y;
73 _width = width;
74 _height = height;
75 };
76
77 private:
78 int _x;
79 int _y;
80 int _width;
81 int _height;
82 };
83
84 TEST_METHOD (BinderTest1)
85 {
86 try
87 {
89 Rectangle* rectangle = binder.Get ();
90 int area = rectangle->GetArea ();
91 Assert::Fail (_T ("RuntimeException not raised.\n"));
92 }
93 catch (const alt::ex::RuntimeException& ex)
94 {
95 Logger::WriteMessage (ex.GetReason ());
96 }
97 catch (...)
98 {
99 Assert::Fail (_T ("unknown exception raised.\n"));
100 }
101 }
102
103 TEST_METHOD (BinderTest2)
104 {
105 Rectangle* rectangle = new Rectangle (1, 1, 10, 20);
107 binder.Set (rectangle);
108 rectangle = nullptr; // no problem. Binder will clean it safely.
109
110 Rectangle* newRectangle = binder.Get ();
111 Assert::AreEqual<int> (200, newRectangle->GetArea (), _T ("invalid value GetArea().\n"));
112 }
113
114 TEST_METHOD (BinderTest3)
115 {
116 Rectangle* rectangle = new Rectangle (1, 1, 20, 20);
117 alt::skeleton::Binder<Rectangle> binder(rectangle);
118 rectangle = nullptr; // no problem. Binder will clean it safely.
119
120 Rectangle* newRectangle = binder.Get ();
121 Assert::AreEqual<int> (400, newRectangle->GetArea (), _T ("invalid value GetArea().\n"));
122 }
123
124 TEST_METHOD (BinderTest4)
125 {
127
128 Assert::IsTrue (binder.New (), _T ("Biner::New() failed.\n"));
129 Rectangle* rectangle = binder.Get ();
130 rectangle->SetRect (10, 10, 10, 10);
131
132 int area = binder->GetArea ();
133 Assert::AreEqual<int> (100, area, _T ("invalid value GetArea().\n"));
134 }
135
136 TEST_METHOD (BinderTest5)
137 {
139 Binder binder;
140
141 Assert::IsTrue (binder.New (10, 10, 10, 10), _T ("Biner::New(10,10,10,10) failed.\n"));
142
143 int area = binder->GetArea ();
144 Assert::AreEqual<int> (100, area, _T ("invalid value GetArea().\n"));
145
146 int area2 = binder->GetArea ();
147 }
148 };
149}
newオブジェクトを管理するコンテナクラス
例外に関するクラス
プリミティブな変数を管理するコンテナクラス
プリコンパイル済みヘッダー ファイルです。
実行時エラー例外
Definition: Exception.h:54
LPCTSTR APIENTRY GetReason() const
原因を取得
Definition: Exception.cpp:43
newオブジェクトを管理するコンテナクラス
Definition: Binder.hpp:18
T *APIENTRY Get() const
管理オブジェクトの取得
Definition: Binder.hpp:59
VOID APIENTRY Set(T *t)
外部から管理オブジェクトを設定
Definition: Binder.hpp:78
bool APIENTRY New(Args... args)
管理オブジェクトの作成
Definition: Binder.hpp:44
TEST_CLASS(ArrayTest)
Definition: ArrayTest.cpp:19