iutest  1.17.99.14
iutest_util_menu.hpp
[詳解]
1 //======================================================================
2 //-----------------------------------------------------------------------
13 //-----------------------------------------------------------------------
14 //======================================================================
15 #ifndef INCG_IRIS_IUTEST_UTIL_MENU_HPP_52925DE1_A4AE_4CCB_B524_8E97AA73E03D_
16 #define INCG_IRIS_IUTEST_UTIL_MENU_HPP_52925DE1_A4AE_4CCB_B524_8E97AA73E03D_
17 
18 //======================================================================
19 // include
20 // IWYU pragma: begin_exports
21 #include "iutest_util_tests.hpp"
22 // IWYU pragma: end_exports
23 
24 #if defined(IUTEST_OS_WINDOWS)
25 #include <windows.h>
26 #include <map>
27 
28 namespace iuutil
29 {
30 
31 //======================================================================
32 // class
36 class TestMenu
37 {
38  typedef ::std::map<WORD, const ::iutest::TestInfo*> TestInfoMap;
39  typedef ::std::map<WORD, const ::iutest::TestSuite*> TestSuiteMap;
40  WORD m_nID;
41  const WORD m_nIDTop;
42  HMENU m_hRootMenu;
43  TestInfoMap m_TestInfoList;
44  TestSuiteMap m_TestSuiteList;
45 public:
46  explicit TestMenu(WORD nIDTop) : m_nIDTop(nIDTop), m_nID(nIDTop), m_hRootMenu(NULL) {}
47 public:
48  bool Create(HMENU hMenu)
49  {
50  if( hMenu == NULL )
51  {
52  return false;
53  }
54  if( m_hRootMenu == NULL )
55  {
56  if( !Create() )
57  {
58  return false;
59  }
60  }
61  return AppendPopup(hMenu, "TestList", m_hRootMenu);
62  }
63  bool Create()
64  {
65  // テストを列挙
66  HMENU hRoot = CreateMenu();
67  if( hRoot == NULL )
68  {
69  return false;
70  }
71 
72  Append(hRoot, "以下をすべて実行", m_nID);
73  ++m_nID;
74 
76  const int testsuite_count = pUnitTest->total_test_suite_count();
77  for( int i=0; i < testsuite_count; ++i )
78  {
79  const ::iutest::TestSuite* pTestSuite = pUnitTest->GetTestSuite(i);
80  const int test_count = pTestSuite->total_test_count();
81  HMENU hTestSuite = AppendPopup(hRoot, pTestSuite->name());
82  Append(hTestSuite, "以下をすべて実行", m_nID);
83 #if IUTEST_HAS_STD_EMPLACE
84  m_TestSuiteList.emplace(m_nID, pTestSuite);
85 #else
86  m_TestSuiteList.insert( ::std::pair<WORD, const ::iutest::TestSuite*>(m_nID, pTestSuite) );
87 #endif
88  ++m_nID;
89  for( int j=0; j < test_count; ++j )
90  {
91  const ::iutest::TestInfo* pTestInfo = pTestSuite->GetTestInfo(j);
92  Append(hTestSuite, pTestInfo->name(), m_nID);
93 #if IUTEST_HAS_STD_EMPLACE
94  m_TestInfoList.emplace(m_nID, pTestInfo);
95 #else
96  m_TestInfoList.insert(::std::pair<WORD, const ::iutest::TestInfo*>(m_nID, pTestInfo));
97 #endif
98  ++m_nID;
99  }
100  }
101  m_hRootMenu = hRoot;
102  return true;
103  }
104 
105  bool OnCommand(WORD wID)
106  {
107  if( wID == m_nIDTop )
108  {
109  ::iutest::IUTEST_FLAG(filter) = "*";
110  return IUTEST_RUN_ALL_TESTS() == 0 ? true : false;
111  }
112 
113  {
114  TestInfoMap::iterator it = m_TestInfoList.find(wID);
115  if( it != m_TestInfoList.end() )
116  {
117  ::iutest::IUTEST_FLAG(filter) = TestFullName(it->second);
118  return IUTEST_RUN_ALL_TESTS() == 0 ? true : false;
119  }
120  }
121  {
122  TestSuiteMap::iterator it = m_TestSuiteList.find(wID);
123  if( it != m_TestSuiteList.end() )
124  {
125  ::std::string filter = it->second->name();
126  filter += ".*";
127  ::iutest::IUTEST_FLAG(filter) = filter;
128  return IUTEST_RUN_ALL_TESTS() == 0 ? true : false;
129  }
130  }
131  return true;
132  }
133 
134  bool TrackPopupMenu(HWND hWnd, POINT point)
135  {
136  if ( !::TrackPopupMenu(m_hRootMenu
137  , TPM_LEFTALIGN | TPM_BOTTOMALIGN
138  , point.x, point.y
139  , 0
140  , hWnd
141  , NULL
142  ) )
143  {
144  return false;
145  }
146  return true;
147  }
148 
149 private:
150  static bool Append(HMENU hMenu, const char* lpszName, WORD nID)
151  {
152  char str[256];
153  MENUITEMINFOA mii = {0};
154  mii.cbSize = sizeof(mii);
155  mii.fMask = MIIM_ID | MIIM_TYPE;
156  mii.fType = MFT_STRING;
157  mii.dwTypeData = str;
158  mii.wID = nID;
159  strcpy_s(str, lpszName);
160  const int num = ::GetMenuItemCount(hMenu);
161  if( !::InsertMenuItemA(hMenu, num, TRUE, &mii) )
162  {
163  return false;
164  }
165  return true;
166  }
167  static bool AppendPopup(HMENU hMenu, const char* lpszName, HMENU hSubMenu)
168  {
169  char str[256];
170  MENUITEMINFOA mii = {0};
171  mii.cbSize = sizeof(mii);
172  mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_SUBMENU;
173  mii.fType = MFT_STRING;
174  mii.dwTypeData = str;
175  mii.hSubMenu = hSubMenu;
176  strcpy_s(str, lpszName);
177  const int num = ::GetMenuItemCount(hMenu);
178  if( !::InsertMenuItemA(hMenu, num, TRUE, &mii) )
179  {
180  return false;
181  }
182  return true;
183  }
184  static HMENU AppendPopup(HMENU hMenu, const char* lpszName)
185  {
186  HMENU hSubMenu = CreateMenu();
187  if( !AppendPopup(hMenu, lpszName, hSubMenu) )
188  {
189  DestroyMenu(hSubMenu);
190  return NULL;
191  }
192  return hSubMenu;
193  }
194 };
195 
196 } // end of namespace iuutil
197 
198 #endif
199 
200 #endif // INCG_IRIS_IUTEST_MENU_TESTS_HPP_52925DE1_A4AE_4CCB_B524_8E97AA73E03D_
int total_test_count() const IUTEST_CXX_NOEXCEPT_SPEC
Definition: iutest_suite.hpp:78
テスト全体の管理者
Definition: iutest_core.hpp:36
static UnitTest * GetInstance()
UnitTest インスタンスの取得
Definition: iutest_core.hpp:43
int total_test_suite_count() const
Definition: iutest_core.hpp:91
const TestSuite * GetTestSuite(int index) const
Definition: iutest_core.hpp:115
#define IUTEST_FLAG(name)
フラグセット
Definition: iutest_env.hpp:57
#define IUTEST_RUN_ALL_TESTS()
すべてのテストを実行する
Definition: iutest.hpp:160
iris unit test テスト情報 utility ファイル
iutest utility namespace
Definition: iutest_util.hpp:36
inline ::std::string TestFullName(const ::iutest::TestInfo *test_info)
Get TestName
Definition: iutest_util_tests.hpp:161