public
Description: The Nu programming language.
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/nu.git
nu / test / test_regex.nu
100644 51 lines (40 sloc) 2.15 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
;; test_regex.nu
;; tests for Nu regular expression support.
;;
;; Copyright (c) 2007 Tim Burks, Neon Design Technology, Inc.
 
(class TestRegex is NuTestCase
     
     (imethod (id) testRegexWithOperator is
          (set r (regex "a(.*)z"))
          (set match (r findInString:"abcdefghijklmnopqrstuvwxyz"))
          (assert_equal 24 ((match groupAtIndex:1) length)))
     
     (if (eq (uname) "Darwin") ;; requires UTF-8
         (imethod (id) testScrapeWithOperator is
              (set s (NSString stringWithContentsOfFile:"test/test.html" encoding:NSUTF8StringEncoding error:nil))
              (set r (regex <<-END
<a href="/search([^\"]*)"END))
              (set matches (r findAllInString:s))
              (assert_equal 10 (matches count))
              (assert_equal "?q=bicycle+pedal&amp;hl=en&amp;start=10&amp;sa=N" ((matches lastObject) groupAtIndex:1))))
     
     (imethod (id) testRegex is
          (set match (/a(.*)z/ findInString:"abcdefghijklmnopqrstuvwxyz"))
          (assert_equal 24 ((match groupAtIndex:1) length)))
     
     (if (eq (uname) "Darwin") ;; requires UTF-8
         (imethod (id) testRegexScraping is
              (set s (NSString stringWithContentsOfFile:"test/test.html" encoding:NSUTF8StringEncoding error:nil))
              (set r /<a href="\/search([^"]*)"/)
(set matches (r findAllInString:s))
(assert_equal 10 (matches count))
(assert_equal "?q=bicycle+pedal&amp;hl=en&amp;start=10&amp;sa=N"
                   ((matches lastObject) groupAtIndex:1))))
     
     (imethod (id) testExtendedRegex is
          (set r /foo # comment
                   bar/x)
          (set match (r findInString:"foobar"))
          (assert_not_equal nil match))
     
     (imethod (id) testMultipleCaptures is
          (set match (/^ab(.*)def(.*)jklmn(.*)tuv(.*)z$/
                          findInString:"abcdefghijklmnopqrstuvwxyz"))
          (assert_equal "c" (match groupAtIndex:1))
          (assert_equal "ghi" (match groupAtIndex:2))
          (assert_equal "opqrs" (match groupAtIndex:3))
          (assert_equal "wxy" (match groupAtIndex:4))))