How to use File.safeList method of io.kotest.matchers.file.matchers class

Best Kotest code snippet using io.kotest.matchers.file.matchers.File.safeList

matchers.kt

Source:matchers.kt Github

copy

Full Screen

1package io.kotest.matchers.file2import io.kotest.matchers.Matcher3import io.kotest.matchers.MatcherResult4import io.kotest.matchers.collections.shouldBeSameSizeAs5import io.kotest.matchers.paths.beSymbolicLink6import io.kotest.matchers.should7import io.kotest.matchers.shouldBe8import io.kotest.matchers.shouldNot9import io.kotest.matchers.shouldNotBe10import java.io.File11import java.io.FileFilter12import java.nio.file.Path13private fun File.safeList(): List<String> = this.list()?.toList() ?: emptyList()14private fun File.safeListFiles(): List<File> = this.listFiles()?.toList() ?: emptyList()15private fun File.safeListFiles(filter: FileFilter): List<File> = this.listFiles(filter)?.toList() ?: emptyList()16fun File.shouldBeEmptyDirectory() = this should beEmptyDirectory()17fun File.shouldNotBeEmptyDirectory() = this shouldNot beEmptyDirectory()18fun beEmptyDirectory(): Matcher<File> = object : Matcher<File> {19 override fun test(value: File): MatcherResult {20 val contents = if (value.isDirectory) value.safeList() else emptyList()21 return MatcherResult(22 contents.isEmpty(),23 { "$value should be an empty directory but contained ${contents.size} file(s) [${contents.joinToString(", ")}]" },24 { "$value should not be a non empty directory" }25 )26 }27}28infix fun File.shouldContainNFiles(n: Int) = this shouldBe containNFiles(n)29infix fun File.shouldNotContainNFiles(n: Int) = this shouldNotBe containNFiles(n)30fun containNFiles(n: Int): Matcher<File> = object : Matcher<File> {31 override fun test(value: File): MatcherResult = MatcherResult(32 value.isDirectory && value.safeList().size == n,33 { "$value should be a directory and contain $n files" },34 { "$value should not be a directory containing $n files" }35 )36}37fun File.shouldBeEmpty() = this shouldBe emptyFile()38fun File.shouldNotBeEmpty() = this shouldNotBe emptyFile()39fun emptyFile(): Matcher<File> = object : Matcher<File> {40 override fun test(value: File): MatcherResult =41 MatcherResult(42 value.length() == 0L,43 { "File $value should be empty" },44 { "File $value should not be empty" }45 )46}47fun File.shouldExist() = this should exist()48fun File.shouldNotExist() = this shouldNot exist()49fun exist() = object : Matcher<File> {50 override fun test(value: File) =51 MatcherResult(52 value.exists(),53 { "File $value should exist" },54 { "File $value should not exist" }55 )56}57infix fun File.shouldContainFile(name: String) = this should containFile(name)58infix fun File.shouldNotContainFile(name: String) = this shouldNot containFile(name)59fun containFile(name: String) = object : Matcher<File> {60 override fun test(value: File): MatcherResult {61 val contents = value.safeList()62 val passed = value.isDirectory && contents.contains(name)63 return MatcherResult(64 passed,65 { "Directory $value should contain a file with filename $name (detected ${contents.size} other files)" },66 { "Directory $value should not contain a file with filename $name" }67 )68 }69}70fun File.shouldBeSymbolicLink() = this.toPath() should beSymbolicLink()71fun File.shouldNotBeSymbolicLink() = this.toPath() shouldNot beSymbolicLink()72infix fun File.shouldHaveParent(name: String) = this should haveParent(name)73infix fun File.shouldNotHaveParent(name: String) = this shouldNot haveParent(name)74fun haveParent(name: String) = object : Matcher<File> {75 private fun isParentEqualExpected(parent: File?): Boolean =76 parent != null && (parent.name == name || isParentEqualExpected(parent.parentFile))77 override fun test(value: File) = MatcherResult(78 isParentEqualExpected(value.parentFile),79 { "File $value should have parent $name" },80 { "File $value should not have parent $name" }81 )82}83fun File.shouldBeADirectory() = this should aDirectory()84fun File.shouldNotBeADirectory() = this shouldNot aDirectory()85fun aDirectory(): Matcher<File> = object : Matcher<File> {86 override fun test(value: File): MatcherResult = MatcherResult(87 value.isDirectory,88 { "File $value should be a directory" },89 { "File $value should not be a directory" }90 )91}92fun File.shouldBeAFile() = this should aFile()93fun File.shouldNotBeAFile() = this shouldNot aFile()94fun aFile(): Matcher<File> = object : Matcher<File> {95 override fun test(value: File): MatcherResult =96 MatcherResult(97 value.isFile,98 { "File $value should be a file" },99 { "File $value should not be a file" })100}101infix fun File.shouldBeSmaller(other: Path) = this should beSmaller(other.toFile())102infix fun File.shouldBeSmaller(other: File) = this should beSmaller(other)103infix fun File.shouldNotBeSmaller(other: Path) = this shouldNot beSmaller(other.toFile())104infix fun File.shouldNotBeSmaller(other: File) = this shouldNot beSmaller(other)105fun beSmaller(other: File): Matcher<File> = object : Matcher<File> {106 override fun test(value: File): MatcherResult {107 val sizea = value.length()108 val sizeb = other.length()109 return MatcherResult(110 value.length() < other.length(),111 { "File $value ($sizea bytes) should be smaller than $other ($sizeb bytes)" },112 { "File $value ($sizea bytes) should not be smaller than $other ($sizeb bytes)" }113 )114 }115}116infix fun File.shouldBeLarger(other: Path) = this should beLarger(other.toFile())117infix fun File.shouldBeLarger(other: File) = this should beLarger(other)118infix fun File.shouldNotBeLarger(other: Path) = this shouldNot beLarger(other.toFile())119infix fun File.shouldNotBeLarger(other: File) = this shouldNot beLarger(other)120fun beLarger(other: File): Matcher<File> = object : Matcher<File> {121 override fun test(value: File): MatcherResult {122 val sizea = value.length()123 val sizeb = other.length()124 return MatcherResult(125 value.length() > other.length(),126 { "File $value ($sizea bytes) should be larger than $other ($sizeb bytes)" },127 { "File $value ($sizea bytes) should not be larger than $other ($sizeb bytes)" }128 )129 }130}131fun File.shouldBeCanonical() = this should beCanonicalPath()132fun File.shouldNotBeCanonical() = this shouldNot beCanonicalPath()133fun beCanonicalPath(): Matcher<File> = object : Matcher<File> {134 override fun test(value: File): MatcherResult = MatcherResult(135 value.canonicalPath == value.path,136 { "File $value should be canonical" },137 { "File $value should not be canonical" }138 )139}140fun File.shouldBeAbsolute() = this should beAbsolute()141fun File.shouldNotBeAbsolute() = this shouldNot beAbsolute()142fun beAbsolute(): Matcher<File> = object : Matcher<File> {143 override fun test(value: File): MatcherResult =144 MatcherResult(145 value.isAbsolute,146 { "File $value should be absolute" },147 { "File $value should not be absolute" })148}149fun File.shouldBeRelative() = this should beRelative()150fun File.shouldNotBeRelative() = this shouldNot beRelative()151fun beRelative(): Matcher<File> = object : Matcher<File> {152 override fun test(value: File): MatcherResult =153 MatcherResult(154 !value.isAbsolute,155 { "File $value should be relative" },156 { "File $value should not be relative" })157}158infix fun File.shouldHaveFileSize(size: Long) = this should haveFileSize(size)159infix fun File.shouldNotHaveFileSize(size: Long) = this shouldNot haveFileSize(size)160fun haveFileSize(size: Long): Matcher<File> = object : Matcher<File> {161 override fun test(value: File): MatcherResult = MatcherResult(162 value.length() == size,163 { "File $value should have size $size" },164 { "File $value should not have size $size" }165 )166}167fun File.shouldBeWriteable() = this should beWriteable()168fun File.shouldNotBeWriteable() = this shouldNot beWriteable()169fun beWriteable(): Matcher<File> = object : Matcher<File> {170 override fun test(value: File): MatcherResult =171 MatcherResult(172 value.canWrite(),173 { "File $value should be writeable" },174 { "File $value should not be writeable" })175}176fun File.shouldBeExecutable() = this should beExecutable()177fun File.shouldNotBeExecutable() = this shouldNot beExecutable()178fun beExecutable(): Matcher<File> = object : Matcher<File> {179 override fun test(value: File): MatcherResult = MatcherResult(180 value.canExecute(),181 { "File $value should be executable" },182 { "File $value should not be executable" }183 )184}185fun File.shouldBeHidden() = this should beHidden()186fun File.shouldNotBeHidden() = this shouldNot beHidden()187fun beHidden(): Matcher<File> = object : Matcher<File> {188 override fun test(value: File): MatcherResult =189 MatcherResult(190 value.isHidden,191 { "File $value should be hidden" },192 { "File $value should not be hidden" })193}194fun File.shouldBeReadable() = this should beReadable()195fun File.shouldNotBeReadable() = this shouldNot beReadable()196fun beReadable(): Matcher<File> = object : Matcher<File> {197 override fun test(value: File): MatcherResult =198 MatcherResult(199 value.canRead(),200 { "File $value should be readable" },201 { "File $value should not be readable" })202}203infix fun File.shouldStartWithPath(path: Path) = this should startWithPath(path)204infix fun File.shouldNotStartWithPath(path: Path) = this shouldNot startWithPath(path)205infix fun File.shouldStartWithPath(prefix: String) = this should startWithPath(prefix)206infix fun File.shouldNotStartWithPath(prefix: String) = this shouldNot startWithPath(prefix)207infix fun File.shouldStartWithPath(file: File) = this should startWithPath(file)208infix fun File.shouldNotStartWithPath(file: File) = this shouldNot startWithPath(file)209infix fun Path.shouldStartWithPath(path: Path) = this.toFile() should startWithPath(path)210infix fun Path.shouldNotStartWithPath(path: Path) = this.toFile() shouldNot startWithPath(path)211fun startWithPath(path: Path) = startWithPath(path.toFile())212fun startWithPath(file: File) = startWithPath(file.toString())213fun startWithPath(prefix: String) = object : Matcher<File> {214 override fun test(value: File): MatcherResult = MatcherResult(215 value.toString().startsWith(prefix),216 { "File $value should start with $prefix" },217 { "File $value should not start with $prefix" }218 )219}220infix fun File.shouldHaveSameStructureAs(file: File) {221 this.shouldHaveSameStructureAs(file) { _, _ -> false }222}223fun File.shouldHaveSameStructureAs(224 file: File,225 compare: (expect: File, actual: File) -> Boolean,226) {227 val expectFiles = this.walkTopDown().toList()228 val actualFiles = file.walkTopDown().toList()229 val expectParentPath = this.path230 val actualParentPath = file.path231 expectFiles shouldBeSameSizeAs actualFiles232 expectFiles.zip(actualFiles) { expect, actual ->233 when {234 compare(expect, actual) -> {}235 expect.isDirectory -> actual.shouldBeADirectory()236 expect.isFile -> {237 expect.path.removePrefix(expectParentPath)238 .shouldBe(actual.path.removePrefix(actualParentPath))239 }240 else -> error("There is an unexpected error analyzing file trees")241 }242 }243}244fun File.shouldHaveSameStructureAs(245 file: File,246 filterLhs: (File) -> Boolean = { false },247 filterRhs: (File) -> Boolean = { false },248) {249 this.shouldHaveSameStructureAs(file) { expect, actual ->250 filterLhs(expect) || filterRhs(actual)251 }252}253infix fun File.shouldHaveSameStructureAndContentAs(file: File) {254 this.shouldHaveSameStructureAndContentAs(file) { _, _ -> false }255}256fun File.shouldHaveSameStructureAndContentAs(257 file: File,258 compare: (expect: File, actual: File) -> Boolean,259) {260 val expectFiles = this.walkTopDown().toList()261 val actualFiles = file.walkTopDown().toList()262 val expectParentPath = this.path263 val actualParentPath = file.path264 expectFiles shouldBeSameSizeAs actualFiles265 expectFiles.zip(actualFiles) { expect, actual ->266 when {267 compare(expect, actual) -> {}268 expect.isDirectory -> actual.shouldBeADirectory()269 expect.isFile -> {270 expect.path.removePrefix(expectParentPath)271 .shouldBe(actual.path.removePrefix(actualParentPath))272 expect.shouldHaveSameContentAs(actual)273 }274 else -> error("There is an unexpected error analyzing file trees")275 }276 }277}278fun File.shouldHaveSameStructureAndContentAs(279 file: File,280 filterLhs: (File) -> Boolean = { false },281 filterRhs: (File) -> Boolean = { false },282) {283 this.shouldHaveSameStructureAndContentAs(file) { expect, actual ->284 filterLhs(expect) || filterRhs(actual)285 }286}...

