Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Official ZPE/YASS documentationUnit testing

Official ZPE/YASS documentationUnit testing

ZPE 1.7.0 added capability for the assertion construct. Although its syntax makes it look like an internal function, it is not. Instead, ZPE's assertion is done in the interpreter much faster than a function can be run and is hard-coded into it.

Assertion is used in unit-testing. It can also be a powerful function of assessing your own assumptions of the expected output.

As teachers, assertions can be used to check the answers of students very quickly. Take the following sample of YASS code below:

YASS
//Students must give an example of a mathematical sequence that:
//Will give 13 as the third index of $a
//Will give 34 as the fifth index of $a

$a = []

asserts nequal($a, [5, 8, 13, 21, 34])

//Students should put their code here

asserts equal($a[2], 13)
asserts equal($a[4], 34)
  

A student might provide their lecturer or teacher with some code as shown below:

YASS
//Students must give an example of a mathematical sequence that:
//Will give 13 as the third index of $a
//Will give 34 as the fifth index of $a

$a = []

asserts nequal($a, [5, 8, 13, 21, 34])

for($i = 0; $i < 10; $i++)
  $x = $i
  $a[$i] = $x
end for

asserts equal($a[2], 13)
asserts equal($a[4], 34)
  

This code is obviously invalid.

For such a problem, a Fibonacci sequence could be generated that would solve the problem:

YASS
//Students must give an example of a mathematical sequence that:
//Will give 13 as the third index of $a
//Will give 34 as the fifth index of $a

$a = []
$x = 2
$y = 3

asserts nequal($a, [5, 8, 13, 21, 34])

for($i = 0 to 10)
  $t = $y
  $y = $x + $y
  $x = $t

  $a[$i] = $y

end for

asserts equal($a[2], 13)
asserts equal($a[4], 34)

  

Educators and assessors can use this tool to verify students' code very quickly.

asserts true and asserts false

As well as the asserts equal and the asserts nequal constructs, there are also asserts true and asserts false constructs, which will check that the value matches true or false respectively.

Prior to version 1.9.6 (St Boswells, July 2021), asserts true worked as the newer asserts equal does and asserts false worked the same way as the asserts nequal. As of version 1.9.6, because of the addition of the asserts equal and asserts nequal constructs, these now evaluate a value against a boolean value.

As of ZPE 1.7.8 (Cedar Lake, July 2018) assertion works differently in compiled programs. In fact, with the addition of compiler optimisations added in ZPE 1.7.8 assertion is no longer written to a compiled file.

Comments
Feedback 👍
Comments are sent via email to me.