Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NameAllocatorTests #96

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added n
Copy link
Collaborator

Choose a reason for hiding this comment

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

oops

Empty file.
105 changes: 105 additions & 0 deletions src/test/java/io/outfoxx/swiftpoet/test/NameAllocatorTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2018 Outfox, 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 io.outfoxx.swiftpoet.test

import io.outfoxx.swiftpoet.NameAllocator
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Test

class NameAllocatorTests {
@Test
fun usage() {
val nameAllocator = NameAllocator()
assertThat(nameAllocator.newName("foo", 1), equalTo("foo"))
assertThat(nameAllocator.newName("bar", 2), equalTo("bar"))
assertThat(nameAllocator[1], equalTo("foo"))
assertThat(nameAllocator[2], equalTo("bar"))
}

@Test fun nameCollision() {
val nameAllocator = NameAllocator()
assertThat(nameAllocator.newName("foo"), equalTo("foo"))
assertThat(nameAllocator.newName("foo"), equalTo("foo_"))
assertThat(nameAllocator.newName("foo"), equalTo("foo__"))
}

@Test fun nameCollisionWithTag() {
val nameAllocator = NameAllocator()
assertThat(nameAllocator.newName("foo", 1), equalTo("foo"))
assertThat(nameAllocator.newName("foo", 2), equalTo("foo_"))
assertThat(nameAllocator.newName("foo", 3), equalTo("foo__"))
assertThat(nameAllocator[1], equalTo("foo"))
assertThat(nameAllocator[2], equalTo("foo_"))
assertThat(nameAllocator[3], equalTo("foo__"))
}

@Test fun characterMappingSubstitute() {
val nameAllocator = NameAllocator()
assertThat(nameAllocator.newName("a-b", 1), equalTo("a_b"))
}

@Test fun characterMappingSurrogate() {
val nameAllocator = NameAllocator()
assertThat(nameAllocator.newName("a\uD83C\uDF7Ab", 1), equalTo("a_b"))
}

@Test fun characterMappingInvalidStartButValidPart() {
val nameAllocator = NameAllocator()
assertThat(nameAllocator.newName("1ab", 1), equalTo("_1ab"))
}

@Test fun characterMappingInvalidStartIsInvalidPart() {
val nameAllocator = NameAllocator()
assertThat(nameAllocator.newName("&ab", 1), equalTo("_ab"))
}

@Test fun swiftKeyword() {
val nameAllocator = NameAllocator()
assertThat(nameAllocator.newName("associatedtype", 1), equalTo("associatedtype_"))
assertThat(nameAllocator[1], equalTo("associatedtype_"))
}

@Test fun tagReuseForbidden() {
val nameAllocator = NameAllocator()
nameAllocator.newName("foo", 1)
assertThrows(IllegalArgumentException::class.java) {
nameAllocator.newName("bar", 1)
}
}

@Test fun useBeforeAllocateForbidden() {
val nameAllocator = NameAllocator()
assertThrows(IllegalArgumentException::class.java) {
nameAllocator[1]
}
}

@Test fun cloneUsage() {
val outerAllocator = NameAllocator()
outerAllocator.newName("foo", 1)

val innerAllocator1 = outerAllocator.copy()
assertThat(innerAllocator1.newName("bar", 2), equalTo("bar"))
assertThat(innerAllocator1.newName("foo", 3), equalTo("foo_"))

val innerAllocator2 = outerAllocator.copy()
assertThat(innerAllocator2.newName("foo", 2), equalTo("foo_"))
assertThat(innerAllocator2.newName("bar", 3), equalTo("bar"))
}
}