Full Screen

Full Screen

WordServiceTest.kt

Source:WordServiceTest.kt Github

copy

Full Screen

1package com.hakob.flashcards2import com.hakob.flashcards.service.MainService3import com.hakob.flashcards.service.WordService4import com.hakob.flashcards.service.movePointerToNextIfSentenceIsNotOver5import com.hakob.flashcards.service.movePointerToPreviousIfSentenceIsNotOver6import com.hakob.flashcards.utils.FileUtils7import com.hakob.flashcards.utils.TranslateUtils8import io.kotest.data.blocking.forAll9import io.kotest.data.row10import io.kotest.matchers.shouldBe11import io.mockk.mockk12import org.jsoup.Jsoup13import org.junit.jupiter.api.Test14class WordServiceTest15{16 val text: String = "version control logs, should. show that test code is checked. in each time product code"17 val wordService: WordService = WordService()18 @Test19 fun `should return correct target sentence for wordId`() = forAll(20 row(0, "version control logs, should."),21 row(2, "version control logs, should."),22 row(3, "version control logs, should."),23 row(4, "show that test code is checked."),24 row(5, "show that test code is checked."),25 row(9, "show that test code is checked."),26 row(14, "in each time product code"),27 ) {28 wordId, expected ->29 // given30 wordService.listOfWords = text.split(" ")31 // when32 val result = wordService.getTargetSentence(wordId)33 // then34 result shouldBe expected35 }36 @Test37 fun `should return true if the word followed by terminator sign is reached`() = forAll(38 row(0, false),39 row(1, true),40 ) {41 wordId, expected ->42 // given43 val list = listOf<String>("This", "is", "test")44 val listIterator = list.listIterator(wordId)45 // when46 val result = listIterator.movePointerToPreviousIfSentenceIsNotOver<String>()47 // then48 result shouldBe expected49 }50 @Test51 fun `should return true if the word by terminator sign is reached`() = forAll(52 row(1, "This is test".split(" "), true),53 row(2, "This is test.".split(" "), false),54 row(3, "This is test".split(" "), false),55 row(2, "This is test!".split(" "), false),56 ) {57 wordId, list, expected ->58 // given59 val listIterator = list.listIterator(wordId)60 // when61 val result = listIterator.movePointerToNextIfSentenceIsNotOver<String>()62 // then63 result shouldBe expected64 }65 @Test66 fun `should return target paragraph`() {67 // given68 wordService.document = Jsoup.parse(69 javaClass.getResource("/com/hakob/flashcards/resultTestHtmlFile2.html").readText()70 )71 val text = "You want to allow untrusted users to supply HTML for output on your website (e.g. as comment submission). You need to clean this HTML to avoid cross-site scripting (XSS) attacks. Use the jsoup HTML Cleaner with a configuration specified by a Safelist. Link Link A cross-site scripting attack against your site can really ruin your day, not to mention your users'. Many sites avoid XSS attacks by not allowing HTML in user submitted content: they enforce plain text only, or use an alternative markup syntax like wiki-text or Markdown. These are seldom optimal solutions for the user, as they lower expressiveness, and force the user to learn a new syntax. A better solution may be to use a rich text WYSIWYG editor (like CKEditor or TinyMCE). These output HTML, and allow the user to work visually. However, their validation is done on the client side: you need to apply a server-side validation to clean up the input and ensure the HTML is safe to place on your site. Otherwise, an attacker can avoid the client-side Javascript validation and inject unsafe HMTL directly into your site This is a context sentence which contains the key word and the word 'key' here should be translated It does not use regular expressions, which are inappropriate for this task. jsoup provides a range of Safelist configurations to suit most requirements; they can be modified if necessary, but take care. The cleaner is useful not only for avoiding XSS, but also in limiting the range of elements the user can provide: you may be OK with textual a, strong elements, but not structural div or table elements."72 wordService.listOfWords = text.split(" ")73// val expected =74 // when75 val result = wordService.getTargetParagraphOrFallbackToSentenceInParagraph(191)76 // then77// result shouldBe78 println(result)79 }80}...

Full Screen

Full Screen

File.safeList

Using AI Code Generation

copy

Full Screen

1File("src/test/resources").safeList().shouldHaveSize(2)2File("src/test/resources").shouldBeDirectory()3File("src/test/resources").shouldBeFile()4File("src/test/resources").shouldBeHidden()5File("src/test/resources").shouldBeReadable()6File("src/test/resources").shouldBeRelative()7File("src/test/resources").shouldBeSymbolicLink()8File("src/test/resources").shouldBeWritable()9File("src/test/resources").shouldContainDirectory("test")10File("src/test/resources").shouldContainFile("test.txt")11File("src/test/resources").shouldContainOnlyDirectories("test")12File("src/test/resources").shouldContainOnlyFiles("test.txt")13File("src/test/resources").shouldContainOnlyFilesAndDirectories("test", "test.txt")14File("src/test/resources").shouldEndWith("resources")15File("src/test/resources").shouldExist()16File("src/test/resources

Full Screen

Full Screen

File.safeList

Using AI Code Generation

copy

Full Screen

1File("src/test/resources").safeList().shouldContain(File("src/test/resources/test.txt"))2File("src/test/resources").shouldBeADirectory()3File("src/test/resources/test.txt").shouldBeAFile()4File("src/test/resources").shouldBeEmptyDirectory()5File("src/test/resources/test.txt").shouldBeEmptyFile()6File("src/test/resources/test.txt").shouldBeExecutable()7File("src/test/resources/test.txt").shouldBeReadable()8File("src/test/resources/test.txt").shouldBeRelative()9File("src/test/resources/test.txt").shouldBeSymlink()10File("src/test/resources/test.txt").shouldBeWritable()11File("src/test/resources/test.txt").shouldBeZeroBytes()12File("src/test/resources").shouldContainFile("test.txt")13File("src/test/resources").shouldContainFiles("test.txt", "test2.txt")14File("src/test/resources/test.txt").shouldExist()15File("src/test/resources/test.txt").shouldHaveExtension("txt")

Full Screen

Full Screen

File.safeList

Using AI Code Generation

copy

Full Screen

1File("myFile.txt").safeList() shouldBe List("line1", "line2")2File("myFile.txt").shouldBeEmpty()3File("myFile.txt").shouldBeDirectory()4File("myFile.txt").shouldBeFile()5File("myFile.txt").shouldBeHidden()6File("myFile.txt").shouldBeReadable()7File("myFile.txt").shouldBeRelative()8File("myFile.txt").shouldBeRoot()9File("myFile.txt").shouldBeSymbolicLink()10File("myFile.txt").shouldBeWritable()11File("myFile.txt").shouldHaveExtension("txt")12File("myFile.txt").shouldHaveName("myFile")13File("myFile.txt").shouldHaveParent("myFile.txt")14File("myFile.txt").shouldHaveParent(File("myFile.txt"))15File("myFile.txt").shouldHavePath("myFile.txt")16File("myFile.txt").shouldHave

Full Screen

Full Screen

File.safeList

Using AI Code Generation

copy

Full Screen

1import io.kotest.matchers.file.matchers.*2val files = File("src/test/resources").listFiles()3files.shouldNotBeEmpty()4files.shouldContain(File("src/test/resources/file1.txt"))5files.shouldNotContain(File("src/test/resources/file3.txt"))6files.shouldHaveSize(2)7files.shouldNotHaveSize(3)8files.shouldExist()9files.shouldNotBeEmpty()10files.shouldBeEmpty()11files.shouldBeReadable()12files.shouldNotBeReadable()13files.shouldBeWritable()14files.shouldNotBeWritable()15files.shouldBeExecutable()16files.shouldNotBeExecutable()17files.shouldBeHidden()18files.shouldNotBeHidden()19files.shouldBeDirectory()20files.shouldNotBeDirectory()21files.shouldBeFile()22files.shouldNotBeFile()23files.shouldBeAbsolute()24files.shouldNotBeAbsolute()25files.shouldHaveParent(File("src/test/resources"))26files.shouldHaveName("file1.txt")27files.shouldHaveExtension(".txt")28files.shouldHaveExtension("txt")29files.shouldHaveNameStartingWith("file")30files.shouldHaveNameEndingWith(".txt")31files.shouldHaveNameContaining("1")32files.shouldHaveNameMatching(Regex("file[0-9]+.txt"))33files.shouldHaveNameNotMatching(Regex("file[0-9]+.txt"))34files.shouldBeAbsolute()35files.shouldNotBeAbsolute()36files.shouldHaveAbsolutePath("/home/user/project/src/test/resources/file1.txt")37files.shouldHaveAbsolutePath("/home/user/project/src/test/resources/file2.txt")38import io.kotest.matchers.file.matchers.*39val files = File("src/test/resources").safeList()40files.shouldNotBeEmpty()41files.shouldContain(File("src/test/resources/file1.txt"))42files.shouldNotContain(File("src/test/resources/file3.txt"))43files.shouldHaveSize(2)44files.shouldNotHaveSize(3)45files.shouldExist()46files.shouldNotBeEmpty()47files.shouldBeEmpty()48files.shouldBeReadable()49files.shouldNotBeReadable()50files.shouldBeWritable()51files.shouldNotBeWritable()52files.shouldBeExecutable()53files.shouldNotBeExecutable()54files.shouldBeHidden()55files.shouldNotBeHidden()56files.shouldBeDirectory()57files.shouldNotBeDirectory()58files.shouldBeFile()59files.shouldNotBeFile()60files.shouldBeAbsolute()61files.shouldNotBeAbsolute()62files.shouldHaveParent(File("src/test/resources"))63files.shouldHaveName("file1.txt")

Full Screen

Full Screen

File.safeList

Using AI Code Generation

copy

Full Screen

1import io.kotest.matchers.file.matchers.*2val files = File("C:\\Users\\johndoe\\Desktop").safeList()3import io.kotest.matchers.file.matchers.*4File("C:\\Users\\johndoe\\Desktop\\test.txt").shouldBeFile()5import io.kotest.matchers.file.matchers.*6File("C:\\Users\\johndoe\\Desktop\\test").shouldBeDirectory()7import io.kotest.matchers.file.matchers.*8File("C:\\Users\\johndoe\\Desktop\\test").shouldContainFile("test.txt")9import io.kotest.matchers.file.matchers.*10File("C:\\Users\\johndoe\\Desktop\\test").shouldContainDirectory("test")11import io.kotest.matchers.file.matchers.*12File("C:\\Users\\johndoe\\Desktop\\test").shouldBeEmpty()13import io.kotest.matchers.file.matchers.*14File("C:\\Users\\johndoe\\Desktop\\test.txt").shouldBeHidden()15import io.kotest.matchers.file.matchers.*16File("C:\\Users\\johndoe\\Desktop\\test.txt").shouldBeReadable()17import io.kotest.matchers.file.matchers.*18File("C:\\Users\\johndoe\\Desktop\\test.txt").shouldBeWritable()19import io.kotest.matchers.file.matchers.*20File("C:\\Users\\johndoe\\Desktop\\test.txt").shouldBeHidden()

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful