<commit_before>package com.ibm.spark.kernel.protocol.v5.interpreter.tasks

import java.io.OutputStream

import akka.actor.{Props, ActorSystem}
import akka.testkit.{ImplicitSender, TestKit}
import com.ibm.spark.interpreter.Interpreter
import com.ibm.spark.kernel.protocol.v5._
import com.ibm.spark.kernel.protocol.v5.content._
import com.typesafe.config.ConfigFactory
import org.mockito.Matchers.{eq => mockEq}
import org.mockito.Matchers._
import org.mockito.Mockito._
import org.scalatest.mock._
import org.scalatest.{FunSpecLike, Matchers}

import com.ibm.spark.interpreter._

import scala.concurrent.duration._
import scala.util.Either.LeftProjection

object ExecuteRequestTaskActorSpec {
  val config = """
    akka {
      loglevel = "WARNING"
    }"""
}

class ExecuteRequestTaskActorSpec extends TestKit(
  ActorSystem(
    "ExecuteRequestTaskActorSpec",
    ConfigFactory.parseString(ExecuteRequestTaskActorSpec.config)
  )
) with ImplicitSender with FunSpecLike with Matchers with MockitoSugar
{
  describe("ExecuteRequestTaskActor") {
    describe("#receive") {
      it("should return an ExecuteReplyOk if the interpreter returns success") {
        val mockInterpreter = mock[Interpreter]
        doReturn((Results.Success, Left(new ExecuteOutput))).when(mockInterpreter)
          .interpret(anyString(), anyBoolean())

        val executeRequestTask =
          system.actorOf(Props(
            classOf[ExecuteRequestTaskActor],
            mockInterpreter
          ))

        val executeRequest = (ExecuteRequest(
          "val x = 3", false, false,
          UserExpressions(), false
        ), mock[OutputStream])

        executeRequestTask ! executeRequest

        val result =
          receiveOne(5.seconds)
            .asInstanceOf[Either[ExecuteOutput, ExecuteError]]

        result.isLeft should be (true)
        result.left.get shouldBe an [ExecuteOutput]
      }

      it("should return an ExecuteReplyError if the interpreter returns error") {
        val mockInterpreter = mock[Interpreter]
        doReturn((Results.Error, Right(mock[ExecuteError]))).when(mockInterpreter)
          .interpret(anyString(), anyBoolean())

        val executeRequestTask =
          system.actorOf(Props(
            classOf[ExecuteRequestTaskActor],
            mockInterpreter
          ))

        val executeRequest = (ExecuteRequest(
          "val x = 3", false, false,
          UserExpressions(), false
        ), mock[OutputStream])

        executeRequestTask ! executeRequest

        val result =
          receiveOne(5.seconds)
            .asInstanceOf[Either[ExecuteOutput, ExecuteError]]

        result.isRight should be (true)
        result.right.get shouldBe an [ExecuteError]<commit_msg>Added additional test for ExecuteRequestTaskActor regarding aborted code<commit_after>package com.ibm.spark.kernel.protocol.v5.interpreter.tasks

import java.io.OutputStream

import akka.actor.{Props, ActorSystem}
import akka.testkit.{ImplicitSender, TestKit}
import com.ibm.spark.interpreter.Interpreter
import com.ibm.spark.kernel.protocol.v5._
import com.ibm.spark.kernel.protocol.v5.content._
import com.typesafe.config.ConfigFactory
import org.mockito.Matchers.{eq => mockEq}
import org.mockito.Matchers._
import org.mockito.Mockito._
import org.scalatest.mock._
import org.scalatest.{FunSpecLike, Matchers}

import com.ibm.spark.interpreter._

import scala.concurrent.duration._
import scala.util.Either.LeftProjection

object ExecuteRequestTaskActorSpec {
  val config = """
    akka {
      loglevel = "WARNING"
    }"""
}

class ExecuteRequestTaskActorSpec extends TestKit(
  ActorSystem(
    "ExecuteRequestTaskActorSpec",
    ConfigFactory.parseString(ExecuteRequestTaskActorSpec.config)
  )
) with ImplicitSender with FunSpecLike with Matchers with MockitoSugar
{
  describe("ExecuteRequestTaskActor") {
    describe("#receive") {
      it("should return an ExecuteReplyOk if the interpreter returns success") {
        val mockInterpreter = mock[Interpreter]
        doReturn((Results.Success, Left(new ExecuteOutput))).when(mockInterpreter)
          .interpret(anyString(), anyBoolean())

        val executeRequestTask =
          system.actorOf(Props(
            classOf[ExecuteRequestTaskActor],
            mockInterpreter
          ))

        val executeRequest = (ExecuteRequest(
          "val x = 3", false, false,
          UserExpressions(), false
        ), mock[OutputStream])

        executeRequestTask ! executeRequest

        val result =
          receiveOne(5.seconds)
            .asInstanceOf[Either[ExecuteOutput, ExecuteError]]

        result.isLeft should be (true)
        result.left.get shouldBe an [ExecuteOutput]
      }

      it("should return an ExecuteReplyAbort if the interpreter returns aborted") {
        val mockInterpreter = mock[Interpreter]
        doReturn((Results.Aborted, Right(mock[ExecuteAborted]))).when(mockInterpreter)
          .interpret(anyString(), anyBoolean())

        val executeRequestTask =
          system.actorOf(Props(
            classOf[ExecuteRequestTaskActor],
            mockInterpreter
          ))

        val executeRequest = (ExecuteRequest(
          "val x = 3", false, false,
          UserExpressions(), false
        ), mock[OutputStream])

        executeRequestTask ! executeRequest

        val result =
          receiveOne(5.seconds)
            .asInstanceOf[Either[ExecuteOutput, ExecuteFailure]]

        result.isRight should be (true)
        result.right.get shouldBe an [ExecuteAborted]
      }

      it("should return an ExecuteReplyError if the interpreter returns error") {
        val mockInterpreter = mock[Interpreter]
        doReturn((Results.Error, Right(mock[ExecuteError]))).when(mockInterpreter)
          .interpret(anyString(), anyBoolean())

        val executeRequestTask =
          system.actorOf(Props(
            classOf[ExecuteRequestTaskActor],
            mockInterpreter
          ))

        val executeRequest = (ExecuteRequest(
          "val x = 3", false, false,
          UserExpressions(), false
        ), mock[OutputStream])

        executeRequestTask ! executeRequest

        val result =
          receiveOne(5.seconds)
            .asInstanceOf[Either[ExecuteOutput, ExecuteError]]

        result.isRight should be (true)
        result.right.get shouldBe an [ExecuteError]
{'fraction_non_alphanumeric': 0.09192507476782623, 'fraction_numerical': 0.0025184951991185267, 'mean_word_length': 2.981203007518797, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3115519', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1714, 'n_words': 363}