LightJason - AgentSpeak(L++)
TestCActionGenericType.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 package org.lightjason.agentspeak.action.builtin;
25 
26 import org.junit.Assert;
27 import org.junit.Test;
44 
45 import java.util.ArrayList;
46 import java.util.Collections;
47 import java.util.HashSet;
48 import java.util.List;
49 import java.util.stream.Collectors;
50 import java.util.stream.Stream;
51 
52 
56 public final class TestCActionGenericType extends IBaseTest
57 {
58 
62  @Test
63  public final void createliteral()
64  {
65  final List<ITerm> l_return = new ArrayList<>();
66 
67  new CCreateLiteral().execute(
68  false, IContext.EMPTYPLAN,
69  Stream.of( "functor", "stringvalue", 1234, true ).map( CRawTerm::from ).collect( Collectors.toList() ),
70  l_return
71  );
72 
73  Assert.assertEquals( l_return.size(), 1 );
74  Assert.assertEquals(
75  l_return.get( 0 ).<ILiteral>raw(),
76  CLiteral.from(
77  "functor",
78  CRawTerm.from( "stringvalue" ),
79  CRawTerm.from( 1234 ),
80  CRawTerm.from( true )
81  )
82  );
83  }
84 
85 
89  @Test
90  public final void parseliteral()
91  {
92  final List<ITerm> l_return = new ArrayList<>();
93 
94  new CParseLiteral().execute(
95  false, IContext.EMPTYPLAN,
96  Stream.of( "main/parsefunctor( \"hello\", 666, false )" ).map( CRawTerm::from ).collect( Collectors.toList() ),
97  l_return
98  );
99 
100  Assert.assertEquals( l_return.size(), 1 );
101  Assert.assertEquals(
102  l_return.get( 0 ).<ILiteral>raw(),
103  CLiteral.from(
104  "main/parsefunctor",
105  CRawTerm.from( "hello" ),
106  CRawTerm.from( 666D ),
107  CRawTerm.from( false )
108  )
109  );
110  }
111 
112 
116  @Test
117  public final void parseliteralerror()
118  {
119  final List<ITerm> l_return = new ArrayList<>();
120 
121  Assert.assertFalse(
122  new CParseLiteral().execute(
123  false, IContext.EMPTYPLAN,
124  Stream.of( "Main/parsefunctor( hello, XXXXX, false )" ).map( CRawTerm::from ).collect( Collectors.toList() ),
125  l_return
126  ).value()
127  );
128  }
129 
130 
134  @Test
135  public final void parsefloat()
136  {
137  final List<ITerm> l_return = new ArrayList<>();
138 
139  new CParseNumber().execute(
140  false, IContext.EMPTYPLAN,
141  Stream.of( "732.489", "64.091248", "-78129.01", "foo" ).map( CRawTerm::from ).collect( Collectors.toList() ),
142  l_return
143  );
144 
145  Assert.assertEquals( l_return.size(), 4 );
146  Assert.assertEquals( l_return.get( 0 ).<Number>raw().doubleValue(), 732.489, 0 );
147  Assert.assertEquals( l_return.get( 1 ).<Number>raw().doubleValue(), 64.091248, 0 );
148  Assert.assertEquals( l_return.get( 2 ).<Number>raw().doubleValue(), -78129.01, 0 );
149  Assert.assertNull( l_return.get( 3 ).raw() );
150  }
151 
152 
156  @Test
157  public final void type()
158  {
159  final List<ITerm> l_return = new ArrayList<>();
160 
161  new CType().execute(
162  false, IContext.EMPTYPLAN,
163  Stream.of( new ArrayList<>(), 123L, "test value", new HashSet<>() ).map( CRawTerm::from ).collect( Collectors.toList() ),
164  l_return
165  );
166 
167  Assert.assertEquals( l_return.size(), 4 );
168  Assert.assertEquals( l_return.get( 0 ).<String>raw(), "java.util.ArrayList" );
169  Assert.assertEquals( l_return.get( 1 ).<String>raw(), "java.lang.Long" );
170  Assert.assertEquals( l_return.get( 2 ).<String>raw(), "java.lang.String" );
171  Assert.assertEquals( l_return.get( 3 ).<String>raw(), "java.util.HashSet" );
172  }
173 
174 
178  @Test
179  public final void is()
180  {
181  Assert.assertFalse(
182  new CIs().execute(
183  false, IContext.EMPTYPLAN,
184  Stream.of( "java.lang.String", "text foo", 123, 88.98 ).map( CRawTerm::from ).collect( Collectors.toList() ),
185  Collections.emptyList()
186  ).value()
187  );
188 
189  Assert.assertTrue(
190  new CIs().execute(
191  false, IContext.EMPTYPLAN,
192  Stream.of( "java.lang.Number", 123, 44.5 ).map( CRawTerm::from ).collect( Collectors.toList() ),
193  Collections.emptyList()
194  ).value()
195  );
196  }
197 
198 
202  @Test
203  public final void isnull()
204  {
205  Assert.assertFalse(
206  new CIsNull().execute(
207  false, IContext.EMPTYPLAN,
208  Stream.of( "test type string", null ).map( CRawTerm::from ).collect( Collectors.toList() ),
209  Collections.emptyList()
210  ).value()
211  );
212 
213  Assert.assertTrue(
214  new CIsNull().execute(
215  false, IContext.EMPTYPLAN,
216  Stream.of( CRawTerm.from( null ) ).collect( Collectors.toList() ),
217  Collections.emptyList()
218  ).value()
219  );
220  }
221 
222 
226  @Test
227  public final void isnumeric()
228  {
229  Assert.assertFalse(
230  new CIsNumeric().execute(
231  false, IContext.EMPTYPLAN,
232  Stream.of( "test type string", 123, 77L, 112.123, 44.5f ).map( CRawTerm::from ).collect( Collectors.toList() ),
233  Collections.emptyList()
234  ).value()
235  );
236 
237  Assert.assertTrue(
238  new CIsNumeric().execute(
239  false, IContext.EMPTYPLAN,
240  Stream.of( 123, 77L, 112.123, 44.5f ).map( CRawTerm::from ).collect( Collectors.toList() ),
241  Collections.emptyList()
242  ).value()
243  );
244  }
245 
246 
250  @Test
251  public final void isstring()
252  {
253  Assert.assertFalse(
254  new CIsString().execute(
255  false, IContext.EMPTYPLAN,
256  Stream.of( "test foobar", 123, "string again", true, new Object(), 77.8, 'a' ).map( CRawTerm::from ).collect( Collectors.toList() ),
257  Collections.emptyList()
258  ).value()
259  );
260 
261 
262  Assert.assertTrue(
263  new CIsString().execute(
264  false, IContext.EMPTYPLAN,
265  Stream.of( "okay 1", 'c', "ok 2" ).map( CRawTerm::from ).collect( Collectors.toList() ),
266  Collections.emptyList()
267  ).value()
268  );
269  }
270 
271 
275  @Test
276  public final void tostring()
277  {
278  final List<ITerm> l_return = new ArrayList<>();
279 
280  new CToString().execute(
281  false, IContext.EMPTYPLAN,
282  Stream.of( "", 123, 5.5, new Object() ).map( CRawTerm::from ).collect( Collectors.toList() ),
283  l_return
284  );
285 
286  Assert.assertEquals( l_return.size(), 4 );
287  Assert.assertTrue( l_return.get( 0 ).<String>raw().isEmpty() );
288  Assert.assertEquals( l_return.get( 1 ).raw(), "123" );
289  Assert.assertEquals( l_return.get( 2 ).raw(), "5.5" );
290  Assert.assertTrue( l_return.get( 3 ).raw() instanceof String );
291  }
292 
293 
297  @Test
298  public final void tofloat()
299  {
300  final List<ITerm> l_return = new ArrayList<>();
301 
302  new CToNumber().execute(
303  false, IContext.EMPTYPLAN,
304  Stream.of( 1, 2, 3.2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
305  l_return
306  );
307 
308  Assert.assertEquals( l_return.size(), 3 );
309  Assert.assertTrue( l_return.get( 0 ).raw() instanceof Double );
310  Assert.assertTrue( l_return.get( 1 ).raw() instanceof Double );
311  Assert.assertTrue( l_return.get( 2 ).raw() instanceof Double );
312  }
313 
314 
318  @Test
319  public final void tofloaterror()
320  {
321  Assert.assertFalse(
322  new CToNumber().execute(
323  false, IContext.EMPTYPLAN,
324  Stream.of( "" ).map( CRawTerm::from ).collect( Collectors.toList() ),
325  Collections.emptyList()
326  ).value()
327  );
328  }
329 
330 }
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: IParse.java:66
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: CType.java:69
base test class with helpers
Definition: IBaseTest.java:33
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
converts a value into the string represenation.
Definition: CToString.java:40
final void parseliteralerror()
test parse literal action with error
execution context with local data
Definition: IContext.java:42
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: ICast.java:70
action to cast a value to a number value.
Definition: CToNumber.java:40
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
Definition: CLiteral.java:64
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
Definition: CLiteral.java:161
returns for each argument the underlying type.
Definition: CType.java:45
action to check if a value is a null value.
Definition: CIsNull.java:47
action to check if a type is a class.
Definition: CIs.java:49
term structure for simple datatypes
Definition: CRawTerm.java:45