3030import static org .assertj .core .api .Assertions .*;
3131
3232
33- public class VTLSubstrTest implements VTLFunctionTest {
33+ public class VTLSubstrTest {
3434
3535 private VTLSubstr vtlSubstr ;
36+ private VTLObject <?> result ;
3637
3738 @ Before
3839 public void setUp () throws Exception {
3940 vtlSubstr = VTLSubstr .getInstance ();
4041 }
4142
4243 @ Test
43- public void testInvokeWithPositiveArgs () throws Exception {
44- VTLObject <?> result = vtlSubstr .invoke (
44+ public void testNonNegativeArgs () throws Exception {
45+ result = vtlSubstr .invoke (
4546 createArguments ("Hello, world!" , 2 , 5 )
4647 );
4748 assertThat (result ).isNotNull ();
@@ -52,25 +53,35 @@ public void testInvokeWithPositiveArgs() throws Exception {
5253 );
5354 assertThat (result ).isNotNull ();
5455 assertThat (result ).isEqualTo (VTLString .of ("Hell" ));
56+ }
5557
58+ @ Test
59+ public void testEmptyInputString () throws Exception {
5660 result = vtlSubstr .invoke (
5761 createArguments ("" , 0 , 4 )
5862 );
5963 assertThat (result ).isNotNull ();
6064 //null in string operations is considered an empty string ("")
61- assertThat (result ).isEqualTo (VTLString .of ((String )null ));
65+ assertThat (result ).isEqualTo (VTLString .of ((String ) null ));
6266 assertThat (result ).isEqualTo (VTLString .of ("" ));
6367 assertThat (result .get ()).isNull ();
68+ }
6469
65- //deviation from the VTL specification 1.1: return empty string
66- //if start position equal or greater than the whole length of the input string
70+ /**
71+ * Deviation from the VTL specification 1.1: return empty string
72+ * if start position equal or greater than the whole length of the input string.
73+ */
74+ @ Test
75+ public void testStartPositionGreaterThanInputStringLength () throws Exception {
6776 result = vtlSubstr .invoke (
6877 createArguments ("Hello" , 6 , 2 )
6978 );
7079 assertThat (result ).isNotNull ();
7180 assertThat (result ).isEqualTo (VTLString .of ("" ));
81+ }
7282
73- //length argument will be ignored
83+ @ Test
84+ public void testIgnoreLengthArgument () throws Exception {
7485 result = vtlSubstr .invoke (
7586 createArguments ("Hello" , 3 , 3 )
7687 );
@@ -79,14 +90,17 @@ public void testInvokeWithPositiveArgs() throws Exception {
7990 }
8091
8192 @ Test
82- public void testInvokeWithNegativeArgs () throws Exception {
93+ public void testStartPositionArgNegative () throws Exception {
8394 assertThatThrownBy (() -> vtlSubstr .invoke (
8495 createArguments ("Hello, world!" , -1 , 4 )
8596 ))
8697 .as ("exception when passing -1 as start position" )
8798 .hasMessage ("Argument{name=startPosition, type=VTLInteger} must be greater than zero, was -1" )
8899 .isExactlyInstanceOf (IllegalArgumentException .class );
100+ }
89101
102+ @ Test
103+ public void testLengthArgNegative () throws Exception {
90104 assertThatThrownBy (() -> vtlSubstr .invoke (
91105 createArguments ("Hello, world!" , 1 , -1 )
92106 ))
@@ -96,7 +110,6 @@ public void testInvokeWithNegativeArgs() throws Exception {
96110 }
97111
98112 @ Test
99- @ Override
100113 public void testInvokeWithTooManyArguments () throws Exception {
101114 assertThatThrownBy (() -> vtlSubstr .invoke (
102115 Lists .newArrayList (
@@ -112,7 +125,6 @@ public void testInvokeWithTooManyArguments() throws Exception {
112125 }
113126
114127 @ Test
115- @ Override
116128 public void testInvokeWithEmptyArgumentsList () throws Exception {
117129 assertThatThrownBy (() -> vtlSubstr .invoke (
118130 Lists .emptyList ()
@@ -123,36 +135,40 @@ public void testInvokeWithEmptyArgumentsList() throws Exception {
123135 }
124136
125137 @ Test
126- @ Override
127- public void testInvokeWithNullValue () throws Exception {
138+ public void testInputStringIsNull () throws Exception {
128139 VTLObject <?> result = vtlSubstr .invoke (
129140 createArguments (null , 0 , 4 )
130141 );
131142
132143 assertThat (result ).isEqualTo (VTLString .of ((String ) null ));
144+ }
133145
134- result = vtlSubstr .invoke (
135- Lists .newArrayList (
136- VTLString .of ("Hello, world!" ),
137- VTLNumber .of (0 ),
138- VTLNumber .of ((Long )null )
139- )
140- );
141-
142- assertThat (result ).isEqualTo (VTLString .of ("Hello, world!" ));
143-
146+ @ Test
147+ public void testStartPositionArgIsNull () throws Exception {
144148 assertThatThrownBy (() -> vtlSubstr .invoke (
145149 Lists .newArrayList (
146150 VTLString .of ("Hello, world!" ),
147151 VTLNumber .of ((Long ) null ),
148152 VTLNumber .of (2 )
149153 )
150154 ))
151- .as ("exception when passing -1 as start position" )
155+ .as ("exception when passing null as start position" )
152156 .hasMessage ("Argument{name=startPosition, type=VTLInteger} must be greater than zero, was [NULL]" )
153157 .isExactlyInstanceOf (IllegalArgumentException .class );
154158 }
155159
160+ @ Test
161+ public void testLengthArgIsNull () throws Exception {
162+ result = vtlSubstr .invoke (
163+ Lists .newArrayList (
164+ VTLString .of ("Hello, world!" ),
165+ VTLNumber .of (0 ),
166+ VTLNumber .of ((Long ) null )
167+ )
168+ );
169+ assertThat (result ).isEqualTo (VTLString .of ("Hello, world!" ));
170+ }
171+
156172 private List <VTLObject > createArguments (String ds , Integer startPosition , Integer length ) {
157173 return Lists .newArrayList (
158174 VTLString .of (ds ),
0 commit comments