Exemplo n.º 1
0
  public void testMid() {
    String testString = "Mid Function Demo";
    assertEquals("Mid", Vba.mid(testString, 1, 3));
    assertEquals("Demo", Vba.mid(testString, 14, 4));
    // It's OK if start+length = string.length
    assertEquals("Demo", Vba.mid(testString, 14, 5));
    // It's OK if start+length > string.length
    assertEquals("Demo", Vba.mid(testString, 14, 500));
    assertEquals("Function Demo", Vba.mid(testString, 5));
    assertEquals("o", Vba.mid("yahoo", 5, 1));

    // Start=0 illegal
    // Note: SSAS 2005 accepts start<=0, treating it as 1, therefore gives
    // different results. We favor the VBA spec over SSAS 2005.
    if (Bug.Ssas2005Compatible) {
      assertEquals("Mid Function Demo", Vba.mid(testString, 0));
      assertEquals("Mid Function Demo", Vba.mid(testString, -2));
      assertEquals("Mid Function Demo", Vba.mid(testString, -2, 5));
    } else {
      try {
        String s = Vba.mid(testString, 0);
        fail("expected error, got " + s);
      } catch (RuntimeException e) {
        assertMessage(
            e, "Invalid parameter. Start parameter of Mid function must " + "be positive");
      }
      // Start<0 illegal
      try {
        String s = Vba.mid(testString, -2);
        fail("expected error, got " + s);
      } catch (RuntimeException e) {
        assertMessage(
            e, "Invalid parameter. Start parameter of Mid function must " + "be positive");
      }
      // Start<0 illegal to 3 args version
      try {
        String s = Vba.mid(testString, -2, 5);
        fail("expected error, got " + s);
      } catch (RuntimeException e) {
        assertMessage(
            e, "Invalid parameter. Start parameter of Mid function must " + "be positive");
      }
    }

    // Length=0 OK
    assertEquals("", Vba.mid(testString, 14, 0));

    // Length<0 illegal
    // Note: SSAS 2005 accepts length<0, treating it as 0, therefore gives
    // different results. We favor the VBA spec over SSAS 2005.
    if (Bug.Ssas2005Compatible) {
      assertEquals("", Vba.mid(testString, 14, -1));
    } else {
      try {
        String s = Vba.mid(testString, 14, -1);
        fail("expected error, got " + s);
      } catch (RuntimeException e) {
        assertMessage(
            e, "Invalid parameter. Length parameter of Mid function must " + "be non-negative");
      }
    }
  }