public
Description: The Nu programming language.
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/nu.git
nu / test / test_strings.nu
100644 144 lines (124 sloc) 5.954 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
;; test_strings.nu
;; tests for Nu string literals.
;;
;; Copyright (c) 2007 Tim Burks, Neon Design Technology, Inc.
 
(class TestStrings is NuTestCase
     
     (imethod (id) testEscapedStrings is
          (assert_equal 10 ("\n" characterAtIndex:0))
          (assert_equal 13 ("\r" characterAtIndex:0))
          (assert_equal 12 ("\f" characterAtIndex:0))
          (assert_equal 9 ("\t" characterAtIndex:0))
          (assert_equal 8 ("\b" characterAtIndex:0))
          (assert_equal 7 ("\a" characterAtIndex:0))
          (assert_equal 27 ("\e" characterAtIndex:0))
          (assert_equal 32 ("\s" characterAtIndex:0))
          (assert_equal 34 ("\"" characterAtIndex:0))
          (assert_equal 92 ("\\" characterAtIndex:0)))
     
     (imethod (id) testInterpolation is
          (set x "")
          (assert_equal "" "#{x}")
 
          (set x "blueberry")
          (assert_equal "blueberry pancakes" "#{x} pancakes")
 
          (assert_equal "24" "#{(* 6 4)}"))
 
     (imethod (id) testOctalEscapedStrings is
          (if (eq (uname) "Darwin") ;; requires UTF-8
              (assert_equal 0 ("\000" characterAtIndex:0)))
          (assert_equal 1 ("\001" characterAtIndex:0))
          (assert_equal 255 ("\377" characterAtIndex:0)))
     
     (imethod (id) testHexEscapedStrings is
          (if (eq (uname) "Darwin") ;; requires UTF-8
              (assert_equal 0 ("\x00" characterAtIndex:0)))
          (assert_equal 1 ("\x01" characterAtIndex:0))
          (assert_equal 255 ("\xfF" characterAtIndex:0)))
     
     (if (eq (uname) "Darwin") ;; requires UTF-8
         (imethod (id) testUnicodeEscapedStrings is
              (assert_equal 0 ("\u0000" characterAtIndex:0))
              (assert_equal 1 ("\u0001" characterAtIndex:0))
              (assert_equal 255 ("\u00ff" characterAtIndex:0))
              (assert_equal 65535 ("\uFfFf" characterAtIndex:0))))
     
     (imethod (id) testEscapedHereStrings is
          (set x <<+END
\n\r\f\t\b\a\e\s\"\\END) ;; " fix textmate!
          (assert_equal 10 (x characterAtIndex:0))
          (assert_equal 13 (x characterAtIndex:1))
          (assert_equal 12 (x characterAtIndex:2))
          (assert_equal 9 (x characterAtIndex:3))
          (assert_equal 8 (x characterAtIndex:4))
          (assert_equal 7 (x characterAtIndex:5))
          (assert_equal 27 (x characterAtIndex:6))
          (assert_equal 32 (x characterAtIndex:7))
          (assert_equal 34 (x characterAtIndex:8))
          (assert_equal 92 (x characterAtIndex:9)))
     
     (imethod (id) testOctalEscapedHereStrings is
          (set x <<+END
\003\001\377END)
          (assert_equal 3 (x characterAtIndex:0))
          (assert_equal 1 (x characterAtIndex:1))
          (assert_equal 255 (x characterAtIndex:2)))
     
     (imethod (id) testHexEscapedHereStrings is
          (set x <<+END
\x03\x01\xffEND)
          (assert_equal 3 (x characterAtIndex:0))
          (assert_equal 1 (x characterAtIndex:1))
          (assert_equal 255 (x characterAtIndex:2)))
     
     (if (eq (uname) "Darwin") ;; requires UTF-8
         (imethod (id) testUnicodeEscapedHereStrings is
              (set x <<+END
\u0000\u0001\u00ff\uFfFfEND)
              (assert_equal 0 (x characterAtIndex:0))
              (assert_equal 1 (x characterAtIndex:1))
              (assert_equal 255 (x characterAtIndex:2))
              (assert_equal 65535 (x characterAtIndex:3))))
     
     (imethod (id) testExplicitlyUnescapedStrings is
          (assert_equal 92 (-"\n" characterAtIndex:0))
          (assert_equal 92 (-"\s" characterAtIndex:0))
          (assert_equal 92 (-"\x20" characterAtIndex:0)))
     
     (imethod (id) testExplicitlyEscapedStrings is
          (assert_equal 10 (+"\n" characterAtIndex:0))
          (assert_equal 32 (+"\s" characterAtIndex:0))
          (assert_equal 32 (+"\x20" characterAtIndex:0)))
     
     (imethod (id) testExplicitlyUnescapedHereStrings is
          (set x <<-END
foo\nbarEND)
          (assert_equal 8 (x length))
          (assert_equal 92 (x characterAtIndex:3)))
     
     (imethod (id) testExplicitlyEscapedHereStrings is
          (set x <<+END
foo\nbarEND)
          (assert_equal 7 (x length))
          (assert_equal 10 (x characterAtIndex:3)))
     
     (imethod (id) testLineSplitting is
          (set x <<-END
Line 0
Line 1
Line 2
END)
          (set lines (x lines))
          (assert_equal 3 (lines count))
          (assert_equal "Line 1" (lines objectAtIndex:1)))
     
     (imethod (id) testEscapedRepresentations is
          ;; verify the named characters
          (assert_equal "(\"\\a\\b\\t\\n\\f\\r\\e\")" ('("\a\b\t\n\f\r\e") stringValue))
          ;; verify escaping of low-valued characters
          (assert_equal "\"\\x01\\x02\"" ("\x01\x02" escapedStringRepresentation))
          ;;verify that 0x1f is the highest character escaped, 0x20 is kept as-is
          (assert_equal "\"\\x1f \"" ("\x1f\x20" escapedStringRepresentation))
          ;; verify that 0x7e is not escaped but 0x7f is
          (assert_equal "\"~\\x7f\"" ("\x7e\x7f" escapedStringRepresentation))
          ;; verify escaping of higher-valued one-byte characters
          (assert_equal "\"\\xe0\\xf0\"" ("\xE0\xf0" escapedStringRepresentation))
          ;; verify escaping of unicode characters
          (assert_equal "\"\\u0100\\uffff\"" ("\u0100\uffFF" escapedStringRepresentation)))
     
     (imethod (id) testStringEach is
          (set start "hello, world")
          (set finish "")
          (start each:(do (c) (finish appendCharacter:c)))
          (assert_equal start finish)
          ;; each with break
          (set finish "")
          (start each:(do (c) (if (eq c ',') (break)) (finish appendCharacter:c)))
          (assert_equal "hello" finish)
          ;; each with continue
          (set finish "")
          (start each:(do (c) (if (eq c ',') (continue)) (finish appendCharacter:c)))
          (assert_equal "hello world" finish)))