1 /*
<lambda>null2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tools.metalava.model.text
18 
19 import java.nio.file.Path
20 import org.junit.Assert.assertEquals
21 import org.junit.Assert.assertThrows
22 import org.junit.Test
23 import org.junit.runner.RunWith
24 import org.junit.runners.Parameterized
25 
26 @RunWith(Parameterized::class)
27 class TokenizerTest(private val params: Params) {
28 
29     data class Params(
30         val input: String,
31         val parenIsSep: Boolean = true,
32         val expectedToken: String? = null,
33         val expectedError: String? = null,
34     ) {
35         init {
36             if (expectedToken == null && expectedError == null) {
37                 throw IllegalArgumentException(
38                     "Expected one of `expectedToken` and `expectedError`, found neither"
39                 )
40             } else if (expectedToken != null && expectedError != null) {
41                 throw IllegalArgumentException(
42                     "Expected one of `expectedToken` and `expectedError`, found both"
43                 )
44             }
45         }
46 
47         override fun toString(): String = input
48     }
49 
50     companion object {
51         private val params =
52             listOf(
53                 Params(
54                     input = """  "string"  """,
55                     expectedToken = """"string"""",
56                 ),
57                 Params(
58                     input = """  "string  """,
59                     expectedError = """api.txt:1: Unexpected end of file for " starting at 1""",
60                 ),
61                 Params(
62                     input = """  "string\""",
63                     expectedError = """api.txt:1: Unexpected end of file for " starting at 1""",
64                 ),
65                 Params(
66                     input = """ @pkg.Annotation("string") """,
67                     parenIsSep = false,
68                     expectedToken = """@pkg.Annotation("string")""",
69                 ),
70                 Params(
71                     input = """ @pkg.Annotation("string """,
72                     parenIsSep = false,
73                     expectedError = """api.txt:1: Unexpected end of file for " starting at 1""",
74                 ),
75             )
76 
77         @JvmStatic
78         @Parameterized.Parameters(name = "#{index} -{0}")
79         fun testParams(): List<Params> = params
80     }
81 
82     @Test
83     fun `check token`() {
84         val tokenizer = Tokenizer(Path.of("api.txt"), params.input.toCharArray())
85 
86         fun requireToken(): String {
87             return tokenizer.requireToken(parenIsSep = params.parenIsSep)
88         }
89 
90         params.expectedError?.let { expectedError ->
91             val exception = assertThrows(ApiParseException::class.java) { requireToken() }
92             assertEquals(expectedError, exception.message)
93         }
94 
95         params.expectedToken?.let { expectedToken ->
96             val token = requireToken()
97             assertEquals(expectedToken, token)
98         }
99     }
100 }
101