Skip to content

Commit

Permalink
Add Path Qualities, closes #174 (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmfs authored Jul 27, 2024
1 parent ef078ea commit 9eaea68
Show file tree
Hide file tree
Showing 17 changed files with 779 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.saynotobugs.confidence.Scribe;
import org.saynotobugs.confidence.utils.ArrayIterable;

import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -71,6 +72,11 @@ else if (value instanceof Set)
{
description = new SetDescription((Set<?>) value);
}
else if (value instanceof Path)
{
// Path is an Iterable of Path, potentially resulting in an infinite loop, hence we treat it seprately here
description = new ToStringDescription(value);
}
else if (value instanceof Iterable)
{
description = new IterableDescription((Iterable<?>) value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 dmfs GmbH
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.saynotobugs.confidence.quality.path;

import org.dmfs.srcless.annotations.staticfactory.StaticFactories;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.quality.composite.QualityComposition;
import org.saynotobugs.confidence.quality.object.Satisfies;

import java.nio.file.Files;
import java.nio.file.Path;

@StaticFactories(
value = "Path",
packageName = "org.saynotobugs.confidence.core.quality")
public final class ADirectory extends QualityComposition<Path>
{
public ADirectory()
{
super(new Satisfies<>(Files::isDirectory, file -> new Text("not a directory"), new Text("a directory")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 dmfs GmbH
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.saynotobugs.confidence.quality.path;

import org.dmfs.srcless.annotations.staticfactory.StaticFactories;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.quality.composite.QualityComposition;
import org.saynotobugs.confidence.quality.object.Satisfies;

import java.nio.file.Files;
import java.nio.file.Path;

@StaticFactories(
value = "Path",
packageName = "org.saynotobugs.confidence.core.quality")
public final class AFile extends QualityComposition<Path>
{
public AFile()
{
super(new Satisfies<>(Files::isRegularFile, file -> new Text("not a file"), new Text("a file")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2024 dmfs GmbH
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.saynotobugs.confidence.quality.path;

import org.dmfs.srcless.annotations.staticfactory.StaticFactories;
import org.saynotobugs.confidence.Quality;
import org.saynotobugs.confidence.description.Spaced;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.description.Value;
import org.saynotobugs.confidence.quality.composite.DescribedAs;
import org.saynotobugs.confidence.quality.composite.Has;
import org.saynotobugs.confidence.quality.composite.QualityComposition;
import org.saynotobugs.confidence.quality.object.EqualTo;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

@StaticFactories(
value = "Path",
packageName = "org.saynotobugs.confidence.core.quality")
public final class ContainsText extends QualityComposition<Path>
{
public ContainsText(String text)
{
this(new EqualTo<>(text));
}

public ContainsText(Quality<? super CharSequence> mDelegate)
{
this(StandardCharsets.UTF_8, mDelegate);
}

public ContainsText(Charset charset, Quality<? super CharSequence> delegate)
{
super(new Has<>(
new Text("contains"),
new Text("contained"),
path -> new String(Files.readAllBytes(path), charset),
new DescribedAs<>(orig -> new Spaced(new Value(charset.name()), new Text("text"), orig), delegate)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 dmfs GmbH
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.saynotobugs.confidence.quality.path;

import org.dmfs.srcless.annotations.staticfactory.StaticFactories;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.quality.composite.QualityComposition;
import org.saynotobugs.confidence.quality.object.Satisfies;

import java.nio.file.Files;
import java.nio.file.Path;

@StaticFactories(
value = "Path",
packageName = "org.saynotobugs.confidence.core.quality")
public final class Exists extends QualityComposition<Path>
{
public Exists()
{
super(new Satisfies<>(Files::exists, file -> new Text("does not exist"), new Text("exists")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2024 dmfs GmbH
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.saynotobugs.confidence.quality.path;

import org.dmfs.srcless.annotations.staticfactory.StaticFactories;
import org.saynotobugs.confidence.Quality;
import org.saynotobugs.confidence.quality.composite.Has;
import org.saynotobugs.confidence.quality.composite.QualityComposition;
import org.saynotobugs.confidence.quality.object.EqualTo;

import java.nio.file.Path;

@StaticFactories(
value = "Path",
packageName = "org.saynotobugs.confidence.core.quality")
public final class HasName extends QualityComposition<Path>
{
public HasName(CharSequence name)
{
this(new EqualTo<>(name));
}

public HasName(Quality<? super CharSequence> nameQuality)
{
super(new Has<>("name", path -> path.getFileName().toString(), nameQuality));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2024 dmfs GmbH
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.saynotobugs.confidence.quality.path;

import org.dmfs.srcless.annotations.staticfactory.StaticFactories;
import org.saynotobugs.confidence.Quality;
import org.saynotobugs.confidence.quality.composite.Has;
import org.saynotobugs.confidence.quality.composite.QualityComposition;
import org.saynotobugs.confidence.quality.object.EqualTo;

import java.nio.file.Path;

@StaticFactories(
value = "Path",
packageName = "org.saynotobugs.confidence.core.quality")
public final class HasParent extends QualityComposition<Path>
{
public HasParent(Path parent)
{
this(new EqualTo<>(parent));
}

public HasParent(Quality<? super Path> parentQuality)
{
super(new Has<>("parent", Path::getParent, parentQuality));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 dmfs GmbH
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.saynotobugs.confidence.quality.path;

import org.dmfs.srcless.annotations.staticfactory.StaticFactories;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.quality.composite.QualityComposition;
import org.saynotobugs.confidence.quality.object.Satisfies;

import java.nio.file.Files;
import java.nio.file.Path;

@StaticFactories(
value = "Path",
packageName = "org.saynotobugs.confidence.core.quality")
public final class Readable extends QualityComposition<Path>
{
public Readable()
{
super(new Satisfies<>(Files::isReadable, file -> new Text("not readable"), new Text("readable")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 dmfs GmbH
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.saynotobugs.confidence.quality.path;

import org.dmfs.srcless.annotations.staticfactory.StaticFactories;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.quality.composite.QualityComposition;
import org.saynotobugs.confidence.quality.object.Satisfies;

import java.nio.file.Files;
import java.nio.file.Path;

@StaticFactories(
value = "Path",
packageName = "org.saynotobugs.confidence.core.quality")
public final class Writeable extends QualityComposition<Path>
{
public Writeable()
{
super(new Satisfies<>(Files::isWritable, file -> new Text("not writeable"), new Text("writeable")));
}
}
Loading

0 comments on commit 9eaea68

Please sign in to comment.