Skip to content

Commit

Permalink
test: Extract copy tests to ArithmeticTest file
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrylo committed Nov 1, 2023
1 parent 3d774bf commit b1cce26
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/

package org.eclipse.kuksa.vssSpecification

import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldStartWith
import org.eclipse.kuksa.extension.vssProperty.div
import org.eclipse.kuksa.extension.vssProperty.minus
import org.eclipse.kuksa.extension.vssProperty.plus
import org.eclipse.kuksa.extension.vssProperty.times
import org.eclipse.kuksa.test.kotest.Unit

class VssSpecificationArithmeticTest : BehaviorSpec({
tags(Unit)

given("VssSpecification values for arithmetic operations") {
val valueInt = VssValueInt(value = 100)
val valueFloat = VssValueFloat(value = 100f)
val valueLong = VssValueLong(value = 100L)
val valueDouble = VssValueDouble(value = 100.0)

val values = listOf<Number>(5, 5L, 5f, 5.0)

`when`("a plus operation is done") {
val newValues = values.map {
listOf(
valueInt + it,
valueFloat + it,
valueLong + it,
valueDouble + it,
)
}

then("it should correctly add the values") {
newValues.forEach { properties ->
properties.forEach {
it.value shouldBe 105
}
}
}
}

`when`("a minus operation is done") {
val newValues = values.map {
listOf(
valueInt - it,
valueFloat - it,
valueLong - it,
valueDouble - it,
)
}

then("it should correctly subtract the values") {
newValues.forEach { properties ->
properties.forEach {
it.value shouldBe 95
}
}
}
}

`when`("a divide operation with zero is done") {
val exception = shouldThrow<ArithmeticException> {
valueInt / 0
}

then("it should throw an exception") {
exception.message shouldStartWith "/ by zero"
}
}

`when`("a divide operation is done") {
val newValues = values.map {
listOf(
valueInt / it,
valueFloat / it,
valueLong / it,
valueDouble / it,
)
}

then("it should correctly divide the values") {
newValues.forEach { properties ->
properties.forEach {
it.value shouldBe 20
}
}
}
}

`when`("a multiply operation is done") {
val newValues = values.map {
listOf(
valueInt * it,
valueFloat * it,
valueLong * it,
valueDouble * it,
)
}

then("it should correctly multiply the values") {
newValues.forEach { properties ->
properties.forEach {
it.value shouldBe 500
}
}
}
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ import io.kotest.matchers.types.shouldBeSameInstanceAs
import org.eclipse.kuksa.extension.copy
import org.eclipse.kuksa.extension.deepCopy
import org.eclipse.kuksa.extension.invoke
import org.eclipse.kuksa.extension.vssProperty.div
import org.eclipse.kuksa.extension.vssProperty.minus
import org.eclipse.kuksa.extension.vssProperty.not
import org.eclipse.kuksa.extension.vssProperty.plus
import org.eclipse.kuksa.extension.vssProperty.times
import org.eclipse.kuksa.proto.v1.Types
import org.eclipse.kuksa.test.kotest.Unit
import org.eclipse.kuksa.vsscore.model.VssProperty
Expand Down Expand Up @@ -66,101 +62,6 @@ class VssSpecificationCopyTest : BehaviorSpec({
}
}

and("values for arithmetic operations") {
val valueInt = VssValueInt(value = 100)
val valueFloat = VssValueFloat(value = 100f)
val valueLong = VssValueLong(value = 100L)
val valueDouble = VssValueDouble(value = 100.0)

val values = listOf<Number>(5, 5L, 5f, 5.0)

`when`("a plus operation is done") {
val newValues = values.map {
listOf(
valueInt + it,
valueFloat + it,
valueLong + it,
valueDouble + it,
)
}

then("it should correctly add the values") {
newValues.forEach { properties ->
properties.forEach {
it.value shouldBe 105
}
}
}
}

`when`("a minus operation is done") {
val newValues = values.map {
listOf(
valueInt - it,
valueFloat - it,
valueLong - it,
valueDouble - it,
)
}

then("it should correctly subtract the values") {
newValues.forEach { properties ->
properties.forEach {
it.value shouldBe 95
}
}
}
}

`when`("a divide operation with zero is done") {
val exception = shouldThrow<ArithmeticException> {
valueInt / 0
}

then("it should throw an exception") {
exception.message shouldStartWith "/ by zero"
}
}

`when`("a divide operation is done") {
val newValues = values.map {
listOf(
valueInt / it,
valueFloat / it,
valueLong / it,
valueDouble / it,
)
}

then("it should correctly divide the values") {
newValues.forEach { properties ->
properties.forEach {
it.value shouldBe 20
}
}
}
}

`when`("a multiply operation is done") {
val newValues = values.map {
listOf(
valueInt * it,
valueFloat * it,
valueLong * it,
valueDouble * it,
)
}

then("it should correctly multiply the values") {
newValues.forEach { properties ->
properties.forEach {
it.value shouldBe 500
}
}
}
}
}

and("a changed value") {
val newValue = 40

Expand Down

0 comments on commit b1cce26

Please sign in to comment.