Vincent Bussol created JEXL-454:
-----------------------------------

             Summary: Switch NaN case not found
                 Key: JEXL-454
                 URL: https://issues.apache.org/jira/browse/JEXL-454
             Project: Commons JEXL
          Issue Type: Bug
    Affects Versions: 3.6.1
            Reporter: Vincent Bussol


The switch / case statement (or expression) does not find a NaN constant case.
{code:java}
let v = NaN;
switch (v) {
  case NaN:
    // not found
    break;
  case 1:
    // some code
} {code}
Without a default case, this example ends in error (no case in switch statement 
for: NaN).

A simple test:
{code:java}
@Test
void testSwitchNaN0() {
  // a one statement script
  final String src = "switch (x) {\n"
          + "  case NaN: return 'NotANumber';\n"
          + "  case 1: return 'one';\n"
          + "  case 2: return 'two';\n"
          + "  case 3: return 'three';\n"
          + "  default: return 'many';\n"
          + "}";
  final JexlEngine jexl = new JexlBuilder().create();
  final JexlScript script = jexl.createScript(src, "x");
  Object result = script.execute(null, Double.NaN);
  assertEquals("NotANumber", result);
  result = script.execute(null, 1);
  assertEquals("one", result);
  result = script.execute(null, 42);
  assertEquals("many", result);
} {code}
It appears that the switch node stores the index using the value Double.NaN and 
then looks it up using the value JexlParser.NAN (via the 'switchCode' method).



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to