1+ package org.utbot.taint.parser.yaml
2+
3+ import com.charleskorn.kaml.*
4+ import org.junit.jupiter.api.Assertions.assertEquals
5+ import org.junit.jupiter.api.DisplayName
6+ import org.junit.jupiter.api.Nested
7+ import org.junit.jupiter.api.Test
8+ import org.junit.jupiter.api.assertThrows
9+ import org.utbot.taint.parser.constants.*
10+ import org.utbot.taint.parser.model.*
11+
12+ class ConditionParserTest {
13+
14+ @Nested
15+ @DisplayName(" parseCondition" )
16+ inner class ParseConditionTest {
17+ @Test
18+ fun `should parse yaml null as ValueCondition` () {
19+ val yamlNull = Yaml .default.parseToYamlNode(" null" )
20+ val expectedCondition = ValueCondition (ArgumentValueNull )
21+
22+ val actualCondition = ConditionParser .parseCondition(yamlNull)
23+ assertEquals(expectedCondition, actualCondition)
24+ }
25+
26+ @Test
27+ fun `should parse yaml scalar with argument value as ValueCondition` () {
28+ val yamlScalar = Yaml .default.parseToYamlNode(" some-string" )
29+ val expectedCondition = ValueCondition (ArgumentValueString (" some-string" ))
30+
31+ val actualCondition = ConditionParser .parseCondition(yamlScalar)
32+ assertEquals(expectedCondition, actualCondition)
33+ }
34+
35+ @Test
36+ fun `should parse yaml scalar with argument type as TypeCondition` () {
37+ val yamlScalar = Yaml .default.parseToYamlNode(" ${k_lt} java.lang.Integer${k_gt} " )
38+ val expectedCondition = TypeCondition (ArgumentTypeString (" java.lang.Integer" ))
39+
40+ val actualCondition = ConditionParser .parseCondition(yamlScalar)
41+ assertEquals(expectedCondition, actualCondition)
42+ }
43+
44+ @Test
45+ fun `should parse yaml list as OrCondition` () {
46+ val yamlList = Yaml .default.parseToYamlNode(" [ 1, true, ${k_lt} java.lang.Integer${k_gt} ]" )
47+ val expectedCondition = OrCondition (listOf (
48+ ValueCondition (ArgumentValueLong (1L )),
49+ ValueCondition (ArgumentValueBoolean (true )),
50+ TypeCondition (ArgumentTypeString (" java.lang.Integer" )),
51+ ))
52+
53+ val actualCondition = ConditionParser .parseCondition(yamlList)
54+ assertEquals(expectedCondition, actualCondition)
55+ }
56+
57+ @Test
58+ fun `should parse yaml map with a key 'not' as NotCondition` () {
59+ val yamlMap = Yaml .default.parseToYamlNode(" $k_not : ${k_lt} int${k_gt} " )
60+ val expectedCondition = NotCondition (TypeCondition (ArgumentTypeString (" int" )))
61+
62+ val actualCondition = ConditionParser .parseCondition(yamlMap)
63+ assertEquals(expectedCondition, actualCondition)
64+ }
65+
66+ @Test
67+ fun `should fail on yaml map without a key 'not'` () {
68+ val yamlMap = Yaml .default.parseToYamlNode(" net: ${k_lt} int${k_gt} " )
69+
70+ assertThrows<ConfigurationParseError > {
71+ ConditionParser .parseCondition(yamlMap)
72+ }
73+ }
74+
75+ @Test
76+ fun `should fail on yaml map with unknown keys` () {
77+ val yamlMap = Yaml .default.parseToYamlNode(" { $k_not : ${k_lt} int${k_gt} , unknown-key: 0 }" )
78+
79+ assertThrows<ConfigurationParseError > {
80+ ConditionParser .parseCondition(yamlMap)
81+ }
82+ }
83+
84+ @Test
85+ fun `should parse complicated yaml node` () {
86+ val yamlMap = Yaml .default.parseToYamlNode(" $k_not : [ { $k_not : 0 }, ${k_lt} int${k_gt} , { $k_not : null } ]" )
87+ val expectedCondition = NotCondition (OrCondition (listOf (
88+ NotCondition (ValueCondition (ArgumentValueLong (0L ))),
89+ TypeCondition (ArgumentTypeString (" int" )),
90+ NotCondition (ValueCondition (ArgumentValueNull ))
91+ )))
92+
93+ val actualCondition = ConditionParser .parseCondition(yamlMap)
94+ assertEquals(expectedCondition, actualCondition)
95+ }
96+
97+ @Test
98+ fun `should fail on another yaml type` () {
99+ val yamlTaggedNode = YamlTaggedNode (" some-tag" , YamlNull (YamlPath .root))
100+
101+ assertThrows<ConfigurationParseError > {
102+ ConditionParser .parseCondition(yamlTaggedNode)
103+ }
104+ }
105+ }
106+
107+ @Nested
108+ @DisplayName(" parseConditions" )
109+ inner class ParseConditionsTest {
110+ @Test
111+ fun `should parse correct yaml map as Conditions` () {
112+ val yamlMap = Yaml .default.parseToYamlNode(" { $k_this : \"\" , ${k_arg} 2: { $k_not : ${k_lt} int${k_gt} }, $k_return : [ 0, 1 ] }" )
113+ val expectedConditions = ConditionsMap (mapOf (
114+ ThisObject to ValueCondition (ArgumentValueString (" " )),
115+ MethodArgument (2u ) to NotCondition (TypeCondition (ArgumentTypeString (" int" ))),
116+ ReturnValue to OrCondition (listOf (ValueCondition (ArgumentValueLong (0L )), ValueCondition (ArgumentValueLong (1L ))))
117+ ))
118+
119+ val actualConditions = ConditionParser .parseConditions(yamlMap)
120+ assertEquals(expectedConditions, actualConditions)
121+ }
122+
123+ @Test
124+ fun `should parse empty yaml map as NoConditions` () {
125+ val yamlMap = Yaml .default.parseToYamlNode(" {}" )
126+ val expectedConditions = NoConditions
127+
128+ val actualConditions = ConditionParser .parseConditions(yamlMap)
129+ assertEquals(expectedConditions, actualConditions)
130+ }
131+
132+ @Test
133+ fun `should fail on another yaml type` () {
134+ val yamlList = Yaml .default.parseToYamlNode(" []" )
135+
136+ assertThrows<ConfigurationParseError > {
137+ ConditionParser .parseConditions(yamlList)
138+ }
139+ }
140+ }
141+
142+ @Nested
143+ @DisplayName(" parseConditionsKey" )
144+ inner class ParseConditionsKeyTest {
145+ @Test
146+ fun `should parse yaml map with a key 'conditions'` () {
147+ val yamlMap = Yaml .default.parseToYamlNode(" $k_conditions : { $k_return : null }" ).yamlMap
148+ val expectedConditions = ConditionsMap (mapOf (ReturnValue to ValueCondition (ArgumentValueNull )))
149+
150+ val actualConditions = ConditionParser .parseConditionsKey(yamlMap)
151+ assertEquals(expectedConditions, actualConditions)
152+ }
153+
154+ @Test
155+ fun `should parse yaml map without a key 'conditions' as NoConditions` () {
156+ val yamlMap = Yaml .default.parseToYamlNode(" $k_marks : []" ).yamlMap
157+ val expectedConditions = NoConditions
158+
159+ val actualConditions = ConditionParser .parseConditionsKey(yamlMap)
160+ assertEquals(expectedConditions, actualConditions)
161+ }
162+ }
163+ }
0 commit comments