This repository has been archived by the owner on Mar 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VCFEF-216 Added traits with enforcing constraints on the value types
- Loading branch information
Jakub Chrobasik
committed
Sep 28, 2016
1 parent
41d7d84
commit dd466cf
Showing
20 changed files
with
379 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/scala/uk/gov/voa/valuetype/constraints/NonEmpty.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2016 HM Revenue & Customs | ||
* | ||
* 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 uk.gov.voa.valuetype.constraints | ||
|
||
import uk.gov.voa.valuetype.StringValue | ||
|
||
trait NonEmpty { | ||
|
||
self: StringValue => | ||
|
||
require(value.length > 0, s"$typeName cannot be empty") | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/scala/uk/gov/voa/valuetype/constraints/numberConstraints.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2016 HM Revenue & Customs | ||
* | ||
* 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 uk.gov.voa.valuetype.constraints | ||
|
||
import uk.gov.voa.valuetype.{IntValue, LongValue, ValueType} | ||
|
||
trait PositiveInt extends IntValue { | ||
|
||
require(value > 0, s"$typeName's value has to be positive") | ||
|
||
} | ||
|
||
trait PositiveLong extends LongValue { | ||
|
||
require(value > 0, s"$typeName's value has to be positive") | ||
|
||
} | ||
|
||
trait PositiveBigDecimal { | ||
|
||
self: ValueType[BigDecimal] => | ||
|
||
require(value > 0, s"$typeName's value has to be positive") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/test/scala/uk/gov/voa/valuetype/constraints/NonEmptySpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2016 HM Revenue & Customs | ||
* | ||
* 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 uk.gov.voa.valuetype.constraints | ||
|
||
import org.scalatest.prop.PropertyChecks | ||
import uk.gov.voa.valuetype.StringValue | ||
import uk.gov.voa.valuetype.tooling.UnitSpec | ||
import uk.gov.voa.valuetype.tooling.generators.GeneratorOf.nonEmptyStrings | ||
|
||
class NonEmptySpec extends UnitSpec with PropertyChecks { | ||
|
||
private case class TestNonEmpty(value: String) extends StringValue with NonEmpty | ||
|
||
"NonEmpty" should { | ||
|
||
"allow instantiation with any non-empty String" in { | ||
forAll(nonEmptyStrings()) { value => | ||
TestNonEmpty(value).value shouldBe value | ||
} | ||
} | ||
|
||
"throw an IllegalArgumentException when instantiating with an empty String" in { | ||
val exception = the[IllegalArgumentException] thrownBy TestNonEmpty("") | ||
exception.getMessage shouldBe "requirement failed: TestNonEmpty cannot be empty" | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/test/scala/uk/gov/voa/valuetype/constraints/PositiveBigDecimalSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2016 HM Revenue & Customs | ||
* | ||
* 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 uk.gov.voa.valuetype.constraints | ||
|
||
import org.scalatest.prop.PropertyChecks | ||
import uk.gov.voa.valuetype.BigDecimalValue | ||
import uk.gov.voa.valuetype.tooling.UnitSpec | ||
import uk.gov.voa.valuetype.tooling.generators.GeneratorOf._ | ||
|
||
class PositiveBigDecimalSpec extends UnitSpec with PropertyChecks { | ||
|
||
private case class TestPositiveBigDecimal(value: BigDecimal) extends BigDecimalValue with PositiveBigDecimal | ||
|
||
"PositiveBigDecimal" should { | ||
|
||
"allow instantiation with any positive number" in { | ||
forAll(positiveBigDecimal) { value => | ||
TestPositiveBigDecimal(value).value shouldBe value | ||
} | ||
} | ||
|
||
"throw an IllegalArgumentException when instantiating with zero" in { | ||
val exception = the[IllegalArgumentException] thrownBy TestPositiveBigDecimal(0) | ||
exception.getMessage shouldBe "requirement failed: TestPositiveBigDecimal's value has to be positive" | ||
} | ||
|
||
"throw an IllegalArgumentException when instantiating with a negative value" in { | ||
val exception = the[IllegalArgumentException] thrownBy TestPositiveBigDecimal(-1) | ||
exception.getMessage shouldBe "requirement failed: TestPositiveBigDecimal's value has to be positive" | ||
} | ||
} | ||
} |
Oops, something went wrong.