Core scala refactor (#5654)
* encoding * Converting synchronous and asynchronous API to observables * Adding different ways of converting sync/async APIs to observalbles. * Replace anonymous class with lambda * update based on comment from Grzegorz Piwowarek * Refactor core-scala examples * Cleanup
This commit is contained in:
parent
03f530126f
commit
6d1e1647d5
|
@ -22,6 +22,6 @@ class Employee(val name : String,
|
||||||
* A Trait which will make the toString return upper case value.
|
* A Trait which will make the toString return upper case value.
|
||||||
*/
|
*/
|
||||||
trait UpperCasePrinter {
|
trait UpperCasePrinter {
|
||||||
override def toString = super.toString toUpperCase
|
override def toString: String = super.toString toUpperCase
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ object HigherOrderFunctions {
|
||||||
def mapReduce(r : (Int, Int) => Int,
|
def mapReduce(r : (Int, Int) => Int,
|
||||||
i : Int,
|
i : Int,
|
||||||
m : Int => Int,
|
m : Int => Int,
|
||||||
a : Int, b : Int) = {
|
a : Int, b : Int): Int = {
|
||||||
def iter(a : Int, result : Int) : Int = {
|
def iter(a : Int, result : Int) : Int = {
|
||||||
if (a > b) result
|
if (a > b) result
|
||||||
else iter(a + 1, r(m(a), result))
|
else iter(a + 1, r(m(a), result))
|
||||||
|
|
|
@ -7,9 +7,9 @@ package com.baeldung.scala
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
object Utils {
|
object Utils {
|
||||||
def average(x : Double, y : Double) = (x + y) / 2
|
def average(x : Double, y : Double): Double = (x + y) / 2
|
||||||
|
|
||||||
def randomLessThan(d : Double) = {
|
def randomLessThan(d : Double): Double = {
|
||||||
var random = 0d
|
var random = 0d
|
||||||
do {
|
do {
|
||||||
random = Math.random()
|
random = Math.random()
|
||||||
|
|
|
@ -1,32 +1,32 @@
|
||||||
package com.baeldung.scala
|
package com.baeldung.scala
|
||||||
|
|
||||||
import com.baeldung.scala.ControlStructuresDemo._
|
import com.baeldung.scala.ControlStructuresDemo._
|
||||||
import org.junit.Test
|
|
||||||
import org.junit.Assert.assertEquals
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
class ControlStructuresDemoUnitTest {
|
class ControlStructuresDemoUnitTest {
|
||||||
@Test
|
@Test
|
||||||
def givenTwoIntegers_whenGcdCalled_thenCorrectValueReturned = {
|
def givenTwoIntegers_whenGcdCalled_thenCorrectValueReturned() = {
|
||||||
assertEquals(3, gcd(15, 27))
|
assertEquals(3, gcd(15, 27))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def givenTwoIntegers_whenGcdIterCalled_thenCorrectValueReturned = {
|
def givenTwoIntegers_whenGcdIterCalled_thenCorrectValueReturned() = {
|
||||||
assertEquals(3, gcdIter(15, 27))
|
assertEquals(3, gcdIter(15, 27))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def givenTwoIntegers_whenRangeSumcalled_thenCorrectValueReturned = {
|
def givenTwoIntegers_whenRangeSumcalled_thenCorrectValueReturned() = {
|
||||||
assertEquals(55, rangeSum(1, 10))
|
assertEquals(55, rangeSum(1, 10))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def givenPositiveInteger_whenFactorialInvoked_thenCorrectValueReturned = {
|
def givenPositiveInteger_whenFactorialInvoked_thenCorrectValueReturned() = {
|
||||||
assertEquals(720, factorial(6))
|
assertEquals(720, factorial(6))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def whenFactorialOf0Invoked_then1Returned = {
|
def whenFactorialOf0Invoked_then1Returned() = {
|
||||||
assertEquals(1, factorial(0))
|
assertEquals(1, factorial(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,20 +6,20 @@ import org.junit.Test
|
||||||
class EmployeeUnitTest {
|
class EmployeeUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def whenEmployeeSalaryIncremented_thenCorrectSalary = {
|
def whenEmployeeSalaryIncremented_thenCorrectSalary() = {
|
||||||
val employee = new Employee("John Doe", 1000)
|
val employee = new Employee("John Doe", 1000)
|
||||||
employee.incrementSalary()
|
employee.incrementSalary()
|
||||||
assertEquals(1020, employee.salary)
|
assertEquals(1020, employee.salary)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def givenEmployee_whenToStringCalled_thenCorrectStringReturned = {
|
def givenEmployee_whenToStringCalled_thenCorrectStringReturned() = {
|
||||||
val employee = new Employee("John Doe", 1000)
|
val employee = new Employee("John Doe", 1000)
|
||||||
assertEquals("Employee(name=John Doe, salary=1000)", employee.toString)
|
assertEquals("Employee(name=John Doe, salary=1000)", employee.toString)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def givenEmployeeWithTrait_whenToStringCalled_thenCorrectStringReturned = {
|
def givenEmployeeWithTrait_whenToStringCalled_thenCorrectStringReturned() = {
|
||||||
val employee =
|
val employee =
|
||||||
new Employee("John Doe", 1000) with UpperCasePrinter
|
new Employee("John Doe", 1000) with UpperCasePrinter
|
||||||
assertEquals("EMPLOYEE(NAME=JOHN DOE, SALARY=1000)", employee.toString)
|
assertEquals("EMPLOYEE(NAME=JOHN DOE, SALARY=1000)", employee.toString)
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
package com.baeldung.scala
|
package com.baeldung.scala
|
||||||
|
|
||||||
|
import com.baeldung.scala.HigherOrderFunctions.mapReduce
|
||||||
import org.junit.Assert.assertEquals
|
import org.junit.Assert.assertEquals
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
import HigherOrderFunctions.mapReduce
|
|
||||||
|
|
||||||
class HigherOrderFunctionsUnitTest {
|
class HigherOrderFunctionsUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def whenCalledWithSumAndSquareFunctions_thenCorrectValueReturned = {
|
def whenCalledWithSumAndSquareFunctions_thenCorrectValueReturned() = {
|
||||||
def square(x : Int) = x * x
|
def square(x : Int) = x * x
|
||||||
|
|
||||||
def sum(x : Int, y : Int) = x + y
|
def sum(x : Int, y : Int) = x + y
|
||||||
|
@ -20,7 +19,7 @@ class HigherOrderFunctionsUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def whenComputingSumOfSquaresWithAnonymousFunctions_thenCorrectValueReturned = {
|
def whenComputingSumOfSquaresWithAnonymousFunctions_thenCorrectValueReturned() = {
|
||||||
def sumSquares(a : Int, b : Int) =
|
def sumSquares(a : Int, b : Int) =
|
||||||
mapReduce((x, y) => x + y, 0, x => x * x, a, b)
|
mapReduce((x, y) => x + y, 0, x => x * x, a, b)
|
||||||
|
|
||||||
|
@ -28,7 +27,7 @@ class HigherOrderFunctionsUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def givenCurriedFunctions_whenInvoked_thenCorrectValueReturned = {
|
def givenCurriedFunctions_whenInvoked_thenCorrectValueReturned() = {
|
||||||
// a curried function
|
// a curried function
|
||||||
def sum(f : Int => Int)(a : Int,
|
def sum(f : Int => Int)(a : Int,
|
||||||
b : Int) : Int =
|
b : Int) : Int =
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
package com.baeldung.scala
|
package com.baeldung.scala
|
||||||
|
|
||||||
import scala.Range
|
|
||||||
|
|
||||||
import org.junit.Assert.assertFalse
|
import org.junit.Assert.assertFalse
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
class IntSetUnitTest {
|
class IntSetUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def givenSetof1To10_whenContains11Called_thenFalse = {
|
def givenSetof1To10_whenContains11Called_thenFalse() = {
|
||||||
|
|
||||||
// Set up a set containing integers 1 to 10.
|
// Set up a set containing integers 1 to 10.
|
||||||
val set1To10 =
|
val set1To10 =
|
||||||
|
|
|
@ -1,34 +1,29 @@
|
||||||
package com.baeldung.scala
|
package com.baeldung.scala
|
||||||
|
|
||||||
import org.junit.Assert.assertEquals
|
import com.baeldung.scala.Utils.{average, fibonacci, power, randomLessThan}
|
||||||
import org.junit.Assert.assertTrue
|
import org.junit.Assert.{assertEquals, assertTrue}
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
import Utils.average
|
|
||||||
import Utils.fibonacci
|
|
||||||
import Utils.power
|
|
||||||
import Utils.randomLessThan
|
|
||||||
|
|
||||||
class UtilsUnitTest {
|
class UtilsUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def whenAverageCalled_thenCorrectValueReturned() = {
|
def whenAverageCalled_thenCorrectValueReturned(): Unit = {
|
||||||
assertEquals(15.0, average(10, 20), 1e-5)
|
assertEquals(15.0, average(10, 20), 1e-5)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def whenRandomLessThanInvokedWithANumber_thenARandomLessThanItReturned = {
|
def whenRandomLessThanInvokedWithANumber_thenARandomLessThanItReturned: Unit = {
|
||||||
val d = 0.1
|
val d = 0.1
|
||||||
assertTrue(randomLessThan(d) < d)
|
assertTrue(randomLessThan(d) < d)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def whenPowerInvokedWith2And3_then8Returned = {
|
def whenPowerInvokedWith2And3_then8Returned: Unit = {
|
||||||
assertEquals(8, power(2, 3))
|
assertEquals(8, power(2, 3))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def whenFibonacciCalled_thenCorrectValueReturned = {
|
def whenFibonacciCalled_thenCorrectValueReturned: Unit = {
|
||||||
assertEquals(1, fibonacci(0))
|
assertEquals(1, fibonacci(0))
|
||||||
assertEquals(1, fibonacci(1))
|
assertEquals(1, fibonacci(1))
|
||||||
assertEquals(fibonacci(6),
|
assertEquals(fibonacci(6),
|
||||||
|
|
Loading…
Reference in New Issue