-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
transpiler/javatests/com/google/j2cl/transpiler/backend/kotlin/source/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
load("//testing/build_defs:junit_test_suites.bzl", "junit_test_suites") | ||
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library") | ||
|
||
package( | ||
default_applicable_licenses = ["//:j2cl_license"], | ||
licenses = ["notice"], | ||
) | ||
|
||
kt_jvm_library( | ||
name = "lib", | ||
testonly = 1, | ||
srcs = [ | ||
"SourceTest.kt", | ||
], | ||
deps = [ | ||
"//third_party/java/junit", | ||
"//third_party/java/truth", | ||
"//transpiler/java/com/google/j2cl/transpiler/backend/kotlin/source", | ||
], | ||
) | ||
|
||
junit_test_suites( | ||
name = "junit", | ||
sizes = ["small"], | ||
deps = [":lib"], | ||
) |
163 changes: 163 additions & 0 deletions
163
transpiler/javatests/com/google/j2cl/transpiler/backend/kotlin/source/SourceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* Copyright 2023 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.google.j2cl.transpiler.backend.kotlin.source | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
|
||
@RunWith(JUnit4::class) | ||
class SourceTest { | ||
private val sourceA: Source | ||
get() = source("a") | ||
|
||
private val sourceB: Source | ||
get() = source("b") | ||
|
||
private val sourceC: Source | ||
get() = source("c") | ||
|
||
private val emptySources: Iterable<Source> | ||
get() = listOf() | ||
|
||
private val sourcesA: Iterable<Source> | ||
get() = listOf(sourceA) | ||
|
||
private val sourcesAB: Iterable<Source> | ||
get() = listOf(sourceA, sourceB) | ||
|
||
private val sourcesABC: Iterable<Source> | ||
get() = listOf(sourceA, sourceB, sourceC) | ||
|
||
private val sourcesABCAndEmpty: Iterable<Source> | ||
get() = listOf(sourceA, emptySource, sourceB, emptySource, sourceC) | ||
|
||
private infix fun Source.gives(string: String) { | ||
assertThat(toString()).isEqualTo(string) | ||
} | ||
|
||
@Test | ||
fun emptySource() { | ||
emptySource gives "" | ||
} | ||
|
||
@Test | ||
fun stringSource() { | ||
source("foo") gives "foo" | ||
} | ||
|
||
@Test | ||
fun sourcePlusSource() { | ||
sourceA + sourceB gives "ab" | ||
} | ||
|
||
@Test | ||
fun joinSources() { | ||
join(sourceA) gives "a" | ||
join(sourceA, sourceB) gives "ab" | ||
join(sourceA, emptySource, sourceB) gives "ab" | ||
} | ||
|
||
@Test | ||
fun stringSeparatedSources() { | ||
", " separated emptySources gives "" | ||
", " separated sourcesA gives "a" | ||
", " separated sourcesAB gives "a, b" | ||
", " separated sourcesABC gives "a, b, c" | ||
", " separated sourcesABCAndEmpty gives "a, b, c" | ||
} | ||
|
||
@Test | ||
fun separatedSources_vararg() { | ||
spaceSeparated(sourceA, emptySource, sourceB, emptySource, sourceC) gives "a b c" | ||
commaSeparated(sourceA, emptySource, sourceB, emptySource, sourceC) gives "a, b, c" | ||
dotSeparated(sourceA, emptySource, sourceB, emptySource, sourceC) gives "a.b.c" | ||
colonSeparated(sourceA, emptySource, sourceB, emptySource, sourceC) gives "a: b: c" | ||
newLineSeparated(sourceA, emptySource, sourceB, emptySource, sourceC) gives "a\nb\nc" | ||
emptyLineSeparated(sourceA, emptySource, sourceB, emptySource, sourceC) gives "a\n\nb\n\nc" | ||
} | ||
|
||
@Test | ||
fun separatedSources_iterable() { | ||
spaceSeparated(sourcesABCAndEmpty) gives "a b c" | ||
commaSeparated(sourcesABCAndEmpty) gives "a, b, c" | ||
commaAndNewLineSeparated(sourcesABCAndEmpty) gives "a,\nb,\nc" | ||
dotSeparated(sourcesABCAndEmpty) gives "a.b.c" | ||
ampersandSeparated(sourcesABCAndEmpty) gives "a & b & c" | ||
colonSeparated(sourcesABCAndEmpty) gives "a: b: c" | ||
semicolonSeparated(sourcesABCAndEmpty) gives "a; b; c" | ||
newLineSeparated(sourcesABCAndEmpty) gives "a\nb\nc" | ||
emptyLineSeparated(sourcesABCAndEmpty) gives "a\n\nb\n\nc" | ||
} | ||
|
||
@Test | ||
fun sourceInCurlyBrackets() { | ||
inCurlyBrackets(source("")) gives "{}" | ||
inCurlyBrackets(source("\n")) gives "{\n \n}" | ||
inCurlyBrackets(source("\nfoo")) gives "{\n foo\n}" | ||
inCurlyBrackets(source("\nfoo\nbar")) gives "{\n foo\n bar\n}" | ||
inCurlyBrackets(source("foo\nbar")) gives "{foo\n bar\n}" | ||
} | ||
|
||
@Test | ||
fun bracketedSources() { | ||
inParentheses(sourceA) gives "(a)" | ||
inAngleBrackets(sourceA) gives "<a>" | ||
inSquareBrackets(sourceA) gives "[a]" | ||
inDoubleQuotes(sourceA) gives "\"a\"" | ||
inInlineCurlyBrackets(sourceA) gives "{ a }" | ||
|
||
inParenthesesIfNotEmpty(emptySource) gives "" | ||
inParenthesesIfNotEmpty(sourceA) gives "(a)" | ||
} | ||
|
||
@Test | ||
fun blockSource() { | ||
block(emptySource) gives "{}" | ||
block(sourceA) gives "{\n a\n}" | ||
block(newLineSeparated(sourceA, sourceB)) gives "{\n a\n b\n}" | ||
} | ||
|
||
@Test | ||
fun infixSource() { | ||
infix(sourceA, "op", sourceB) gives "a op b" | ||
} | ||
|
||
@Test | ||
fun sourceIf() { | ||
sourceIf(true) { sourceA } gives "a" | ||
sourceIf(false) { sourceA } gives "" | ||
} | ||
|
||
@Test | ||
fun sourceIfEmpty() { | ||
sourceA.ifEmpty { sourceB } gives "a" | ||
emptySource.ifEmpty { sourceB } gives "b" | ||
} | ||
|
||
@Test | ||
fun sourceIfNotEmpty() { | ||
sourceA.ifNotEmpty(::inParentheses) gives "(a)" | ||
emptySource.ifNotEmpty(::inParentheses) gives "" | ||
} | ||
|
||
@Test | ||
fun ifNotNullSource() { | ||
128.ifNotNullSource { source(it.inc().toString()) } gives "129" | ||
(null as Int?).ifNotNullSource { source(it.inc().toString()) } gives "" | ||
} | ||
} |