]> git.mdlowis.com Git - archive/dlang.git/commitdiff
Convert example code to asserts for unit testing purposes
authorMike D. Lowis <mike@mdlowis.com>
Fri, 30 Mar 2012 18:35:37 +0000 (14:35 -0400)
committerMike D. Lowis <mike@mdlowis.com>
Fri, 30 Mar 2012 18:35:37 +0000 (14:35 -0400)
example.dl

index 3f729fa3023baae0392c278b86abe0a73a973447..6ae7a66d41e870242140ccf8729d9ca7c6f28506 100644 (file)
@@ -1,23 +1,24 @@
 #------------------------------------------------------------------------------
 # Literal Definition and Usage
 #------------------------------------------------------------------------------
+foo := Nil
 
 # Nums
-foo := 1
-foo = 1.0
-foo = 1.0e1
-foo = -1
-foo = -1.0
-foo = -1.0e-1
+assert( 1 == 1,           "Positive integer parses correctly")
+assert( -1 == -1,         "Negative integer parses correctly")
+assert( 1.0 == 1.0,       "Parse postive floating point value without exponent")
+assert( -1.0 == -1.0,     "Parse negative floating point value without exponent")
+assert( 10.0e-1 == 1.0,   "Parse positive floating point value with exponent")
+assert( -10.0e-1 == -1.0, "Parse negative floating point value with exponent")
 
 # Char
-foo = 'a'
-foo = '\a'    # bell
-foo = '\b'    # backspace
-foo = '\n'    # newline
-foo = '\r'    # return
-foo = '\t'    # tab
-foo = '\v'    # vtab
+assert('a',  "Parse a character")
+assert('\a', "Parse escape sequence for alarm")
+assert('\b', "Parse escape sequence for backspace")
+assert('\n', "Parse escape sequence for newline")
+assert('\r', "Parse escape sequence for return")
+assert('\t', "Parse escape sequence for tab")
+assert('\v', "Parse escape sequence for vertical tab")
 #foo = '\xFF'  # Hex Value
 
 # String
@@ -41,9 +42,9 @@ foo["stuff"] = 3
 foo.stuff = 5
 
 # Accessing map elements
-print( foo[$bar] )
-print( foo["stuff"] )
-print( foo.stuff )
+#print( foo[$bar] )
+#print( foo["stuff"] )
+#print( foo.stuff )
 
 # Vector
 foo = []
@@ -72,35 +73,63 @@ foo = {|a,b| a + b }
 foo = foo(1,2)
 foo = ({|a,b| a + b })(1,2)
 
+#------------------------------------------------------------------------------
+# Operators and Precedence Tests
+#------------------------------------------------------------------------------
+
+
 #------------------------------------------------------------------------------
 # Macro Definition and Usage
 #------------------------------------------------------------------------------
 
+# Define a macro that represents a traditional if statement
 @ if [
     (E B B) : exec_if($1, $2, $3),
     (E B)   : exec_if($1, $2),
 ]
 
+test_result = false
 if (1 < 2)
 {
-    print(1 + 1)
+    test_result = true
+}{
+    test_result = false
+}
+assert( test_result, "First branch should be taken when test condition is true" )
+
+test_result = false
+if (1 > 2)
+{
+    test_result = false
 }{
-    print(2 + 2)
+    test_result = true
+}
+assert( test_result, "Second branch should be taken when test condition is false" )
+
+test_result = false
+if (1 < 2)
+{
+    test_result = true
 }
+assert( test_result, "Branch should be taken when test condition is true" )
 
-if (1 == 1)
+test_result = true
+if (1 > 2)
 {
-    print(1 + 1)
+    test_result = false
 }
+assert( test_result, "Branch should not be taken when test condition is false" )
 
 #------------------------------------------------------------------------------
 # Delayed Evaluation
 #------------------------------------------------------------------------------
 
+# Define a macro that will delay the evaluation of the following expression
 @ delay [
     (E) : make_promise({ $1 })
 ]
 
+# Define a macro that will force the evaluation of the following delayed statement
 @ force [
     (E) : $1()
 ]