From 746349d4e65c2fad7608cb3fb82d44e987bc8193 Mon Sep 17 00:00:00 2001 From: Adam Cervenka Date: Wed, 9 Feb 2022 16:44:08 +0100 Subject: [PATCH 1/2] commons #93 scalatest uri matcher --- .../commons/scalatest/CommonMatchers.scala | 39 +++++++++++++++++++ .../scalatest/CommonMatchersSpec.scala | 33 ++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/main/scala/za/co/absa/commons/scalatest/CommonMatchers.scala create mode 100644 src/test/scala/za/co/absa/commons/scalatest/CommonMatchersSpec.scala diff --git a/src/main/scala/za/co/absa/commons/scalatest/CommonMatchers.scala b/src/main/scala/za/co/absa/commons/scalatest/CommonMatchers.scala new file mode 100644 index 0000000..5892840 --- /dev/null +++ b/src/main/scala/za/co/absa/commons/scalatest/CommonMatchers.scala @@ -0,0 +1,39 @@ +/* + * Copyright 2021 ABSA Group Limited + * + * 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 za.co.absa.commons.scalatest + +import org.scalatest.matchers.{MatchResult, Matcher} + +import java.net.URI + +trait CommonMatchers { + + class EqualToUri(expectedUri: String) extends Matcher[String] { + + def apply(left: String): MatchResult = { + URI.create(left) == URI.create(expectedUri) + + MatchResult( + URI.create(left) == URI.create(expectedUri), + s"URI $left was not the same as expected $expectedUri", + s"URI $left was the same as expected $expectedUri" + ) + } + } + + def equalToUri(expectedUri: String) = new EqualToUri(expectedUri) +} diff --git a/src/test/scala/za/co/absa/commons/scalatest/CommonMatchersSpec.scala b/src/test/scala/za/co/absa/commons/scalatest/CommonMatchersSpec.scala new file mode 100644 index 0000000..3208df2 --- /dev/null +++ b/src/test/scala/za/co/absa/commons/scalatest/CommonMatchersSpec.scala @@ -0,0 +1,33 @@ +/* + * Copyright 2021 ABSA Group Limited + * + * 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 za.co.absa.commons.scalatest + +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers + +class CommonMatchersSpec extends AnyFlatSpec with Matchers with CommonMatchers { + + it should "match on the same uris" in { + "file:///foo.txt" should equalToUri("file:/foo.txt") + "file:/foo.txt" should equalToUri("file:///foo.txt") + + "http:/bar" should not (equalToUri("http:/bar/")) + + new EqualToUri("file:/foo.txt").apply("http://foo").matches shouldBe false + } + +} From cb838852ff39dafeb3a62931d13af25f27768aa8 Mon Sep 17 00:00:00 2001 From: Adam Cervenka Date: Thu, 10 Feb 2022 08:22:27 +0100 Subject: [PATCH 2/2] commons #93 scalatest uri matcher - example --- README.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 712f150..2143e30 100644 --- a/README.md +++ b/README.md @@ -513,12 +513,12 @@ myVer.buildMeta == ver"build.555" } ``` -1. `ConsoleStubs` - stubs console IO +2. `ConsoleStubs` - stubs console IO ```scala captureStdOut(Console.out.print("foo")) should be("foo") ``` -1. `SystemExitFixture` - intercepts `System.exit()` and asserts status +3. `SystemExitFixture` - intercepts `System.exit()` and asserts status ```scala captureExitStatus(System.exit(42)) should be(42) @@ -529,7 +529,7 @@ myVer.buildMeta == ver"build.555" } ``` -1. `EnvFixture` - adds an API to set an environment variable for the scope of a single test method +4. `EnvFixture` - adds an API to set an environment variable for the scope of a single test method ```scala class MySpec ... with EnvFixture { it should "set FOO variable for this test body only" in { @@ -540,7 +540,7 @@ myVer.buildMeta == ver"build.555" } ``` -1. `WhitespaceNormalizations` - extends Scalatest DSL with some whitespace treatment methods +5. `WhitespaceNormalizations` - extends Scalatest DSL with some whitespace treatment methods ```scala ( """ @@ -556,6 +556,18 @@ myVer.buildMeta == ver"build.555" ) ``` +6. `CommonMatchers` - provides matchers, currently only URI matcher + ```scala + class MySpec ... with CommonMatchers { + it should "produce the correct URI" in { + val uri: String = ??? + + uri should equalToUri("file:///foo.txt") + // compares using java.net.URI`s equals method + } + } + ``` + # S3 Utils ### S3 Location Utils