# How to test this function. Not sure how to formulate the expected result

#1

This function accept a string and a length to do the word wrap.
If I input ‘ABC,DEF’ as string and length as 3, I’m expecting
ABC
,DE
F

How do I check the expected result?

Thnaks
Vimal

FUNCTION word_wrap(i_string IN VARCHAR2,
i_length IN NUMBER DEFAULT 66) RETURN VARCHAR2 AS
l_string LONG DEFAULT REPLACE(i_string || chr(10), chr(9), ’ ');
l_result LONG DEFAULT NULL;
l_piece LONG;
l_ws VARCHAR2(25) DEFAULT ’ ’ || chr(9);
l_sep VARCHAR2(5) DEFAULT NULL;
n NUMBER;
BEGIN
LOOP
EXIT WHEN l_string IS NULL;

n := instr(l_string, chr(10));
l_piece := substr(l_string, 1, n - 1);
l_string := substr(l_string, n + 1);

LOOP
EXIT WHEN l_piece IS NULL;

n := length(l_piece);
IF (n > i_length)
THEN
n := instr(substr(translate(l_piece,
l_ws,
1,
i_length),
’ ',
-1);
IF (nvl(n, 0) = 0)
THEN
n := i_length;
END IF;
END IF;
l_result := l_result || l_sep || substr(l_piece, 1, n);
l_sep := chr(10);
l_piece := substr(l_piece, n + 1);
END LOOP;
END LOOP;
RETURN l_result;
END;

#2

Vimal,

Funny you should ask this! I ran into the same challenge myself a couple of days ago as I tested a new program (without_whitespace) in the qu_helper package (available in the Library).

To put carriage returns in your string, go to Test Builder and then the Outcome Grid. Click on the […] button next to the value in the Expected Results column. Then check “Single value or expression” if not already selected.

Next, check “Evaluate as an Expression”. Then if you click into the field, you will see a whole big multi-line field. So your expression is a string literal. Therefore, the first character is a single ', then you add all your text including new line characters, and close it with another '. As in:

‘abc
def
hij’

Alternatively, you can define a “real” expression as follows:

‘abc’ || chr(10) || ‘def’ || chr(10) || ‘hij’

Hope that helps…SF

#3