33import com .spun .util .Tuple ;
44import org .approvaltests .core .Options ;
55import org .lambda .functions .Function1 ;
6- import org .lambda .functions .Function2 ;
76import org .lambda .query .Queryable ;
87
98import java .util .HashMap ;
109
1110public class ParseInput <OUT >
1211{
1312 private final String expected ;
13+ private final boolean multiline ;
1414 private final Function1 <String , Tuple <String , OUT >> transformer ;
15- private boolean multiline ;
16- public ParseInput (String expected , Function1 <String , Tuple <String , OUT >> transformer , boolean multiline )
15+ ParseInput (String expected , Function1 <String , Tuple <String , OUT >> transformer , boolean multiline )
1716 {
1817 this .expected = expected ;
1918 this .transformer = transformer ;
2019 this .multiline = multiline ;
2120 }
22- ParseInput (String expected , Function1 <String , Tuple <String , OUT >> transformer )
23- {
24- this (expected , transformer , false );
25- }
2621 public static ParseInput <String > from (String expected )
2722 {
28- return new ParseInput <String >(expected , s -> new Tuple <>(s , s ));
23+ return new ParseInput <String >(expected , s -> new Tuple <>(s , s ), false );
2924 }
30-
31- public static <OUT > ParseInput <OUT > create (String expected , Function1 <Queryable <String >, OUT > transformer )
25+ public ParseInput <OUT > multiline ()
3226 {
33- return new ParseInput <OUT >(expected , s -> {
34- var temp = Queryable .as (s .split ("," )).select (String ::trim );
35- return new Tuple <>(s , transformer .call (temp ));
36- });
27+ return new ParseInput <>(expected , transformer , true );
3728 }
38- public static <IN1 , IN2 , OUT > ParseInput <OUT > create (String expected , Function2 <IN1 , IN2 , OUT > transformer ,
39- Class <IN1 > in1 , Class <IN2 > in2 )
29+ public Queryable <Tuple <String , OUT >> parse ()
4030 {
41- Function1 <String , IN1 > t1 = getTransformerForClass (in1 );
42- Function1 <String , IN2 > t2 = getTransformerForClass (in2 );
43- return new ParseInput <OUT >(expected , s -> {
44- var temp = Queryable .as (s .split ("," )).select (String ::trim );
45- IN1 v1 = t1 .call (temp .get (0 ));
46- IN2 v2 = t2 .call (temp .get (1 ));
47- OUT out = transformer .call (v1 , v2 );
48- return new Tuple <>(temp .join (", " ), out );
49- });
31+ Function1 <String , Boolean > f = multiline ? s -> s .contains ("->" ) : s -> true ;
32+ return Queryable .as (expected .lines ()) //
33+ .where (f ) //
34+ .select (l -> l .split ("->" )[0 ].trim ()) //
35+ .where (l -> !l .isEmpty ()) //
36+ .select (l -> transformer .call (l ));
5037 }
51- public static <OUT > ParseInput < OUT > from ( String expected , Class < OUT > tranformTo )
38+ public Queryable <OUT > getInputs ( )
5239 {
53- Function1 <String , OUT > transformer1 = getTransformerForClass (tranformTo );
54- return new ParseInput <OUT >(expected , s -> new Tuple <>(s , transformer1 .call (s )));
40+ return parse ().select (Tuple ::getSecond );
5541 }
56- public < OUT > ParseInputWith1Parameters < OUT > withTypes ( Class <OUT > type1 )
42+ public void verifyAll ( Function1 <OUT , Object > transform )
5743 {
58- return ParseInputWith1Parameters .create (expected , type1 , multiline );
44+ Approvals .verifyAll ("" , parse (), s -> String .format ("%s -> %s" , s .getFirst (), transform .call (s .getSecond ())),
45+ new Options ().inline (expected ));
5946 }
60- // public <OUT> ParseInput<OUT> transformTo(Function1<String, OUT> transformer)
61- // {
62- // return new ParseInput<OUT>(expected, s -> new Tuple<>(s, transformer.call(s)));
63- // }
64- public static <OUT > Function1 <String , OUT > getTransformerForClass (Class <OUT > tranformTo )
47+ public static <OUT > Function1 <String , OUT > getTransformerForClass (Class <OUT > targetType )
6548 {
6649 var transformers = new HashMap <Class <?>, Function1 <String , Object >>()
6750 {
@@ -75,47 +58,27 @@ public static <OUT> Function1<String, OUT> getTransformerForClass(Class<OUT> tra
7558 put (Short .class , s -> Short .parseShort (s ));
7659 }
7760 };
78- Function1 <String , OUT > transformer1 = (Function1 <String , OUT >) transformers .get (tranformTo );
61+ Function1 <String , OUT > transformer1 = (Function1 <String , OUT >) transformers .get (targetType );
7962 return transformer1 ;
8063 }
81- public Queryable <Tuple <String , OUT >> parse ()
64+ // ************* 1 parameter
65+ public <T1 > ParseInputWith1Parameters <T1 > withTypes (Class <T1 > type1 )
8266 {
83- Function1 <String , Boolean > f = multiline ? s -> s .contains ("->" ) : s -> true ;
84- return Queryable .as (expected .lines ()) //
85- .where (f ) //
86- .select (l -> l .split ("->" )[0 ].trim ()) //
87- .where (l -> !l .isEmpty ()) //
88- .select (l -> transformer .call (l ));
89- }
90- public String print (String input , Object output )
91- {
92- return input + " -> " + output ;
67+ return ParseInputWith1Parameters .create (expected , type1 , multiline );
9368 }
94- public void verifyAll (Function1 <OUT , Object > transform )
69+ public < T1 > ParseInputWith1Parameters < T1 > transformTo (Function1 <String , T1 > transformer )
9570 {
96- Approvals .verifyAll ("" , parse (), s -> print (s .getFirst (), transform .call (s .getSecond ())),
97- new Options ().inline (expected ));
71+ return new ParseInputWith1Parameters <>(expected , transformer , multiline );
9872 }
73+ // ************* 2 parameters
9974 public <T1 , T2 > ParseInputWith2Parameters <T1 , T2 > withTypes (Class <T1 > type1 , Class <T2 > type2 )
10075 {
101- return ParseInputWith2Parameters .create (expected , getTransformerForClass (type1 ),
102- getTransformerForClass ( type2 ) );
76+ return ParseInputWith2Parameters .create (expected , getTransformerForClass (type1 ), getTransformerForClass ( type2 ),
77+ multiline );
10378 }
10479 public <T1 , T2 > ParseInputWith2Parameters <T1 , T2 > transformTo (Function1 <String , T1 > transformer1 ,
10580 Function1 <String , T2 > transformer2 )
10681 {
107- return ParseInputWith2Parameters .create (expected , transformer1 , transformer2 );
108- }
109- public ParseInput <OUT > multiline ()
110- {
111- return new ParseInput <>(expected , transformer , true );
112- }
113- public <OUT > ParseInputWith1Parameters <OUT > transformTo (Function1 <String , OUT > transformer )
114- {
115- return new ParseInputWith1Parameters <>(expected , transformer , multiline );
116- }
117- public Queryable <OUT > getInputs ()
118- {
119- return parse ().select (Tuple ::getSecond );
82+ return ParseInputWith2Parameters .create (expected , transformer1 , transformer2 , multiline );
12083 }
12184}
0 commit comments