All Tests (AppleScript)

Monday, July 1, 2002

Test Suite for the Text Suite.

This script performs a number of tests in an attempt to have some idea whether a given application complies with the
Apple Event Object Model and implements the Text Suite correctly.

It also tries to determine whether the scripting implementation avoids some common pitfalls (like removing the wrong ranges when asked to delete a list of words in arbitrary order) and supports complex “whose” clauses.

All scriptable text editors should pass at least the
simple test. Decent implementations should also pass the ranges and whoseClauses test. Careful implementations will pass the gotchas test, and editors that support basic styles shouldn’t fail the styles test. It is okay to fail the numberOf test, since that requires a nonstandard AppleScript construct.

See “Coding Your Object Model for Advanced Scriptability” by Ron Reuter, develop issue 28 (December 1996).
<http://www.mactech.com/articles/develop/issue_28/reuter.html>
on test_simple(targetApplication)
   
try
      
tell targetApplication
         
make new window
         
tell document 1
            
set contents to "In Xanadu did Kubla Khan a stately pleasure dome decree, where Alph, the sacred river ran through caverns measureless to men down to a sunless sea."
            
            --
Character class, absolute & relative forms, counts
            
if character 1 ≠ "I" then return false
            
if last character ≠ "." then return false
            
if middle character ≠ "s" then return false
            
if character after character 3 ≠ "X" then return false
            
if character before character 3 ≠ "n" then return false
            
if number of characters147 then return false
            
            --
Word class, absolute & relative forms, counts
            
if word 1 ≠ "In" then return false
            
if last word ≠ "sea" then return false
            
if middle word ≠ "the" then return false
            
if word after character 3 ≠ "Xanadu" then return false
            
if word before word 5 ≠ "Kubla" then return false
            
if number of words26 then return false
            
            --
Lists
            
set x to every word
            
if length of x ≠ 26 then return false
            
            
close saving no
         
end tell
         
return true
      
end tell
   
on error
      
return false
   
end try
end test_simple

on test_gotchas(targetApplication)
   
try
      
tell targetApplication
         
make new window
         
tell document 1
            
            --
Gotcha #1, "An Apple event parameter can be an object specifier"
            
set contents to "Ping Pong"
            
set word 2 to word 1
            
if contents ≠ "Ping Ping" then return false
            
            --
Gotcha #2, "Any resolution"
            
set contents to "Have a nice day."
            
if (characters of words ≠ {{"H", "a", "v", "e"}, {"a"}, {"n", "i", "c", "e"}, {"d", "a", "y"}}) then return false
            
if (characters 1 thru 2 of (words whose length > 1) ≠ {{"H", "a"}, {"n", "i"}, {"d", "a"}}) then return false
            
            --
Gotcha #3, "Preserve a Token's Meaning"
            
set contents to "I don’t like it at all."
            
delete {word 2, word 5} -- "don’t" and "at"
            
if last word ≠ "all" then return false
            
            --
Now delete the same words, but backwards
            --
STE 1.1 fails this test
            
set contents to "I don’t like it at all."
            
delete {word 5, word 2} -- "at" and "don’t"
            
if last word ≠ "all" then return false
            
            
close saving no
         
end tell
         
return true
      
end tell
   
on error
      
return false
   
end try
end test_gotchas

on test_numberOf(targetApplication)
   
try
      
tell targetApplication
         
make new window
         
tell document 1
            
set contents to "In Xanadu" & return & "did" & return & "Kubla Khan" & return & "a stately pleasure dome decree."
            
            --
test the "number" property of lists
            
if first character of (lines where number of words = 2) ≠ {"I", "K"} then return false
            
            
close saving no
         
end tell
         
return true
      
end tell
   
on error
      
return false
   
end try
end test_numberOf

on test_ranges(targetApplication)
   
try
      
tell targetApplication
         
make new window
         
tell document 1
            
set contents to "In Xanadu did Kubla Khan a stately pleasure dome decree, where Alph, the sacred river ran through caverns measureless to men down to a sunless sea."
            
            --
character ranges
            
if characters 1 thru 2 ≠ {"I", "n"} then return false
            
if characters -4 thru -2 ≠ {"s", "e", "a"} then return false
            
if characters 4 thru 4 ≠ {"X"} then return false
            
            --
word ranges
            
if words 1 thru 2 ≠ {"In", "Xanadu"} then return false
            
if words -4 thru -2 ≠ {"to", "a", "sunless"} then return false
            
if words 4 thru 4 ≠ {"Kubla"} then return false
            
            
close saving no
         
end tell
         
return true
      
end tell
   
on error
      
return false
   
end try
end test_ranges

on test_styles(targetApplication)
   
