Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 {
Expand All @@ -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
(
"""
Expand All @@ -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
Expand Down
39 changes: 39 additions & 0 deletions src/main/scala/za/co/absa/commons/scalatest/CommonMatchers.scala
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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
}

}