[vala-tests] Add a script to update examples from wiki



commit 033ff892f75513c94e03be2dc7c216cbee7c9106
Author: Marc-André Lureau <marcandre lureau gmail com>
Date:   Fri Jul 17 00:30:21 2009 +0300

    Add a script to update examples from wiki

 tests/examples/advanced.vala                |    3 +
 tests/examples/basic.vala                   |    3 +
 tests/examples/list.vala                    |    3 +-
 tests/examples/number-guessing.vala         |   57 ++++++++++++++++++++
 tests/examples/preprocessor.vala            |   27 ++++++++++
 tests/examples/properties-construction.vala |    3 +
 tests/examples/properties.vala              |    5 ++-
 tests/examples/signals.vala                 |   26 +++++++++
 tests/examples/string.vala                  |   76 ++++++++++++++++++++-------
 tests/examples/value.vala                   |   26 +++++++++
 update-from-wiki.sh                         |   39 ++++++++++++++
 11 files changed, 246 insertions(+), 22 deletions(-)
---
diff --git a/tests/examples/advanced.vala b/tests/examples/advanced.vala
index 7a0e860..ca6e622 100644
--- a/tests/examples/advanced.vala
+++ b/tests/examples/advanced.vala
@@ -1,3 +1,6 @@
+
+// http://live.gnome.org/Vala/AdvancedSample vala-test:examples/advanced.vala
+
 /* Advanced Vala sample code */
 
 using GLib;
diff --git a/tests/examples/basic.vala b/tests/examples/basic.vala
index 4d0a88d..0562f3c 100644
--- a/tests/examples/basic.vala
+++ b/tests/examples/basic.vala
@@ -1,3 +1,6 @@
+
+// http://live.gnome.org/Vala/BasicSample vala-test:examples/basic.vala
+
 /* Basic Vala sample code */
 
 using GLib;
