You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestTFunc.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula.functions;
  16. import junit.framework.TestCase;
  17. import org.apache.poi.ss.formula.eval.*;
  18. /**
  19. * Test cases for Excel function T()
  20. *
  21. * @author Josh Micich
  22. */
  23. public final class TestTFunc extends TestCase {
  24. /**
  25. * @return the result of calling function T() with the specified argument
  26. */
  27. private static ValueEval invokeT(ValueEval arg) {
  28. ValueEval[] args = { arg, };
  29. ValueEval result = new T().evaluate(args, -1, (short)-1);
  30. assertNotNull("result may never be null", result);
  31. return result;
  32. }
  33. /**
  34. * Simulates call: T(A1)
  35. * where cell A1 has the specified innerValue
  36. */
  37. private ValueEval invokeTWithReference(ValueEval innerValue) {
  38. ValueEval arg = EvalFactory.createRefEval("$B$2", innerValue);
  39. return invokeT(arg);
  40. }
  41. private static void confirmText(String text) {
  42. ValueEval arg = new StringEval(text);
  43. ValueEval eval = invokeT(arg);
  44. StringEval se = (StringEval) eval;
  45. assertEquals(text, se.getStringValue());
  46. }
  47. public void testTextValues() {
  48. confirmText("abc");
  49. confirmText("");
  50. confirmText(" ");
  51. confirmText("~");
  52. confirmText("123");
  53. confirmText("TRUE");
  54. }
  55. private static void confirmError(ValueEval arg) {
  56. ValueEval eval = invokeT(arg);
  57. assertTrue(arg == eval);
  58. }
  59. public void testErrorValues() {
  60. confirmError(ErrorEval.VALUE_INVALID);
  61. confirmError(ErrorEval.NA);
  62. confirmError(ErrorEval.REF_INVALID);
  63. }
  64. private static void confirmString(ValueEval eval, String expected) {
  65. assertTrue(eval instanceof StringEval);
  66. assertEquals(expected, ((StringEval)eval).getStringValue());
  67. }
  68. private static void confirmOther(ValueEval arg) {
  69. ValueEval eval = invokeT(arg);
  70. confirmString(eval, "");
  71. }
  72. public void testOtherValues() {
  73. confirmOther(new NumberEval(2));
  74. confirmOther(BoolEval.FALSE);
  75. confirmOther(BlankEval.instance); // can this particular case be verified?
  76. }
  77. public void testRefValues() {
  78. ValueEval eval;
  79. eval = invokeTWithReference(new StringEval("def"));
  80. confirmString(eval, "def");
  81. eval = invokeTWithReference(new StringEval(" "));
  82. confirmString(eval, " ");
  83. eval = invokeTWithReference(new NumberEval(2));
  84. confirmString(eval, "");
  85. eval = invokeTWithReference(BoolEval.TRUE);
  86. confirmString(eval, "");
  87. eval = invokeTWithReference(ErrorEval.NAME_INVALID);
  88. assertTrue(eval == ErrorEval.NAME_INVALID);
  89. }
  90. public void testAreaArg() {
  91. ValueEval[] areaValues = new ValueEval[] {
  92. new StringEval("abc"), new StringEval("def"),
  93. new StringEval("ghi"), new StringEval("jkl"),
  94. };
  95. AreaEval ae = EvalFactory.createAreaEval("C10:D11", areaValues);
  96. ValueEval ve = invokeT(ae);
  97. confirmString(ve, "abc");
  98. areaValues[0] = new NumberEval(5.0);
  99. ve = invokeT(ae);
  100. confirmString(ve, "");
  101. }
  102. }