Test a function: Difference between revisions

Add bruijn
(Added Odin variant)
(Add bruijn)
(3 intermediate revisions by 3 users not shown)
Line 209:
 
4 tests, 4 assertions, 1 failures.</pre>
 
=={{header|Bruijn}}==
Bruijn has equivalency testing builtin. The <code>:test</code> instruction checks whether two terms are beta-equivalent (alpha-equivalent after beta-reduction) and prints an error if they aren't. Alpha-conversion is not needed because of the usage of De Bruijn indices. All tests in imported files get run automatically (can be disabled using a CLI flag).
}</syntaxhighlight lang="bruijn">
:import std/String .
 
main [<~>0 =? 0]
 
:test (main "tacocat") ([[1]])
:test (main "bruijn") ([[1]])
</syntaxhighlight>
 
{{out}}
<pre>
ERROR test failed: (main [((0 [[[(0 (1 (0 (0 (0 (1 (1 (0 2))))))))]]]) [((0 [[[(0 (1 (0 (0 (1 (1 (1 (0 2))))))))]]]) [((0 [[[(1 (0 (1 (0 (1 (1 (1 (0 2))))))))]]]) [((0 [[[(1 (0 (0 (1 (0 (1 (1 (0 2))))))))]]]) [((0 [[[(0 (1 (0 (1 (0 (1 (1 (0 2))))))))]]]) [((0 [[[(0 (1 (1 (1 (0 (1 (1 (0 2))))))))]]]) [[0]])])])])])])]) = [[1]]
reduced to [[0]] = [[1]]
</pre>
 
=={{header|C}}==
Line 896 ⟶ 913:
->
</pre>
 
=={{header|Insitux}}==
It is possible to mock any built-in or user-defined function, and assert values are truthy. Testing [[Palindrome_detection#Insitux|Insitux's Palindrome detection entry]].
<syntaxhighlight lang="insitux">(var palindrome? (comp (filter letter?) lower-case (= (reverse %))))
 
;Arrange
(var calls [])
(function record f
(fn (var! calls (append f))
(... (unmocked f) args)))
 
(mock comp (record comp)
filter (record filter)
letter? (record letter?)
lower-case (record lower-case)
= (record =)
reverse (record reverse))
 
(var sentence "In girum imus nocte et consumimur igni.")
 
;Act
(var result (palindrome? sentence))
 
(unmock comp filter letter? lower-case = reverse)
 
;Assert
(assert result)
 
(var occurred (freqs calls))
(assert (= (len sentence) (occurred letter?))) ;letter? is called (len sentence) times
(assert (... = 1 (map occurred [filter lower-case reverse =]))) ;other functions are called only once</syntaxhighlight>
 
=={{header|J}}==
Line 1,980 ⟶ 2,028:
sub palin( Str $string) { so $string.lc.comb(/\w/) eq $string.flip.lc.comb(/\w/) }
 
for
my %tests =
'A man, a plan, a canal: Panama.' => True,
'My dog has fleas' => False,
Line 1,986 ⟶ 2,034:
'1 on 1' => False,
'In girum imus nocte et consumimur igni' => True,
'' => True,
{
;
for %tests.kv ->my ($test, $expected-result) {= .kv;
 
plan %tests.elems;
 
for %tests.kv -> $test, $expected-result {
is palin($test), $expected-result,
"\"$test\" is {$expected-result??''!!'not '}a palindrome.";
}
}</syntaxhighlight>
 
done-testing;</syntaxhighlight>
Output:
{{out}}
<pre>1..6
ok 1 - "1 on 1" is not a palindrome.
Line 2,461 ⟶ 2,507:
=={{header|Wren}}==
{{libheader|Wren-test}}
<syntaxhighlight lang="ecmascriptwren">import "./module" for Expect, Suite, ConsoleReporter
 
var isPal = Fn.new { |word| word == ((word.count > 0) ? word[-1..0] : "") }
55

edits