55import com .annimon .ownlang .modules .Module ;
66import java .text .DecimalFormat ;
77import java .util .Collections ;
8- import java .util .LinkedHashMap ;
98import java .util .List ;
109import java .util .Map ;
1110
@@ -22,110 +21,91 @@ public Map<String, Value> constants() {
2221
2322 @ Override
2423 public Map <String , Function > functions () {
25- final var result = new LinkedHashMap < String , Function >( 16 );
26- result . put ( "assertEquals" , new assertEquals ());
27- result . put ( "assertNotEquals" , new assertNotEquals ());
28- result . put ( "assertSameType" , new assertSameType ());
29- result . put ( "assertTrue" , new assertTrue ());
30- result . put ( "assertFalse" , new assertFalse ());
31- result . put ( "runTests" , new runTests ());
32- return result ;
24+ return Map . of (
25+ "assertEquals" , this :: assertEquals ,
26+ "assertNotEquals" , this :: assertNotEquals ,
27+ "assertSameType" , this :: assertSameType ,
28+ "assertTrue" , this :: assertTrue ,
29+ "assertFalse" , this :: assertFalse ,
30+ "runTests" , this :: runTests
31+ ) ;
3332 }
3433
3534 private static String microsToSeconds (long micros ) {
3635 return new DecimalFormat ("#0.0000" ).format (micros / 1000d / 1000d ) + " sec" ;
3736 }
38-
39- private static class assertEquals implements Function {
40- @ Override
41- public Value execute (Value [] args ) {
42- Arguments .check (2 , args .length );
43- if (args [0 ].equals (args [1 ])) return NumberValue .ONE ;
44- throw new OUnitAssertionException ("Values are not equal: "
45- + "1: " + args [0 ] + ", 2: " + args [1 ]);
46- }
37+
38+ private Value assertEquals (Value [] args ) {
39+ Arguments .check (2 , args .length );
40+ if (args [0 ].equals (args [1 ])) return NumberValue .ONE ;
41+ throw new OUnitAssertionException ("Values are not equal: "
42+ + "1: " + args [0 ] + ", 2: " + args [1 ]);
4743 }
4844
49- private static class assertNotEquals implements Function {
50- @ Override
51- public Value execute (Value [] args ) {
52- Arguments .check (2 , args .length );
53- if (!args [0 ].equals (args [1 ])) return NumberValue .ONE ;
54- throw new OUnitAssertionException ("Values are equal: " + args [0 ]);
55- }
45+ private Value assertNotEquals (Value [] args ) {
46+ Arguments .check (2 , args .length );
47+ if (!args [0 ].equals (args [1 ])) return NumberValue .ONE ;
48+ throw new OUnitAssertionException ("Values are equal: " + args [0 ]);
5649 }
5750
58- private static class assertSameType implements Function {
59- @ Override
60- public Value execute (Value [] args ) {
61- Arguments .check (2 , args .length );
62- if (args [0 ].type () == args [1 ].type ()) return NumberValue .ONE ;
63- throw new OUnitAssertionException ("Types mismatch. "
64- + "1: " + Types .typeToString (args [0 ].type ())
65- + ", 2: " + Types .typeToString (args [1 ].type ()));
66- }
51+ private Value assertSameType (Value [] args ) {
52+ Arguments .check (2 , args .length );
53+ if (args [0 ].type () == args [1 ].type ()) return NumberValue .ONE ;
54+ throw new OUnitAssertionException ("Types mismatch. "
55+ + "1: " + Types .typeToString (args [0 ].type ())
56+ + ", 2: " + Types .typeToString (args [1 ].type ()));
6757 }
6858
69- private static class assertTrue implements Function {
70- @ Override
71- public Value execute (Value [] args ) {
72- Arguments .check (1 , args .length );
73- if (args [0 ].asInt () != 0 ) return NumberValue .ONE ;
74- throw new OUnitAssertionException ("Expected true, but found false." );
75- }
59+ private Value assertTrue (Value [] args ) {
60+ Arguments .check (1 , args .length );
61+ if (args [0 ].asInt () != 0 ) return NumberValue .ONE ;
62+ throw new OUnitAssertionException ("Expected true, but found false." );
7663 }
7764
78- private static class assertFalse implements Function {
79- @ Override
80- public Value execute (Value [] args ) {
81- Arguments .check (1 , args .length );
82- if (args [0 ].asInt () == 0 ) return NumberValue .ONE ;
83- throw new OUnitAssertionException ("Expected false, but found true." );
84- }
65+ private Value assertFalse (Value [] args ) {
66+ Arguments .check (1 , args .length );
67+ if (args [0 ].asInt () == 0 ) return NumberValue .ONE ;
68+ throw new OUnitAssertionException ("Expected false, but found true." );
8569 }
86-
87- private static class runTests implements Function {
88-
89- @ Override
90- public Value execute (Value [] args ) {
91- final var testFunctions = ScopeHandler .functions ().entrySet ().stream ()
92- .filter (e -> e .getKey ().toLowerCase ().startsWith ("test" ))
93- .toList ();
94- List <TestInfo > tests = testFunctions .stream ()
95- .map (e -> runTest (e .getKey (), e .getValue ()))
96- .toList ();
9770
98- int failures = 0 ;
99- long summaryTime = 0 ;
100- final StringBuilder result = new StringBuilder ();
101- for (TestInfo test : tests ) {
102- if (!test .isPassed ) failures ++;
103- summaryTime += test .elapsedTimeInMicros ;
104- result .append (Console .newline ());
105- result .append (test .info ());
106- }
71+ private Value runTests (Value [] args ) {
72+ final var testFunctions = ScopeHandler .functions ().entrySet ().stream ()
73+ .filter (e -> e .getKey ().toLowerCase ().startsWith ("test" ))
74+ .toList ();
75+ List <TestInfo > tests = testFunctions .stream ()
76+ .map (e -> runTest (e .getKey (), e .getValue ()))
77+ .toList ();
78+
79+ int failures = 0 ;
80+ long summaryTime = 0 ;
81+ final StringBuilder result = new StringBuilder ();
82+ for (TestInfo test : tests ) {
83+ if (!test .isPassed ) failures ++;
84+ summaryTime += test .elapsedTimeInMicros ;
10785 result .append (Console .newline ());
108- result .append (String .format ("Tests run: %d, Failures: %d, Time elapsed: %s" ,
109- tests .size (), failures ,
110- microsToSeconds (summaryTime )));
111- return new StringValue (result .toString ());
86+ result .append (test .info ());
11287 }
88+ result .append (Console .newline ());
89+ result .append (String .format ("Tests run: %d, Failures: %d, Time elapsed: %s" ,
90+ tests .size (), failures ,
91+ microsToSeconds (summaryTime )));
92+ return new StringValue (result .toString ());
93+ }
11394
114- private TestInfo runTest (String name , Function f ) {
115- final long startTime = System .nanoTime ();
116- boolean isSuccessfull ;
117- String failureDescription ;
118- try {
119- f .execute ();
120- isSuccessfull = true ;
121- failureDescription = "" ;
122- } catch (OUnitAssertionException oae ) {
123- isSuccessfull = false ;
124- failureDescription = oae .getMessage ();
125- }
126- final long elapsedTime = System .nanoTime () - startTime ;
127- return new TestInfo (name , isSuccessfull , failureDescription , elapsedTime / 1000 );
95+ private TestInfo runTest (String name , Function f ) {
96+ final long startTime = System .nanoTime ();
97+ boolean isSuccessfull ;
98+ String failureDescription ;
99+ try {
100+ f .execute ();
101+ isSuccessfull = true ;
102+ failureDescription = "" ;
103+ } catch (OUnitAssertionException oae ) {
104+ isSuccessfull = false ;
105+ failureDescription = oae .getMessage ();
128106 }
107+ final long elapsedTime = System .nanoTime () - startTime ;
108+ return new TestInfo (name , isSuccessfull , failureDescription , elapsedTime / 1000 );
129109 }
130110
131111 private static class OUnitAssertionException extends RuntimeException {
@@ -142,7 +122,7 @@ private record TestInfo(
142122 long elapsedTimeInMicros
143123 ) {
144124 public String info () {
145- return String . format ( "%s [%s]\n %sElapsed: %s\n " ,
125+ return "%s [%s]\n %sElapsed: %s\n " . formatted (
146126 name ,
147127 isPassed ? "passed" : "FAILED" ,
148128 isPassed ? "" : (failureDescription + "\n " ),
0 commit comments