Coder Social home page Coder Social logo

dakusui / pcond Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 13.99 MB

pcond is a library that allows you to write a program in the "Design by Contract"[DbC] style. Not only that, it gives a very informative message when a check you defined fails.

Home Page: http://dakusui.github.io/pcond

License: Apache License 2.0

Java 99.80% Shell 0.20%

pcond's People

Contributors

dakusui avatar dependabot[bot] avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

pcond's Issues

Improve report output

Improve report output

  • Squash "negate" predicate if target is a leaf
  • Use and and allOf instead of &&
  • Use or and anyOf instead of ||

Improve the `findElements`'s output

Currently, it's shown like below.

["Hello","world",""...;9]        ->transform
                                     toCursoredList                            ->["Hello","world",""...;9]
                                     identity                                  ->["Hello","world",""...;9]
                                   VERIFY
                                     &&                                        ->false
                                       findElementBy[isEqualTo["world"]]       ->true
["","everyone","quick"...;7]     ->    findElementBy[isEqualTo["cat"]]         ->false
                                       findElementBy[isEqualTo["organization"]]->false
                                       findElementBy[isNotNull]                ->true
["everyone","quick","brown"...;6]->    findElementBy[isEqualTo["fox"]]         ->true
["runs","forever"]               ->    findElementBy[isEqualTo["world"]]       ->false
                                       (end)                                   ->true

But it can be improved by using snapshotting and explain mechanism.

Support nested loop

I'd like to be able write a following requirement.

  public void hello() {
    require(
        asList("hello", "world"),
        when(stream().andThen(nest(asList("1", "2", "o")))).then(anyMatch(test(String::endsWith(), 0, 1))));
  }

where, 0,1 represents the order of variables in the context passed to the endsWith function.

Add procedure to deploy site

Add procedure to deploy site for reports and JavaDoc to somewhere (probably README.adoc).

mvn -Dmaven.site.deploy.skip=false compile test site

Add $() method to Checker interface

Add $(): ObjectChecker method to Checker interface.
This will be necessary for valid8j's all(Statement<T>... statements): boolean method.
Without having it, you cannot use all(...) method for checking multiple values of different types.

Improve output message of "MoreFluent" style

In case, we use "WHEN" phrase but no transformation is given, we see the following output.

"Doe"               ->WHEN:treatAs[NOUT]    ->"Doe"
                      THEN:&&              ->true
                          isNotNull        ->true
                          !                ->true
                            isEmpty        ->false
["John","Doe","PhD"]->WHEN:treatAsList     ->["John","Doe","PhD"]
                      THEN:contains["Doe_"]->true

The part treatAs[NOUT] is an internal term and if it is treatAsIs, the output will be more readable.

Doesn't print intended error meesage

		requireArgument(asList(args),
				and(when(size()).then(isEqualTo(1)),
						when(elementAt(0)).then(and(isNotNull(), isInstanceOf(String.class)))));

In case args[0] = (int)0,
This results in a java's native lambda printed message.

`findSubstring` output improvement

Following is the "as-is" output.

.ASIS

    "Gallia est omnis divis...Bello Gallicco."->transform
                                                  findTokens                       ->"Gallia est omnis divis...Bello Gallicco."
                                                  identity                         ->"Gallia est omnis divis...Bello Gallicco."
                                                VERIFY
                                                  &&                               ->true
                                                    findTokenBy[substring[Gallia]] ->true
    " est omnis divisa in p...Bello Gallicco."->    findTokenBy[substring[quarum]] ->true
[0] " unum incolunt Belgae,...Bello Gallicco."->    findTokenBy[substring[Belgium]]->true
                                                    findTokenBy[substring[nostra]] ->true
    " Galli Appellantur. De Bello Gallicco."  ->    findTokenBy[substring[De]]     ->true
[1] " Bello Gallicco."                        ->    findTokenBy[substring[Bellum]] ->true
                                                    (end)                          ->true

.Detail of failure [0]
----
Gallia est omnis divisa in partes tres, quarum
<<NOTFOUND:substring[Belgium]>>
 unum incolunt Belgae, aliam Acquitanii, tertiam nostra Galli Appellantur. De Bello Gallicco.
----

.Detail of failure [1]
----
Gallia est omnis divisa in partes tres, quarum unum incolunt Belgae, aliam Acquitanii, tertiam nostra Galli Appellantur. De
<<NOTFOUND:substring[Bellum]>>
 Bello Gallicco.
----

I want to make it like following.

.TOBE

    "Gallia est omnis divis...Bello Gallicco."->transform
                                                  findTokens                       ->"Gallia est omnis divis...Bello Gallicco."
                                                  identity                         ->"Gallia est omnis divis...Bello Gallicco."
                                                VERIFY
                                                  &&                               ->false
                                                    findTokenBy[substring[Gallia]] ->true
    " est omnis divisa in p...Bello Gallicco."->    findTokenBy[substring[quarum]] ->true
[0] " unum incolunt Belgae,...Bello Gallicco."->    findTokenBy[substring[Belgium]]->false
                                                    findTokenBy[substring[nostra]] ->true
    " Galli Appellantur. De Bello Gallicco."  ->    findTokenBy[substring[De]]     ->true
[1] " Bello Gallicco."                        ->    findTokenBy[substring[Bellum]] ->false
[2]                                                   (end)                          ->false

