I'd like to add searching for partial string match...
# kotest-contributors
a
I'd like to add searching for partial string matches to
"Apple" shouldBe "Apples"
, but
shouldBe
for strings is so complicated already. I'd rather add a separate simple matcher so that users can opt in if they want. I mean the search similar to this one:
Copy code
"should find first mismatch before its expected place" {
            val message = shouldThrowAny {
               "The quick brown fox jumps over the lazy dog".shouldContainInOrder(
                  "The", "brown", "fox", "jumps", "over", "the", "quick brown", "lazy", "dog"
               )
            }.message
            assertSoftly {
               message.shouldContain("""Did not match substring[6]: <"quick brown">""")
               message.shouldContain("Match[0]: expected[0..10] matched actual[4..14]")
               message.shouldContain("""Line[0] ="The quick brown fox jumps over the lazy dog"""")
               message.shouldContain(  "Match[0]= ----+++++++++++----------------------------")
            }
WDYT?
s
It's hard to see exactly what's changing here. I would try to refactor shouldBe for strings so it's available to all.
👍🏼 1
gratitude thank you 1
a
I'll be invoking
describePartialMatchesInString(expected, value).toString()
````` and incorporating its output if a similar substring is found. If we make it available to all via
shouldBe
it will slow down some test a bit. I'll give it a try, TY
e
Diff algorithms are typically used for showing differences in texts, usually highlighting the minimum changes required to match properly. Is it possible to use something like that?
a
I did not manage to find a good one, can you suggest @Emil Kantis. The one kotest currently uses comes from github, and it was not helpful enough for me.
e
which one are we currently using? This one looks like a good first stab at getting something decent.. https://github.com/google/diff-match-patch/tree/master Would need to be converted to Kotlin, but should be easy to do
a
yes it's inspired by git diff I guess. so it won't match out-of-order substrings
unfortuantely
but it is very fast
s
Can we copy it into the codebase
Don't really want extra deps added
a
we can convert it to kotlin and add
but I am not super happy with what it does.
if we swap two parts of text, it won;t find both. it wil only find longer one
I wrote code which is slower, but finds both
as long as they are long enough
s
Speed doesn't matter that much in tests if we're talking 100ns vs 200ns
Obviously not seconds
a
So I've swapped two halves of lorem ipsum, and this library does the following:
it won't tell you last half is moved to start
e
how would it look in your proposed solution?
a
Copy code
Match[0]: expected[0..213] matched actual[234..447]
Match[1]: expected[215..448] matched actual[0..233]
Line[0] ="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
Match[0]= ----------------------------------------------------------------------------------------------------------------------------
Match[1]= ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Line[1] ="Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. "
Match[0]= ------------------------------------------------------------------------------------------------------------
Match[1]= ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Line[2] ="Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. "
Match[0]= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Match[1]= -------------------------------------------------------------------------------------------------------
Line[3] ="Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Match[0]= ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Match[1]= --------------------------------------------------------------------------------------------------------------"
the test:
Copy code
"lorem ipsum" {
            val text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \n" +
               "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. \n" +
               "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. \n" +
               "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
            val textWithSwappedHalves =                "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. \n" +
               "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n" +
               "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \n" +
               "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. \n"
            shouldThrowAny {
               text.shouldStartWith(textWithSwappedHalves)
            }.message shouldBe ""
         }
as you can see it finds both