ocornut / imgui_test_engine

Dear ImGui Automation Engine & Test Suite
386 stars 40 forks source link

How to query value from ref? #26

Open S-A-Martin opened 1 year ago

S-A-Martin commented 1 year ago

How do I check the value of something by querying its ref? I'm hoping to do something like this:

ctx->ItemInputValue("sVal", 5.5f); // sets the sVal DragFloat to 5.5f
IM_CHECK_EQ("sVal", 5.5f); // Checks that the value has been set correctly

I'm not using GuiFunc, so I don't 'think' using GetVars is what I need, and I don't have the variable tied to an app class or anything.

It's being used more like:

    ImGui::NewFrame();
    {
      ImGui::Begin("My Window", NULL, window_flags);
      static float sVal = 0.0f;
      ImGui::DragFloat("sVal", &sVal);`
      ImGui::End();
    }

I just want to verify that the slider value is set correctly.

ocornut commented 1 year ago

Hello,

It's not really possible as the value is owned by you, so you need access to your data if you want to test against it.

We recently improved that section of the docs here: Automation-API -> Accessing your data

We have a WIP set of API (non-published yet) to "read" from emitted vertices or capture text logs, so you could technically capture the text output of the "sVal" widget (which would be e.g. "5.5" + "sVal") which may later be leveraged for this as an alternative solution.

ocornut commented 2 months ago

I have pushed helpers to do this by using selection + clipboard + parse as a way to extract values. This is not the more general version I'm hoping to further develop but it does the job for simple cases, and is a good workaround.

t = IM_REGISTER_TEST(e, "testengine", "testengine_select_read_value");
t->GuiFunc = [](ImGuiTestContext* ctx)
{
    ImGui::Begin("Test Window", NULL, ImGuiWindowFlags_NoSavedSettings);

    int v_int = 123;
    float v_float = 0.456f;
    ImGui::SliderInt("int", &v_int, 0, 200);
    ImGui::DragFloat("float", &v_float, 0.01f, 0.0f, 1.0f);
    ImGui::End();
};
t->TestFunc = [](ImGuiTestContext* ctx)
{
    ctx->SetRef("Test Window");
    int v_int = 0;
    float v_float = 0;
    ctx->ItemSelectAndReadValue("int", &v_int);
    ctx->ItemSelectAndReadValue("float", &v_float);
    IM_CHECK_EQ(v_int, 123);
    IM_CHECK_EQ(v_float, 0.456f);
};