Created
March 21, 2018 09:39
-
-
Save GlulkAlex/d2763a5331a9375d439c5807de33c58f to your computer and use it in GitHub Desktop.
PartialFunction combination testing, using 47deg.github.io LambdaTest
This file contains hidden or 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
//package ??? | |
//import java.time.LocalDateTime | |
// | |
//import scala.language.implicitConversions | |
import scala.PartialFunction._ | |
// | |
import akka.actor.{ Actor, ActorLogging, ActorRef, Props } | |
import akka.event.LoggingReceive | |
// | |
import com.fortysevendeg.lambdatest._ | |
//import com.fortysevendeg.lambdatest.LambdaTest | |
import com.fortysevendeg.lambdatestasync._ | |
// format: OFF | |
class LambdaTest_PF_Combo | |
extends LambdaTest | |
{ | |
var state_Val_1: Int = 0 | |
var state_Val_2: Int = 0 | |
var state_Val_Sum: Int = 0 | |
/** careful when use val -> because evaluates only once */ | |
//val reset_State: Unit => Unit = { | |
def reset_State: Unit = { | |
state_Val_1 = 0 | |
state_Val_2 = 0 | |
state_Val_Sum = 0 | |
println( | |
s"reset to default state:(v1:$state_Val_1, v2:$state_Val_2, s:$state_Val_Sum)" | |
) | |
} | |
val update_Before_1: PartialFunction[ Int, Int ] = { | |
case input_Val | |
if input_Val == 1 => { | |
println( s"Inside update_Before_1($input_Val)" ) | |
println( s"\tstate was:\t(v1:$state_Val_1, v2:$state_Val_2, s:$state_Val_Sum)" ) | |
state_Val_1 += input_Val | |
println( s"\tupdated:\t(v1:$state_Val_1, v2:$state_Val_2, s:$state_Val_Sum)" ) | |
// return | |
input_Val | |
} | |
case input_Val => input_Val | |
} | |
val update_Before_2: PartialFunction[ Int, Int ] = { | |
case input_Val | |
if input_Val == 2 => { | |
println( s"Inside update_Before_2($input_Val)" ) | |
println( s"\tstate was:\t(v1:$state_Val_1, v2:$state_Val_2, s:$state_Val_Sum)" ) | |
state_Val_2 += input_Val | |
println( s"\tupdated:\t(v1:$state_Val_1, v2:$state_Val_2, s:$state_Val_Sum)" ) | |
// return | |
input_Val | |
} | |
case input_Val => input_Val | |
} | |
val add_Odd: PartialFunction[ Int, Int ] = { | |
case input_Val | |
if input_Val % 2 != 0 => { | |
println( s"Inside add_Odd($input_Val)" ) | |
println( s"\tstate was:\t(v1:$state_Val_1, v2:$state_Val_2, s:$state_Val_Sum)" ) | |
state_Val_1 += input_Val | |
state_Val_Sum += 3 * input_Val | |
println( s"\tupdated:\t(v1:$state_Val_1, v2:$state_Val_2, s:$state_Val_Sum)" ) | |
// return | |
input_Val | |
} | |
} | |
val add_Even: PartialFunction[ Int, Int ] = { | |
case input_Val | |
if input_Val % 2 == 0 => { | |
println( s"Inside add_Even($input_Val)" ) | |
println( s"\tstate was:\t(v1:$state_Val_1, v2:$state_Val_2, s:$state_Val_Sum)" ) | |
state_Val_2 += input_Val | |
state_Val_Sum += 2 * input_Val | |
println( s"\tupdated:\t(v1:$state_Val_1, v2:$state_Val_2, s:$state_Val_Sum)" ) | |
// return | |
input_Val | |
} | |
} | |
/** last step */ | |
val update_After: PartialFunction[ Int, Int ] = { | |
case input_Val | |
if state_Val_1 == 1 | |
|| state_Val_2 == 2 => { | |
println( s"Inside update_After($input_Val)" ) | |
println( s"\tstate was:\t(v1:$state_Val_1, v2:$state_Val_2, s:$state_Val_Sum)" ) | |
state_Val_1 = 2 | |
state_Val_2 = 1 | |
state_Val_Sum = 3 | |
println( s"\tupdated:\t(v1:$state_Val_1, v2:$state_Val_2, s:$state_Val_Sum)" ) | |
// return | |
state_Val_Sum | |
} | |
case input_Val => state_Val_Sum//42 | |
} | |
val combo_5 = update_Before_1 | |
.andThen( | |
update_Before_2 | |
.andThen( | |
add_Odd | |
.orElse( add_Even ) | |
.andThen( update_After ) | |
) | |
) | |
val combo_5_2 = update_After | |
.compose( | |
add_Odd | |
.orElse( add_Even ) | |
.compose( | |
update_Before_2 | |
.compose( update_Before_1 ) | |
) | |
) | |
object PF_Combo_Actor { | |
def props( | |
// adding an optional listener to the class | |
/// @forDebug | |
listener_Opt: Option[ ActorRef ] = None | |
): Props = //Props[ PF_Combo_Actor ] | |
Props( | |
/* | |
classOf[ PF_Combo_Actor ] | |
, listener_Opt | |
*/ | |
new PF_Combo_Actor( listener_Opt ) | |
) | |
final case object Reset_State | |
} | |
class PF_Combo_Actor( | |
/// @forDebug | |
listener_Opt: Option[ ActorRef ] = None | |
) | |
extends Actor | |
with ActorLogging | |
{ | |
// to avoid prefixing access with context | |
import context._ | |
import PF_Combo_Actor._ | |
def receive: Receive = LoggingReceive { | |
// required: PartialFunction[Any,Unit] | |
//# General | intended | anticipated case(s) | |
case event: Int => listener_Opt.map( _ => sender() ! combo_5_2( event ) ) | |
//# Special case | |
case event | |
if event == Reset_State => { | |
reset_State | |
listener_Opt.map( _ => sender() ! event ) | |
} | |
//# Unexpected | unhandled case(s) | |
case event => listener_Opt.map( _ => sender() ! event ) | |
} | |
} | |
import PF_Combo_Actor._ | |
// an object contains an ordered list of transforms | |
val act: LambdaAct = label( | |
"PartialFunction combination test" | |
//, tags = Set( "SKIP" , "ignore" ) | |
) { | |
test( | |
"it should return right computation(al) result as Int" | |
//, tags = Set( "SKIP" , "ignore" ) | |
) { | |
val expected_Result = 1 | |
// return | |
//assert( true, "starting from `true`" ) | |
assertEq( | |
combo_5( -2 ) | |
, -4 | |
, "combo_5 result on -2" | |
) + | |
exec { | |
reset_State | |
} + | |
assertEq( | |
combo_5( -1 ) | |
, -3 | |
, "combo_5 result on -1" | |
) + | |
exec { | |
reset_State | |
} + | |
assertEq( | |
combo_5( 0 ) | |
, 0 | |
, "combo_5 result on 0" | |
) + | |
exec { | |
reset_State | |
} + | |
assertEq( | |
combo_5( 1 ) | |
, 3 | |
, "combo_5 result on 1" | |
) + | |
exec { | |
reset_State | |
} + | |
assertEq( | |
combo_5( 2 ) | |
, 4 | |
, "combo_5 result on 2" | |
) + | |
exec { | |
reset_State | |
} + | |
assertEq( | |
combo_5( 3 ) | |
, 9 | |
, "combo_5 result on 3" | |
) + | |
exec { | |
reset_State | |
} + | |
assertEq( | |
combo_5( 4 ) | |
, 8 | |
, "combo_5 result on 4" | |
) | |
} + | |
test( | |
"it should return 3 for 1" | |
//, tags = Set( "SKIP" , "ignore" ) | |
) { | |
//reset_State | |
// return | |
exec { | |
reset_State | |
} + | |
assertEq( | |
combo_5_2( 1 ) | |
, 3 | |
, "combo_5_2 result on 1" | |
) | |
} + | |
test( | |
"it should return 4 for 2" | |
//, tags = Set( "SKIP" , "ignore" ) | |
) { | |
reset_State | |
// return | |
/*exec { | |
reset_State | |
} +*/ | |
assertEq( | |
combo_5_2( 2 ) | |
, 4 | |
, "combo_5_2 result on 2" | |
) | |
} + | |
actorSystemWrap() { implicit system => | |
val ( | |
test_Probe_Ref | |
, probe_Events | |
) = Probe("test-probe") | |
val pf_Combo_Props = PF_Combo_Actor | |
.props( | |
Some( | |
//Option( | |
test_Probe_Ref ) | |
) | |
// actor under test | |
val ( | |
pf_Combo_Actor_Ref | |
, pf_Combo_Actor_Events | |
) = Intercept( | |
"pf-combo-actor" | |
, pf_Combo_Props | |
) | |
label( | |
"PF_Combo_Actor test" | |
//, tags = Set( "SKIP" , "ignore" ) | |
) { | |
test( | |
"it should respond with 3 on message 1 after Reset_State" | |
//, tags = Set( "SKIP" , "ignore" ) | |
) { | |
expectEvents( | |
probe_Events | |
//pf_Combo_Actor_Events | |
, "send msg to test-probe" | |
, show = msgShow | |
) { | |
// body | |
exec { | |
//reset_State | |
// or | |
test_Probe_Ref ! SendMsg( pf_Combo_Actor_Ref, Reset_State ) | |
( 1 to 1 ).foreach { i => | |
test_Probe_Ref ! SendMsg( pf_Combo_Actor_Ref, 1 ) | |
} | |
} | |
} | |
// event-test | |
{ | |
// return | |
MsgHasExact( | |
List( | |
Reset_State | |
, 3 | |
) | |
) | |
} | |
} | |
} | |
} | |
// | |
} | |
} | |
/** | |
// companion | |
$ sbt | |
> testOnly LambdaTest_PF_Combo | |
*/ | |
object LambdaTest_PF_Combo | |
extends App | |
{ | |
// This method is used to run test. | |
run( | |
name = "PartialFunction combination test" | |
, body = new LambdaTest_PF_Combo | |
, parallel = false | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment