Skip to content

Commit 726b4fe

Browse files
committed
Option specs and fixes
PullRequest: truffleruby/647
2 parents 9cf8836 + df2a5a0 commit 726b4fe

File tree

10 files changed

+153
-12
lines changed

10 files changed

+153
-12
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
slow:Options have a default
2+
slow:Options can be set explicitly back to their default value
3+
slow:Options can be set explicitly to a value
4+
slow:Options can be set explicitly to an implicit value
5+
slow:Options can be set using a simple referenced option
6+
slow:Options can be set using a negated referenced option
7+
slow:Options take an explicit value over a modified referenced option
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
slow:Options can correctly parse booleans
2+
slow:Options can correctly parse integers
3+
slow:Options can correctly parse enum values
4+
slow:Options can correctly parse strings
5+
slow:Options can correctly parse arrays of strings
6+
slow:Options handles parsing errors with booleans
7+
slow:Options handles parsing errors with integers
8+
slow:Options handles parsing errors with enum values

spec/truffle/options/defaults_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. This
2+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3+
# redistribute it and/or modify it under the terms of the:
4+
#
5+
# Eclipse Public License version 1.0, or
6+
# GNU General Public License version 2, or
7+
# GNU Lesser General Public License version 2.1.
8+
9+
require_relative '../../ruby/spec_helper'
10+
11+
describe "Options" do
12+
13+
it "have a default" do
14+
ruby_exe("p Truffle::Boot.get_option('dispatch.cache')").should == "8\n"
15+
ruby_exe("p Truffle::Boot.get_option('polyglot.stdio')").should == "false\n"
16+
end
17+
18+
it "can be set explicitly back to their default value" do
19+
ruby_exe("p Truffle::Boot.get_option('dispatch.cache')", options: "--dispatch.cache=8").should == "8\n"
20+
ruby_exe("p Truffle::Boot.get_option('polyglot.stdio')", options: "--polyglot.stdio=false").should == "false\n"
21+
end
22+
23+
it "can be set explicitly to a value" do
24+
ruby_exe("p Truffle::Boot.get_option('dispatch.cache')", options: "--dispatch.cache=99").should == "99\n"
25+
ruby_exe("p Truffle::Boot.get_option('polyglot.stdio')", options: "--polyglot.stdio=true").should == "true\n"
26+
end
27+
28+
it "can be set explicitly to an implicit value" do
29+
ruby_exe("p Truffle::Boot.get_option('polyglot.stdio')", options: "--polyglot.stdio").should == "true\n"
30+
end
31+
32+
it "can be set using a simple referenced option" do
33+
ruby_exe("p Truffle::Boot.get_option('dispatch.cache')", options: "--default_cache=99").should == "99\n"
34+
ruby_exe("p Truffle::Boot.get_option('polyglot.stdio')", options: "--embedded").should == "true\n"
35+
end
36+
37+
it "can be set using a negated referenced option" do
38+
ruby_exe("p Truffle::Boot.get_option('polyglot.stdio')", options: "--platform.native=false").should == "true\n"
39+
end
40+
41+
it "take an explicit value over a modified referenced option" do
42+
ruby_exe("p Truffle::Boot.get_option('dispatch.cache')", options: "--default_cache=101 --dispatch.cache=99").should == "99\n"
43+
ruby_exe("p Truffle::Boot.get_option('dispatch.cache')", options: "--dispatch.cache=99 --default_cache=101").should == "99\n"
44+
ruby_exe("p Truffle::Boot.get_option('polyglot.stdio')", options: "--embedded --polyglot.stdio=false").should == "false\n"
45+
ruby_exe("p Truffle::Boot.get_option('polyglot.stdio')", options: "--polyglot.stdio=false --embedded").should == "false\n"
46+
end
47+
48+
end

spec/truffle/options/parsing_spec.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. This
2+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3+
# redistribute it and/or modify it under the terms of the:
4+
#
5+
# Eclipse Public License version 1.0, or
6+
# GNU General Public License version 2, or
7+
# GNU Lesser General Public License version 2.1.
8+
9+
require_relative '../../ruby/spec_helper'
10+
11+
describe "Options" do
12+
13+
describe "can correctly parse" do
14+
15+
it "booleans" do
16+
ruby_exe("p Truffle::Boot.get_option('frozen_string_literals')").should_not == "true\n"
17+
ruby_exe("p Truffle::Boot.get_option('frozen_string_literals')", options: "--frozen_string_literals=true").should == "true\n"
18+
ruby_exe("p Truffle::Boot.get_option('frozen_string_literals')", options: "--frozen_string_literals").should == "true\n"
19+
end
20+
21+
it "integers" do
22+
ruby_exe("p Truffle::Boot.get_option('default_cache')").should_not == "99\n"
23+
ruby_exe("p Truffle::Boot.get_option('default_cache')", options: "--default_cache=99").should == "99\n"
24+
end
25+
26+
it "enum values" do
27+
ruby_exe("p Truffle::Boot.get_option('verbosity')").should_not == ":NIL\n"
28+
ruby_exe("p Truffle::Boot.get_option('verbosity')", options: "--verbosity=NIL").should == ":NIL\n"
29+
end
30+
31+
it "strings" do
32+
ruby_exe("p Truffle::Boot.get_option('launcher')", options: "--launcher=ruby_spec_test_value").should == "\"ruby_spec_test_value\"\n"
33+
ruby_exe("p Truffle::Boot.get_option('launcher')", options: "'--launcher=ruby spec test value with spaces'").should == "\"ruby spec test value with spaces\"\n"
34+
end
35+
36+
it "arrays of strings" do
37+
ruby_exe("p Truffle::Boot.get_option('load_paths')", options: "--load_paths=ruby_spec_test_value").should == "[\"ruby_spec_test_value\"]\n"
38+
ruby_exe("p Truffle::Boot.get_option('load_paths')", options: "--load_paths=a,b,c").should == "[\"a\", \"b\", \"c\"]\n"
39+
ruby_exe("p Truffle::Boot.get_option('load_paths')", options: "--load_paths=a\\\\,b,c").should == "[\"a,b\", \"c\"]\n"
40+
end
41+
42+
end
43+
44+
describe "handles parsing errors with" do
45+
46+
it "booleans" do
47+
ruby_exe("14", options: "--frozen_string_literals=foo", args: "2>&1").should include("Invalid boolean option value 'foo'")
48+
end
49+
50+
it "integers" do
51+
ruby_exe("14", options: "--default_cache=foo", args: "2>&1").should include("Invalid argument --default_cache=foo specified")
52+
end
53+
54+
it "enum values" do
55+
ruby_exe("14", options: "--verbosity=foo", args: "2>&1").should include("Invalid argument --verbosity=foo specified. No enum constant org.truffleruby.shared.options.Verbosity.FOO'")
56+
end
57+
58+
end
59+
60+
end

src/launcher/java/org/truffleruby/launcher/options/CommandLineException.java renamed to src/launcher/java/org/truffleruby/launcher/CommandLineException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* the provisions above, a recipient may use your version of this file under
2525
* the terms of any one of the EPL, the GPL or the LGPL.
2626
***** END LICENSE BLOCK *****/
27-
package org.truffleruby.launcher.options;
27+
package org.truffleruby.launcher;
2828