diff --git a/tests/examples/list.vala b/tests/examples/list.vala
index 34ffd7a..c5bfea6 100644
--- a/tests/examples/list.vala
+++ b/tests/examples/list.vala
@@ -1,4 +1,5 @@
-using GLib;
+
+// http://live.gnome.org/Vala/ListSample vala-test:examples/list.vala
 
 public static int main (string[] args) {
     List<string> list = new List<string> ();
diff --git a/tests/examples/number-guessing.vala b/tests/examples/number-guessing.vala
new file mode 100644
index 0000000..2b06c24
--- /dev/null
+++ b/tests/examples/number-guessing.vala
@@ -0,0 +1,57 @@
+
+// http://live.gnome.org/Vala/AdvancedSample vala-test:examples/number-guessing.vala
+
+public class NumberGuessing {
+
+    private int min;
+    private int max;
+
+    public NumberGuessing (int min, int max) {
+        this.min = min;
+        this.max = max;
+    }
+
+    public void start () {
+        int try_count = 0;
+        int number = Random.int_range (min, max);
+
+        stdout.printf ("Welcome to Number Guessing!\n\n");
+        stdout.printf ("I have thought up a number between %d and %d\n", min, max);
+        stdout.printf ("which you have to guess now. Don't worry, I will\n");
+        stdout.printf ("give you some hints.\n\n");
+
+        while (true) {
+            try_count++;
+
+            stdout.printf ("Try #%d\n", try_count);
+            stdout.printf ("Please enter a number between %d and %d: ", min, max);
+            int input = read_line ().to_int ();
+
+            if (number == input) {
+                stdout.printf ("Congratulations! You win.\n");
+                break;
+            } else {
+                stdout.printf ("Wrong. The wanted number is %s than %d.\n",
+                               number > input ? "greater" : "less", input);
+            }
+        }
+    }
+
+    public static int main (string[] args) {
+        var game = new NumberGuessing (1, 100);
+        game.start ();
+        return 0;
+    }
+}
+
+/* This method could be included in glib-2.0.vapi for
+   class FileStream, see Bugzilla entry #582178 */
+static string read_line () {
+    var input = new StringBuilder ();
+    while (!stdin.eof ()) {
+        char c = (char) stdin.getc ();
+        if (c == '\n') break;
+        input.append_c (c);
+    }
+    return input.str;
+}
diff --git a/tests/examples/preprocessor.vala b/tests/examples/preprocessor.vala
new file mode 100644
index 0000000..557324f
--- /dev/null
+++ b/tests/examples/preprocessor.vala
@@ -0,0 +1,27 @@
+
+// http://live.gnome.org/Vala/PreprocessorSample vala-test:examples/preprocessor.vala
+
+void main () {
+
+#if ( FOOBAR || FOO || BAR ) && (FOOBAR == FOO && FOO == BAR)
+    message ("FOOBAR == FOO == BAR");
+#endif
+
+#if ! NOFOO && (FOOBAR || (FOO && BAR)) 
+    message ("FOOBAR");
+#elif FOO && ! NOFOO 
+    message ("FOO");
+#elif BAR && ! NOFOO
+    message ("BAR");
+#elif NOFOO
+#if FOOBAR || (FOO && BAR)
+    message ("NOFOO FOOBAR");
+#else
+    message ("NOFOO");
+#endif
+#else
+    message ("Nothing relevant defined");
+#endif
+
+}
+
diff --git a/tests/examples/properties-construction.vala b/tests/examples/properties-construction.vala
index 68a89b4..063757d 100644
--- a/tests/examples/properties-construction.vala
+++ b/tests/examples/properties-construction.vala
@@ -1,3 +1,6 @@
+
+// http://live.gnome.org/Vala/PropertiesSample vala-test:examples/properties-construction.vala
+
 using GLib;
 
 public class MyProperty : Object {
diff --git a/tests/examples/properties.vala b/tests/examples/properties.vala
index 43c743d..595fdcf 100644
--- a/tests/examples/properties.vala
+++ b/tests/examples/properties.vala
@@ -1,3 +1,6 @@
+
+// http://live.gnome.org/Vala/PropertiesSample vala-test:examples/properties.vala
+
 using GLib;
 
 public class Sample : Object {
@@ -6,7 +9,7 @@ public class Sample : Object {
 
     private string _name;
     [Notify]                // will be unnecessary with Vala 0.3.5
-		public string name {
+    public string name {
         get { return _name; }
         set { _name = value; }
     }
diff --git a/tests/examples/signals.vala b/tests/examples/signals.vala
new file mode 100644
index 0000000..80c3de6
--- /dev/null
+++ b/tests/examples/signals.vala
@@ -0,0 +1,26 @@
+
+// http://live.gnome.org/Vala/SignalsAndCallbacks vala-test:examples/signals.vala
+
+public class Foo : Object {
+    public signal void some_event ();   // definition of the signal
+
+    public void method () {
+        some_event ();                  // emitting the signal (callbacks get invoked)
+    }
+}
+
+static void callback_a () {
+    stdout.printf ("Callback A\n");
+}
+
+static void callback_b () {
+    stdout.printf ("Callback B\n");
+}
+
+static int main (string[] args) {
+    var foo = new Foo ();
+    foo.some_event += callback_a;      // connecting the callback functions
+    foo.some_event += callback_b;
+    foo.method ();
+    return 0;
+}
diff --git a/tests/examples/string.vala b/tests/examples/string.vala
index abc457e..8489a07 100644
--- a/tests/examples/string.vala
+++ b/tests/examples/string.vala
@@ -1,4 +1,5 @@
-using GLib;
+
+// http://live.gnome.org/Vala/StringSample vala-test:examples/string.vala
 
 static void println (string str) {
     stdout.printf ("%s\n", str);
@@ -6,14 +7,20 @@ static void println (string str) {
 
 static int main (string[] args) {
 
-    // Concatenating strings
+    /* Strings are of data type 'string' and can get concatenated with the plus
+     * operator resulting in a new string:
+     */
 
     string a = "Concatenated ";
     string b = "string";
     string c = a + b;
     println (c);
 
-    // Building strings
+    /* If you want to have a mutable string you should use StringBuilder.
+     * With its help you are able to build strings ad libitum by prepending,
+     * appending, inserting or removing parts. In order to obtain the final
+     * product you access the field '.str'.
+     */
 
     var builder = new StringBuilder ();
     builder.append ("built ");
@@ -21,21 +28,32 @@ static int main (string[] args) {
     builder.append ("StringBuilder");
     builder.append_unichar ('.');
     builder.insert (13, "by ");
-    println (builder.str);
+    println (builder.str);      // => "String built by StringBuilder."
 
-    // Building strings by format
+    /* You can create a new string according to a format string by calling the
+     * method 'printf' on it. Format strings follow the usual rules, known from
+     * C and similar programming languages.
+     */
 
     string formatted = "PI %s equals %g.".printf ("approximately", Math.PI);
     println (formatted);
 
-    // Comparing strings
+    /* The equality operator compares the content of two strings, contrary to
+     * Java's behaviour which in this case would check for referential equality.
+     */
 
     a = "foo";
     b = "foo";
     if (a == b) {
         println ("String == operator compares content, not reference.");
     } else {
-        println ("Shouldn't happen.");
+        assert_not_reached ();
+    }
+
+    /* You can compare strings lexicographically with the < and > operators: */
+
+    if ("blue" < "red" && "orange" > "green") {
+        // That's correct
     }
 
     // Switch statement
@@ -43,34 +61,52 @@ static int main (string[] args) {
     string s = "vala";
     switch (s) {
     case "java":
-        println ("Shouldn't happen.");
-        break;
+        assert_not_reached ();
     case "vala":
         println ("Switch statement works fine with strings.");
         break;
     case "ruby":
-        println ("Shouldn't happen.");
-        break;
+        assert_not_reached ();
     }
 
-    // Verbatim strings
+    /* Vala offers a feature called verbatim strings. These are strings in
+     * which escape sequences (such as \n) won't get interpreted, line breaks
+     * will be preserved and quotation marks don't have to be masked. They get
+     * enclosed with triple double quotation marks. Possible indentations
+     * after a line break are part of the string as well. Note that syntax
+     * highlighting in this Wiki is not aware of verbatim strings.
+     */
 
     string verbatim = """This is a so-called "verbatim string".
 Verbatim strings don't process escape sequences, such as \n, \t, \\, etc.
-They may contain quotes and may span multiple lines. Since Vala 0.3.3""";
+They may contain quotes and may span multiple lines.""";
     println (verbatim);
 
-    // Some string operations
+    /* You can apply various operations on strings. Here's a small selection: */
 
     println ("from lower case to upper case".up ());
     println ("reversed string".reverse ());
     println ("...substring...".substring (3, 9));
 
-    // String dimensions
+    /* The 'in' keyword is syntactic sugar for checking if one string contains
+     * another string. The following expression is identical to
+     * "swordfish".contains ("word")
+     */
+
+    if ("word" in "swordfish") {
+        // ...
+    }
 
-    string dessert = "Crème brûlée";
-    print ("The string '%s' is %ld characters long and is stored in %ld bytes\n",
-           dessert, dessert.len (), dessert.size ());
+    /* You can determine the length of a string in characters with the method
+     * len (). In order to obtain the size of a string in bytes you use the
+     * method size (). This value potentially differs from the length! The
+     * reason is that a Unicode character may occupy more than one byte of
+     * memory.
+     */
+
+    string dessert = "crème brûlée";
+    stdout.printf ("The string '%s' is %ld characters long and is stored in %zd bytes\n",
+                   dessert, dessert.len (), dessert.size ());
 
     // Regular expressions
 
@@ -79,8 +115,8 @@ They may contain quotes and may span multiple lines. Since Vala 0.3.3""";
         s = "wolf, tiger, eagle, jaguar, leopard, bear";
         println (regex.replace (s, s.size (), 0, "kitty"));
     } catch (RegexError e) {
-        warning (e.message);
+        warning ("%s", e.message);
     }
 
     return 0;
-}
\ No newline at end of file
+}
diff --git a/tests/examples/value.vala b/tests/examples/value.vala
new file mode 100644
index 0000000..08ea652
--- /dev/null
+++ b/tests/examples/value.vala
@@ -0,0 +1,26 @@
+
+// http://live.gnome.org/Vala/ValueSample vala-test:examples/value.vala
+
+public static int main (string[] args) {
+        // initialize a GLib.Value of type int (G_TYPE_INT)
+        Value prop_val = Value (typeof(int));
+
+        // set a value
+        prop_val.set_int (5);
+        print ("value: %d\n", prop_val.get_int ());
+
+        // reset to its default value
+        prop_val.reset ();
+        print ("value: %d\n", prop_val.get_int ());
+
+        // unset the value
+        prop_val.unset ();
+
+        // init with type string
+        prop_val.init (typeof(string));
+        prop_val.set_string ("string");
+        print ("value: %s\n", prop_val.get_string ());
+
+        // return from this main method
+        return 0;
+}
diff --git a/update-from-wiki.sh b/update-from-wiki.sh
new file mode 100755
index 0000000..75b8f73
--- /dev/null
+++ b/update-from-wiki.sh
@@ -0,0 +1,39 @@
+#!/bin/bash
+
+PAGE="
+BasicSample
+AdvancedSample
+ValueSample
+ListSample
+StringSample
+SignalsAndCallbacks
+PropertiesSample
+PreprocessorSample
+"
+
+for page in $PAGE ; do
+  url="http://live.gnome.org/Vala/$page";
+  curl -s "$url?action=raw" -A "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" > "$page-raw"
+
+  # awk '/^}}}/{close(f); f=""} f{print > f} /^{{{#/{ f=gensub(/.*vala-test:([\w]*)$/, "tests/\\1", "g"); }' $page-raw
+
+  IFS="
+"
+  while read -r line ; do
+      line=$(echo "$line" | tr -d '\r')
+      if [[ "$line" =~ vala-test:([-a-zA-Z0-9\.\/]*) ]] ; then
+	  file="tests/${BASH_REMATCH[1]}"
+	  echo "Updating $file..."
+	  > "$file"
+	  echo "
+// $url vala-test:${BASH_REMATCH[1]}
+" >> "$file"
+      elif [[ "$line" =~ }}} ]] ; then
+	  file=
+      elif [[ "$file" != "" ]] ; then
+	  echo "$line" >> "$file"
+      fi
+  done < "$page-raw"
+
+  rm -f "$page-raw"
+done



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]