Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@

import org.csstudio.apputil.formula.spi.FormulaFunction;
import org.epics.util.array.ListNumber;
import org.epics.vtype.Alarm;
import org.epics.vtype.Time;
import org.epics.vtype.VNumberArray;
import org.epics.vtype.VString;
import org.epics.vtype.VStringArray;
import org.epics.vtype.VType;
import org.epics.vtype.*;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -56,19 +51,22 @@ public VType compute(VType... args) throws Exception {
} else if (isString(arg))
{
stringBuilder.append(((VString)arg).getValue());
} else if (isEnum(arg))
{
stringBuilder.append(((VEnum)arg).getValue());
}
});
return VString.of(stringBuilder.toString(), Alarm.none(), Time.now());
}

/**
* Returns true is the value is a StringArray or can be converted to a StringArray
* @param value
* @param value
* @return boolean true if value can be used as a string array
*/
private boolean isStringArray(VType value)
{
return value instanceof VStringArray
return value instanceof VStringArray
|| value instanceof VNumberArray;
}

Expand All @@ -93,4 +91,9 @@ private boolean isString(VType value)
{
return value instanceof VString;
}

private boolean isEnum(VType value)
{
return value instanceof VEnum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.csstudio.apputil.formula.string;

import org.epics.util.array.ArrayDouble;
import org.epics.vtype.*;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;

class StringConcatFunctionTest {

@Test
void concatStrings() throws Exception {
StringConcatFunction concatFunction = new StringConcatFunction();

VString a = VString.of("a", Alarm.none(), Time.now());
VString b = VString.of("b", Alarm.none(), Time.now());
VString c = VString.of("c", Alarm.none(), Time.now());

VString res = (VString) concatFunction.compute(a,b,c);
assertEquals("abc", res.getValue());
}

@Test
void concatStringArray() throws Exception {
StringConcatFunction concatFunction = new StringConcatFunction();

VType array = VStringArray.of(Arrays.asList("a", "b", "c"), Alarm.none(), Time.now());

VString res = (VString) concatFunction.compute(array);
assertEquals("abc", res.getValue());
}

@Test
void concatDoubleArray() throws Exception {
StringConcatFunction concatFunction = new StringConcatFunction();

VType array = VNumberArray.of(ArrayDouble.of(1.0, 2.0, 3.0), Alarm.none(), Time.now(), Display.none());

VString res = (VString) concatFunction.compute(array);
assertEquals("1.02.03.0", res.getValue());
}

@Test
void concatInvalidVType() throws Exception {
StringConcatFunction concatFunction = new StringConcatFunction();

VType num1 = VNumber.of(1.0, Alarm.none(), Time.now(), Display.none());
VType num2 = VNumber.of(2.0, Alarm.none(), Time.now(), Display.none());
VType num3 = VNumber.of(3.0, Alarm.none(), Time.now(), Display.none());

VString res = (VString) concatFunction.compute(num1, num2, num3);
// Will not attempt to concat and will return empty string
assertEquals("", res.getValue());
}

@Test
void concatEnums() throws Exception {
StringConcatFunction concatFunction = new StringConcatFunction();

VEnum enum1 = VEnum.of(0, EnumDisplay.of("a", "b", "c"), Alarm.none(), Time.now());
VEnum enum2 = VEnum.of(1, EnumDisplay.of("a", "b", "c"), Alarm.none(), Time.now());
VEnum enum3 = VEnum.of(2, EnumDisplay.of("a", "b", "c"), Alarm.none(), Time.now());

VString res = (VString) concatFunction.compute(enum1, enum2, enum3);
assertEquals("abc", res.getValue());
}
}
Loading