JsDiagApiTest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "catch.hpp"
  7. #include <process.h>
  8. #pragma warning(disable:4100) // unreferenced formal parameter
  9. #pragma warning(disable:6387) // suppressing preFAST which raises warning for passing null to the JsRT APIs
  10. #pragma warning(disable:6262) // CATCH is using stack variables to report errors, suppressing the preFAST warning.
  11. namespace JsDiagApiTest
  12. {
  13. static void CALLBACK DebugEventCallback(JsDiagDebugEvent debugEvent, JsValueRef eventData, void* callbackState)
  14. {
  15. }
  16. bool TestSetup(JsRuntimeHandle *runtime)
  17. {
  18. JsValueRef context = JS_INVALID_REFERENCE;
  19. JsValueRef setContext = JS_INVALID_REFERENCE;
  20. // Create runtime, context and set current context
  21. REQUIRE(JsCreateRuntime(JsRuntimeAttributeNone, nullptr, runtime) == JsNoError);
  22. REQUIRE(JsCreateContext(*runtime, &context) == JsNoError);
  23. REQUIRE(JsSetCurrentContext(context) == JsNoError);
  24. REQUIRE(((JsGetCurrentContext(&setContext) == JsNoError) || setContext == context));
  25. // Enable debugging
  26. REQUIRE(JsDiagStartDebugging(*runtime, JsDiagApiTest::DebugEventCallback, nullptr) == JsNoError);
  27. return true;
  28. }
  29. bool TestCleanup(JsRuntimeHandle runtime)
  30. {
  31. if (runtime != nullptr)
  32. {
  33. // Disable debugging
  34. JsDiagStopDebugging(runtime, nullptr);
  35. JsSetCurrentContext(nullptr);
  36. JsDisposeRuntime(runtime);
  37. }
  38. return true;
  39. }
  40. template <class Handler>
  41. void WithSetup(Handler handler)
  42. {
  43. JsRuntimeHandle runtime = JS_INVALID_RUNTIME_HANDLE;
  44. if (!TestSetup(&runtime))
  45. {
  46. REQUIRE(false);
  47. return;
  48. }
  49. handler(runtime);
  50. TestCleanup(runtime);
  51. }
  52. #ifndef BUILD_WITHOUT_SCRIPT_DEBUG
  53. void BreakpointsContextTest(JsRuntimeHandle runtime)
  54. {
  55. JsContextRef context = JS_INVALID_REFERENCE;
  56. REQUIRE(JsGetCurrentContext(&context) == JsNoError);
  57. // Verify no errors with a context set
  58. JsValueRef scriptsArray = JS_INVALID_REFERENCE;
  59. REQUIRE(JsDiagGetScripts(&scriptsArray) == JsNoError);
  60. // Verify the APIs return an error when no current context set
  61. JsSetCurrentContext(nullptr);
  62. CHECK(JsDiagGetScripts(&scriptsArray) == JsErrorNoCurrentContext);
  63. JsValueRef breakpoint = JS_INVALID_REFERENCE;
  64. CHECK(JsDiagSetBreakpoint(0, 0, 0, &breakpoint) == JsErrorNoCurrentContext);
  65. CHECK(JsDiagRemoveBreakpoint(0) == JsErrorNoCurrentContext);
  66. }
  67. TEST_CASE("JsDiagApiTest_BreakpointsContextTest", "[JsDiagApiTest]")
  68. {
  69. JsDiagApiTest::WithSetup(JsDiagApiTest::BreakpointsContextTest);
  70. }
  71. #endif // BUILD_WITHOUT_SCRIPT_DEBUG
  72. }