|
| 1 | +/* |
| 2 | + * Copyright 2025 ABSA Group Limited |
| 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 za.co.absa.db.fadb.utils |
| 18 | + |
| 19 | +import org.scalatest.funsuite.AnyFunSuiteLike |
| 20 | +import za.co.absa.db.fadb.naming.LettersCase |
| 21 | +import za.co.absa.db.fadb.naming.implementations.AsIsNaming |
| 22 | +import za.co.absa.db.fadb.utils.ClassFieldNamesExtractorUnitTests._ |
| 23 | + |
| 24 | +class ClassFieldNamesExtractorUnitTests extends AnyFunSuiteLike { |
| 25 | + test("Extract from case class returns its fields") { |
| 26 | + val expected = Seq( |
| 27 | + "int_field", |
| 28 | + "string_field" |
| 29 | + ) |
| 30 | + val fieldNames = ClassFieldNamesExtractor.extract[TestCaseClass]() |
| 31 | + assert(fieldNames == expected) |
| 32 | + } |
| 33 | + |
| 34 | + test("Extract from class constructor fields returns its constructor fields, explicit naming convention") { |
| 35 | + val expected = Seq( |
| 36 | + "XFIELD", |
| 37 | + "YFIELD" |
| 38 | + ) |
| 39 | + val fieldNames = ClassFieldNamesExtractor.extract[TestClass](new AsIsNaming(LettersCase.UpperCase)) |
| 40 | + assert(fieldNames == expected) |
| 41 | + } |
| 42 | + |
| 43 | + test("Extract from class constructor fields returns its constructor fields, implicit naming convention") { |
| 44 | + implicit val namingConvention: AsIsNaming = new AsIsNaming(LettersCase.LowerCase) |
| 45 | + val expected = Seq( |
| 46 | + "xfield", |
| 47 | + "yfield" |
| 48 | + ) |
| 49 | + val fieldNames = ClassFieldNamesExtractor.extract[TestClass]() |
| 50 | + assert(fieldNames == expected) |
| 51 | + } |
| 52 | + |
| 53 | + test("Extract from trait fails") { |
| 54 | + intercept[IllegalArgumentException] { |
| 55 | + ClassFieldNamesExtractor.extract[TestTrait] |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + test("Extract fails on simple type") { |
| 60 | + intercept[IllegalArgumentException] { |
| 61 | + ClassFieldNamesExtractor.extract[Boolean] |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +object ClassFieldNamesExtractorUnitTests { |
| 68 | + case class TestCaseClass(intField: Int, stringField: String) { |
| 69 | + def stringFunction: String = intField.toString + stringField |
| 70 | + } |
| 71 | + |
| 72 | + class TestClass(val xField: Int, val yField: String) { |
| 73 | + val w: String = xField.toString + yField |
| 74 | + |
| 75 | + def z: String = xField.toString + yField |
| 76 | + } |
| 77 | + |
| 78 | + trait TestTrait { |
| 79 | + def foo: String |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments