Giter VIP home page Giter VIP logo

Comments (6)

OsirisTerje avatar OsirisTerje commented on June 2, 2024

The StringAsserts are part of the legacy/classic assertions. Although we don't expect to do anything more with these, what you suggest can be added, as they seem to be just additions, causing no breaking changes or changes to existing assertions.

All the StringAssert methods are implemented in terms of the constraint model, like:

 public static void StartsWith(string expected, string actual, string message, params object?[]? args)
 {
      Assert.That(actual, Does.StartWith(expected), () => ConvertMessageWithArgs(message, args));
 }

And this is the way your suggestions should be implemented too.

Suggestion 1 and 3 can easily be added, as they are just simple translations like:

Assert.That(somestring,Is.Not.Null.And.Not.Empty);

PS: Have you considered using the constraint syntax instead?

No 2 and 4 include whitespace, and we currently don't have a constraint for whitespace. (We could probably have that, as it is frequently used in code. ). A solution would be to use the Regex constraint.

We do have an open issue on this, see #3918. Perhaps due time to fix this @stevenaw @manfred-brands ?

The team will probably not do this (except for creating a whitespace constraint or modifier at some time), but we are very open to receive a Pull Request for StringAsserts. That PR also has to contain unit tests for the asserts. It should be easy to add, and placed along with the other string assert tests.

Since this adds functionality it has to go along with the same assertions being documented in the docs repo, under the section for StringAssert.

from nunit.

manfred-brands avatar manfred-brands commented on June 2, 2024

@OsirisTerje I wouldn't mind adding a Whitespace constraint and also implement #3918.
I currently work around that issue using Assert.That(NoSpaces(json), Is.EqualTo(NoSpaces(expectedJson))

from nunit.

OsirisTerje avatar OsirisTerje commented on June 2, 2024

@manfred-brands Awesome if you implement that!

How should the syntax be ? There are different alternatives, and we should try to be as close as we can to the constraint design.

There might be more than a few scenarios.

Some random thoughts:

Is.EqualTo(sometring).IgnoreWhiteSpace() // Ignores all whitespace in string

or

Is.EqualTo(sometring).WhiteSpace.IgnoreAll // Looks a bit weird to me, but doesnt need multiple variations of WhiteSpace, only modifiers.

so could also be:

Is.EqualTo(sometring).WhiteSpace(IgnoreAll)

Is.EqualTo(somestring).TrimWhiteSpace() // Ignore leading and ending whitespace

or

Is.EqualTo(somestring).WhiteSpace().Trim() or as above include as parameter

Assert.That(sometring,Is.WhiteSpace() // string consist only of whitespace
Assert.That(somestring,Has.WhiteSpace() // string has some whitespace in it

and it should be possible to write
Assert.That(somestring,Is.Not.Null.And.Not.WhiteSpace

from nunit.

manfred-brands avatar manfred-brands commented on June 2, 2024

@OsirisTerje See #4664

from nunit.

OsirisTerje avatar OsirisTerje commented on June 2, 2024

Awesome! Looks very good!

from nunit.

MgSam avatar MgSam commented on June 2, 2024

Thanks, I will try and work on this when I have free time, hopefully over the next few months.

from nunit.

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.