Skip to content

Improve settings, part 1 #898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1053,13 +1053,13 @@ enum class MockStrategyApi(
override val displayName: String,
override val description: String
) : CodeGenerationSettingItem {
NO_MOCKS("No mocks", "Do not use mock frameworks at all"),
NO_MOCKS("Do not mock", "Do not use mock frameworks at all"),
OTHER_PACKAGES(
"Other packages: $MOCKITO",
"Mock package environment",
"Mock all classes outside the current package except system ones"
),
OTHER_CLASSES(
"Other classes: $MOCKITO",
"Mock class environment",
"Mock all classes outside the class under test except system ones"
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,11 +593,11 @@ enum class RuntimeExceptionTestsBehaviour(
override val description: String
) : CodeGenerationSettingItem {
PASS(
displayName = "Passing",
displayName = "Pass",
description = "Tests that produce Runtime exceptions should pass (by inserting throwable assertion)"
),
FAIL(
displayName = "Failing",
displayName = "Fail",
description = "Tests that produce Runtime exceptions should fail" +
"(WARNING!: failing tests may appear in testing class)"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,36 @@ import com.intellij.ui.layout.labelTable
import com.intellij.ui.layout.panel
import com.intellij.ui.layout.slider
import com.intellij.ui.layout.withValueBinding
import com.intellij.util.ui.UIUtil
import javax.swing.DefaultComboBoxModel
import javax.swing.JCheckBox
import javax.swing.JLabel
import javax.swing.JPanel
import kotlin.reflect.KClass
import org.utbot.framework.UtSettings
import org.utbot.framework.codegen.ForceStaticMocking
import org.utbot.framework.codegen.HangingTestsTimeout
import org.utbot.framework.codegen.RuntimeExceptionTestsBehaviour
import org.utbot.framework.plugin.api.CodeGenerationSettingItem
import org.utbot.framework.plugin.api.CodegenLanguage
import org.utbot.framework.plugin.api.MockStrategyApi
import org.utbot.framework.plugin.api.TreatOverflowAsError
import javax.swing.DefaultComboBoxModel
import javax.swing.JLabel
import javax.swing.JPanel
import kotlin.reflect.KClass

@Suppress("UNCHECKED_CAST")
class SettingsWindow(val project: Project) {
private val settings = project.service<Settings>()

// TODO it is better to use something like SearchEverywhere for classes but it is complicated to implement
private val excludeTable = MockAlwaysClassesTable(project)
private lateinit var forceMockCheckBox: JCheckBox

val panel: JPanel = panel {
val valuesComboBox: LayoutBuilder.(KClass<*>, Array<*>) -> Unit = { loader, values ->
val serviceLabels = mapOf(
MockStrategyApi::class to "Mock strategy:",
CodegenLanguage::class to "Language generation:",
CodegenLanguage::class to "Generated test language:",
RuntimeExceptionTestsBehaviour::class to "Test with exceptions:",
ForceStaticMocking::class to "Force static mocking:",
TreatOverflowAsError::class to "Overflow detection:",
)

val serviceComments = mapOf(
RuntimeExceptionTestsBehaviour::class to "Test behavior when runtime exception occurs",
val tooltipLabels = mapOf(
CodegenLanguage::class to "You can generate test methods in Java or Kotlin regardless of your source code language."
)

row(serviceLabels[loader] ?: error("Unknown service loader: $loader")) {
Expand All @@ -50,24 +49,12 @@ class SettingsWindow(val project: Project) {
DefaultComboBoxModel(values),
getter = { settings.providerNameByServiceLoader(loader) },
setter = { settings.setProviderByLoader(loader, it as CodeGenerationSettingItem) },
).apply {
val comment = serviceComments[loader]
if (comment != null) {
ContextHelpLabel.create(comment)()
}
}
).apply { ContextHelpLabel.create(tooltipLabels[loader] ?: return@apply)() }
}
}
}

mapOf(
MockStrategyApi::class to MockStrategyApi.values(),
CodegenLanguage::class to CodegenLanguage.values(),
RuntimeExceptionTestsBehaviour::class to RuntimeExceptionTestsBehaviour.values(),
TreatOverflowAsError::class to TreatOverflowAsError.values()
).forEach { (loader, values) ->
valuesComboBox(loader, values)
}
valuesComboBox(CodegenLanguage::class, CodegenLanguage.values())

row("Hanging test timeout:") {
cell {
Expand All @@ -76,26 +63,62 @@ class SettingsWindow(val project: Project) {
settings.hangingTestsTimeout.timeoutMs
.coerceIn(HangingTestsTimeout.MIN_TIMEOUT_MS, HangingTestsTimeout.MAX_TIMEOUT_MS).toInt()
},
setter = { settings.hangingTestsTimeout = HangingTestsTimeout(it.toLong()) },
setter = {
settings.hangingTestsTimeout = HangingTestsTimeout(it.toLong())
},
minValue = HangingTestsTimeout.MIN_TIMEOUT_MS.toInt(),
maxValue = HangingTestsTimeout.MAX_TIMEOUT_MS.toInt(),
step = 50,
)
comment("milliseconds")

label("milliseconds")
.apply {
ContextHelpLabel.create(
"Test generation may hang due to infinite loops or other code conditions. " +
"Set timeout to stop waiting for hanging process."
)()
}
}
}

mapOf(
ForceStaticMocking::class to ForceStaticMocking.values(),
RuntimeExceptionTestsBehaviour::class to RuntimeExceptionTestsBehaviour.values(),
TreatOverflowAsError::class to TreatOverflowAsError.values()
).forEach { (loader, values) ->
valuesComboBox(loader, values)
}



row {
excludeTable.component(CCFlags.grow)
cell {
forceMockCheckBox = checkBox("Force mocking static methods")
.onApply {
settings.state.forceStaticMocking =
if (forceMockCheckBox.isSelected) ForceStaticMocking.FORCE else ForceStaticMocking.DO_NOT_FORCE
}
.onReset { forceMockCheckBox.isSelected = settings.forceStaticMocking == ForceStaticMocking.FORCE }
.onIsModified { forceMockCheckBox.isSelected xor (settings.forceStaticMocking != ForceStaticMocking.DO_NOT_FORCE) }
.apply { ContextHelpLabel.create("Overrides other mocking settings")() }
.component
}
}

row("Classes to be forcedly mocked:") {}
row {
val excludeTableCellBuilder = excludeTable.component(CCFlags.grow)
val updater = Runnable {
UIUtil.setEnabled(excludeTableCellBuilder.component, forceMockCheckBox.isSelected, true)
}
excludeTableCellBuilder
.onApply { excludeTable.apply() }
.onReset { excludeTable.reset() }
.onReset {
excludeTable.reset()
updater.run()
}
.onIsModified { excludeTable.isModified() }
forceMockCheckBox.addActionListener { updater.run() }


}

row("Code analysis:") {
Expand Down Expand Up @@ -128,4 +151,4 @@ class SettingsWindow(val project: Project) {
excludeTable.reset()
(panel as DialogPanel).reset()
}
}
}
Loading