2929
public class CommandLineException extends Exception {
3030

src/launcher/java/org/truffleruby/launcher/options/CommandLineParser.java renamed to src/launcher/java/org/truffleruby/launcher/CommandLineParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* the provisions above, a recipient may use your version of this file under
3333
* the terms of any one of the EPL, the GPL or the LGPL.
3434
*/
35-
package org.truffleruby.launcher.options;
35+
package org.truffleruby.launcher;
3636

3737
import org.truffleruby.shared.options.CommandLineOptions;
3838
import org.truffleruby.shared.options.DefaultExecutionAction;

src/launcher/java/org/truffleruby/launcher/RubyLauncher.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
import org.graalvm.polyglot.Context;
1616
import org.graalvm.polyglot.PolyglotException;
1717
import org.graalvm.polyglot.Source;
18-
import org.truffleruby.launcher.options.CommandLineException;
1918
import org.truffleruby.shared.options.CommandLineOptions;
20-
import org.truffleruby.launcher.options.CommandLineParser;
2119
import org.truffleruby.shared.options.DefaultExecutionAction;
2220
import org.truffleruby.shared.options.ExecutionAction;
2321
import org.truffleruby.shared.options.OptionsCatalog;

src/main/java/org/truffleruby/language/RubyBaseNode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ protected DynamicObject createArray(Object store, int size) {
6262
return ArrayHelpers.createArray(getContext(), store, size);
6363
}
6464

65+
protected DynamicObject createArray(Object[] store) {
66+
return createArray(store, store.length);
67+
}
68+
6569
protected DynamicObject createBignum(BigInteger value) {
6670
return BignumOperations.createBignum(getContext(), value);
6771
}

src/main/java/org/truffleruby/language/TruffleBootNodes.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,18 +337,35 @@ public abstract static class GetOptionNode extends CoreMethodArrayArgumentsNode
337337
@TruffleBoundary
338338
@Specialization(guards = "isRubyString(optionName)")
339339
public Object getOption(DynamicObject optionName) {
340-
final OptionDescription<?> description = OptionsCatalog.fromName("ruby." + StringOperations.getString(optionName));
341-
assert description != null;
340+
final String optionNameString = StringOperations.getString(optionName);
341+
342+
final OptionDescription<?> description = OptionsCatalog.fromName("ruby." + optionNameString);
343+
344+
if (description == null) {
345+
throw new RaiseException(getContext(), coreExceptions().nameError("option not defined", nil(), optionNameString, this));
346+
}
347+
342348
final Object value = getContext().getOptions().fromDescription(description);
343349

344-
if (value instanceof String) {
345-
return makeStringNode.executeMake(value, UTF8Encoding.INSTANCE, CodeRange.CR_UNKNOWN);
350+
if (value instanceof Boolean || value instanceof Integer) {
351+
return value;
346352
} else if (value instanceof Enum) {
347353
return getSymbol(((Enum<?>) value).name());
354+
} else if (value instanceof String) {
355+
return makeStringNode.executeMake(value, UTF8Encoding.INSTANCE, CodeRange.CR_UNKNOWN);
356+
} else if (value instanceof String[]) {
357+
return toRubyArray((String[]) value);
348358
} else {
349-
assert value instanceof Integer || value instanceof Boolean;
350-
return value;
359+
throw new UnsupportedOperationException();
360+
}
361+
}
362+
363+
private DynamicObject toRubyArray(String[] strings) {
364+
final Object[] objects = new Object[strings.length];
365+
for (int n = 0; n < strings.length; n++) {
366+
objects[n] = makeStringNode.executeMake(strings[n], UTF8Encoding.INSTANCE, CodeRange.CR_UNKNOWN);
351367
}
368+
return createArray(objects);
352369
}
353370

354371
}

src/shared/java/org/truffleruby/shared/TruffleRuby.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* GNU General Public License version 2, or
88
* GNU Lesser General Public License version 2.1.
99
*/
10-
1110
package org.truffleruby.shared;
1211

1312
import org.graalvm.polyglot.Engine;
@@ -66,7 +65,7 @@ public static String getEngineVersion() {
6665
return systemVersion + "-" + BuildInformationImpl.INSTANCE.getRevision();
6766
}
6867

69-
7068
return systemVersion;
7169
}
70+
7271
}

0 commit comments

Comments
 (0)