use of org.graalvm.options.OptionDescriptors in project graal by oracle.
the class LanguageSPITest method testLazyOptionInit.
@Test
@SuppressWarnings("all")
public void testLazyOptionInit() {
AtomicInteger getOptionDescriptors = new AtomicInteger(0);
AtomicInteger iterator = new AtomicInteger(0);
AtomicInteger get = new AtomicInteger(0);
ProxyLanguage.setDelegate(new ProxyLanguage() {
final OptionKey<String> option = new OptionKey<>("");
final List<OptionDescriptor> descriptors;
{
descriptors = new ArrayList<>();
descriptors.add(OptionDescriptor.newBuilder(option, ProxyLanguage.ID + ".option").build());
}
@Override
protected OptionDescriptors getOptionDescriptors() {
getOptionDescriptors.incrementAndGet();
return new OptionDescriptors() {
public Iterator<OptionDescriptor> iterator() {
iterator.incrementAndGet();
return descriptors.iterator();
}
public OptionDescriptor get(String optionName) {
get.incrementAndGet();
for (OptionDescriptor optionDescriptor : descriptors) {
if (optionDescriptor.getName().equals(optionName)) {
return optionDescriptor;
}
}
return null;
}
};
}
});
Context c = Context.create();
c.close();
assertEquals(0, getOptionDescriptors.get());
assertEquals(0, iterator.get());
assertEquals(0, get.get());
c = Context.newBuilder(ProxyLanguage.ID).option(ProxyLanguage.ID + ".option", "foobar").build();
assertEquals(1, getOptionDescriptors.get());
assertEquals(1, get.get());
boolean assertionsEnabled = false;
assert assertionsEnabled = true;
if (assertionsEnabled) {
// with assertions enabled we assert the options.
assertEquals(1, iterator.get());
} else {
// for valid options we should not need the iterator
assertEquals(0, iterator.get());
}
c.close();
// test lazyness when using meta-data
getOptionDescriptors.set(0);
iterator.set(0);
get.set(0);
c = Context.create();
OptionDescriptors descriptors = c.getEngine().getLanguages().get(ProxyLanguage.ID).getOptions();
assertEquals(1, getOptionDescriptors.get());
if (assertionsEnabled) {
// with assertions enabled we assert the options.
assertEquals(1, iterator.get());
} else {
// for valid options we should not need the iterator
assertEquals(0, iterator.get());
}
assertEquals(0, get.get());
for (OptionDescriptor descriptor : descriptors) {
}
assertEquals(1, getOptionDescriptors.get());
if (assertionsEnabled) {
// with assertions enabled we assert the options.
assertEquals(2, iterator.get());
} else {
// for valid options we should not need the iterator
assertEquals(1, iterator.get());
}
assertEquals(0, get.get());
assertNotNull(descriptors.get(ProxyLanguage.ID + ".option"));
assertEquals(1, getOptionDescriptors.get());
assertEquals(1, get.get());
c.close();
}
use of org.graalvm.options.OptionDescriptors in project graal by oracle.
the class OptionProcessorTest method testOptionsInstrument.
@Test
public void testOptionsInstrument() {
Engine engine = Engine.create();
OptionDescriptors descriptors = engine.getInstruments().get("optiontestinstr1").getOptions();
OptionDescriptor descriptor;
OptionDescriptor descriptor1;
OptionDescriptor descriptor2;
descriptor1 = descriptor = descriptors.get("optiontestinstr1.StringOption1");
assertNotNull(descriptor);
assertTrue(descriptor.isDeprecated());
assertSame(OptionCategory.USER, descriptor.getCategory());
assertEquals("StringOption1 help", descriptor.getHelp());
assertSame(OptionTestInstrument1.StringOption1, descriptor.getKey());
descriptor2 = descriptor = descriptors.get("optiontestinstr1.StringOption2");
assertNotNull(descriptor);
assertEquals("StringOption2 help", descriptor.getHelp());
assertFalse(descriptor.isDeprecated());
assertSame(OptionCategory.EXPERT, descriptor.getCategory());
assertSame(OptionTestInstrument1.StringOption2, descriptor.getKey());
Iterator<OptionDescriptor> iterator = descriptors.iterator();
assertTrue(iterator.hasNext());
assertEquals(descriptor1, iterator.next());
assertTrue(iterator.hasNext());
assertEquals(descriptor2, iterator.next());
assertFalse(iterator.hasNext());
assertNull(descriptors.get("optiontestinstr1.StringOption3"));
}
use of org.graalvm.options.OptionDescriptors in project graal by oracle.
the class OptionProcessorTest method testTestLang.
@Test
public void testTestLang() {
Engine engine = Engine.create();
OptionDescriptors descriptors = engine.getLanguages().get("optiontestlang1").getOptions();
OptionDescriptor descriptor;
OptionDescriptor descriptor1;
OptionDescriptor descriptor2;
descriptor1 = descriptor = descriptors.get("optiontestlang1.StringOption1");
assertNotNull(descriptor);
assertTrue(descriptor.isDeprecated());
assertSame(OptionCategory.USER, descriptor.getCategory());
assertEquals("StringOption1 help", descriptor.getHelp());
assertSame(OptionTestLang1.StringOption1, descriptor.getKey());
descriptor2 = descriptor = descriptors.get("optiontestlang1.StringOption2");
assertNotNull(descriptor);
assertEquals("StringOption2 help", descriptor.getHelp());
assertFalse(descriptor.isDeprecated());
assertSame(OptionCategory.EXPERT, descriptor.getCategory());
assertSame(OptionTestLang1.StringOption2, descriptor.getKey());
Iterator<OptionDescriptor> iterator = descriptors.iterator();
assertTrue(iterator.hasNext());
assertEquals(descriptor1, iterator.next());
assertTrue(iterator.hasNext());
assertEquals(descriptor2, iterator.next());
assertFalse(iterator.hasNext());
assertNull(descriptors.get("optiontestlang1.StringOption3"));
}
use of org.graalvm.options.OptionDescriptors in project graal by oracle.
the class OptionProcessorTest method testOptionValues.
@Test
public void testOptionValues() {
Engine engine = Engine.create();
OptionDescriptors descriptors = engine.getInstruments().get("optiontestinstr1").getOptions();
OptionValues optionValues = engine.getInstruments().get("optiontestinstr1").lookup(OptionValues.class);
assertSame(descriptors, optionValues.getDescriptors());
assertFalse(optionValues.hasSetOptions());
OptionKey<?> optionKey1 = descriptors.get("optiontestinstr1.StringOption1").getKey();
OptionKey<?> optionKey2 = descriptors.get("optiontestinstr1.StringOption2").getKey();
assertFalse(optionValues.hasBeenSet(optionKey1));
assertEquals("defaultValue", optionValues.get(optionKey1));
assertEquals("defaultValue", optionValues.get(optionKey2));
engine = Engine.newBuilder().option("optiontestinstr1.StringOption1", "test").build();
optionValues = engine.getInstruments().get("optiontestinstr1").lookup(OptionValues.class);
assertTrue(optionValues.hasSetOptions());
optionKey1 = descriptors.get("optiontestinstr1.StringOption1").getKey();
optionKey2 = descriptors.get("optiontestinstr1.StringOption2").getKey();
assertTrue(optionValues.hasBeenSet(optionKey1));
assertFalse(optionValues.hasBeenSet(optionKey2));
assertEquals("test", optionValues.get(optionKey1));
assertEquals("defaultValue", optionValues.get(optionKey2));
engine = Engine.newBuilder().option("optiontestlang1.StringOption1", "testLang").build();
optionValues = engine.getInstruments().get("optiontestinstr1").lookup(OptionValues.class);
// A language option was set, not the instrument one. Instrument sees no option set:
assertFalse(optionValues.hasSetOptions());
}
Aggregations