try
      
tell targetApplication
         
make new window
         
tell document 1
            
set contents to "The quick brown fox jumped over the lazy dog."
            
            --
Fonts & font sizes
            
set font of contents to "Monaco"
            
set size of contents to 20
            
if font of word 2 ≠ "Monaco" then return false
            
if size of last character20 then return false
            
            --
Styles
            
set style of word 2 to bold
            
set style of word 4 to {bold, italic}
            
set style of word 6 to {bold, italic, underline}
            
set style of word 6 to {on styles:{}, off styles:{bold}} -- turns off bold, leaves other styles intact
            
if ((words whose style contains bold) ≠ {"quick", "fox"}) then return false
            
if ((words whose style contains underline) ≠ {"over"}) then return false
            
            
close saving no
         
end tell
         
return true
      
end tell
   
on error
      
return false
   
end try
end test_styles

on test_whoseClauses(targetApplication)
   
try
      
tell targetApplication
         
make new window
         
tell document 1
            
set contents to "In Xanadu did Kubla Khan a stately pleasure dome decree, where Alph, the sacred river ran through caverns measureless to men down to a sunless sea."
            
            --
simple, "flat" whose clauses
            
if (words where it begins with "me") ≠ {"measureless", "men"} then return false
            
if (words where it ends with "an") ≠ {"Khan", "ran"} then return false
            
if (words where it contains "w") ≠ {"where", "down"} then return false
            
            --
simple, one-level deep whose clauses
            
if (words whose length8) ≠ {"pleasure", "measureless"} then return false
            
if (words whose first character is "K") ≠ {"Kubla", "Khan"} then return false
            
if (words where it ends with "e") ≠ (words whose last character is "e") then return false
            
            --
using an object specifier in the second operand
            
if (words where first character = last character) ≠ {"did", "a", "river", "a", "sunless"} then return false
            
if (words where it = last character) ≠ {"a", "a"} then return false
            
            --
simple, two-level deep whose clauses
            
if (words where character before last character is "r") ≠ {"pleasure", "where"} then return false
            
            --
complex clauses (clauses with logical operators)
            
if (words where length = 5 and it contains "e") ≠ {"where", "river"} then return false
            
            --
picking a single element from a whose list
            
if (first word whose length > 5) ≠ "Xanadu" then return false
            
if (middle word whose length > 5) ≠ "sacred" then return false
            
if (last word whose length > 5) ≠ "sunless" then return false
            
            --
picking a simple range of elements from a whose clause
            --
(note that the following two forms are equivalent)
            
if (words 3 thru 4 whose length > 5) ≠ {"pleasure", "decree"} then return false
            
if (words 3 thru 4 of (words whose length > 5)) ≠ {"pleasure", "decree"} then return false
            
            --
picking ranges from whose clause
            --
in this case, the range class is not the same as the whose class
            --
so a list of lists should be returned
            
if characters 7 thru 8 of (words whose length8) ≠ {{"r", "e"}, {"e", "l"}} then return false
            
            
close saving no
         
end tell
         
return true
      
end tell
   
on error
      
return false
   
end try
end test_whoseClauses

on run
   
local targetApplication
   
local theMessage
   
   
set targetApplication to (choose application)
   
set theMessage to ""
   
   
if test_simple(targetApplication) then
      
set theMessage to theMessage & "Test \"simple\" passed." & return
   
else
      
set theMessage to theMessage & "Test \"simple\" failed." & return
   
end if
   
   
if test_gotchas(targetApplication) then
      
set theMessage to theMessage & "Test \"gotchas\" passed." & return
   
else
      
set theMessage to theMessage & "Test \"gotchas\" failed." & return
   
end if
   
   
if test_numberOf(targetApplication) then
      
set theMessage to theMessage & "Test \"number of\" passed." & return
   
else
      
set theMessage to theMessage & "Test \"number of\" failed." & return
   
end if
   
   
if test_ranges(targetApplication) then
      
set theMessage to theMessage & "Test \"ranges\" passed." & return
   
else
      
set theMessage to theMessage & "Test \"ranges\" failed." & return
   
end if
   
   
if test_styles(targetApplication) then
      
set theMessage to theMessage & "Test \"styles\" passed." & return
   
else
      
set theMessage to theMessage & "Test \"styles\" failed." & return
   
end if
   
   
if test_whoseClauses(targetApplication) then
      
set theMessage to theMessage & "Test \"whose clauses\" passed." & return
   
else
      
set theMessage to theMessage & "Test \"whose clauses\" failed." & return
   
end if
   
   
return theMessage
end run
Top | Made with Script Debugger 3.0

Valid XHTML 1.0! Copyright © 2002 Merzwaren