Skip to content

Commit e944d8f

Browse files
committed
Main tests
1 parent 109b90e commit e944d8f

1 file changed

Lines changed: 253 additions & 0 deletions

File tree

core/src/test/java/com/facebook/ktfmt/cli/MainTest.kt

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,4 +561,257 @@ class MainTest {
561561
assertThat(returnValue).isEqualTo(1)
562562
assertThat(err.toString(testCharset)).contains("foo.kt:1:14: error: ")
563563
}
564+
565+
@Test
566+
fun `--lines does not format file lines before the selection`() {
567+
val code =
568+
"""
569+
|fun untouched ( ) = 1
570+
|
571+
|fun test() {
572+
| val selected = 2
573+
| val adjacent = 3
574+
|}
575+
|"""
576+
.trimMargin()
577+
val file = root.resolve("foo.kt")
578+
file.writeText(code, UTF_8)
579+
580+
val exitCode =
581+
Main(emptyInput, PrintStream(out), PrintStream(err), arrayOf("--lines=4", file.toString()))
582+
.run()
583+
584+
assertThat(exitCode).isEqualTo(0)
585+
assertThat(file.readText(UTF_8))
586+
.isEqualTo(
587+
"""
588+
|fun untouched ( ) = 1
589+
|
590+
|fun test() {
591+
| val selected = 2
592+
| val adjacent = 3
593+
|}
594+
|"""
595+
.trimMargin()
596+
)
597+
}
598+
599+
@Test
600+
fun `--lines does not format stdin lines before the selection`() {
601+
val code =
602+
"""
603+
|fun untouched ( ) = 1
604+
|
605+
|fun test() {
606+
| val selected = 2
607+
| val adjacent = 3
608+
|}
609+
|"""
610+
.trimMargin()
611+
612+
val exitCode =
613+
Main(code.byteInputStream(), PrintStream(out), PrintStream(err), arrayOf("--lines=4", "-"))
614+
.run()
615+
616+
assertThat(exitCode).isEqualTo(0)
617+
assertThat(out.toString(UTF_8))
618+
.isEqualTo(
619+
"""
620+
|fun untouched ( ) = 1
621+
|
622+
|fun test() {
623+
| val selected = 2
624+
| val adjacent = 3
625+
|}
626+
|"""
627+
.trimMargin()
628+
)
629+
}
630+
631+
@Test
632+
fun `--lines applies import cleanup after selected formatting`() {
633+
val code =
634+
"""
635+
|import com.unused.Sample
636+
|import com.used.FooBarBaz as Baz
637+
|import com.used.bar
638+
|
639+
|fun untouched ( ) = 1
640+
|
641+
|fun test() {
642+
| val selected = 2
643+
| Baz(bar)
644+
|}
645+
|"""
646+
.trimMargin()
647+
648+
val exitCode =
649+
Main(code.byteInputStream(), PrintStream(out), PrintStream(err), arrayOf("--lines=8", "-"))
650+
.run()
651+
652+
assertThat(exitCode).isEqualTo(0)
653+
assertThat(out.toString(UTF_8))
654+
.isEqualTo(
655+
"""
656+
|import com.used.FooBarBaz as Baz
657+
|import com.used.bar
658+
|
659+
|fun untouched ( ) = 1
660+
|
661+
|fun test() {
662+
| val selected = 2
663+
| Baz(bar)
664+
|}
665+
|"""
666+
.trimMargin()
667+
)
668+
}
669+
670+
@Test
671+
fun `--lines applies multiline string cleanup after selected formatting`() {
672+
val code =
673+
"""
674+
|val indent =
675+
| ""${'"'}
676+
| example
677+
| of
678+
| a
679+
|
680+
| multiline
681+
| string
682+
| ""${'"'}
683+
| .trimIndent()
684+
|
685+
|fun untouched ( ) = 1
686+
|
687+
|fun test() {
688+
| val selected = 2
689+
|}
690+
|"""
691+
.trimMargin()
692+
693+
val exitCode =
694+
Main(code.byteInputStream(), PrintStream(out), PrintStream(err), arrayOf("--lines=15", "-"))
695+
.run()
696+
697+
assertThat(exitCode).isEqualTo(0)
698+
assertThat(out.toString(UTF_8))
699+
.isEqualTo(
700+
"""
701+
|val indent =
702+
| ""${'"'}
703+
| example
704+
| of
705+
| a
706+
|
707+
| multiline
708+
| string
709+
| ""${'"'}
710+
| .trimIndent()
711+
|
712+
|fun untouched ( ) = 1
713+
|
714+
|fun test() {
715+
| val selected = 2
716+
|}
717+
|"""
718+
.trimMargin()
719+
)
720+
}
721+
722+
@Test
723+
fun `--offset and --length format the selected file cursor line`() {
724+
val code =
725+
"""
726+
|fun untouched ( ) = 1
727+
|
728+
|fun test() {
729+
| val selected = 2
730+
| val adjacent = 3
731+
|}
732+
|"""
733+
.trimMargin()
734+
val file = root.resolve("foo.kt")
735+
file.writeText(code, UTF_8)
736+
737+
val exitCode =
738+
Main(
739+
emptyInput,
740+
PrintStream(out),
741+
PrintStream(err),
742+
arrayOf(
743+
"--offset=${code.indexOf("selected")}",
744+
"--length=0",
745+
file.toString(),
746+
),
747+
)
748+
.run()
749+
750+
assertThat(exitCode).isEqualTo(0)
751+
assertThat(file.readText(UTF_8))
752+
.isEqualTo(
753+
"""
754+
|fun untouched ( ) = 1
755+
|
756+
|fun test() {
757+
| val selected = 2
758+
| val adjacent = 3
759+
|}
760+
|"""
761+
.trimMargin()
762+
)
763+
}
764+
765+
@Test
766+
fun `--offset and --length format the selected stdin cursor line`() {
767+
val code =
768+
"""
769+
|fun untouched ( ) = 1
770+
|
771+
|fun test() {
772+
| val selected = 2
773+
| val adjacent = 3
774+
|}
775+
|"""
776+
.trimMargin()
777+
778+
val exitCode =
779+
Main(
780+
code.byteInputStream(),
781+
PrintStream(out),
782+
PrintStream(err),
783+
arrayOf("--offset=${code.indexOf("selected")}", "--length=0", "-"),
784+
)
785+
.run()
786+
787+
assertThat(exitCode).isEqualTo(0)
788+
assertThat(out.toString(UTF_8))
789+
.isEqualTo(
790+
"""
791+
|fun untouched ( ) = 1
792+
|
793+
|fun test() {
794+
| val selected = 2
795+
| val adjacent = 3
796+
|}
797+
|"""
798+
.trimMargin()
799+
)
800+
}
801+
802+
@Test
803+
fun `--lines rejects directories that expand to multiple files`() {
804+
val dir = root.resolve("dir")
805+
dir.mkdirs()
806+
dir.resolve("foo.kt").writeText("fun foo () = 1", UTF_8)
807+
dir.resolve("bar.kt").writeText("fun bar () = 1", UTF_8)
808+
809+
val exitCode =
810+
Main(emptyInput, PrintStream(out), PrintStream(err), arrayOf("--lines=1", dir.toString()))
811+
.run()
812+
813+
assertThat(exitCode).isEqualTo(1)
814+
assertThat(err.toString(testCharset))
815+
.contains("partial formatting is only supported for a single file")
816+
}
564817
}

0 commit comments

Comments
 (0)