Commit Diff


commit - /dev/null
commit + 570c7c09d6071cd1c73092d692ad6fe6600e08e5
blob - /dev/null
blob + a765843f4f51385b1d23c4b6eca81c02562a5451 (mode 644)
--- /dev/null
+++ README.md
@@ -0,0 +1,21 @@
+### Introduction
+
+This module provides a draft of Golang package with implementation of
+collection of decorators that makes it easy to write software using contracts.
+
+Contracts are a debugging and verification tool. They are declarative
+statements about what states a program must be in to be considered "correct" at
+runtime. They are similar to assertions, and are verified automatically at
+various well-defined points in the program. Contracts can be specified on
+functions and on classes.
+
+Contracts consist of two parts: a description and a condition. The description
+is simply a human-readable string that describes what the contract is testing,
+while the condition is a single function that tests that condition. The
+condition is executed automatically and passed certain arguments (which vary
+depending on the type of contract), and must return a boolean value: True if
+the condition has been met, and False otherwise.
+
+The main idea of using contracts was described in [Applying 'design by
+contract'](http://cs.uns.edu.ar/~dcm/tdp/downloads/Material%20de%20Estudio/Design-by-Contract.pdf)
+by B. Meyer.
blob - /dev/null
blob + 728810fb442a9bf94e7b04c5d988e0987341978b (mode 644)
--- /dev/null
+++ contracts.go
@@ -0,0 +1,43 @@
+package contracts
+
+import (
+	"fmt"
+	"log"
+)
+
+/*
+ * Contracts on functions consist of preconditions and postconditions.
+ */
+
+type fn func(string)
+
+func require(f fn, s string, desc string, expression bool) bool {
+
+	fmt.Println("require")
+	fmt.Println(desc)
+
+	if !expression {
+		log.Fatal("Aaaa!")
+	}
+	/* example of expression */
+	if s == "" {
+		log.Fatal("Aaaaa!")
+	}
+	return true
+}
+
+func ensure(f fn, s string, desc string, expression bool) func(s string) {
+	fmt.Println("ensure")
+	fmt.Println(desc)
+
+	return func(s string) {
+		f(s)
+	}
+}
+
+func Contract(f fn, s string, desc string, expression bool) func(s string) {
+	fmt.Println("contract")
+	require(f, s, desc, expression)
+
+	return ensure(f, s, desc, expression)
+}
blob - /dev/null
blob + 02488789034c236aae6d56397ffd7470560be56d (mode 644)
--- /dev/null
+++ example/sample.go
@@ -0,0 +1,15 @@
+package main
+
+import (
+	"fmt"
+	contracts "github.com/ligurio/go-contracts"
+)
+
+func test(s string) {
+	fmt.Println(s)
+}
+
+func main() {
+	contracts.Contract(test, "Hello, world!", "Hello", false)
+	contracts.Contract(test, "Bye, world!", "Bye", true)
+}