mirror of
https://github.com/apple/pkl.git
synced 2026-02-23 10:54:54 +01:00
Initial commit
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.config.java.mapper.ConverterFactories;
|
||||
import org.pkl.core.SecurityManagers;
|
||||
|
||||
public class ConfigEvaluatorBuilderTest {
|
||||
@Test
|
||||
public void preconfiguredBuilderHasPreconfiguredUnderlyingBuilders() {
|
||||
var builder = ConfigEvaluatorBuilder.preconfigured();
|
||||
|
||||
var evaluatorBuilder = builder.getEvaluatorBuilder();
|
||||
assertThat(evaluatorBuilder).isNotNull();
|
||||
assertThat(evaluatorBuilder.getEnvironmentVariables()).isEqualTo(System.getenv());
|
||||
assertThat(evaluatorBuilder.getExternalProperties()).isEqualTo(System.getProperties());
|
||||
|
||||
var mapperBuilder = builder.getValueMapperBuilder();
|
||||
assertThat(mapperBuilder).isNotNull();
|
||||
assertThat(mapperBuilder.getConverterFactories()).isEqualTo(ConverterFactories.all);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unconfiguredBuilderHasUnconfiguredUnderlyingBuilders() {
|
||||
var builder = ConfigEvaluatorBuilder.unconfigured();
|
||||
|
||||
var evaluatorBuilder = builder.getEvaluatorBuilder();
|
||||
assertThat(evaluatorBuilder).isNotNull();
|
||||
assertThat(evaluatorBuilder.getEnvironmentVariables()).isEmpty();
|
||||
assertThat(evaluatorBuilder.getExternalProperties()).isEmpty();
|
||||
|
||||
var mapperBuilder = builder.getValueMapperBuilder();
|
||||
assertThat(mapperBuilder).isNotNull();
|
||||
assertThat(mapperBuilder.getConverterFactories()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preconfiguredBuilderContainsProcessEnvironmentVariables() {
|
||||
var builder = ConfigEvaluatorBuilder.preconfigured();
|
||||
assertThat(builder.getEnvironmentVariables()).isEqualTo(System.getenv());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unconfiguredBuilderContainsNoEnvironmentVariables() {
|
||||
var builder = ConfigEvaluatorBuilder.unconfigured();
|
||||
assertThat(builder.getEnvironmentVariables()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addEnvironmentVariables() {
|
||||
var builder = ConfigEvaluatorBuilder.unconfigured();
|
||||
builder.addEnvironmentVariable("ONE", "one");
|
||||
var envVars = Map.of("TWO", "two", "THREE", "three");
|
||||
builder.addEnvironmentVariables(envVars);
|
||||
|
||||
assertThat(builder.getEnvironmentVariables()).hasSize(3);
|
||||
assertThat(builder.getEnvironmentVariables()).containsEntry("ONE", "one");
|
||||
assertThat(builder.getEnvironmentVariables()).containsAllEntriesOf(envVars);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideEnvironmentVariables() {
|
||||
var builder = ConfigEvaluatorBuilder.unconfigured();
|
||||
|
||||
var envVars1 = Map.of("TWO", "two", "THREE", "three");
|
||||
builder.addEnvironmentVariables(envVars1);
|
||||
|
||||
var envVars2 = Map.of("FOUR", "four", "FIVE", "five");
|
||||
builder.setEnvironmentVariables(envVars2);
|
||||
|
||||
assertThat(builder.getEnvironmentVariables()).hasSize(2);
|
||||
assertThat(builder.getEnvironmentVariables()).containsAllEntriesOf(envVars2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preconfiguredBuilderContainsSystemProperties() {
|
||||
var builder = ConfigEvaluatorBuilder.preconfigured();
|
||||
assertThat(builder.getExternalProperties()).isEqualTo(System.getProperties());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unconfiguredBuilderContainsNoExternalProperties() {
|
||||
var builder = ConfigEvaluatorBuilder.unconfigured();
|
||||
assertThat(builder.getExternalProperties()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addExternalProperties() {
|
||||
var builder = ConfigEvaluatorBuilder.unconfigured();
|
||||
builder.addExternalProperty("ONE", "one");
|
||||
var properties = Map.of("TWO", "two", "THREE", "three");
|
||||
builder.addExternalProperties(properties);
|
||||
|
||||
assertThat(builder.getExternalProperties()).hasSize(3);
|
||||
assertThat(builder.getExternalProperties()).containsEntry("ONE", "one");
|
||||
assertThat(builder.getExternalProperties()).containsAllEntriesOf(properties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideExternalProperties() {
|
||||
var builder = ConfigEvaluatorBuilder.unconfigured();
|
||||
|
||||
var properties1 = Map.of("TWO", "two", "THREE", "three");
|
||||
builder.addExternalProperties(properties1);
|
||||
|
||||
var properties2 = Map.of("FOUR", "four", "FIVE", "five");
|
||||
builder.setExternalProperties(properties2);
|
||||
|
||||
assertThat(builder.getExternalProperties()).hasSize(2);
|
||||
assertThat(builder.getExternalProperties()).containsAllEntriesOf(properties2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSecurityManager() {
|
||||
var builder = ConfigEvaluatorBuilder.preconfigured();
|
||||
|
||||
assertThat(builder.getAllowedModules()).isEqualTo(SecurityManagers.defaultAllowedModules);
|
||||
assertThat(builder.getAllowedResources()).isEqualTo(SecurityManagers.defaultAllowedResources);
|
||||
|
||||
var manager =
|
||||
SecurityManagers.standard(List.of(), List.of(), SecurityManagers.defaultTrustLevels, null);
|
||||
|
||||
builder = ConfigEvaluatorBuilder.preconfigured().setSecurityManager(manager);
|
||||
|
||||
assertThat(builder.getSecurityManager()).isSameAs(manager);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.pkl.core.ModuleSource.text;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.config.java.mapper.Named;
|
||||
import org.pkl.config.java.mapper.Types;
|
||||
import org.pkl.core.PObject;
|
||||
|
||||
public class ConfigTest {
|
||||
private final ConfigEvaluator evaluator = ConfigEvaluator.preconfigured();
|
||||
|
||||
private final Config pigeonConfig =
|
||||
evaluator.evaluate(
|
||||
text(
|
||||
"pigeon { age = 30; friends = List(\"john\", \"mary\"); address { street = \"Fuzzy St.\" } }"));
|
||||
|
||||
private final Config pigeonModuleConfig =
|
||||
evaluator.evaluate(
|
||||
text("age = 30; friends = List(\"john\", \"mary\"); address { street = \"Fuzzy St.\" }"));
|
||||
|
||||
private final Config pairConfig =
|
||||
evaluator.evaluate(text("x { first = \"file/path\"; second = 42 }"));
|
||||
|
||||
private final Config mapConfig = evaluator.evaluate(text("x = Map(\"one\", 1, \"two\", 2)"));
|
||||
|
||||
@Test
|
||||
public void navigate() {
|
||||
var pigeon = pigeonConfig.get("pigeon");
|
||||
assertThat(pigeon.getQualifiedName()).isEqualTo("pigeon");
|
||||
assertThat(pigeon.getRawValue()).isInstanceOf(PObject.class);
|
||||
|
||||
var address = pigeon.get("address");
|
||||
assertThat(address.getQualifiedName()).isEqualTo("pigeon.address");
|
||||
assertThat(address.getRawValue()).isInstanceOf(PObject.class);
|
||||
|
||||
var street = address.get("street");
|
||||
assertThat(street.getQualifiedName()).isEqualTo("pigeon.address.street");
|
||||
assertThat(street.getRawValue()).isInstanceOf(String.class);
|
||||
|
||||
assertThat(street.as(String.class)).isEqualTo("Fuzzy St.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void navigateToNonExistingObjectChild() {
|
||||
var pigeon = pigeonConfig.get("pigeon");
|
||||
var t = catchThrowable(() -> pigeon.get("non-existing"));
|
||||
|
||||
assertThat(t)
|
||||
.isInstanceOf(NoSuchChildException.class)
|
||||
.hasMessageStartingWith(
|
||||
"Node `pigeon` of type `pkl.base#Dynamic` "
|
||||
+ "does not have a property named `non-existing`.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void navigateToNonExistingMapChild() {
|
||||
var map = mapConfig.get("x");
|
||||
var t = catchThrowable(() -> map.get("non-existing"));
|
||||
|
||||
assertThat(t)
|
||||
.isInstanceOf(NoSuchChildException.class)
|
||||
.hasMessageStartingWith(
|
||||
"Node `x` of type `pkl.base#Map` " + "does not have a key named `non-existing`.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void navigateToNonExistingLeafChild() {
|
||||
var age = pigeonConfig.get("pigeon").get("age");
|
||||
var t = catchThrowable(() -> age.get("non-existing"));
|
||||
|
||||
assertThat(t)
|
||||
.isInstanceOf(NoSuchChildException.class)
|
||||
.hasMessageStartingWith(
|
||||
"Leaf node `pigeon.age` of type `pkl.base#Int` "
|
||||
+ "does not have a child named `non-existing`.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToPojoByType() {
|
||||
Person pigeon = pigeonConfig.get("pigeon").as(Person.class);
|
||||
checkPigeon(pigeon);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToPojoByJavaType() {
|
||||
var pigeon = pigeonConfig.get("pigeon").as(JavaType.of(Person.class));
|
||||
checkPigeon(pigeon);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertModuleToPojoByType() {
|
||||
var pigeon = pigeonModuleConfig.as(Person.class);
|
||||
checkPigeon(pigeon);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertModuleToPojoByJavaType() {
|
||||
var pigeon = pigeonModuleConfig.as(JavaType.of(Person.class));
|
||||
checkPigeon(pigeon);
|
||||
}
|
||||
|
||||
private void checkPigeon(Person pigeon) {
|
||||
assertThat(pigeon).isNotNull();
|
||||
assertThat(pigeon.age).isEqualTo(30);
|
||||
assertThat(pigeon.friends).containsExactly("john", "mary");
|
||||
assertThat(pigeon.address.street).isEqualTo("Fuzzy St.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertToParameterizedTypeByType() {
|
||||
Pair<Path, Integer> pair =
|
||||
pairConfig.get("x").as(Types.parameterizedType(Pair.class, Path.class, Integer.class));
|
||||
checkPair(pair);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertToParameterizedTypeByJavaType() {
|
||||
var pair = pairConfig.get("x").as(new JavaType<Pair<Path, Integer>>() {});
|
||||
checkPair(pair);
|
||||
}
|
||||
|
||||
private void checkPair(Pair<?, ?> pair) {
|
||||
assertThat(pair).isNotNull();
|
||||
assertThat(pair.first).isEqualTo(Path.of("file/path"));
|
||||
assertThat(pair.second).isEqualTo(42);
|
||||
}
|
||||
|
||||
public static class Person {
|
||||
final int age;
|
||||
final List<String> friends;
|
||||
final Address address;
|
||||
|
||||
public Person(
|
||||
@Named("age") int age,
|
||||
@Named("friends") List<String> friends,
|
||||
@Named("address") Address address) {
|
||||
this.age = age;
|
||||
this.friends = friends;
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Address {
|
||||
final String street;
|
||||
|
||||
public Address(@Named("street") String street) {
|
||||
this.street = street;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Pair<S, T> {
|
||||
final S first;
|
||||
final T second;
|
||||
|
||||
public Pair(@Named("first") S first, @Named("second") T second) {
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.config.java.mapper.Reflection;
|
||||
import org.pkl.config.java.mapper.Types;
|
||||
|
||||
public class JavaTypeTest {
|
||||
@Test
|
||||
public void constructOptionalType() {
|
||||
var type = JavaType.optionalOf(String.class);
|
||||
assertThat(type).isEqualTo(new JavaType<Optional<String>>() {});
|
||||
assertThat(type).isEqualTo(JavaType.optionalOf(JavaType.of(String.class)));
|
||||
assertThat(Reflection.toRawType(type.getType())).isEqualTo(Optional.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructArrayType() {
|
||||
var type = JavaType.arrayOf(String.class);
|
||||
assertThat(type).isEqualTo(new JavaType<String[]>() {});
|
||||
assertThat(type).isEqualTo(JavaType.arrayOf(JavaType.of(String.class)));
|
||||
assertThat(Reflection.toRawType(type.getType()).isArray()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructIterableType() {
|
||||
var type = JavaType.iterableOf(String.class);
|
||||
assertThat(type).isEqualTo(new JavaType<Iterable<String>>() {});
|
||||
assertThat(type).isEqualTo(JavaType.iterableOf(JavaType.of(String.class)));
|
||||
assertThat(Reflection.toRawType(type.getType())).isEqualTo(Iterable.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructCollectionType() {
|
||||
var type = JavaType.collectionOf(String.class);
|
||||
assertThat(type).isEqualTo(new JavaType<Collection<String>>() {});
|
||||
assertThat(type).isEqualTo(JavaType.collectionOf(JavaType.of(String.class)));
|
||||
assertThat(Reflection.toRawType(type.getType())).isEqualTo(Collection.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructListType() {
|
||||
var type = JavaType.listOf(String.class);
|
||||
assertThat(type).isEqualTo(new JavaType<List<String>>() {});
|
||||
assertThat(type).isEqualTo(JavaType.listOf(JavaType.of(String.class)));
|
||||
assertThat(Reflection.toRawType(type.getType())).isEqualTo(List.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructSetType() {
|
||||
var type = JavaType.setOf(String.class);
|
||||
assertThat(type).isEqualTo(new JavaType<Set<String>>() {});
|
||||
assertThat(type).isEqualTo(JavaType.setOf(JavaType.of(String.class)));
|
||||
assertThat(Reflection.toRawType(type.getType())).isEqualTo(Set.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructMapType() {
|
||||
var type = JavaType.mapOf(String.class, URI.class);
|
||||
assertThat(type).isEqualTo(new JavaType<Map<String, URI>>() {});
|
||||
assertThat(type).isEqualTo(JavaType.mapOf(JavaType.of(String.class), JavaType.of(URI.class)));
|
||||
assertThat(Reflection.toRawType(type.getType())).isEqualTo(Map.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usageAsTypeToken() {
|
||||
var javaType = new JavaType<Map<String, List<URI>>>() {};
|
||||
|
||||
assertThat(javaType.getType()).isEqualTo(Types.mapOf(String.class, Types.listOf(URI.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameTypesConstructedInDifferentWaysAreEqual() {
|
||||
var type1 = JavaType.mapOf(JavaType.of(String.class), JavaType.listOf(URI.class));
|
||||
var type2 = new JavaType<Map<String, List<URI>>>() {};
|
||||
var type3 = JavaType.of(Types.mapOf(String.class, Types.listOf(URI.class)));
|
||||
|
||||
assertThat(type1).isEqualTo(type1);
|
||||
assertThat(type2).isEqualTo(type1);
|
||||
assertThat(type3).isEqualTo(type2);
|
||||
|
||||
assertThat(type2.hashCode()).isEqualTo(type1.hashCode());
|
||||
assertThat(type3.hashCode()).isEqualTo(type2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void differentTypesAreNotEqual() {
|
||||
var type1 = JavaType.mapOf(JavaType.of(String.class), JavaType.listOf(URI.class));
|
||||
var type2 = new JavaType<Map<String, List<URL>>>() {};
|
||||
var type3 = JavaType.of(Types.mapOf(String.class, Types.listOf(Path.class)));
|
||||
|
||||
assertThat(type2).isNotEqualTo(type1);
|
||||
assertThat(type3).isNotEqualTo(type1);
|
||||
assertThat(type2).isNotEqualTo(type3);
|
||||
|
||||
// hopefully
|
||||
assertThat(type2.hashCode()).isNotEqualTo(type1.hashCode());
|
||||
assertThat(type3.hashCode()).isNotEqualTo(type1.hashCode());
|
||||
assertThat(type3.hashCode()).isNotEqualTo(type2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameStringRepresentationAsJavaLangReflectType() {
|
||||
var type = JavaType.mapOf(String.class, URI.class);
|
||||
assertThat(type.toString()).isEqualTo("java.util.Map<java.lang.String, java.net.URI>");
|
||||
assertThat(type.toString()).isEqualTo(type.getType().toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.regex.Pattern;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.core.Duration;
|
||||
import org.pkl.core.DurationUnit;
|
||||
|
||||
public class ConversionsTest {
|
||||
@Test
|
||||
public void pStringToFile() {
|
||||
var file = Conversions.pStringToFile.converter.convert("relative/path", null);
|
||||
assertThat(file).isEqualTo(new File("relative/path"));
|
||||
|
||||
var file2 = Conversions.pStringToFile.converter.convert("/absolute/path", null);
|
||||
assertThat(file2).isEqualTo(new File("/absolute/path"));
|
||||
|
||||
var file3 = Conversions.pStringToFile.converter.convert("", null);
|
||||
assertThat(file3).isEqualTo(new File(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pStringToPath() {
|
||||
var path = Conversions.pStringToPath.converter.convert("relative/path", null);
|
||||
assertThat(path).isEqualTo(Path.of("relative/path"));
|
||||
|
||||
var path2 = Conversions.pStringToPath.converter.convert("/absolute/path", null);
|
||||
assertThat(path2).isEqualTo(Path.of("/absolute/path"));
|
||||
|
||||
var path3 = Conversions.pStringToPath.converter.convert("", null);
|
||||
assertThat(path3).isEqualTo(Path.of(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pStringToPattern() {
|
||||
var str = "(?i)\\w*";
|
||||
var pattern = Conversions.pStringToPattern.converter.convert(str, null);
|
||||
assertThat(pattern.pattern()).isEqualTo(str);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pRegexToString() {
|
||||
var regex = Pattern.compile("(?i)\\w*");
|
||||
var str = Conversions.pRegexToString.converter.convert(regex, null);
|
||||
assertThat(str).isEqualTo("(?i)\\w*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pDurationToDuration() {
|
||||
var pDuration = new Duration(100, DurationUnit.MINUTES);
|
||||
var duration = Conversions.pDurationToDuration.converter.convert(pDuration, null);
|
||||
assertThat(duration).isEqualTo(java.time.Duration.of(100, ChronoUnit.MINUTES));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.pkl.core.ModuleSource.modulePath;
|
||||
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.core.Evaluator;
|
||||
import org.pkl.core.PModule;
|
||||
|
||||
public class PAnyToOptionalTest {
|
||||
private static final Evaluator evaluator = Evaluator.preconfigured();
|
||||
|
||||
private static final PModule module =
|
||||
evaluator.evaluate(modulePath("org/pkl/config/java/mapper/PAnyToOptionalTest.pkl"));
|
||||
|
||||
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
|
||||
|
||||
@AfterAll
|
||||
public static void afterAll() {
|
||||
evaluator.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex1() {
|
||||
var ex1 = module.getProperty("ex1");
|
||||
Optional<String> mapped = mapper.map(ex1, Types.optionalOf(String.class));
|
||||
|
||||
assertThat(mapped).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex2() {
|
||||
var ex2 = module.getProperty("ex2");
|
||||
Optional<String> mapped = mapper.map(ex2, Types.optionalOf(String.class));
|
||||
|
||||
assertThat(mapped).contains("str");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex3() {
|
||||
var ex3 = module.getProperty("ex3");
|
||||
Optional<List<Integer>> mapped = mapper.map(ex3, Types.optionalOf(Types.listOf(Integer.class)));
|
||||
|
||||
assertThat(mapped).contains(List.of(1, 2, 3));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.pkl.core.ModuleSource.modulePath;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.core.Evaluator;
|
||||
import org.pkl.core.PModule;
|
||||
|
||||
public class PCollectionToArrayTest {
|
||||
private static final Evaluator evaluator = Evaluator.preconfigured();
|
||||
|
||||
private static final PModule module =
|
||||
evaluator.evaluate(modulePath("org/pkl/config/java/mapper/PCollectionToArrayTest.pkl"));
|
||||
|
||||
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
|
||||
|
||||
@AfterAll
|
||||
public static void afterAll() {
|
||||
evaluator.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex1() {
|
||||
var ex1 = module.getProperty("ex1");
|
||||
assertThat(mapper.map(ex1, byte[].class)).isEqualTo(new byte[0]);
|
||||
assertThat(mapper.map(ex1, short[].class)).isEqualTo(new short[0]);
|
||||
assertThat(mapper.map(ex1, int[].class)).isEqualTo(new int[0]);
|
||||
assertThat(mapper.map(ex1, long[].class)).isEqualTo(new long[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex2() {
|
||||
var ex2 = module.getProperty("ex2");
|
||||
assertThat(mapper.map(ex2, byte[].class)).isEqualTo(new byte[] {1, 2, 3});
|
||||
assertThat(mapper.map(ex2, short[].class)).isEqualTo(new short[] {1, 2, 3});
|
||||
assertThat(mapper.map(ex2, int[].class)).isEqualTo(new int[] {1, 2, 3});
|
||||
assertThat(mapper.map(ex2, long[].class)).isEqualTo(new long[] {1, 2, 3});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex3() {
|
||||
var ex3 = module.getProperty("ex3");
|
||||
assertThat(mapper.map(ex3, byte[].class)).isEqualTo(new byte[] {1, 2, 3});
|
||||
assertThat(mapper.map(ex3, short[].class)).isEqualTo(new short[] {1, 2, 3});
|
||||
assertThat(mapper.map(ex3, int[].class)).isEqualTo(new int[] {1, 2, 3});
|
||||
assertThat(mapper.map(ex3, long[].class)).isEqualTo(new long[] {1, 2, 3});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex4() {
|
||||
var ex4 = module.getProperty("ex4");
|
||||
assertThat(mapper.map(ex4, float[].class)).isEqualTo(new float[] {1f, 2f, 3.3f});
|
||||
assertThat(mapper.map(ex4, double[].class)).isEqualTo(new double[] {1d, 2d, 3.3d});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex5() {
|
||||
var ex5 = module.getProperty("ex5");
|
||||
assertThat(mapper.map(ex5, boolean[].class)).isEqualTo(new boolean[] {true, false, true});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex6() {
|
||||
var ex6 = module.getProperty("ex6");
|
||||
assertThat(mapper.map(ex6, Person[].class))
|
||||
.isEqualTo(new Person[] {new Person("pigeon", 40), new Person("parrot", 30)});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.pkl.core.ModuleSource.modulePath;
|
||||
|
||||
import java.util.List;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.core.Evaluator;
|
||||
import org.pkl.core.PModule;
|
||||
|
||||
public class PCollectionToCollectionTest {
|
||||
private static final Evaluator evaluator = Evaluator.preconfigured();
|
||||
|
||||
private static final PModule module =
|
||||
evaluator.evaluate(modulePath("org/pkl/config/java/mapper/PCollectionToCollectionTest.pkl"));
|
||||
|
||||
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
|
||||
|
||||
@AfterAll
|
||||
public static void afterAll() {
|
||||
evaluator.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex1() {
|
||||
var ex1 = module.getProperty("ex1");
|
||||
|
||||
List<Byte> mapped1 = mapper.map(ex1, Types.listOf(Byte.class));
|
||||
assertThat(mapped1).isEmpty();
|
||||
|
||||
List<Short> mapped2 = mapper.map(ex1, Types.listOf(Short.class));
|
||||
assertThat(mapped2).isEmpty();
|
||||
|
||||
List<Integer> mapped3 = mapper.map(ex1, Types.listOf(Integer.class));
|
||||
assertThat(mapped3).isEmpty();
|
||||
|
||||
List<Long> mapped4 = mapper.map(ex1, Types.listOf(Long.class));
|
||||
assertThat(mapped4).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex2() {
|
||||
var ex2 = module.getProperty("ex2");
|
||||
|
||||
List<Byte> mapped1 = mapper.map(ex2, Types.listOf(Byte.class));
|
||||
assertThat(mapped1).containsExactly((byte) 1, (byte) 2, (byte) 3);
|
||||
|
||||
List<Short> mapped2 = mapper.map(ex2, Types.listOf(Short.class));
|
||||
assertThat(mapped2).containsExactly((short) 1, (short) 2, (short) 3);
|
||||
|
||||
List<Integer> mapped3 = mapper.map(ex2, Types.listOf(Integer.class));
|
||||
assertThat(mapped3).containsExactly(1, 2, 3);
|
||||
|
||||
List<Long> mapped4 = mapper.map(ex2, Types.listOf(Long.class));
|
||||
assertThat(mapped4).containsExactly(1L, 2L, 3L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex3() {
|
||||
var ex3 = module.getProperty("ex3");
|
||||
|
||||
List<Byte> mapped1 = mapper.map(ex3, Types.listOf(Byte.class));
|
||||
assertThat(mapped1).containsExactly((byte) 1, (byte) 2, (byte) 3);
|
||||
|
||||
List<Short> mapped2 = mapper.map(ex3, Types.listOf(Short.class));
|
||||
assertThat(mapped2).containsExactly((short) 1, (short) 2, (short) 3);
|
||||
|
||||
List<Integer> mapped3 = mapper.map(ex3, Types.listOf(Integer.class));
|
||||
assertThat(mapped3).containsExactly(1, 2, 3);
|
||||
|
||||
List<Long> mapped4 = mapper.map(ex3, Types.listOf(Long.class));
|
||||
assertThat(mapped4).containsExactly(1L, 2L, 3L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex4() {
|
||||
var ex4 = module.getProperty("ex4");
|
||||
|
||||
List<Float> mapped1 = mapper.map(ex4, Types.listOf(Float.class));
|
||||
assertThat(mapped1).containsExactly(1f, 2f, 3.3f);
|
||||
|
||||
List<Double> mapped2 = mapper.map(ex4, Types.listOf(Double.class));
|
||||
assertThat(mapped2).containsExactly(1d, 2d, 3.3d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex5() {
|
||||
var ex5 = module.getProperty("ex5");
|
||||
List<Boolean> mapped = mapper.map(ex5, Types.listOf(Boolean.class));
|
||||
assertThat(mapped).containsExactly(true, false, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex6() {
|
||||
var ex6 = module.getProperty("ex6");
|
||||
List<Person> mapped = mapper.map(ex6, Types.listOf(Person.class));
|
||||
Assertions.assertThat(mapped)
|
||||
.containsExactly(new Person("pigeon", 40), new Person("parrot", 30));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.pkl.core.ModuleSource.modulePath;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.core.*;
|
||||
|
||||
public class PMapToMapTest {
|
||||
private static final Evaluator evaluator = Evaluator.preconfigured();
|
||||
|
||||
private static final PModule module =
|
||||
evaluator.evaluate(modulePath("org/pkl/config/java/mapper/PMapToMapTest.pkl"));
|
||||
|
||||
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
|
||||
|
||||
@AfterAll
|
||||
public static void afterAll() {
|
||||
evaluator.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex1() {
|
||||
var ex1 = module.getProperty("ex1");
|
||||
Map<Integer, Integer> mapped = mapper.map(ex1, Types.mapOf(Integer.class, Integer.class));
|
||||
|
||||
assertThat(mapped).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex2() {
|
||||
var ex2 = module.getProperty("ex2");
|
||||
Map<Integer, Integer> mapped = mapper.map(ex2, Types.mapOf(Integer.class, Integer.class));
|
||||
assertThat(mapped).containsOnly(entry(1, 2), entry(2, 4), entry(3, 6));
|
||||
|
||||
Map<Byte, Double> mapped2 = mapper.map(ex2, Types.mapOf(Byte.class, Double.class));
|
||||
assertThat(mapped2).containsOnly(entry((byte) 1, 2d), entry((byte) 2, 4d), entry((byte) 3, 6d));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex3() {
|
||||
var ex3 = module.getProperty("ex3");
|
||||
Map<Integer, Double> mapped = mapper.map(ex3, Types.mapOf(Integer.class, Double.class));
|
||||
|
||||
assertThat(mapped).containsOnly(entry(1, 2d), entry(2, 4d), entry(3, 6.6d));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex4() {
|
||||
var ex4 = module.getProperty("ex4");
|
||||
|
||||
Map<String, Map<String, Object>> mapped =
|
||||
mapper.map(ex4, Types.mapOf(String.class, Types.mapOf(String.class, Object.class)));
|
||||
|
||||
Map<String, Object> pigeon = Map.of("name", "pigeon", "age", 40L);
|
||||
Map<String, Object> parrot = Map.of("name", "parrot", "age", 30L);
|
||||
|
||||
assertThat(mapped).containsOnly(entry("pigeon", pigeon), entry("parrot", parrot));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex5() {
|
||||
var ex5 = module.getProperty("ex5");
|
||||
Map<String, Person> mapped = mapper.map(ex5, Types.mapOf(String.class, Person.class));
|
||||
|
||||
assertThat(mapped)
|
||||
.containsOnly(
|
||||
entry("pigeon", new Person("pigeon", 40)), entry("parrot", new Person("parrot", 30)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex6() {
|
||||
var ex6 = module.getProperty("ex6");
|
||||
Map<Person, String> mapped = mapper.map(ex6, Types.mapOf(Person.class, String.class));
|
||||
|
||||
assertThat(mapped)
|
||||
.containsOnly(
|
||||
entry(new Person("pigeon", 40), "pigeon"), entry(new Person("parrot", 30), "parrot"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex7() {
|
||||
var mapper =
|
||||
ValueMapperBuilder.preconfigured()
|
||||
.addConversion(
|
||||
Conversion.of(PClassInfo.Int, String.class, (num, mapper2) -> String.valueOf(num)))
|
||||
.build();
|
||||
var ex7 = module.getProperty("ex7");
|
||||
// conversion from PInt to String kicks in because PMapToMap treats Properties as
|
||||
// Map<String,String>
|
||||
var properties = mapper.map(ex7, Properties.class);
|
||||
|
||||
assertThat(properties).hasSize(2);
|
||||
assertThat(properties.getProperty("1")).isEqualTo("2");
|
||||
assertThat(properties.getProperty("2")).isEqualTo("4");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.pkl.core.ModuleSource.modulePath;
|
||||
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.config.java.mapper.PObjectToDataObjectTest.Address;
|
||||
import org.pkl.config.java.mapper.PObjectToDataObjectTest.Hobby;
|
||||
import org.pkl.core.Evaluator;
|
||||
import org.pkl.core.PModule;
|
||||
|
||||
public class PModuleToDataObjectTest {
|
||||
private static final Evaluator evaluator = Evaluator.preconfigured();
|
||||
|
||||
private static final PModule module =
|
||||
evaluator.evaluate(modulePath("org/pkl/config/java/mapper/PModuleToDataObjectTest.pkl"));
|
||||
PObjectToDataObjectTest.Person pigeon =
|
||||
new PObjectToDataObjectTest.Person(
|
||||
"pigeon",
|
||||
40,
|
||||
EnumSet.of(Hobby.SURFING, Hobby.SWIMMING),
|
||||
new Address("sesame street", 94105));
|
||||
|
||||
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
|
||||
|
||||
@AfterAll
|
||||
public static void afterAll() {
|
||||
evaluator.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doit() {
|
||||
assertThat(mapper.map(module, PObjectToDataObjectTest.Person.class)).isEqualTo(pigeon);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.core.PNull;
|
||||
|
||||
public class PNullToAnyTest {
|
||||
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
// due to Conversions.identities
|
||||
assertThat(mapper.map(PNull.getInstance(), PNull.class)).isEqualTo(PNull.getInstance());
|
||||
|
||||
assertThat(mapper.map(PNull.getInstance(), String.class)).isNull();
|
||||
assertThat(mapper.map(PNull.getInstance(), Person.class)).isNull();
|
||||
assertThat(mapper.map(PNull.getInstance(), Integer.class)).isNull();
|
||||
|
||||
assertThatThrownBy(() -> mapper.map(PNull.getInstance(), int.class))
|
||||
.isInstanceOf(ConversionException.class);
|
||||
}
|
||||
|
||||
public static class Person {}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.pkl.core.ModuleSource.modulePath;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import javax.inject.Named;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.core.Evaluator;
|
||||
import org.pkl.core.PModule;
|
||||
import org.pkl.core.util.Nullable;
|
||||
|
||||
public class PObjectToDataObjectJavaxInjectTest {
|
||||
private static final Evaluator evaluator = Evaluator.preconfigured();
|
||||
|
||||
private static final PModule module =
|
||||
evaluator.evaluate(modulePath("org/pkl/config/java/mapper/PObjectToDataObjectTest.pkl"));
|
||||
|
||||
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
|
||||
|
||||
private static final Person pigeon =
|
||||
new Person(
|
||||
"pigeon",
|
||||
40,
|
||||
EnumSet.of(Hobby.SURFING, Hobby.SWIMMING),
|
||||
new Address("sesame street", 94105));
|
||||
|
||||
@AfterAll
|
||||
public static void afterAll() {
|
||||
evaluator.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex1() {
|
||||
var ex1 = module.getProperty("ex1");
|
||||
|
||||
assertThat(mapper.map(ex1, Person.class)).isEqualTo(pigeon);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex2() {
|
||||
var ex2 = module.getProperty("ex2");
|
||||
|
||||
assertThat(mapper.map(ex2, Person.class)).isEqualTo(pigeon);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex3() {
|
||||
var ex3 = module.getProperty("ex3");
|
||||
Object mapped =
|
||||
mapper.map(ex3, Types.parameterizedType(Pair.class, String.class, Integer.class));
|
||||
|
||||
assertThat(mapped).isEqualTo(new Pair<>("foo", 42));
|
||||
}
|
||||
|
||||
static class Person {
|
||||
final String name;
|
||||
final int age;
|
||||
final Set<Hobby> hobbies;
|
||||
final Address address;
|
||||
|
||||
Person(
|
||||
@Named("name") String name,
|
||||
@Named("age") int age,
|
||||
@Named("hobbies") Set<Hobby> hobbies,
|
||||
@Named("address") Address address) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.hobbies = hobbies;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof Person)) return false;
|
||||
|
||||
var other = (Person) obj;
|
||||
return name.equals(other.name)
|
||||
&& age == other.age
|
||||
&& hobbies.equals(other.hobbies)
|
||||
&& address.equals(other.address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name, age, hobbies, address);
|
||||
}
|
||||
}
|
||||
|
||||
static class Address {
|
||||
final String street;
|
||||
final int zip;
|
||||
|
||||
Address(@Named("street") String street, @Named("zip") int zip) {
|
||||
this.street = street;
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof Address)) return false;
|
||||
|
||||
var other = (Address) obj;
|
||||
return street.equals(other.street) && zip == other.zip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(street, zip);
|
||||
}
|
||||
}
|
||||
|
||||
public enum Hobby {
|
||||
SWIMMING,
|
||||
SURFING,
|
||||
READING
|
||||
}
|
||||
|
||||
public static class Pair<S, T> {
|
||||
public final S first;
|
||||
public final T second;
|
||||
|
||||
public Pair(@Named("first") S first, @Named("second") T second) {
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof Pair)) return false;
|
||||
|
||||
var other = (Pair<?, ?>) obj;
|
||||
return first.equals(other.first) && second.equals(other.second);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(first, second);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.example.OverriddenProperty;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.config.java.ConfigEvaluator;
|
||||
import org.pkl.core.ModuleSource;
|
||||
|
||||
class PObjectToDataObjectOverriddenPropertyTest {
|
||||
@Test
|
||||
void overriddenProperty() {
|
||||
try (var evaluator = ConfigEvaluator.preconfigured()) {
|
||||
var result =
|
||||
evaluator
|
||||
.evaluate(ModuleSource.modulePath("/codegenPkl/OverriddenProperty.pkl"))
|
||||
.as(OverriddenProperty.class);
|
||||
assertThat(result.theClass.bar.get(0).prop1).isEqualTo("hello");
|
||||
assertThat(result.theClass.bar.get(0).prop2).isEqualTo("hello again");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.pkl.core.ModuleSource.modulePath;
|
||||
|
||||
import java.beans.ConstructorProperties;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.core.Evaluator;
|
||||
import org.pkl.core.PModule;
|
||||
import org.pkl.core.util.Nullable;
|
||||
|
||||
public class PObjectToDataObjectTest {
|
||||
private static final Evaluator evaluator = Evaluator.preconfigured();
|
||||
|
||||
private static final PModule module =
|
||||
evaluator.evaluate(modulePath("org/pkl/config/java/mapper/PObjectToDataObjectTest.pkl"));
|
||||
|
||||
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
|
||||
|
||||
private static final Person pigeon =
|
||||
new Person(
|
||||
"pigeon",
|
||||
40,
|
||||
EnumSet.of(Hobby.SURFING, Hobby.SWIMMING),
|
||||
new Address("sesame street", 94105));
|
||||
|
||||
private static final PersonConstructoProperties pigeon2 =
|
||||
new PersonConstructoProperties(
|
||||
"pigeon",
|
||||
40,
|
||||
EnumSet.of(Hobby.SURFING, Hobby.SWIMMING),
|
||||
new Address("sesame street", 94105));
|
||||
|
||||
@AfterAll
|
||||
public static void afterAll() {
|
||||
evaluator.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex1() {
|
||||
var ex1 = module.getProperty("ex1");
|
||||
|
||||
assertThat(mapper.map(ex1, Person.class)).isEqualTo(pigeon);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex1_constructor_properties() {
|
||||
var ex1 = module.getProperty("ex1");
|
||||
assertThat(mapper.map(ex1, PersonConstructoProperties.class)).isEqualTo(pigeon2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex2() {
|
||||
var ex2 = module.getProperty("ex2");
|
||||
|
||||
assertThat(mapper.map(ex2, Person.class)).isEqualTo(pigeon);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex3() {
|
||||
var ex3 = module.getProperty("ex3");
|
||||
Object mapped =
|
||||
mapper.map(ex3, Types.parameterizedType(Pair.class, String.class, Integer.class));
|
||||
|
||||
assertThat(mapped).isEqualTo(new Pair<>("foo", 42));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex4() {
|
||||
var ex4 = module.getProperty("ex4");
|
||||
var t = catchThrowable(() -> mapper.map(ex4, Address.class));
|
||||
|
||||
assertThat(t).isInstanceOf(ConversionException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex5() {
|
||||
var ex5 = module.getProperty("ex5");
|
||||
|
||||
assertThat(mapper.map(ex5, UpperBounds.class).numbers).isEqualTo(List.of(1L, 2L, 3L));
|
||||
assertThat(mapper.map(ex5, LowerBounds.class).numbers).isEqualTo(List.of(1, 2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void errorMessageNamesPropertyWhoseConversionFailed() {
|
||||
var ex3 = module.getProperty("ex3");
|
||||
|
||||
var t =
|
||||
catchThrowable(
|
||||
() ->
|
||||
mapper.map(ex3, Types.parameterizedType(Pair.class, Integer.class, Integer.class)));
|
||||
|
||||
assertThat(t).isInstanceOf(ConversionException.class);
|
||||
assertThat(t.getMessage())
|
||||
.startsWith(
|
||||
"Error converting property `first` in Pkl object of type `Dynamic` "
|
||||
+ "to equally named constructor parameter in Java class "
|
||||
+ "`org.pkl.config.java.mapper."
|
||||
+ "PObjectToDataObjectTest$Pair`:");
|
||||
}
|
||||
|
||||
static class Person {
|
||||
final String name;
|
||||
final int age;
|
||||
final Set<Hobby> hobbies;
|
||||
final Address address;
|
||||
|
||||
Person(
|
||||
@Named("name") String name,
|
||||
@Named("age") int age,
|
||||
@Named("hobbies") Set<Hobby> hobbies,
|
||||
@Named("address") Address address) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.hobbies = hobbies;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof Person)) return false;
|
||||
|
||||
var other = (Person) obj;
|
||||
return name.equals(other.name)
|
||||
&& age == other.age
|
||||
&& hobbies.equals(other.hobbies)
|
||||
&& address.equals(other.address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name, age, hobbies, address);
|
||||
}
|
||||
}
|
||||
|
||||
static class PersonConstructoProperties {
|
||||
final String name;
|
||||
final int age;
|
||||
final Set<Hobby> hobbies;
|
||||
final Address address;
|
||||
|
||||
@ConstructorProperties({"name", "age", "hobbies", "address"})
|
||||
PersonConstructoProperties(String name, int age, Set<Hobby> hobbies, Address address) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.hobbies = hobbies;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof PersonConstructoProperties)) return false;
|
||||
|
||||
var other = (PersonConstructoProperties) obj;
|
||||
return name.equals(other.name)
|
||||
&& age == other.age
|
||||
&& hobbies.equals(other.hobbies)
|
||||
&& address.equals(other.address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name, age, hobbies, address);
|
||||
}
|
||||
}
|
||||
|
||||
static class Address {
|
||||
final String street;
|
||||
final int zip;
|
||||
|
||||
Address(@Named("street") String street, @Named("zip") int zip) {
|
||||
this.street = street;
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof Address)) return false;
|
||||
|
||||
var other = (Address) obj;
|
||||
return street.equals(other.street) && zip == other.zip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(street, zip);
|
||||
}
|
||||
}
|
||||
|
||||
enum Hobby {
|
||||
SWIMMING,
|
||||
SURFING,
|
||||
READING
|
||||
}
|
||||
|
||||
static class Pair<S, T> {
|
||||
final S first;
|
||||
final T second;
|
||||
|
||||
Pair(@Named("first") S first, @Named("second") T second) {
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof Pair)) return false;
|
||||
|
||||
var other = (Pair<?, ?>) obj;
|
||||
return first.equals(other.first) && second.equals(other.second);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(first, second);
|
||||
}
|
||||
}
|
||||
|
||||
static class UpperBounds {
|
||||
final List<? extends Number> numbers;
|
||||
|
||||
UpperBounds(@Named("numbers") List<? extends Number> numbers) {
|
||||
this.numbers = numbers;
|
||||
}
|
||||
}
|
||||
|
||||
static class LowerBounds {
|
||||
final List<? super Integer> numbers;
|
||||
|
||||
LowerBounds(@Named("numbers") List<? super Integer> numbers) {
|
||||
this.numbers = numbers;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.pkl.core.ModuleSource.text;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.config.java.ConfigEvaluator;
|
||||
|
||||
public class PObjectToInnerClassTest {
|
||||
@SuppressWarnings("InnerClassMayBeStatic")
|
||||
public class InnerConfig {
|
||||
final String text;
|
||||
|
||||
public InnerConfig(@Named("text") String text) {
|
||||
this.text = text;
|
||||
}
|
||||
}
|
||||
|
||||
// verify that a workaround for https://bugs.openjdk.java.net/browse/JDK-8025806 is in place
|
||||
// conversion to inner class is still expected to fail but with the usual `ConversionException`
|
||||
@Test
|
||||
public void attemptToConvertToInnerClassDoesNotFailWithIndexOutOfBoundsException() {
|
||||
try (var evaluator = ConfigEvaluator.preconfigured()) {
|
||||
var config =
|
||||
evaluator.evaluate(
|
||||
text("class Inner {\n" + " text: String = \"Bar\"\n" + "}\n" + "inner: Inner"));
|
||||
|
||||
assertThatExceptionOfType(ConversionException.class)
|
||||
.isThrownBy(() -> config.get("inner").as(InnerConfig.class));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.pkl.core.ModuleSource.modulePath;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.core.*;
|
||||
|
||||
public class PObjectToPObjectTest {
|
||||
private static final Evaluator evaluator = Evaluator.preconfigured();
|
||||
|
||||
private static final PModule module =
|
||||
evaluator.evaluate(modulePath("org/pkl/config/java/mapper/PObjectToPObjectTest.pkl"));
|
||||
|
||||
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
|
||||
|
||||
@AfterAll
|
||||
public static void afterAll() {
|
||||
evaluator.close();
|
||||
}
|
||||
|
||||
private PObject pigeon;
|
||||
private PObject parrot;
|
||||
|
||||
@BeforeEach
|
||||
public void before() {
|
||||
Map<String, Object> pigeonProps = Map.of("name", "pigeon", "age", 40L);
|
||||
pigeon = new PObject(PClassInfo.Dynamic, pigeonProps);
|
||||
|
||||
Map<String, Object> parrotProps = Map.of("name", "parrot", "age", 30L);
|
||||
parrot = new PObject(PClassInfo.Dynamic, parrotProps);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex1() {
|
||||
var ex1 = module.getProperty("ex1");
|
||||
|
||||
assertThat(mapper.map(ex1, PObject.class)).isEqualTo(pigeon);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex2() {
|
||||
var ex2 = module.getProperty("ex2");
|
||||
List<PObject> mapped = mapper.map(ex2, Types.listOf(PObject.class));
|
||||
|
||||
assertThat(mapped).containsExactly(pigeon, parrot);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
import static org.pkl.core.ModuleSource.modulePath;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.core.*;
|
||||
|
||||
public class PPairToPairTest {
|
||||
private static final Evaluator evaluator = Evaluator.preconfigured();
|
||||
|
||||
private static final PModule module =
|
||||
evaluator.evaluate(modulePath("org/pkl/config/java/mapper/PPairToPairTest.pkl"));
|
||||
|
||||
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
|
||||
|
||||
@AfterAll
|
||||
public static void afterAll() {
|
||||
evaluator.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex1() {
|
||||
var ex1 = module.getProperty("ex1");
|
||||
Pair<Integer, Duration> mapped = mapper.map(ex1, Types.pairOf(Integer.class, Duration.class));
|
||||
assertThat(mapped).isEqualTo(new Pair<>(1, new Duration(3.0, DurationUnit.SECONDS)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex2() {
|
||||
var ex2 = module.getProperty("ex2");
|
||||
Pair<PObject, PObject> mapped = mapper.map(ex2, Types.pairOf(PObject.class, PObject.class));
|
||||
|
||||
assertThat(mapped.getFirst().getProperties())
|
||||
.containsOnly(entry("name", "pigeon"), entry("age", 40L));
|
||||
|
||||
assertThat(mapped.getSecond().getProperties())
|
||||
.containsOnly(entry("name", "parrot"), entry("age", 30L));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.pkl.core.ModuleSource.modulePath;
|
||||
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.core.DataSizeUnit;
|
||||
import org.pkl.core.DurationUnit;
|
||||
import org.pkl.core.Evaluator;
|
||||
import org.pkl.core.PModule;
|
||||
|
||||
public class PStringToEnumTest {
|
||||
private static final Evaluator evaluator = Evaluator.preconfigured();
|
||||
|
||||
private static final PModule module =
|
||||
evaluator.evaluate(modulePath("org/pkl/config/java/mapper/PStringToEnumTest.pkl"));
|
||||
|
||||
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
|
||||
|
||||
@AfterAll
|
||||
public static void afterAll() {
|
||||
evaluator.close();
|
||||
}
|
||||
|
||||
public enum Hobby {
|
||||
READING,
|
||||
SWIMMING,
|
||||
COUCH_SURFING
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex1() {
|
||||
assertThat(mapper.map(module.getProperty("ex1"), Hobby.class)).isEqualTo(Hobby.COUCH_SURFING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex2() {
|
||||
assertThat(mapper.map(module.getProperty("ex2"), Hobby.class)).isEqualTo(Hobby.COUCH_SURFING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex3() {
|
||||
assertThat(mapper.map(module.getProperty("ex3"), Hobby.class)).isEqualTo(Hobby.COUCH_SURFING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex4() {
|
||||
assertThat(mapper.map(module.getProperty("ex4"), Hobby.class)).isEqualTo(Hobby.COUCH_SURFING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex5() {
|
||||
assertThat(mapper.map(module.getProperty("ex5"), Hobby.class)).isEqualTo(Hobby.COUCH_SURFING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex6() {
|
||||
assertThat(mapper.map(module.getProperty("ex6"), Hobby.class)).isEqualTo(Hobby.COUCH_SURFING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex7() {
|
||||
List<Hobby> mapped = mapper.map(module.getProperty("ex7"), Types.listOf(Hobby.class));
|
||||
|
||||
assertThat(mapped).containsExactly(Hobby.SWIMMING, Hobby.READING, Hobby.COUCH_SURFING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex8() {
|
||||
List<Hobby> mapped = mapper.map(module.getProperty("ex8"), Types.listOf(Hobby.class));
|
||||
|
||||
assertThat(mapped).containsExactly(Hobby.COUCH_SURFING, Hobby.COUCH_SURFING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex9() {
|
||||
var t = catchThrowable(() -> mapper.map(module.getProperty("ex9"), Types.listOf(Hobby.class)));
|
||||
|
||||
assertThat(t)
|
||||
.isInstanceOf(ConversionException.class)
|
||||
.hasMessage(
|
||||
"Cannot convert String `other` to Enum value "
|
||||
+ "of type `org.pkl.config.java.mapper.PStringToEnumTest$Hobby`.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex11() {
|
||||
var unit = mapper.map(module.getProperty("ex11"), DurationUnit.class);
|
||||
assertThat(unit).isEqualTo(DurationUnit.MINUTES);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex12() {
|
||||
var unit = mapper.map(module.getProperty("ex12"), DataSizeUnit.class);
|
||||
assertThat(unit).isEqualTo(DataSizeUnit.GIGABYTES);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.pkl.core.ModuleSource.modulePath;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.core.Evaluator;
|
||||
import org.pkl.core.PModule;
|
||||
import org.pkl.core.Version;
|
||||
|
||||
public class PStringToVersionTest {
|
||||
private static final Evaluator evaluator = Evaluator.preconfigured();
|
||||
|
||||
private static final PModule module =
|
||||
evaluator.evaluate(modulePath("org/pkl/config/java/mapper/PStringToVersionTest.pkl"));
|
||||
|
||||
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
|
||||
|
||||
@AfterAll
|
||||
public static void afterAll() {
|
||||
evaluator.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex1() {
|
||||
var ex1 = module.get("ex1");
|
||||
assert ex1 != null;
|
||||
var mapped = mapper.map(ex1, Version.class);
|
||||
assertThat(mapped).isEqualTo(Version.parse("1.2.3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex2() {
|
||||
var ex2 = module.get("ex2");
|
||||
assert ex2 != null;
|
||||
var mapped = mapper.map(ex2, Version.class);
|
||||
assertThat(mapped).isEqualTo(Version.parse("1.2.3-rc.1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex3() {
|
||||
var ex3 = module.get("ex3");
|
||||
assert ex3 != null;
|
||||
var mapped = mapper.map(ex3, Version.class);
|
||||
assertThat(mapped).isEqualTo(Version.parse("1.2.3+456.789"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex4() {
|
||||
var ex4 = module.get("ex4");
|
||||
assert ex4 != null;
|
||||
var mapped = mapper.map(ex4, Version.class);
|
||||
assertThat(mapped).isEqualTo(Version.parse("1.2.3-rc.1+456.789"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex5() {
|
||||
var ex5 = module.get("ex5");
|
||||
assertThatThrownBy(
|
||||
() -> {
|
||||
assert ex5 != null;
|
||||
mapper.map(ex5, Version.class);
|
||||
})
|
||||
.isInstanceOf(ConversionException.class)
|
||||
.hasCauseInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex6() {
|
||||
var ex6 = module.get("ex6");
|
||||
assertThatThrownBy(
|
||||
() -> {
|
||||
assert ex6 != null;
|
||||
mapper.map(ex6, Version.class);
|
||||
})
|
||||
.isInstanceOf(ConversionException.class)
|
||||
.hasCauseInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.pkl.core.ModuleSource.modulePath;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.core.Evaluator;
|
||||
import org.pkl.core.PModule;
|
||||
|
||||
public class PVersionToStringTest {
|
||||
private static final Evaluator evaluator = Evaluator.preconfigured();
|
||||
|
||||
private static final PModule module =
|
||||
evaluator.evaluate(modulePath("org/pkl/config/java/mapper/PVersionToVersionTest.pkl"));
|
||||
|
||||
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
|
||||
|
||||
@AfterAll
|
||||
public static void afterAll() {
|
||||
evaluator.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex1() {
|
||||
var ex1 = module.get("ex1");
|
||||
assert ex1 != null;
|
||||
var mapped = mapper.map(ex1, String.class);
|
||||
assertThat(mapped).isEqualTo("1.2.3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex2() {
|
||||
var ex2 = module.get("ex2");
|
||||
assert ex2 != null;
|
||||
var mapped = mapper.map(ex2, String.class);
|
||||
assertThat(mapped).isEqualTo("1.2.3-rc.1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex3() {
|
||||
var ex3 = module.get("ex3");
|
||||
assert ex3 != null;
|
||||
var mapped = mapper.map(ex3, String.class);
|
||||
assertThat(mapped).isEqualTo("1.2.3+456.789");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex4() {
|
||||
var ex4 = module.get("ex4");
|
||||
assert ex4 != null;
|
||||
var mapped = mapper.map(ex4, String.class);
|
||||
assertThat(mapped).isEqualTo("1.2.3-rc.1+456.789");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex5() {
|
||||
var ex5 = module.get("ex5");
|
||||
assert ex5 != null;
|
||||
var mapped = mapper.map(ex5, String.class);
|
||||
assertThat(mapped).isEqualTo("999999999999999.0.0");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.pkl.core.ModuleSource.modulePath;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pkl.core.*;
|
||||
|
||||
public class PVersionToVersionTest {
|
||||
private static final Evaluator evaluator = Evaluator.preconfigured();
|
||||
|
||||
private static final PModule module =
|
||||
evaluator.evaluate(modulePath("org/pkl/config/java/mapper/PVersionToVersionTest.pkl"));
|
||||
|
||||
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
|
||||
|
||||
@AfterAll
|
||||
public static void afterAll() {
|
||||
evaluator.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex1() {
|
||||
var ex1 = module.get("ex1");
|
||||
assert ex1 != null;
|
||||
var mapped = mapper.map(ex1, Version.class);
|
||||
assertThat(mapped).isEqualTo(Version.parse("1.2.3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex2() {
|
||||
var ex2 = module.get("ex2");
|
||||
assert ex2 != null;
|
||||
var mapped = mapper.map(ex2, Version.class);
|
||||
assertThat(mapped).isEqualTo(Version.parse("1.2.3-rc.1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex3() {
|
||||
var ex3 = module.get("ex3");
|
||||
assert ex3 != null;
|
||||
var mapped = mapper.map(ex3, Version.class);
|
||||
assertThat(mapped).isEqualTo(Version.parse("1.2.3+456.789"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex4() {
|
||||
var ex4 = module.get("ex4");
|
||||
assert ex4 != null;
|
||||
var mapped = mapper.map(ex4, Version.class);
|
||||
assertThat(mapped).isEqualTo(Version.parse("1.2.3-rc.1+456.789"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ex5() {
|
||||
var ex5 = module.get("ex5");
|
||||
assertThatThrownBy(
|
||||
() -> {
|
||||
assert ex5 != null;
|
||||
mapper.map(ex5, Version.class);
|
||||
})
|
||||
.isInstanceOf(ConversionException.class)
|
||||
.hasCauseInstanceOf(ArithmeticException.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import org.pkl.core.util.Nullable;
|
||||
|
||||
public class Person {
|
||||
public final String name;
|
||||
public final int age;
|
||||
|
||||
public Person(@Named("name") String name, @Named("age") int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof Person)) return false;
|
||||
|
||||
var other = (Person) obj;
|
||||
return name.equals(other.name) && age == other.age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode() * 31 + Integer.hashCode(age);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.pkl.config.java.mapper
|
||||
|
||||
import org.pkl.config.java.ConfigEvaluator
|
||||
import org.pkl.core.ModuleSource
|
||||
import com.example.Lib
|
||||
import com.example.PolymorphicModuleTest
|
||||
import com.example.PolymorphicModuleTest.Strudel
|
||||
import com.example.PolymorphicModuleTest.TurkishDelight
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class PolymorphicTest {
|
||||
@Test
|
||||
fun `deserializing polymorphic objects`() {
|
||||
val evaluator = ConfigEvaluator.preconfigured()
|
||||
val module = evaluator.evaluate(ModuleSource.modulePath("/codegenPkl/PolymorphicModuleTest.pkl")).`as`(PolymorphicModuleTest::class.java)
|
||||
assertThat(module.desserts[0]).isInstanceOf(Strudel::class.java)
|
||||
assertThat(module.desserts[1]).isInstanceOf(TurkishDelight::class.java)
|
||||
assertThat(module.planes[0]).isInstanceOf(Lib.Jet::class.java)
|
||||
assertThat(module.planes[1]).isInstanceOf(Lib.Propeller::class.java)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ReflectionTest {
|
||||
@SuppressWarnings("unused")
|
||||
static class Container<T> {
|
||||
Container(T element) {}
|
||||
|
||||
void setElement(T element) {}
|
||||
}
|
||||
|
||||
static class Person {}
|
||||
|
||||
@Test
|
||||
public void isMissingTypeArguments() {
|
||||
assertThat(
|
||||
Reflection.isMissingTypeArguments(
|
||||
Types.parameterizedType(Container.class, Person.class)))
|
||||
.isFalse();
|
||||
assertThat(Reflection.isMissingTypeArguments(Container.class)).isTrue();
|
||||
assertThat(Reflection.isMissingTypeArguments(Person.class)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toRawType() {
|
||||
var type = Types.listOf(Person.class);
|
||||
|
||||
assertThat(Reflection.toRawType(type)).isEqualTo(List.class);
|
||||
assertThat(Reflection.toRawType(List.class)).isEqualTo(List.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toWrapperType() {
|
||||
assertThat(Reflection.toWrapperType(float.class)).isEqualTo(Float.class);
|
||||
assertThat(Reflection.toWrapperType(Person.class)).isEqualTo(Person.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getArrayElementType() {
|
||||
assertThat(Reflection.getArrayElementType(int[].class)).isEqualTo(int.class);
|
||||
assertThat(Reflection.getArrayElementType(Person[].class)).isEqualTo(Person.class);
|
||||
|
||||
var containerOfPerson = Types.parameterizedType(Container.class, Person.class);
|
||||
assertThat(Reflection.getArrayElementType(Types.arrayOf(containerOfPerson)))
|
||||
.isEqualTo(containerOfPerson);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getExactSupertype() {
|
||||
assertThat(
|
||||
Reflection.getExactSupertype(
|
||||
Types.parameterizedType(ArrayList.class, Person.class), Collection.class))
|
||||
.isEqualTo(Types.parameterizedType(Collection.class, Person.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getExactSubtype() {
|
||||
assertThat(
|
||||
Reflection.getExactSubtype(
|
||||
Types.parameterizedType(Collection.class, Person.class), ArrayList.class))
|
||||
.isEqualTo(Types.parameterizedType(ArrayList.class, Person.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getExactParameterTypes() {
|
||||
var type = Types.parameterizedType(Container.class, Person.class);
|
||||
|
||||
var ctor = Container.class.getDeclaredConstructors()[0];
|
||||
assertThat(Reflection.getExactParameterTypes(ctor, type)).isEqualTo(new Type[] {Person.class});
|
||||
|
||||
var method = Container.class.getDeclaredMethods()[0];
|
||||
assertThat(Reflection.getExactParameterTypes(method, type))
|
||||
.isEqualTo(new Type[] {Person.class});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.pkl.config.java.mapper;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TypesTest {
|
||||
@Test
|
||||
public void createParameterizedType() {}
|
||||
|
||||
@Test
|
||||
public void createParameterizedTypeForClassWithoutTypeParameters() {
|
||||
var t = catchThrowable(() -> Types.parameterizedType(String.class));
|
||||
|
||||
assertThat(t)
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage(
|
||||
"Cannot parameterize `java.lang.String` "
|
||||
+ "because it does not have any type parameters.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createParameterizedTypeWithWrongNumberOfTypeArguments() {
|
||||
var t =
|
||||
catchThrowable(
|
||||
() -> Types.parameterizedType(Map.class, Integer.class, String.class, URL.class));
|
||||
|
||||
assertThat(t)
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("Expected 2 type arguments for `java.util.Map`, but got 3.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createParameterizedTypeWithPrimitiveTypeArgument() {
|
||||
Throwable t = catchThrowable(() -> Types.parameterizedType(List.class, int.class));
|
||||
|
||||
assertThat(t)
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("`int.class` is not a valid type argument. Did you mean `Integer.class`?");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static class Foo<T extends Bar> {}
|
||||
|
||||
static class Bar {}
|
||||
|
||||
@Test
|
||||
public void createParameterizedTypeWithIncompatibleTypeArgument() {
|
||||
Throwable t = catchThrowable(() -> Types.parameterizedType(Foo.class, String.class));
|
||||
|
||||
assertThat(t)
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage(
|
||||
"Type argument `java.lang.String` for type parameter `T` is "
|
||||
+ "not within bound `org.pkl.config.java.mapper.TypesTest$Bar`.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createPrimitiveArrayType() {
|
||||
assertThat(Types.arrayOf(int.class)).isEqualTo(int[].class);
|
||||
}
|
||||
|
||||
static class Person {}
|
||||
|
||||
@Test
|
||||
public void createObjectArrayType() {
|
||||
assertThat(Types.arrayOf(Person.class)).isEqualTo(Person[].class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createIterableType() {
|
||||
ParameterizedType type = Types.iterableOf(Person.class);
|
||||
assertThat(type.getRawType()).isEqualTo(Iterable.class);
|
||||
assertThat(type.getActualTypeArguments()).isEqualTo(new Type[] {Person.class});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createCollectionType() {
|
||||
ParameterizedType type = Types.collectionOf(Person.class);
|
||||
assertThat(type.getRawType()).isEqualTo(Collection.class);
|
||||
assertThat(type.getActualTypeArguments()).isEqualTo(new Type[] {Person.class});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createListType() {
|
||||
ParameterizedType type = Types.listOf(Person.class);
|
||||
assertThat(type.getRawType()).isEqualTo(List.class);
|
||||
assertThat(type.getActualTypeArguments()).isEqualTo(new Type[] {Person.class});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createSetType() {
|
||||
ParameterizedType type = Types.setOf(Person.class);
|
||||
assertThat(type.getRawType()).isEqualTo(Set.class);
|
||||
assertThat(type.getActualTypeArguments()).isEqualTo(new Type[] {Person.class});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createMapType() {
|
||||
ParameterizedType type = Types.mapOf(String.class, Person.class);
|
||||
assertThat(type.getRawType()).isEqualTo(Map.class);
|
||||
assertThat(type.getActualTypeArguments()).isEqualTo(new Type[] {String.class, Person.class});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
module com.example.OverriddenProperty
|
||||
|
||||
abstract class BaseClass {
|
||||
fixed bar: Listing<BaseBar> = new {
|
||||
new {
|
||||
prop1 = "hello"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
theClass: TheClass
|
||||
|
||||
class TheClass extends BaseClass {
|
||||
fixed bar: Listing<Bar> = new {
|
||||
new {
|
||||
prop1 = "hello"
|
||||
prop2 = "hello again"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
open class BaseBar {
|
||||
prop1: String
|
||||
}
|
||||
|
||||
class Bar extends BaseBar {
|
||||
prop2: String
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
module com.example.lib
|
||||
|
||||
open class Airplane {
|
||||
name: String
|
||||
numSeats: Int
|
||||
}
|
||||
|
||||
class Jet extends Airplane {
|
||||
isSuperSonic: Boolean
|
||||
}
|
||||
|
||||
class Propeller extends Airplane {
|
||||
isTurboprop: Boolean
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/// Gets generated into a Java via Gradle task `generateTestConfigClasses`.
|
||||
module com.example.PolymorphicModuleTest
|
||||
|
||||
import "PolymorphicLib.pkl"
|
||||
|
||||
abstract class Dessert
|
||||
|
||||
class Strudel extends Dessert {
|
||||
numberOfRolls: Int
|
||||
}
|
||||
|
||||
class TurkishDelight extends Dessert {
|
||||
isOfferedToEdmund: Boolean
|
||||
}
|
||||
|
||||
desserts: Listing<Dessert> = new {
|
||||
new Strudel { numberOfRolls = 3 }
|
||||
new TurkishDelight { isOfferedToEdmund = true }
|
||||
}
|
||||
|
||||
planes: Listing<PolymorphicLib.Airplane> = new {
|
||||
new PolymorphicLib.Jet { name = "Concorde"; numSeats = 128; isSuperSonic = true }
|
||||
new PolymorphicLib.Propeller { name = "Cessna 172"; numSeats = 4; isTurboprop = true }
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
ex1 = null
|
||||
ex2 = "str"
|
||||
ex3 = List(1, 2, 3)
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
ex1 = List()
|
||||
ex2 = List(1, 2, 3)
|
||||
ex3 = Set(1, 2, 3)
|
||||
ex4 = List(1, 2, 3.3)
|
||||
ex5 = List(true, false, true)
|
||||
ex6 = List(new Dynamic { name = "pigeon"; age = 40 }, new Dynamic { name = "parrot"; age = 30 })
|
||||
@@ -0,0 +1,6 @@
|
||||
ex1 = List()
|
||||
ex2 = List(1, 2, 3)
|
||||
ex3 = Set(1, 2, 3)
|
||||
ex4 = List(1, 2, 3.3)
|
||||
ex5 = List(true, false, true)
|
||||
ex6 = List(new Dynamic { name = "pigeon"; age = 40 }, new Dynamic { name = "parrot"; age = 30 })
|
||||
@@ -0,0 +1,7 @@
|
||||
ex1 = Map()
|
||||
ex2 = Map(1, 2, 2, 4, 3, 6)
|
||||
ex3 = Map(1, 2, 2, 4, 3, 6.6)
|
||||
ex4 = Map("pigeon", Map("name", "pigeon", "age", 40), "parrot", Map("name", "parrot", "age", 30))
|
||||
ex5 = Map("pigeon", new Dynamic { name = "pigeon"; age = 40 }, "parrot", new Dynamic { name = "parrot"; age = 30 })
|
||||
ex6 = Map(new Dynamic { name = "pigeon"; age = 40 }, "pigeon", new Dynamic { name = "parrot"; age = 30 }, "parrot")
|
||||
ex7 = Map(1, 2, 2, 4)
|
||||
@@ -0,0 +1,7 @@
|
||||
name = "pigeon"
|
||||
age = 40
|
||||
hobbies = List("swimming", "surfing")
|
||||
address {
|
||||
street = "sesame street"
|
||||
zip = 94105
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
module org.pkl.test.PObjectToDataObject
|
||||
|
||||
abstract class Thing {
|
||||
isThing: Boolean
|
||||
}
|
||||
|
||||
class Person extends Thing {
|
||||
name: String
|
||||
}
|
||||
|
||||
thing: Thing = new Person {
|
||||
name = "Bob"
|
||||
isThing = true
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
ex1 {
|
||||
name = "pigeon"
|
||||
age = 40
|
||||
hobbies = List("swimming", "surfing")
|
||||
address {
|
||||
street = "sesame street"
|
||||
zip = 94105
|
||||
}
|
||||
}
|
||||
|
||||
ex2 = new Person {
|
||||
name = "pigeon"
|
||||
age = 40
|
||||
hobbies = List("swimming", "surfing")
|
||||
address {
|
||||
street = "sesame street"
|
||||
zip = 94105
|
||||
}
|
||||
}
|
||||
|
||||
ex3 {
|
||||
first = "foo"
|
||||
second = 42
|
||||
}
|
||||
|
||||
ex4 {
|
||||
street = "sesame street"
|
||||
zipp = 94105 // intentional typo
|
||||
}
|
||||
|
||||
ex5 {
|
||||
numbers = List(1, 2, 3)
|
||||
}
|
||||
|
||||
class Person {
|
||||
name: String
|
||||
age: Int
|
||||
hobbies: List<String>
|
||||
address: Address
|
||||
}
|
||||
|
||||
class Address {
|
||||
street: String
|
||||
zip: Int
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
ex1 = new { name = "pigeon"; age = 40 }
|
||||
ex2 = List(new Dynamic { name = "pigeon"; age = 40 }, new Dynamic { name = "parrot"; age = 30 })
|
||||
@@ -0,0 +1,7 @@
|
||||
class Person {
|
||||
name: String
|
||||
age: Int
|
||||
}
|
||||
|
||||
ex1 = Pair(1, 3.s)
|
||||
ex2 = Pair(new Person { name = "pigeon"; age = 40 }, new Dynamic { name = "parrot"; age = 30 })
|
||||
@@ -0,0 +1,12 @@
|
||||
ex1 = "couch surfing"
|
||||
ex2 = "couch_surfing"
|
||||
ex3 = "COUCH SURFING"
|
||||
ex4 = "COUCH_SURFING"
|
||||
ex5 = "couchSurfing"
|
||||
ex6 = "couch Surfing"
|
||||
ex7 = List("swimming", "reading", "couch surfing")
|
||||
ex8 = List("couch surfing", "COUCH_SURFING")
|
||||
ex9 = List("couch surfing", "other")
|
||||
ex10 = "couchSurfing"
|
||||
ex11 = "min"
|
||||
ex12 = "gb"
|
||||
@@ -0,0 +1,8 @@
|
||||
import "pkl:semver"
|
||||
|
||||
ex1 = "1.2.3"
|
||||
ex2 = "1.2.3-rc.1"
|
||||
ex3 = "1.2.3+456.789"
|
||||
ex4 = "1.2.3-rc.1+456.789"
|
||||
ex5 = "999999999999999.0.0"
|
||||
ex6 = "not a version number"
|
||||
@@ -0,0 +1,7 @@
|
||||
import "pkl:semver"
|
||||
|
||||
ex1 = semver.Version("1.2.3")
|
||||
ex2 = semver.Version("1.2.3-rc.1")
|
||||
ex3 = semver.Version("1.2.3+456.789")
|
||||
ex4 = semver.Version("1.2.3-rc.1+456.789")
|
||||
ex5 = semver.Version("999999999999999.0.0")
|
||||
Reference in New Issue
Block a user