Coder Social home page Coder Social logo

Comments (8)

fornellas avatar fornellas commented on April 28, 2024

I believe what you need is merge_context, and not nest_context: https://testslide.readthedocs.io/en/1.4.2/testslide_dsl/shared_contexts/index.html#sharing-contexts.

The docs can certainly be improved, but the gist is:

  • merge_context, puts whatever is defined in the shared context, inside the context where it is being merged, including any hooks (before, after) and examples.
  • nest_context, creates a new sub context, under the context where it is being nested, with the contents of the shared context.

Generally speaking:

  • You merge contexts when you want common hooks to be used elsewhere (and perhaps also examples).
  • You nest contexts when the same set of examples makes sense in different scenarios.

Makes sense?

from testslide.

xush6528 avatar xush6528 commented on April 28, 2024

I see. So nest context is not really a shared "context" but essentially a set of shared "hooks" + "examples". It can't host more examples, but it lives like a guest in other host contexts..
Then it's so confusing to name it as context, as from the API design, I feel a context should always be an environment that can host examples.

from testslide.

fornellas avatar fornellas commented on April 28, 2024

I agree, this is far from obvious. RSpec (which heavily inspired the DSL) calls them shared examples (and include_examples) and shared contexts (and include_context). How about using the same naming scheme?

from testslide.

xush6528 avatar xush6528 commented on April 28, 2024

It's better anyway.

I think "merge_context" is good and easy to understand.

"nest_context" would be better to be called "include_context".
"Include" sounds more like reusing a of piece of code, indicating what it does clearly.

from testslide.

xush6528 avatar xush6528 commented on April 28, 2024

There is the confusion to expect "merge_context" and "nest_context" behave similarly, because they are both derived from "shared_context".

I hope the concept of "shared_context" could be taken down (as #13 suggested), so users will not have this assumption.

from testslide.

xush6528 avatar xush6528 commented on April 28, 2024

I have a new idea about this issue.

I realized that the problem is actually that when I want to re-use a context, what parts of the context I want to bring in.

A context has two parts basically,

  • one is for setting up some constants, helper function and mocks,
  • the other part is the examples enclosed in the context.

Instead of having two decorator "merge_context" and "nest_context" that users have to read doc to understand their differences.

I think it's better to just provide a single decorator but with arguments allowing users to choose what to bring in, the setups or the examples or all.

from testslide.

fornellas avatar fornellas commented on April 28, 2024

That makes sense. But there's another aspect of that as well. Sometimes you actually want to merge another context hooks / examples at the same level, and other times, you want to create another sub_context, and put it there. If we don't provide that, the latter case would require defining a sub_context, and then merging it there. Having nest_context is a convenience.

Summarizing things that were said:

  • Contexts are a gorup of hooks and/or examples.
  • They can be "real", meaning its examples will be executed, or "virtual", that examples are not executed.
  • "virtual" contexts are useful for importing its hooks and/or examples into another context (code sharing).
    • Potentially, this can happen in a new sub_context for convenience.

(I said "real" and "virtual" here to avoid biasing towards names)

Question now is: what naming / interface would make all of this clear?

Looking back at RSpec's naming scheme, shared_examples can actually... contain hooks as well, which can be misleading...

From your suggestion, I suppose you meant something like this:

context.include_shared_context(
  nested=True/False,
  examples=True/False,
  hooks=True/False,
)

Which seems a bit cumbersome... not to mention, that if you pull examples from a context, you must pull the hooks as well... And if you pull just hooks, it can be a bit confusing...

I feel like we're prematurely going after problems that are not necessarily here yet.

My suggestion to close this issue is:

  • Keep the current model, as it seems to cover all use cases.
  • Find more appropriate names for the DSL methods.
  • Update the docs.

Fair?

from testslide.

fornellas avatar fornellas commented on April 28, 2024

I updated the docs to make things clearer. I'm closing this issue now, as it seems to be mostly a documentation issue than anything else. Feel free to ping back if you find a working solution I haven't seen.

Thanks for the report!

from testslide.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.