Assertions in design by contract: Difference between revisions

From Rosetta Code
Content added Content deleted
(Initial setup)
 
m (Tasks start as draft tasks, use interwiki link)
Line 1: Line 1:
According to [http://en.wikipedia.org/wiki/Assertion_(software_development)#Assertions_in_design_by_contract wikipedia]; Assertions can function as a form of documentation: they can describe the state the code expects to find before it runs (its preconditions), and the state the code expects to result in when it is finished running (postconditions); they can also specify invariants of a class.
{{draft task}}According to [[wp:Assertion_(software_development)#Assertions_in_design_by_contract|Wikipedia]]; Assertions can function as a form of documentation: they can describe the state the code expects to find before it runs (its preconditions), and the state the code expects to result in when it is finished running (postconditions); they can also specify invariants of a class.
{{task}} Show in the program language of your choice an example of the expecting results as a form of documentation.
Show in the program language of your choice an example of the expecting results as a form of documentation.
=={{header|Scala}}==
=={{header|Scala}}==
[[Category:Scala Implementations]]
[[Category:Scala Implementations]]

Revision as of 16:32, 20 August 2014

Assertions in design by contract is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

According to Wikipedia; Assertions can function as a form of documentation: they can describe the state the code expects to find before it runs (its preconditions), and the state the code expects to result in when it is finished running (postconditions); they can also specify invariants of a class.

Show in the program language of your choice an example of the expecting results as a form of documentation.

Scala

Library: Scala

The last line is only executed if all assertions are met.<lang scala>import java.net.{URLDecoder, URLEncoder}

import scala.compat.Platform.currentTime

object UrlCoded extends App {

 val original = """http://foo bar/"""
 val encoded: String = URLEncoder.encode(original, "UTF-8")
 assert(encoded == "http%3A%2F%2Ffoo+bar%2F", s"Original: $original not properly encoded: $encoded")
 val percentEncoding = encoded.replace("+", "%20")
 assert(percentEncoding == "http%3A%2F%2Ffoo%20bar%2F", s"Original: $original not properly percent-encoded: $percentEncoding")
 assert(URLDecoder.decode(encoded, "UTF-8") == URLDecoder.decode(percentEncoding, "UTF-8"))
 println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]")

}</lang>