GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Other class is not mocked as required #747 by Markoutte · Pull Request #1033 · UnitTestBot/UTBotJava

Conversation

@Markoutte

@Markoutte Markoutte commented Sep 29, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

Description

Adds mocks for values that are passed into a method by fuzzer. Note, that fuzzer cannot mock any internal values if they are met in the code block.

Fixes #747

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Automated Testing

org.utbot.framework.plugin.api.MockOfObjectModelProviderTest

Manual Scenario

Reproduce examples from the issue.

Checklist:

  • The change followed the style guidelines of the UTBot project
  • Self-review of the code is passed
  • The change contains enough commentaries, particularly in hard-to-understand areas
  • New documentation is provided or existed one is altered
  • No new warnings
  • New tests have been added
  • All tests pass locally with my changes

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Markoutte
Markoutte requested a review from CaelmBleidd October 4, 2022 10:06
@Markoutte
Markoutte force-pushed the pelevin/747_Other_class_is_not_mocked_as_required branch from 7a7eeb8 to 35408f2 Compare October 4, 2022 10:07
@Markoutte
Markoutte marked this pull request as ready for review October 4, 2022 10:07
CaelmBleidd
CaelmBleidd approved these changes Oct 5, 2022
View reviewed changes

@CaelmBleidd CaelmBleidd left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/objects/FuzzerMockableMethodId.kt Outdated

}

internal fun MethodId.toFuzzerMockable(block: suspend SequenceScope<Pair<MethodId, List<UtModel>>>.() -> Unit): FuzzerMockableMethodId {

@CaelmBleidd CaelmBleidd Oct 5, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make a data class for these pairs?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Markoutte Markoutte Oct 11, 2022

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Pair" is used as convenient and well-known construction like "key to value". Because this method is used by dsl I'd prefer to keep it this way.

Also, I have some doubt about using data classes for such small pairs. Maybe, typealias is more proper way in these cases?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/providers/ObjectModelProvider.kt Outdated
constructorId.classId,
g.name,
g.returnType.id,
emptyList()

@CaelmBleidd CaelmBleidd Oct 5, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, use a named argument here

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/providers/ObjectModelProvider.kt Outdated
Comment on lines +164 to +165
setterAndGetter?.first,
setterAndGetter?.second,

@CaelmBleidd CaelmBleidd Oct 5, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here (a data class). From a browser, it's not obvious which one is what.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/Fuzzer.kt
values[index].add(model)
val mock = replaceToMock(model.model, description.shouldMock)
values[index].add(FuzzedValue(mock, model.createdBy).apply {
summary = model.summary

@CaelmBleidd CaelmBleidd Oct 5, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it not a part of the constructor?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/objects/AssembleModelUtils.kt Outdated
Comment on lines +31 to +59
fun replaceToMock(assembleModel: UtModel, shouldMock: (ClassId) -> Boolean): UtModel {
if (assembleModel !is UtAssembleModel) return assembleModel
if (shouldMock(assembleModel.classId)) {
return UtCompositeModel(assembleModel.id, assembleModel.classId, true).apply {
assembleModel.modificationsChain.forEach {
if (it is UtDirectSetFieldModel) {
fields[it.fieldId] = replaceToMock(it.fieldModel, shouldMock)
}
if (it is UtExecutableCallModel && it.executable is FuzzerMockableMethodId) {
(it.executable as FuzzerMockableMethodId).mock().forEach { (executionId, models) ->
mocks[executionId] = models.map { p -> replaceToMock(p, shouldMock) }
}
}
}
}
} else {
val models = assembleModel.modificationsChain.map { call ->
var mockedStatementModel: UtStatementModel? = null
if (call is UtDirectSetFieldModel) {
val mock = replaceToMock(call.fieldModel, shouldMock)
if (mock != call.fieldModel) {
mockedStatementModel = UtDirectSetFieldModel(call.instance, call.fieldId, mock)
}
} else if (call is UtExecutableCallModel) {
val params = call.params.map { m -> replaceToMock(m, shouldMock) }
if (params != call.params) {
mockedStatementModel = UtExecutableCallModel(call.instance, call.executable, params)
}
}

@CaelmBleidd CaelmBleidd Oct 5, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for me, it is hard to read because of missing spaces. My suggestion is something like:

fun replaceToMock(
    assembleModel: UtModel,
    shouldMock: (ClassId) -> Boolean
): UtModel = with(assembleModel) {
    if (this !is UtAssembleModel) return this

    if (shouldMock(classId)) {
        UtCompositeModel(id, classId, isMock = true).apply {
            modificationsChain.forEach {
                if (it is UtDirectSetFieldModel) {
                    fields[it.fieldId] = replaceToMock(it.fieldModel, shouldMock)
                }

                if (it is UtExecutableCallModel && it.executable is FuzzerMockableMethodId) {
                    (it.executable as FuzzerMockableMethodId).mock().forEach { (executionId, models) ->
                        mocks[executionId] = models.map { p -> replaceToMock(p, shouldMock) }
                    }
                }
            }
        }
    } else {
        val models = modificationsChain.map { call ->
            var mockedStatementModel: UtStatementModel? = null

            if (call is UtDirectSetFieldModel) {
                val mock = replaceToMock(call.fieldModel, shouldMock)

                if (mock != call.fieldModel) {
                    mockedStatementModel = UtDirectSetFieldModel(call.instance, call.fieldId, mock)
                }
            } else if (call is UtExecutableCallModel) {
                val params = call.params.map { m -> replaceToMock(m, shouldMock) }

                if (params != call.params) {
                    mockedStatementModel = UtExecutableCallModel(call.instance, call.executable, params)
                }
            }

            mockedStatementModel ?: call
        }

        with(assembleModel) {
            UtAssembleModel(id, classId, modelName, instantiationCall, origin) { models }
        }
    }
}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Markoutte added 3 commits October 11, 2022 13:21
@Markoutte
Other class is not mocked as required #747
14f4b94
@Markoutte
Fix tests
0299e13
@Markoutte
Fixes after review
23dc52a
@Markoutte
Markoutte force-pushed the pelevin/747_Other_class_is_not_mocked_as_required branch from a899cec to 23dc52a Compare October 11, 2022 12:35
@Markoutte
Markoutte merged commit 1ba00d4 into main Oct 11, 2022
@Markoutte
Markoutte deleted the pelevin/747_Other_class_is_not_mocked_as_required branch October 11, 2022 13:40
AbdullinAM pushed a commit to AbdullinAM/UTBotJava that referenced this pull request Oct 17, 2022
@Markoutte @AbdullinAM
Other class is not mocked as required UnitTestBot#747 (UnitTestBot#1033)
341a917
AbdullinAM pushed a commit to AbdullinAM/UTBotJava that referenced this pull request Oct 17, 2022
@Markoutte @AbdullinAM
Other class is not mocked as required UnitTestBot#747 (UnitTestBot#1033)
1a18c53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@CaelmBleidd CaelmBleidd CaelmBleidd approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Other class is not mocked as required

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@Markoutte @CaelmBleidd
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.