help-smalltalk
[Top][All Lists]
Advanced

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

[Help-smalltalk] Two that we missed in regex.st


From: Mike Anderson
Subject: [Help-smalltalk] Two that we missed in regex.st
Date: Wed, 24 Aug 2005 20:24:51 +0000
User-agent: Mozilla Thunderbird 0.7.1 (X11/20040626)


I'm attaching my (small) set of tests for this, if it's of any interest.

Mike
--- orig/examples/regex.st      2005-08-24 19:59:30.605439512 +0000
+++ mod/examples/regex.st       2005-08-24 20:01:26.984747168 +0000
@@ -515,7 +515,7 @@
     | regs beg end repl res |
     regs := self searchRegex: pattern from: from to: to.
 
-    regs notNil
+    regs matched
        ifTrue: [
            beg := regs from.
            end := regs to.
@@ -544,7 +544,7 @@
     idx := from.
     [
         regs := self searchRegex: regex from: idx to: to.
-       regs notNil
+       regs matched
     ] whileTrue: [
        beg := regs from.
        end := regs to.
TestCase subclass: #RegexTest
        instanceVariableNames: ''
        classVariableNames: ''
        poolDictionaries: ''
        category: 'Testing'
!

RegexTest methodsFor: 'testing'!

testFromEqualsFromAt
        | mr |
        mr := 'abc' searchRegex: '([a-z]+)'.
        self assert: mr matched description: 'Should have matched'.
        self assert: (mr from = (mr fromAt: 1)) description: 'from ~= fromAt'.
!

testToEqualsToAt
        | mr |
        mr := 'abc' searchRegex: '([a-z]+)'.
        self assert: mr matched description: 'Should have matched'.
        self assert: (mr to = (mr toAt: 1)) description: 'to ~= toAt'.
!

testNoMatch
        | mr |
        mr := 'abc' searchRegex: '([0-9]+)'.
        self assert: mr matched not description: 'Should not have matched'.
!

testEmptyString
        | mr |
        mr := '' searchRegex: '([a-z]+)'.
        self assert: mr matched not description: 'Should not have matched'.
!

testEmptyPattern
        | mr |
        mr := 'abc' searchRegex: ''.
        self assert: mr matched description: 'Should have matched'.
        self assert: (mr from = 1) description: 'from ~= 1'.
        self assert: (mr match isEmpty) description: 'match should be empty'.
        self assert: (mr to < mr from) description: 'to >= from ( = ', mr to 
printString, ' )'.
!

testMatch
        | mr |
        mr := 'abc123' searchRegex: '[a-z]+'.
        self assert: mr matched description: 'Should have matched'.
        self assert: (mr match = 'abc') description: 'Should have matched only 
''abc'''.
!

testSubExpsCapture
        | mr |
        mr := 'abc123' searchRegex: '([a-z]+)([0-9]+)'.
        self assert: mr matched description: 'Should have matched'.
        self assert: (mr match = 'abc123') description: '0'.
        self assert: ((mr at: 1) = 'abc') description: '1'.
        self assert: ((mr at: 2) = '123') description: '2'.
!

testSubExpsPartial
        | mr |
        mr := 'abc123' searchRegex: '[a-z]+([0-9]+)'.
        self assert: mr matched description: 'Should have matched'.
        self assert: (mr match = 'abc123') description: '0'.
        self assert: ((mr at: 1) = '123') description: '1'.
!

testEmptySubExp
        | mr |
        mr := 'abcdef' searchRegex: '([a-z]+)([0-9]*)([a-z]+)'.
        self assert: mr matched description: 'Should have matched'.
        self assert: (mr match = 'abcdef') description: '0'.
        self assert: ((mr at: 1) = 'abcde') description: '1'. "Should be greedy"
        self assert: ((mr at: 2) = '') description: '2: ', (mr at: 2) 
printString.
        self assert: ((mr at: 3) = 'f') description: '3'.
!

testMatchAtEnd
        | mr |
        mr := 'abc123' searchRegex: '([0-9]+)'.
        self assert: mr matched description: 'Should have matched'.
        self assert: (mr match = '123') description: '0: ', mr match 
printString.
        self assert: ((mr at: 1) = '123') description: '1'.
!

testMatchInMiddle
        | mr |
        mr := 'abc123def' searchRegex: '([0-9]+)'.
        self assert: mr matched description: 'Should have matched'.
        self assert: (mr match = '123') description: '0: ', mr match 
printString.
        self assert: ((mr at: 1) = '123') description: '1'.
!

testMatchInMiddle2
        | mr |
        mr := 'abc123def' searchRegex: '([a-z]+([0-9]+)[a-z]+)'.
        self assert: mr matched description: 'Should have matched'.
        self assert: (mr match = 'abc123def') description: '0: ', mr match 
printString.
        self assert: ((mr at: 1) = 'abc123def') description: '1'.
        self assert: ((mr at: 2) = '123') description: '2'.
!

testCopyReplacing
        | s |
        s := 'Great Small Tall'.
        3 timesRepeat:
                [ s := s copyReplacingRegex: '([A-Z])[a-z]+' with: '%1.'.].
        self assert: s = 'G. S. T.' description: 'Failed: ', s.
!

testCopyReplacingNoMatch
        | s1 s2 |
        s1 := 'Great Small Tall'.
        s2 := s copyReplacingRegex: '([0-9]+)' with: 'nnn'.
        self assert: s1 = s2 description: 'Failed: ', s2.
!

testCopyReplacingAll
        | s |
        s := 'Great Small Tall' 
                copyReplacingAllRegex: '([A-Z])[a-z]+' 
                with: '%1.'.
        self assert: s = 'G. S. T.' description: 'Failed: ', s.
!

testCopyReplacingAll2
        | s |
        self 
                shouldnt:
                        [ s := 'Less than &lt; .' 
                                copyReplacingAllRegex: '&lt;' 
                                with: '<'. ]
                raise: MessageNotUnderstood.
        self assert: s = 'Less than < .' description: 'Failed: ', s.
!

testCopyReplacingAll3
        self 
                shouldnt:
                        [ 'Less than < .' copyReplacingAllRegex: '&lt;' with: 
'<'. ]
                raise: MessageNotUnderstood.
!


testCaseSensitive
        self 
                deny: ('GST' matchRegex: '[a-z]+')
                description: 'Should not have matched.'.
!
!

reply via email to

[Prev in Thread] Current Thread [Next in Thread]