.Detail of failure [0]
----
Gallia est omnis divisa in partes tres, quarum
<<NOTFOUND:substring[Belgium]>>
 ----

.Detail of failure [1]
----
...Galli Appellantur. De
<<NOTFOUND:substring[Bello]>>
----
.Detail of failure [2]
----
 Bello Gallicco.
----

A public method overriding a public in a superclass cannot be accessed if the class is private

A public method overriding a public in a superclass cannot be accessed if the class is private.

com.github.dakusui.pcond.internals.InternalException: An exception(com.github.dakusui.pcond.internals.MethodAccessException: Method access to 'public java.util.Set com.github.dakusui.pcond.ut.bugfixes.issuexyz.IssueXyzUtils$PrivateExtendingPublic.keySet()' was failed) was thrown during evaluation of value: {}: <>.keySet() isEmpty
transform("{}")          
  <>.keySet()("{}")      com.github.dakusui.pcond.internals.MethodAccessException: Method access to 'public java.util.Set com.github.dakusui.pcond.ut.bugfixes.issuexyz.IssueXyzUtils$PrivateExtendingPublic.keySet()' was failed

	at com.github.dakusui.pcond.internals.InternalUtils.wrap(InternalUtils.java:94)
	at com.github.dakusui.pcond.provider.impls.DefaultAssertionProvider.checkValue(DefaultAssertionProvider.java:97)
	at com.github.dakusui.pcond.provider.AssertionProviderBase.require(AssertionProviderBase.java:32)
	at com.github.dakusui.pcond.provider.AssertionProviderBase.require(AssertionProviderBase.java:27)
	at com.github.dakusui.pcond.Preconditions.require(Preconditions.java:26)
	at com.github.dakusui.pcond.ut.bugfixes.IssueXyzTest.givenPrivateClassExtendingPublic$whenPublicMethodIsCalled$thenCallSucceeds(IssueXyzTest.java:20)
Caused by: com.github.dakusui.pcond.internals.MethodAccessException: Method access to 'public java.util.Set com.github.dakusui.pcond.ut.bugfixes.issuexyz.IssueXyzUtils$PrivateExtendingPublic.keySet()' was failed
	at com.github.dakusui.pcond.core.refl.ReflUtils.invokeMethod(ReflUtils.java:84)
	at com.github.dakusui.pcond.core.refl.ReflUtils.invokeMethod(ReflUtils.java:63)
	at com.github.dakusui.pcond.functions.Functions.lambda$call$2(Functions.java:175)

Improve output of assertWhen.

For the following test code:

  @Test
  public void givenKnownLastName_whenFindMembersByFullName_thenLastNastIsNotNullAndContainedInFullName() {
    MemberDatabase database = new MemberDatabase();
    String lastName = database.lookUp("0001")
        .orElseThrow(NoSuchElementException::new)
        .lastName();
    List<String> fullName = database.findMembersByLastName(lastName).get(0).toFullName();
    assertWhen(
        valueOf(lastName)
            .then()
            .verifyWith(allOf(
                isNotNull(),
                not(isEmptyString()))),
        valueOf(fullName).asListOfClass(String.class)
            .then()
            .contains(lastName + "_"));
  }

pcond currently gives following output:

["Doe",["John","Doe","PhD"]]->&&                    ->false
                                transform
                                  at[0]             ->"Doe"
"Doe"                       ->  check
                                  WHEN
                                    treatAs[NOUT]   ->"Doe"
                                  THEN
                                    &&              ->true
                                      isNotNull     ->true
                                      !             ->true
                                        isEmpty     ->false
["Doe",["John","Doe","PhD"]]->  transform
                                  at[1]             ->["John","Doe","PhD"]
["John","Doe","PhD"]        ->  check
                                  WHEN
                                    treatAsList     ->["John","Doe","PhD"]
                                  THEN
                                    contains["Doe_"]->false

This is redundant and it should be like the following.

"Doe"                       ->  check
                                  WHEN
                                    treatAs[NOUT]   ->"Doe"
                                  THEN
                                    &&              ->true
                                      isNotNull     ->true
                                      !             ->true
                                        isEmpty     ->false
["John","Doe","PhD"]        ->  check
                                  WHEN
                                    treatAsList     ->["John","Doe","PhD"]
                                  THEN
                                    contains["Doe_"]->false

If this can be

"Doe"                       ->  WHEN
                                    treatAs[NOUT]   ->"Doe"
                                  THEN
                                    &&              ->true
                                      isNotNull     ->true
                                      !             ->true
                                        isEmpty     ->false
["John","Doe","PhD"]        ->  WHEN
                                    treatAsList     ->["John","Doe","PhD"]
                                  THEN
                                    contains["Doe_"]->false

it will be even better.

Support a feature to convert a method into a function or predicate

Introduce a feature, where following example is possible.

  public void chainExample() {
    String var = "hello, world";

    requireArgument(
        var,
        transform(
            chain("substring", 7)
                .andThen(chain("toUpperCase")))
            .castAndCheck(chainp("equals", "WORLD"))
    );
  }

Instead of defining a printable function every time, directly create it from a method.

IDEA reports a warning that uses methods in Assertions class

IDEA reports a warning that uses methods in Assertions class because they always true, if a condition given to a method in the class becomes true always, as long as the condition is met, even if an exception is thrown in case the condition is violated.

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.