JsDiagApiTest.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "stdafx.h"
  6. #pragma warning(disable:26434) // Function definition hides non-virtual function in base class
  7. #pragma warning(disable:26439) // Implicit noexcept
  8. #pragma warning(disable:26451) // Arithmetic overflow
  9. #pragma warning(disable:26495) // Uninitialized member variable
  10. #include "catch.hpp"
  11. #include <process.h>
  12. #pragma warning(disable:4100) // unreferenced formal parameter
  13. #pragma warning(disable:6387) // suppressing preFAST which raises warning for passing null to the JsRT APIs
  14. #pragma warning(disable:6262) // CATCH is using stack variables to report errors, suppressing the preFAST warning.
  15. namespace JsDiagApiTest
  16. {
  17. static void CALLBACK DebugEventCallback(JsDiagDebugEvent debugEvent, JsValueRef eventData, void* callbackState)
  18. {
  19. }
  20. bool TestSetup(JsRuntimeHandle *runtime)
  21. {
  22. JsValueRef context = JS_INVALID_REFERENCE;
  23. JsValueRef setContext = JS_INVALID_REFERENCE;
  24. // Create runtime, context and set current context
  25. REQUIRE(JsCreateRuntime(JsRuntimeAttributeNone, nullptr, runtime) == JsNoError);
  26. REQUIRE(JsCreateContext(*runtime, &context) == JsNoError);
  27. REQUIRE(JsSetCurrentContext(context) == JsNoError);
  28. REQUIRE(((JsGetCurrentContext(&setContext) == JsNoError) || setContext == context));
  29. // Enable debugging
  30. REQUIRE(JsDiagStartDebugging(*runtime, JsDiagApiTest::DebugEventCallback, nullptr) == JsNoError);
  31. return true;
  32. }
  33. bool TestCleanup(JsRuntimeHandle runtime)
  34. {
  35. if (runtime != nullptr)
  36. {
  37. // Disable debugging
  38. JsDiagStopDebugging(runtime, nullptr);
  39. JsSetCurrentContext(nullptr);
  40. JsDisposeRuntime(runtime);
  41. }
  42. return true;
  43. }
  44. template <class Handler>
  45. void WithSetup(Handler handler)
  46. {
  47. JsRuntimeHandle runtime = JS_INVALID_RUNTIME_HANDLE;
  48. if (!TestSetup(&runtime))
  49. {
  50. REQUIRE(false);
  51. return;
  52. }
  53. handler(runtime);
  54. TestCleanup(runtime);
  55. }
  56. #ifndef BUILD_WITHOUT_SCRIPT_DEBUG
  57. void BreakpointsContextTest(JsRuntimeHandle runtime)
  58. {
  59. JsContextRef context = JS_INVALID_REFERENCE;
  60. REQUIRE(JsGetCurrentContext(&context) == JsNoError);
  61. // Verify no errors with a context set
  62. JsValueRef scriptsArray = JS_INVALID_REFERENCE;
  63. REQUIRE(JsDiagGetScripts(&scriptsArray) == JsNoError);
  64. // Verify the APIs return an error when no current context set
  65. JsSetCurrentContext(nullptr);
  66. CHECK(JsDiagGetScripts(&scriptsArray) == JsErrorNoCurrentContext);
  67. JsValueRef breakpoint = JS_INVALID_REFERENCE;
  68. CHECK(JsDiagSetBreakpoint(0, 0, 0, &breakpoint) == JsErrorNoCurrentContext);
  69. CHECK(JsDiagRemoveBreakpoint(0) == JsErrorNoCurrentContext);
  70. }
  71. TEST_CASE("JsDiagApiTest_BreakpointsContextTest", "[JsDiagApiTest]")
  72. {
  73. JsDiagApiTest::WithSetup(JsDiagApiTest::BreakpointsContextTest);
  74. }
  75. #endif // BUILD_WITHOUT_SCRIPT_DEBUG
  76. }