Assertions in design by contract: Difference between revisions
Content added Content deleted
m (Tasks start as draft tasks, use interwiki link) |
m (Language implementation categories are for different implementations of the language not for pages with examples. Pages with examples show up in the language's main category page.) |
||
Line 2: | Line 2: | ||
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]] |
|||
{{libheader|Scala}}The last line is only executed if all assertions are met.<lang scala>import java.net.{URLDecoder, URLEncoder} |
{{libheader|Scala}}The last line is only executed if all assertions are met.<lang scala>import java.net.{URLDecoder, URLEncoder} |
||
import scala.compat.Platform.currentTime |
import scala.compat.Platform.currentTime |
Revision as of 16:34, 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
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>