Skip to content

Commit

Permalink
Core scala refactor (eugenp#5654)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pivovarit authored Nov 10, 2018
1 parent 03f5301 commit 6d1e164
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ class Employee(val name : String,
* A Trait which will make the toString return upper case value.
*/
trait UpperCasePrinter {
override def toString = super.toString toUpperCase
override def toString: String = super.toString toUpperCase
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object HigherOrderFunctions {
def mapReduce(r : (Int, Int) => Int,
i : Int,
m : Int => Int,
a : Int, b : Int) = {
a : Int, b : Int): Int = {
def iter(a : Int, result : Int) : Int = {
if (a > b) result
else iter(a + 1, r(m(a), result))
Expand Down
4 changes: 2 additions & 2 deletions core-scala/src/main/scala/com/baeldung/scala/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package com.baeldung.scala
*
*/
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
do {
random = Math.random()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package com.baeldung.scala

import com.baeldung.scala.ControlStructuresDemo._
import org.junit.Test
import org.junit.Assert.assertEquals
import org.junit.Test

class ControlStructuresDemoUnitTest {
@Test
def givenTwoIntegers_whenGcdCalled_thenCorrectValueReturned = {
def givenTwoIntegers_whenGcdCalled_thenCorrectValueReturned() = {
assertEquals(3, gcd(15, 27))
}

@Test
def givenTwoIntegers_whenGcdIterCalled_thenCorrectValueReturned = {
def givenTwoIntegers_whenGcdIterCalled_thenCorrectValueReturned() = {
assertEquals(3, gcdIter(15, 27))
}

@Test
def givenTwoIntegers_whenRangeSumcalled_thenCorrectValueReturned = {
def givenTwoIntegers_whenRangeSumcalled_thenCorrectValueReturned() = {
assertEquals(55, rangeSum(1, 10))
}

@Test
def givenPositiveInteger_whenFactorialInvoked_thenCorrectValueReturned = {
def givenPositiveInteger_whenFactorialInvoked_thenCorrectValueReturned() = {
assertEquals(720, factorial(6))
}

@Test
def whenFactorialOf0Invoked_then1Returned = {
def whenFactorialOf0Invoked_then1Returned() = {
assertEquals(1, factorial(0))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import org.junit.Test
class EmployeeUnitTest {

@Test
def whenEmployeeSalaryIncremented_thenCorrectSalary = {
def whenEmployeeSalaryIncremented_thenCorrectSalary() = {
val employee = new Employee("John Doe", 1000)
employee.incrementSalary()
assertEquals(1020, employee.salary)
}

@Test
def givenEmployee_whenToStringCalled_thenCorrectStringReturned = {
def givenEmployee_whenToStringCalled_thenCorrectStringReturned() = {
val employee = new Employee("John Doe", 1000)
assertEquals("Employee(name=John Doe, salary=1000)", employee.toString)
}

@Test
def givenEmployeeWithTrait_whenToStringCalled_thenCorrectStringReturned = {
def givenEmployeeWithTrait_whenToStringCalled_thenCorrectStringReturned() = {
val employee =
new Employee("John Doe", 1000) with UpperCasePrinter
assertEquals("EMPLOYEE(NAME=JOHN DOE, SALARY=1000)", employee.toString)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.baeldung.scala

import com.baeldung.scala.HigherOrderFunctions.mapReduce
import org.junit.Assert.assertEquals
import org.junit.Test

import HigherOrderFunctions.mapReduce

class HigherOrderFunctionsUnitTest {

@Test
def whenCalledWithSumAndSquareFunctions_thenCorrectValueReturned = {
def whenCalledWithSumAndSquareFunctions_thenCorrectValueReturned() = {
def square(x : Int) = x * x

def sum(x : Int, y : Int) = x + y
Expand All @@ -20,15 +19,15 @@ class HigherOrderFunctionsUnitTest {
}

@Test
def whenComputingSumOfSquaresWithAnonymousFunctions_thenCorrectValueReturned = {
def whenComputingSumOfSquaresWithAnonymousFunctions_thenCorrectValueReturned() = {
def sumSquares(a : Int, b : Int) =
mapReduce((x, y) => x + y, 0, x => x * x, a, b)

assertEquals(385, sumSquares(1, 10))
}

@Test
def givenCurriedFunctions_whenInvoked_thenCorrectValueReturned = {
def givenCurriedFunctions_whenInvoked_thenCorrectValueReturned() = {
// a curried function
def sum(f : Int => Int)(a : Int,
b : Int) : Int =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.baeldung.scala

import scala.Range

import org.junit.Assert.assertFalse
import org.junit.Test

class IntSetUnitTest {

@Test
def givenSetof1To10_whenContains11Called_thenFalse = {
def givenSetof1To10_whenContains11Called_thenFalse() = {

// Set up a set containing integers 1 to 10.
val set1To10 =
Expand Down
17 changes: 6 additions & 11 deletions core-scala/src/test/scala/com/baeldung/scala/UtilsUnitTest.scala
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
package com.baeldung.scala

import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import com.baeldung.scala.Utils.{average, fibonacci, power, randomLessThan}
import org.junit.Assert.{assertEquals, assertTrue}
import org.junit.Test

import Utils.average
import Utils.fibonacci
import Utils.power
import Utils.randomLessThan

class UtilsUnitTest {

@Test
def whenAverageCalled_thenCorrectValueReturned() = {
def whenAverageCalled_thenCorrectValueReturned(): Unit = {
assertEquals(15.0, average(10, 20), 1e-5)
}

@Test
def whenRandomLessThanInvokedWithANumber_thenARandomLessThanItReturned = {
def whenRandomLessThanInvokedWithANumber_thenARandomLessThanItReturned: Unit = {
val d = 0.1
assertTrue(randomLessThan(d) < d)
}

@Test
def whenPowerInvokedWith2And3_then8Returned = {
def whenPowerInvokedWith2And3_then8Returned: Unit = {
assertEquals(8, power(2, 3))
}

@Test
def whenFibonacciCalled_thenCorrectValueReturned = {
def whenFibonacciCalled_thenCorrectValueReturned: Unit = {
assertEquals(1, fibonacci(0))
assertEquals(1, fibonacci(1))
assertEquals(fibonacci(6),
Expand Down

0 comments on commit 6d1e164

Please sign in to comment.