Skip to content

Commit

Permalink
SentimnetAnalysisProcessorTest modified based on new files
Browse files Browse the repository at this point in the history
  • Loading branch information
soheilabadifard committed Jan 12, 2024
1 parent 99b3b7a commit 47991d1
Showing 1 changed file with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
package com.github.soheilabadifard.kotlincommentssentimentanalysis.core

import com.github.soheilabadifard.kotlincommentssentimentanalysis.statistics.FileStatistics
import com.intellij.psi.PsiManager
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.PsiComment
import java.nio.file.Files
import java.nio.file.Path
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import kotlinx.coroutines.runBlocking
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.mockito.kotlin.verify


class SentimentAnalysisProcessorTest : BasePlatformTestCase() {

private lateinit var myAction: SentimentAnalysisProcessor
private lateinit var mockProcessorFactory: SentimentAnalysisProcessorFactory
private lateinit var mockProcessor: SentimentAnalysisProcessor

override fun setUp() {
super.setUp()
myAction = SentimentAnalysisProcessor(project)
mockProcessorFactory = mock()
mockProcessor = mock()
whenever(mockProcessorFactory.create(myFixture.project)).thenReturn(mockProcessor)

setupMockProcessorBehavior()
// Copy tokenizer resources to the test project
copyTokenizerResources()
setupVirtualFileSystem()
}
private fun setupMockProcessorBehavior() {
// FileStatsMap is a Map<String, FileStatistics>
// Set up the mock to return a predefined map or specific values when accessed
val mockFileStatsMap = mapOf(
"MyTestFile.kt" to createMockFileStatistics()
)
whenever(mockProcessor.fileStatisticsMap).thenReturn(mockFileStatsMap as MutableMap<String, FileStatistics>?)
}

private fun createMockFileStatistics(): FileStatistics {
// Create an instance of FileStatistics with predefined values
val mockFileStats = FileStatistics("MyTestFile.kt")

// FileStatistics class has methods to add sentiment data
// Add mock data for positive comments
mockFileStats.addPositive(0.8F) // Example probability value for a positive comment

return mockFileStats
}

private fun copyTokenizerResources() {
// Define the path to your tokenizer resources within your project
Expand Down Expand Up @@ -59,12 +85,13 @@ class SentimentAnalysisProcessorTest : BasePlatformTestCase() {
}

fun testActionPerformed() {
runBlocking {
myAction.performAnalysis()
}

// access fileStatisticsMap from myAction
val fileStatsMap = myAction.fileStatisticsMap
val processor = mockProcessorFactory.create(myFixture.project)
processor.performAnalysis()
verify(mockProcessor).performAnalysis()

val fileStatsMap = mockProcessor.fileStatisticsMap


val psiFile = myFixture.findFileInTempDir("src/main/kotlin/org/intellij/sdk/kotlin/MyTestFile.kt")
assertNotNull("PsiFile should not be null", psiFile)
Expand All @@ -74,6 +101,16 @@ class SentimentAnalysisProcessorTest : BasePlatformTestCase() {

assertFalse("There should be comments in the file", comments.isEmpty())

val expectedFileName = "MyTestFile.kt"
val fileStats = fileStatsMap[expectedFileName]
assertNotNull("Stats for $expectedFileName should exist", fileStats)

// Verify the statistics of the file
assertTrue("There should be at least one positive comment", fileStats!!.positiveCount > 0)

assertTrue("Average probability for positive comments should be within range",
fileStats.averagePositiveProbability in 0.0..1.0)

// Check if the map is not empty
//assertFalse("fileStatisticsMap should not be empty", fileStatsMap.isEmpty())

Expand Down

0 comments on commit 47991d1

Please sign in to comment.